Skip to content

Commit

Permalink
Replace requests module with standard library http
Browse files Browse the repository at this point in the history
There's no need to pull in an extra dependency for such a simple use
case.

Signed-off-by: Tom Wieczorek <[email protected]>
  • Loading branch information
twz123 committed Nov 26, 2024
1 parent e77e442 commit 3fa3c68
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 19 deletions.
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ require (
github.com/asaskevich/govalidator v0.0.0-20230301143203-a9d515a09cc2
github.com/avast/retry-go v3.0.0+incompatible
github.com/bombsimon/logrusr/v4 v4.1.0
github.com/carlmjohnson/requests v0.24.2
github.com/cilium/ebpf v0.16.0
github.com/cloudflare/cfssl v1.6.4
github.com/containerd/cgroups/v3 v3.0.4
Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,6 @@ github.com/bugsnag/osext v0.0.0-20130617224835-0dd3f918b21b h1:otBG+dV+YK+Soembj
github.com/bugsnag/osext v0.0.0-20130617224835-0dd3f918b21b/go.mod h1:obH5gd0BsqsP2LwDJ9aOkm/6J86V6lyAXCoQWGw3K50=
github.com/bugsnag/panicwrap v0.0.0-20151223152923-e2c28503fcd0 h1:nvj0OLI3YqYXer/kZD8Ri1aaunCxIEsOst1BVJswV0o=
github.com/bugsnag/panicwrap v0.0.0-20151223152923-e2c28503fcd0/go.mod h1:D/8v3kj0zr8ZAKg1AQ6crr+5VwKN5eIywRkfhyM/+dE=
github.com/carlmjohnson/requests v0.24.2 h1:JDakhAmTIKL/qL/1P7Kkc2INGBJIkIFP6xUeUmPzLso=
github.com/carlmjohnson/requests v0.24.2/go.mod h1:duYA/jDnyZ6f3xbcF5PpZ9N8clgopubP2nK5i6MVMhU=
github.com/cenkalti/backoff/v4 v4.3.0 h1:MyRJ/UdXutAwSAT+s3wNd7MfTIcy71VQueUuFK343L8=
github.com/cenkalti/backoff/v4 v4.3.0/go.mod h1:Y3VNntkOUPxTVeUxJ/G5vcM//AlwfmyYozVcomhLiZE=
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
Expand Down
6 changes: 5 additions & 1 deletion pkg/node/nodename.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,11 @@ func GetNodename(override string) (string, error) {

func getNodename(ctx context.Context, override string) (string, error) {
if override == "" {
override = defaultNodenameOverride(ctx)
var err error
override, err = defaultNodenameOverride(ctx)
if err != nil {
return "", err
}
}
nodeName, err := nodeutil.GetHostname(override)
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions pkg/node/nodename_other.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,6 @@ import (
"context"
)

func defaultNodenameOverride(context.Context) string {
return "" // no default override
func defaultNodenameOverride(context.Context) (string, error) {
return "", nil // no default override
}
34 changes: 21 additions & 13 deletions pkg/node/nodename_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,31 +18,39 @@ package node

import (
"context"
"io"
"net/http"
"time"

"github.com/k0sproject/k0s/pkg/k0scontext"

"github.com/carlmjohnson/requests"
)

// A URL that may be retrieved to determine the nodename.
type nodenameURL string

func defaultNodenameOverride(ctx context.Context) string {
func defaultNodenameOverride(ctx context.Context) (string, error) {
// we need to check if we have EC2 dns name available
url := k0scontext.ValueOr[nodenameURL](ctx, "http://169.254.169.254/latest/meta-data/local-hostname")

ctx, cancel := context.WithTimeout(ctx, 1*time.Second)
defer cancel()
client := http.Client{
Transport: &http.Transport{DisableKeepAlives: true},
Timeout: 1 * time.Second,
}

var s string
err := requests.
URL(string(url)).
ToString(&s).
Fetch(ctx)
// if status code is 2XX and no transport error, we assume we are running on ec2
req, err := http.NewRequestWithContext(ctx, http.MethodGet, string(url), nil)
if err != nil {
return ""
return "", err
}
return s

// if status code is 2XX we assume we are running on ec2
if resp, err := client.Do(req); err == nil {
defer resp.Body.Close()
if resp.StatusCode >= 200 && resp.StatusCode <= 299 {
if bytes, err := io.ReadAll(resp.Body); err == nil {
return string(bytes), nil
}
}
}

return "", nil
}

0 comments on commit 3fa3c68

Please sign in to comment.