Skip to content

Commit

Permalink
fix golangci_lint check in execute.go
Browse files Browse the repository at this point in the history
  • Loading branch information
dciangot committed Nov 19, 2024
1 parent f83e349 commit 495442f
Showing 1 changed file with 55 additions and 74 deletions.
129 changes: 55 additions & 74 deletions pkg/virtualkubelet/execute.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,24 +114,23 @@ func PingInterLink(ctx context.Context, config Config) (bool, int, error) {
spanHTTP.SetAttributes(attribute.Int("exit.code", http.StatusInternalServerError))
return false, retVal, err
}
defer resp.Body.Close()

if resp != nil {
types.SetDurationSpan(startHTTPCall, spanHTTP, types.WithHTTPReturnCode(resp.StatusCode))
retBytes, err := io.ReadAll(resp.Body)
if err != nil {
log.G(ctx).Error(err)
return false, retVal, err
}
retVal, err = strconv.Atoi(string(retBytes))
if err != nil {
log.G(ctx).Error(err)
return false, retVal, err
}
types.SetDurationSpan(startHTTPCall, spanHTTP, types.WithHTTPReturnCode(resp.StatusCode))
retBytes, err := io.ReadAll(resp.Body)
if err != nil {
log.G(ctx).Error(err)
return false, retVal, err
}
retVal, err = strconv.Atoi(string(retBytes))
if err != nil {
log.G(ctx).Error(err)
return false, retVal, err
}

if resp.StatusCode != http.StatusOK {
log.G(ctx).Error("server error: " + fmt.Sprint(resp.StatusCode))
return false, retVal, nil
}
if resp.StatusCode != http.StatusOK {
log.G(ctx).Error("server error: " + fmt.Sprint(resp.StatusCode))
return false, retVal, nil
}

return true, retVal, nil
Expand Down Expand Up @@ -166,11 +165,11 @@ func updateCacheRequest(ctx context.Context, config Config, pod v1.Pod, token st
log.L.Error(err)
return err
}
if resp != nil {
types.SetDurationSpan(startHTTPCall, *spanHTTP, types.WithHTTPReturnCode(resp.StatusCode))
if resp.StatusCode != http.StatusOK {
return errors.New("Unexpected error occured while updating InterLink cache. Status code: " + strconv.Itoa(resp.StatusCode) + ". Check InterLink's logs for further informations")
}
defer resp.Body.Close()

types.SetDurationSpan(startHTTPCall, *spanHTTP, types.WithHTTPReturnCode(resp.StatusCode))
if resp.StatusCode != http.StatusOK {
return errors.New("Unexpected error occured while updating InterLink cache. Status code: " + strconv.Itoa(resp.StatusCode) + ". Check InterLink's logs for further informations")
}

return err
Expand All @@ -181,11 +180,6 @@ func updateCacheRequest(ctx context.Context, config Config, pod v1.Pod, token st
func createRequest(ctx context.Context, config Config, pod types.PodCreateRequests, token string) ([]byte, error) {
tracer := otel.Tracer("interlink-service")
interLinkEndpoint := getSidecarEndpoint(ctx, config.InterlinkURL, config.Interlinkport)
returnValue, err := json.Marshal(types.CreateStruct{})
if err != nil {
log.L.Error(err)
return nil, err
}

bodyBytes, err := json.Marshal(pod)
if err != nil {
Expand Down Expand Up @@ -216,17 +210,15 @@ func createRequest(ctx context.Context, config Config, pod types.PodCreateReques
}
defer resp.Body.Close()

if resp != nil {
types.SetDurationSpan(startHTTPCall, spanHTTP, types.WithHTTPReturnCode(resp.StatusCode))
types.SetDurationSpan(startHTTPCall, spanHTTP, types.WithHTTPReturnCode(resp.StatusCode))

if resp.StatusCode != http.StatusOK {
return nil, errors.New("Unexpected error occured while creating Pods. Status code: " + strconv.Itoa(resp.StatusCode) + ". Check InterLink's logs for further informations")
}
returnValue, err = io.ReadAll(resp.Body)
if err != nil {
log.L.Error(err)
return nil, err
}
if resp.StatusCode != http.StatusOK {
return nil, errors.New("Unexpected error occured while creating Pods. Status code: " + strconv.Itoa(resp.StatusCode) + ". Check InterLink's logs for further informations")
}
returnValue, err := io.ReadAll(resp.Body)
if err != nil {
log.L.Error(err)
return nil, err
}

return returnValue, nil
Expand Down Expand Up @@ -259,26 +251,24 @@ func deleteRequest(ctx context.Context, config Config, pod *v1.Pod, token string
}
defer resp.Body.Close()

if resp != nil {
statusCode := resp.StatusCode
types.SetDurationSpan(startHTTPCall, *spanHTTP, types.WithHTTPReturnCode(resp.StatusCode))
statusCode := resp.StatusCode
types.SetDurationSpan(startHTTPCall, *spanHTTP, types.WithHTTPReturnCode(resp.StatusCode))

if statusCode != http.StatusOK {
return nil, errors.New("Unexpected error occured while deleting Pods. Status code: " + strconv.Itoa(resp.StatusCode) + ". Check InterLink's logs for further informations")
}
if statusCode != http.StatusOK {
return nil, errors.New("Unexpected error occured while deleting Pods. Status code: " + strconv.Itoa(resp.StatusCode) + ". Check InterLink's logs for further informations")
}

returnValue, err := io.ReadAll(resp.Body)
if err != nil {
log.G(context.Background()).Error(err)
return nil, err
}
log.G(context.Background()).Info(string(returnValue))
var response []types.PodStatus
err = json.Unmarshal(returnValue, &response)
if err != nil {
log.G(context.Background()).Error(err)
return nil, err
}
returnValue, err = io.ReadAll(resp.Body)
if err != nil {
log.G(context.Background()).Error(err)
return nil, err
}
log.G(context.Background()).Info(string(returnValue))
var response []types.PodStatus
err = json.Unmarshal(returnValue, &response)
if err != nil {
log.G(context.Background()).Error(err)
return nil, err
}

return returnValue, nil
Expand All @@ -289,11 +279,6 @@ func deleteRequest(ctx context.Context, config Config, pod *v1.Pod, token string
// Returns the call response expressed in bytes and/or the first encountered error
func statusRequest(ctx context.Context, config Config, podsList []*v1.Pod, token string) ([]byte, error) {
tracer := otel.Tracer("interlink-service")
returnValue, err := json.Marshal(types.PodStatus{})
if err != nil {
log.L.Error(err)
return nil, err
}

interLinkEndpoint := getSidecarEndpoint(ctx, config.InterlinkURL, config.Interlinkport)

Expand Down Expand Up @@ -324,16 +309,14 @@ func statusRequest(ctx context.Context, config Config, podsList []*v1.Pod, token
}
defer resp.Body.Close()

if resp != nil {
types.SetDurationSpan(startHTTPCall, spanHTTP, types.WithHTTPReturnCode(resp.StatusCode))
if resp.StatusCode != http.StatusOK {
return nil, errors.New("Unexpected error occured while getting status. Status code: " + strconv.Itoa(resp.StatusCode) + ". Check InterLink's logs for further informations")
}
returnValue, err = io.ReadAll(resp.Body)
if err != nil {
log.L.Error(err)
return nil, err
}
types.SetDurationSpan(startHTTPCall, spanHTTP, types.WithHTTPReturnCode(resp.StatusCode))
if resp.StatusCode != http.StatusOK {
return nil, errors.New("Unexpected error occured while getting status. Status code: " + strconv.Itoa(resp.StatusCode) + ". Check InterLink's logs for further informations")
}
returnValue, err := io.ReadAll(resp.Body)
if err != nil {
log.L.Error(err)
return nil, err
}

return returnValue, nil
Expand Down Expand Up @@ -387,11 +370,9 @@ func LogRetrieval(ctx context.Context, config Config, logsRequest types.LogStruc
}
defer resp.Body.Close()

if resp != nil {
types.SetDurationSpan(startHTTPCall, spanHTTP, types.WithHTTPReturnCode(resp.StatusCode))
if resp.StatusCode != http.StatusOK {
err = errors.New("Unexpected error occured while getting logs. Status code: " + strconv.Itoa(resp.StatusCode) + ". Check InterLink's logs for further informations")
}
types.SetDurationSpan(startHTTPCall, spanHTTP, types.WithHTTPReturnCode(resp.StatusCode))
if resp.StatusCode != http.StatusOK {
err = errors.New("Unexpected error occured while getting logs. Status code: " + strconv.Itoa(resp.StatusCode) + ". Check InterLink's logs for further informations")
}

return io.NopCloser(bufio.NewReader(resp.Body)), err
Expand Down

0 comments on commit 495442f

Please sign in to comment.