Skip to content

Commit

Permalink
WIP remove requests
Browse files Browse the repository at this point in the history
Signed-off-by: Tom Wieczorek <[email protected]>
  • Loading branch information
twz123 committed Jul 16, 2024
1 parent 0291632 commit 6a309c4
Show file tree
Hide file tree
Showing 7 changed files with 152 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-20200428143746-21a406dcc535
github.com/avast/retry-go v3.0.0+incompatible
github.com/bombsimon/logrusr/v4 v4.1.0
github.com/carlmjohnson/requests v0.23.5
github.com/cavaliergopher/grab/v3 v3.0.1
github.com/cilium/ebpf v0.15.0
github.com/cloudflare/cfssl v1.6.4
Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,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.23.5 h1:NPANcAofwwSuC6SIMwlgmHry2V3pLrSqRiSBKYbNHHA=
github.com/carlmjohnson/requests v0.23.5/go.mod h1:zG9P28thdRnN61aD7iECFhH5iGGKX2jIjKQD9kqYH+o=
github.com/cavaliergopher/grab/v3 v3.0.1 h1:4z7TkBfmPjmLAAmkkAZNX/6QJ1nNFdv3SdIHXju0Fr4=
github.com/cavaliergopher/grab/v3 v3.0.1/go.mod h1:1U/KNnD+Ft6JJiYoYBAimKH2XrYptb8Kl3DFGmsjpq4=
github.com/cenkalti/backoff/v4 v4.2.1 h1:y4OZtCnogmCPw98Zjyt5a6+QwPLGkiQsYW5oUqylYbM=
Expand Down
71 changes: 0 additions & 71 deletions pkg/node/nodehostname.go

This file was deleted.

48 changes: 48 additions & 0 deletions pkg/node/nodename.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
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)
}

// An URL that may be used to determine the nodename.
type nodenameURL string

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
}
9 changes: 9 additions & 0 deletions pkg/node/nodename_other.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
//go:build !windows

package node

import "context"

func defaultNodenameOverride(ctx context.Context) (string, error) {
return "", nil // no-op on non-windows
}
68 changes: 39 additions & 29 deletions pkg/node/nodehostname_test.go → pkg/node/nodename_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,51 +21,61 @@ import (
"net"
"net/http"
"net/url"
"runtime"
"testing"

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

"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) {
ctx := k0scontext.WithValue[nodenameURL](context.TODO(), "unused")

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.Nil(t, err)
})
kubeHostname, err := nodeutil.GetHostname("")
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.Nil(t, err)
require.Nil(t, err2)
})

t.Run("windows_no_metadata_service_available", func(t *testing.T) {
name, err := getNodeNameWindows("", baseURL)
nodename, err2 := nodeutil.GetHostname("")
require.Nil(t, err)
require.Nil(t, err2)
require.Equal(t, nodename, name)
t.Run("should_always_return_override_if_given", func(t *testing.T) {
name, err := getNodename(ctx, "override")
if assert.NoError(t, err) {
assert.Equal(t, "override", 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.Nil(t, err)
require.Nil(t, err2)
require.NotEqual(t, nodename, name)
})
if runtime.GOOS == "windows" {
baseURL := startFakeMetadataServer(t)

t.Run("windows_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) {
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)
}
})
} else {
t.Run("should_call_kubernetes_hostname_helper", func(t *testing.T) {
name, err := getNodename(ctx, "")
if assert.NoError(t, err) {
assert.Equal(t, kubeHostname, 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
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 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"
"io"
"net/http"
"time"

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

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

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

// tests may inject another URL
url := k0scontext.ValueOr(ctx, awsMetaInformationHostnameURL)

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
}

0 comments on commit 6a309c4

Please sign in to comment.