-
Notifications
You must be signed in to change notification settings - Fork 373
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5294 from twz123/remove-requests
Replace requests module with standard library http
- Loading branch information
Showing
8 changed files
with
192 additions
and
103 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters