Skip to content

Commit

Permalink
Merge pull request #5294 from twz123/remove-requests
Browse files Browse the repository at this point in the history
Replace requests module with standard library http
  • Loading branch information
twz123 authored Dec 10, 2024
2 parents 537f1bb + 3fa3c68 commit fa0ab80
Show file tree
Hide file tree
Showing 8 changed files with 192 additions and 103 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
71 changes: 0 additions & 71 deletions pkg/node/nodehostname.go

This file was deleted.

44 changes: 44 additions & 0 deletions pkg/node/nodename.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
Copyright 2023 k0s authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package node

import (
"context"
"fmt"

nodeutil "k8s.io/component-helpers/node/util"
)

// GetNodename returns the node name for the node taking OS, cloud provider and override into account
func GetNodename(override string) (string, error) {
return getNodename(context.TODO(), override)
}

func getNodename(ctx context.Context, override string) (string, error) {
if override == "" {
var err error
override, err = defaultNodenameOverride(ctx)
if err != nil {
return "", err
}
}
nodeName, err := nodeutil.GetHostname(override)
if err != nil {
return "", fmt.Errorf("failed to determine node name: %w", err)
}
return nodeName, nil
}
27 changes: 27 additions & 0 deletions pkg/node/nodename_other.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
//go:build !windows

/*
Copyright 2024 k0s authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package node

import (
"context"
)

func defaultNodenameOverride(context.Context) (string, error) {
return "", nil // no default override
}
47 changes: 47 additions & 0 deletions pkg/node/nodename_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
Copyright 2023 k0s authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package node

import (
"runtime"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
nodeutil "k8s.io/component-helpers/node/util"
)

func TestGetNodename(t *testing.T) {
t.Run("should_always_return_override_if_given", func(t *testing.T) {
name, err := GetNodename("override")
if assert.NoError(t, err) {
assert.Equal(t, "override", name)
}
})

if runtime.GOOS != "windows" {
kubeHostname, err := nodeutil.GetHostname("")
require.NoError(t, err)

t.Run("should_call_kubernetes_hostname_helper_on_linux", func(t *testing.T) {
name, err := GetNodename("")
if assert.NoError(t, err) {
assert.Equal(t, kubeHostname, name)
}
})
}
}
56 changes: 56 additions & 0 deletions pkg/node/nodename_windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
Copyright 2024 k0s authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package node

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

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

// A URL that may be retrieved to determine the nodename.
type nodenameURL 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")

client := http.Client{
Transport: &http.Transport{DisableKeepAlives: true},
Timeout: 1 * time.Second,
}

req, err := http.NewRequestWithContext(ctx, http.MethodGet, string(url), nil)
if err != nil {
return "", err
}

// 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
}
47 changes: 18 additions & 29 deletions pkg/node/nodehostname_test.go → pkg/node/nodename_windows_test.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright 2023 k0s authors
Copyright 2024 k0s authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand All @@ -23,49 +23,38 @@ import (
"net/url"
"testing"

"github.com/k0sproject/k0s/pkg/k0scontext"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
nodeutil "k8s.io/component-helpers/node/util"
)

func TestGetNodename(t *testing.T) {

func TestGetNodenameWindows(t *testing.T) {
kubeHostname, err := nodeutil.GetHostname("")
require.NoError(t, err)
baseURL := startFakeMetadataServer(t)
t.Run("should_always_return_override_if_given", func(t *testing.T) {
name, err := GetNodename("override")
require.Equal(t, "override", name)
require.NoError(t, err)
})

t.Run("should_call_kubernetes_hostname_helper_on_linux", func(t *testing.T) {
name, err := GetNodename("")
name2, err2 := nodeutil.GetHostname("")
require.Equal(t, name, name2)
require.NoError(t, err)
require.NoError(t, err2)
})

t.Run("windows_no_metadata_service_available", func(t *testing.T) {
name, err := getNodeNameWindows("", baseURL)
nodename, err2 := nodeutil.GetHostname("")
require.NoError(t, err)
require.NoError(t, err2)
require.Equal(t, nodename, name)
t.Run("no_metadata_service_available", func(t *testing.T) {
ctx := k0scontext.WithValue(context.TODO(), nodenameURL(baseURL))
name, err := getNodename(ctx, "")
if assert.NoError(t, err) {
assert.Equal(t, kubeHostname, name)
}
})

t.Run("windows_metadata_service_is_available", func(t *testing.T) {
name, err := getNodeNameWindows("", baseURL+"/latest/meta-data/local-hostname")
nodename, err2 := nodeutil.GetHostname("")
require.NoError(t, err)
require.NoError(t, err2)
require.NotEqual(t, nodename, name)
t.Run("metadata_service_is_available", func(t *testing.T) {
ctx := k0scontext.WithValue(context.TODO(), nodenameURL(baseURL+"/latest/meta-data/local-hostname"))
name, err := getNodename(ctx, "")
if assert.NoError(t, err) {
assert.Equal(t, "some-hostname from aws_metadata", name)
}
})
}

func startFakeMetadataServer(t *testing.T) string {
mux := http.NewServeMux()
mux.HandleFunc("/latest/meta-data/local-hostname", func(w http.ResponseWriter, r *http.Request) {
_, err := w.Write([]byte("some-hostname-from-metadata"))
_, err := w.Write([]byte("Some-hostname from AWS_metadata\n"))
assert.NoError(t, err)
})
server := &http.Server{Addr: "localhost:0", Handler: mux}
Expand Down

0 comments on commit fa0ab80

Please sign in to comment.