diff --git a/go.mod b/go.mod index 8f9630f1adf2..df0e2318226f 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum index ea030e6039b4..8c83b65aa2fa 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/pkg/node/nodehostname.go b/pkg/node/nodehostname.go deleted file mode 100644 index 6e133cde30e5..000000000000 --- a/pkg/node/nodehostname.go +++ /dev/null @@ -1,71 +0,0 @@ -/* -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" - "runtime" - "time" - - "github.com/carlmjohnson/requests" - 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) { - if runtime.GOOS == "windows" { - return getNodeNameWindows(override, awsMetaInformationHostnameURL) - } - nodeName, err := nodeutil.GetHostname(override) - if err != nil { - return "", fmt.Errorf("failed to determine node name: %w", err) - } - return nodeName, nil -} - -const awsMetaInformationHostnameURL = "http://169.254.169.254/latest/meta-data/local-hostname" - -func getHostnameFromAwsMeta(url string) string { - ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second) - defer cancel() - - var s string - err := requests. - URL(url). - ToString(&s). - Fetch(ctx) - // if status code is 2XX and no transport error, we assume we are running on ec2 - if err != nil { - return "" - } - return s -} - -func getNodeNameWindows(override string, metadataURL string) (string, error) { - // if we have explicit hostnameOverride, we use it as is even on windows - if override != "" { - return nodeutil.GetHostname(override) - } - - // we need to check if we have EC2 dns name available - if h := getHostnameFromAwsMeta(metadataURL); h != "" { - return h, nil - } - // otherwise we use the k8s hostname helper - return nodeutil.GetHostname(override) -} diff --git a/pkg/node/nodename.go b/pkg/node/nodename.go new file mode 100644 index 000000000000..e8f0b710aa9d --- /dev/null +++ b/pkg/node/nodename.go @@ -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 +} diff --git a/pkg/node/nodename_other.go b/pkg/node/nodename_other.go new file mode 100644 index 000000000000..57fcb36b2aa0 --- /dev/null +++ b/pkg/node/nodename_other.go @@ -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 +} diff --git a/pkg/node/nodename_test.go b/pkg/node/nodename_test.go new file mode 100644 index 000000000000..a6e5ee99945c --- /dev/null +++ b/pkg/node/nodename_test.go @@ -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) + } + }) + } +} diff --git a/pkg/node/nodename_windows.go b/pkg/node/nodename_windows.go new file mode 100644 index 000000000000..f70367aef95f --- /dev/null +++ b/pkg/node/nodename_windows.go @@ -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 +} diff --git a/pkg/node/nodehostname_test.go b/pkg/node/nodename_windows_test.go similarity index 59% rename from pkg/node/nodehostname_test.go rename to pkg/node/nodename_windows_test.go index 73d416ec634f..58d611bca585 100644 --- a/pkg/node/nodehostname_test.go +++ b/pkg/node/nodename_windows_test.go @@ -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. @@ -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}