Skip to content

Commit 29f7f58

Browse files
committed
fix: lint
1 parent 565c83c commit 29f7f58

File tree

2 files changed

+18
-14
lines changed

2 files changed

+18
-14
lines changed

cns/imds/client.go

+13-10
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ package imds
66
import (
77
"context"
88
"encoding/json"
9-
"fmt"
109
"net/http"
1110
"net/url"
1211

@@ -28,17 +27,17 @@ type clientConfig struct {
2827
retryAttempts uint
2928
}
3029

31-
type clientOption func(*clientConfig)
30+
type ClientOption func(*clientConfig)
3231

3332
// Endpoint overrides the default endpoint for a Client
34-
func Endpoint(url string) clientOption {
33+
func Endpoint(endpoint string) ClientOption {
3534
return func(c *clientConfig) {
36-
c.endpoint = url
35+
c.endpoint = endpoint
3736
}
3837
}
3938

4039
// RetryAttempts overrides the default retry attempts for the client
41-
func RetryAttempts(attempts uint) clientOption {
40+
func RetryAttempts(attempts uint) ClientOption {
4241
return func(c *clientConfig) {
4342
c.retryAttempts = attempts
4443
}
@@ -53,10 +52,13 @@ const (
5352
defaultIMDSEndpoint = "http://169.254.169.254"
5453
)
5554

56-
var ErrVMUniqueIDNotFound = errors.New("vm unique ID not found")
55+
var (
56+
ErrVMUniqueIDNotFound = errors.New("vm unique ID not found")
57+
ErrUnexpectedStatusCode = errors.New("imds returned an unexpected status code")
58+
)
5759

5860
// NewClient creates a new imds client
59-
func NewClient(opts ...clientOption) *Client {
61+
func NewClient(opts ...ClientOption) *Client {
6062
config := clientConfig{
6163
endpoint: defaultIMDSEndpoint,
6264
}
@@ -98,12 +100,12 @@ func (c *Client) GetVMUniqueID(ctx context.Context) (string, error) {
98100
}
99101

100102
func (c *Client) getInstanceComputeMetadata(ctx context.Context) (map[string]any, error) {
101-
url, err := url.JoinPath(c.config.endpoint, imdsComputePath)
103+
imdsComputeURL, err := url.JoinPath(c.config.endpoint, imdsComputePath)
102104
if err != nil {
103105
return nil, errors.Wrap(err, "unable to build path to IMDS compute metadata")
104106
}
105107

106-
req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
108+
req, err := http.NewRequestWithContext(ctx, http.MethodGet, imdsComputeURL, http.NoBody)
107109
if err != nil {
108110
return nil, errors.Wrap(err, "error building IMDS http request")
109111
}
@@ -115,9 +117,10 @@ func (c *Client) getInstanceComputeMetadata(ctx context.Context) (map[string]any
115117
if err != nil {
116118
return nil, errors.Wrap(err, "error querying IMDS")
117119
}
120+
defer resp.Body.Close()
118121

119122
if resp.StatusCode != http.StatusOK {
120-
return nil, fmt.Errorf("received unexpected status code %d from IMDS", resp.StatusCode)
123+
return nil, errors.Wrapf(ErrUnexpectedStatusCode, "unexpected status code %d", resp.StatusCode)
121124
}
122125

123126
var m map[string]any

cns/imds/client_test.go

+5-4
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ func TestGetVMUniqueID(t *testing.T) {
2424
metadataHeader := r.Header.Get("Metadata")
2525
assert.Equal(t, "true", metadataHeader)
2626
w.WriteHeader(http.StatusOK)
27-
_, err := w.Write(computeMetadata)
28-
require.NoError(t, err, "error writing response")
27+
_, writeErr := w.Write(computeMetadata)
28+
require.NoError(t, writeErr, "error writing response")
2929
}))
3030
defer mockIMDSServer.Close()
3131

@@ -52,13 +52,14 @@ func TestIMDSInternalServerError(t *testing.T) {
5252
imdsClient := imds.NewClient(imds.Endpoint(mockIMDSServer.URL), imds.RetryAttempts(1))
5353

5454
_, err := imdsClient.GetVMUniqueID(context.Background())
55-
require.Error(t, err, "expected internal server error")
55+
require.ErrorIs(t, err, imds.ErrUnexpectedStatusCode, "expected internal server error")
5656
}
5757

5858
func TestIMDSInvalidJSON(t *testing.T) {
5959
mockIMDSServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
6060
w.WriteHeader(http.StatusOK)
61-
w.Write([]byte("not json"))
61+
_, err := w.Write([]byte("not json"))
62+
require.NoError(t, err)
6263
}))
6364
defer mockIMDSServer.Close()
6465

0 commit comments

Comments
 (0)