mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-30 06:54:01 +00:00
Merge pull request #49081 from brendandburns/imds
Automatic merge from submit-queue (batch tested with PRs 49081, 49318, 49219, 48989, 48486) Fix instance metadata service URL. Small fix @colemickens @jackfrancis
This commit is contained in:
commit
9b8bc25838
@ -129,6 +129,7 @@ type Cloud struct {
|
||||
DisksClient disk.DisksClient
|
||||
operationPollRateLimiter flowcontrol.RateLimiter
|
||||
resourceRequestBackoff wait.Backoff
|
||||
metadata *InstanceMetadata
|
||||
|
||||
*BlobDiskController
|
||||
*ManagedDiskController
|
||||
@ -318,6 +319,8 @@ func NewCloud(configReader io.Reader) (cloudprovider.Interface, error) {
|
||||
az.CloudProviderBackoffJitter)
|
||||
}
|
||||
|
||||
az.metadata = NewInstanceMetadata()
|
||||
|
||||
if err := initDiskControllers(&az); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
@ -22,14 +22,7 @@ import (
|
||||
"net/http"
|
||||
)
|
||||
|
||||
// This is just for tests injection
|
||||
var metadataURL = "http://169.254.169.254/metadata"
|
||||
|
||||
// SetMetadataURLForTesting is used to modify the URL used for
|
||||
// accessing the metadata server. Should only be used for testing!
|
||||
func SetMetadataURLForTesting(url string) {
|
||||
metadataURL = url
|
||||
}
|
||||
const metadataURL = "http://169.254.169.254/metadata/"
|
||||
|
||||
// NetworkMetadata contains metadata about an instance's network
|
||||
type NetworkMetadata struct {
|
||||
@ -61,9 +54,26 @@ type Subnet struct {
|
||||
Prefix string `json:"prefix"`
|
||||
}
|
||||
|
||||
// InstanceMetadata represents access to the Azure instance metadata server.
|
||||
type InstanceMetadata struct {
|
||||
baseURL string
|
||||
}
|
||||
|
||||
// NewInstanceMetadata creates an instance of the InstanceMetadata accessor object.
|
||||
func NewInstanceMetadata() *InstanceMetadata {
|
||||
return &InstanceMetadata{
|
||||
baseURL: metadataURL,
|
||||
}
|
||||
}
|
||||
|
||||
// makeMetadataURL makes a complete metadata URL from the given path.
|
||||
func (i *InstanceMetadata) makeMetadataURL(path string) string {
|
||||
return i.baseURL + path
|
||||
}
|
||||
|
||||
// QueryMetadataJSON queries the metadata server and populates the passed in object
|
||||
func QueryMetadataJSON(path string, obj interface{}) error {
|
||||
data, err := queryMetadataBytes(path, "json")
|
||||
func (i *InstanceMetadata) QueryMetadataJSON(path string, obj interface{}) error {
|
||||
data, err := i.queryMetadataBytes(path, "json")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@ -71,18 +81,18 @@ func QueryMetadataJSON(path string, obj interface{}) error {
|
||||
}
|
||||
|
||||
// QueryMetadataText queries the metadata server and returns the corresponding text
|
||||
func QueryMetadataText(path string) (string, error) {
|
||||
data, err := queryMetadataBytes(path, "text")
|
||||
func (i *InstanceMetadata) QueryMetadataText(path string) (string, error) {
|
||||
data, err := i.queryMetadataBytes(path, "text")
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return string(data), err
|
||||
}
|
||||
|
||||
func queryMetadataBytes(path, format string) ([]byte, error) {
|
||||
func (i *InstanceMetadata) queryMetadataBytes(path, format string) ([]byte, error) {
|
||||
client := &http.Client{}
|
||||
|
||||
req, err := http.NewRequest("GET", metadataURL+path, nil)
|
||||
req, err := http.NewRequest("GET", i.makeMetadataURL(path), nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
@ -30,7 +30,7 @@ import (
|
||||
// NodeAddresses returns the addresses of the specified instance.
|
||||
func (az *Cloud) NodeAddresses(name types.NodeName) ([]v1.NodeAddress, error) {
|
||||
if az.UseInstanceMetadata {
|
||||
text, err := QueryMetadataText("instance/network/interface/0/ipv4/ipAddress/0/privateIpAddress")
|
||||
text, err := az.metadata.QueryMetadataText("instance/network/interface/0/ipv4/ipAddress/0/privateIpAddress")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
@ -821,6 +821,14 @@ func TestSplitProviderID(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestMetadataURLGeneration(t *testing.T) {
|
||||
metadata := NewInstanceMetadata()
|
||||
fullPath := metadata.makeMetadataURL("some/path")
|
||||
if fullPath != "http://169.254.169.254/metadata/some/path" {
|
||||
t.Errorf("Expected http://169.254.169.254/metadata/some/path saw %s", fullPath)
|
||||
}
|
||||
}
|
||||
|
||||
func TestMetadataParsing(t *testing.T) {
|
||||
data := `
|
||||
{
|
||||
@ -866,10 +874,12 @@ func TestMetadataParsing(t *testing.T) {
|
||||
}))
|
||||
defer server.Close()
|
||||
|
||||
SetMetadataURLForTesting(server.URL)
|
||||
metadata := &InstanceMetadata{
|
||||
baseURL: server.URL,
|
||||
}
|
||||
|
||||
networkJSON := NetworkMetadata{}
|
||||
if err := QueryMetadataJSON("/some/path", &networkJSON); err != nil {
|
||||
if err := metadata.QueryMetadataJSON("/some/path", &networkJSON); err != nil {
|
||||
t.Errorf("Unexpected error: %v", err)
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user