Skip to content

Commit

Permalink
feat: gather plaform dns names
Browse files Browse the repository at this point in the history
Retrieve the DNS names of instances from the platform metadata.

Signed-off-by: Serge Logvinov <serge.logvinov@sinextra.dev>
Signed-off-by: Andrey Smirnov <andrey.smirnov@siderolabs.com>
  • Loading branch information
sergelogvinov authored and smira committed May 7, 2024
1 parent 0b0f999 commit f4d7b9d
Show file tree
Hide file tree
Showing 17 changed files with 223 additions and 62 deletions.
2 changes: 2 additions & 0 deletions api/resource/definitions/runtime/runtime.proto
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,8 @@ message PlatformMetadataSpec {
string instance_id = 6;
string provider_id = 7;
bool spot = 8;
string internal_dns = 9;
string external_dns = 10;
}

// SecurityStateSpec describes the security state resource properties.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ func (a *AWS) ParseMetadata(metadata *MetadataConfig) (*runtime.PlatformNetworkC
InstanceID: metadata.InstanceID,
ProviderID: fmt.Sprintf("aws:///%s/%s", metadata.Zone, metadata.InstanceID),
Spot: metadata.InstanceLifeCycle == "spot",
InternalDNS: metadata.InternalDNS,
ExternalDNS: metadata.ExternalDNS,
}

return networkConfig, nil
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,15 @@ type MetadataConfig struct {
InstanceLifeCycle string `json:"instance-life-cycle,omitempty"`
PublicIPv4 string `json:"public-ipv4,omitempty"`
PublicIPv6 string `json:"ipv6,omitempty"`
InternalDNS string `json:"local-hostname,omitempty"`
ExternalDNS string `json:"public-hostname,omitempty"`
Region string `json:"region,omitempty"`
Zone string `json:"zone,omitempty"`
}

//nolint:gocyclo
func (a *AWS) getMetadata(ctx context.Context) (*MetadataConfig, error) {
// https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/instancedata-data-retrieval.html
getMetadataKey := func(key string) (string, error) {
resp, err := a.metadataClient.GetMetadata(ctx, &imds.GetMetadataInput{
Path: key,
Expand Down Expand Up @@ -77,6 +80,14 @@ func (a *AWS) getMetadata(ctx context.Context) (*MetadataConfig, error) {
return nil, err
}

if metadata.InternalDNS, err = getMetadataKey("local-hostname"); err != nil {
return nil, err
}

if metadata.ExternalDNS, err = getMetadataKey("public-hostname"); err != nil {
return nil, err
}

if metadata.Region, err = getMetadataKey("placement/region"); err != nil {
return nil, err
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,14 +154,19 @@ func (a *Azure) ParseMetadata(metadata *ComputeMetadata, interfaceAddresses []Ne
zone = fmt.Sprintf("%s-%s", metadata.Location, metadata.Zone)
}

providerID, err := convertResourceGroupNameToLower(metadata.ResourceID)
if err != nil {
return nil, err
}

networkConfig.Metadata = &runtimeres.PlatformMetadataSpec{
Platform: a.Name(),
Hostname: metadata.OSProfile.ComputerName,
Region: strings.ToLower(metadata.Location),
Zone: strings.ToLower(zone),
InstanceType: metadata.VMSize,
InstanceID: metadata.ResourceID,
ProviderID: fmt.Sprintf("azure://%s", metadata.ResourceID),
ProviderID: fmt.Sprintf("azure://%s", providerID),
Spot: metadata.EvictionPolicy != "",
}

Expand Down Expand Up @@ -344,3 +349,19 @@ func (a *Azure) NetworkConfiguration(ctx context.Context, _ state.State, ch chan

return nil
}

// convertResourceGroupNameToLower converts the resource group name in the resource ID to be lowered.
// https://github.com/kubernetes-sigs/cloud-provider-azure/blob/4192b264611aebef8070505dd56680a862acfbbf/pkg/provider/azure_wrap.go#L91
func convertResourceGroupNameToLower(resourceID string) (string, error) {
// https://github.com/kubernetes-sigs/cloud-provider-azure/blob/4192b264611aebef8070505dd56680a862acfbbf/pkg/provider/azure_wrap.go#L37
azureResourceGroupNameRE := regexp.MustCompile(`.*/subscriptions/(?:.*)/resourceGroups/(.+)/providers/(?:.*)`)

matches := azureResourceGroupNameRE.FindStringSubmatch(resourceID)
if len(matches) != 2 {
return "", fmt.Errorf("%q isn't in Azure resource ID format %q", resourceID, azureResourceGroupNameRE.String())
}

resourceGroup := matches[1]

return strings.Replace(resourceID, resourceGroup, strings.ToLower(resourceGroup), 1), nil
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ import (
)

const (
// AzureMetadata documentation
// ref: https://learn.microsoft.com/en-us/azure/virtual-machines/instance-metadata-service
// ref: https://github.com/Azure/azure-rest-api-specs/blob/main/specification/imds/data-plane/Microsoft.InstanceMetadataService/stable/2023-07-01/examples/GetInstanceMetadata.json

// AzureInternalEndpoint is the Azure Internal Channel IP
// https://blogs.msdn.microsoft.com/mast/2015/05/18/what-is-the-ip-address-168-63-129-16/
AzureInternalEndpoint = "http://168.63.129.16"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@
"location": "CentralUS",
"name": "IMDSCanary",
"offer": "RHEL",
"osProfile": {
"computerName": "examplevmname"
},
"osType": "Linux",
"platformFaultDomain": "0",
"platformUpdateDomain": "0",
"publisher": "RedHat",
"resourceId": "000-000-000-000-000",
"resourceId": "/subscriptions/xxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxx/resourceGroups/Test/providers/Microsoft.Compute/virtualMachines/examplevmname",
"sku": "7.2",
"version": "7.2.20161026",
"vmId": "5c08b38e-4d57-4c23-ac45-aca61037f084",
"vmSize": "Standard_DS2"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,9 @@ externalIPs:
- 20.10.5.34
metadata:
platform: azure
hostname: examplevmname
region: centralus
zone: "0"
instanceType: Standard_DS2
instanceId: 000-000-000-000-000
providerId: azure://000-000-000-000-000
instanceId: /subscriptions/xxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxx/resourceGroups/Test/providers/Microsoft.Compute/virtualMachines/examplevmname
providerId: azure:///subscriptions/xxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxx/resourceGroups/test/providers/Microsoft.Compute/virtualMachines/examplevmname
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
)

const (
// https://cloud.google.com/compute/docs/metadata/overview
gcpResolverServer = "169.254.169.254"
gcpTimeServer = "metadata.google.internal"
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,13 +107,14 @@ type Bonds struct {

// MetadataConfig holds meta info.
type MetadataConfig struct {
Hostname string `yaml:"hostname,omitempty"`
LocalHostname string `yaml:"local-hostname,omitempty"`
InstanceID string `yaml:"instance-id,omitempty"`
InstanceType string `yaml:"instance-type,omitempty"`
ProviderID string `yaml:"provider-id,omitempty"`
Region string `yaml:"region,omitempty"`
Zone string `yaml:"zone,omitempty"`
Hostname string `yaml:"hostname,omitempty"`
InternalDNS string `json:"local-hostname,omitempty"`
ExternalDNS string `json:"public-hostname,omitempty"`
InstanceID string `yaml:"instance-id,omitempty"`
InstanceType string `yaml:"instance-type,omitempty"`
ProviderID string `yaml:"provider-id,omitempty"`
Region string `yaml:"region,omitempty"`
Zone string `yaml:"zone,omitempty"`
}

func (n *Nocloud) configFromNetwork(ctx context.Context, metaBaseURL string, r state.State) (metaConfig []byte, networkConfig []byte, machineConfig []byte, err error) {
Expand Down Expand Up @@ -264,11 +265,11 @@ func (n *Nocloud) acquireConfig(ctx context.Context, r state.State) (metadataCon

// Some providers may provide the hostname via user-data instead of meta-data (e.g. Proxmox VE)
// As long as the user doesn't use it for machine config, it can still be used to obtain the hostname
if metadata.Hostname == "" && metadata.LocalHostname == "" && machineConfigDl != nil {
if metadata.Hostname == "" && metadata.InternalDNS == "" && machineConfigDl != nil {
fallbackMetadata := &MetadataConfig{}
_ = yaml.Unmarshal(machineConfigDl, fallbackMetadata) //nolint:errcheck
metadata.Hostname = fallbackMetadata.Hostname
metadata.LocalHostname = fallbackMetadata.LocalHostname
metadata.InternalDNS = fallbackMetadata.InternalDNS
}

return metadataConfigDl, metadataNetworkConfigDl, machineConfigDl, metadata, err
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func (n *Nocloud) ParseMetadata(unmarshalledNetworkConfig *NetworkConfig, st sta

hostname := metadata.Hostname
if hostname == "" {
hostname = metadata.LocalHostname
hostname = metadata.InternalDNS
}

if hostname != "" {
Expand Down Expand Up @@ -71,6 +71,8 @@ func (n *Nocloud) ParseMetadata(unmarshalledNetworkConfig *NetworkConfig, st sta
ProviderID: metadata.ProviderID,
Region: metadata.Region,
Zone: metadata.Zone,
InternalDNS: metadata.InternalDNS,
ExternalDNS: metadata.ExternalDNS,
}

return networkConfig, nil
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,13 @@ func TestParseMetadata(t *testing.T) {
require.NoError(t, yaml.Unmarshal(tt.raw, &m))

mc := nocloud.MetadataConfig{
Hostname: "talos.fqdn",
InstanceID: "0",
Hostname: "talos.fqdn",
InternalDNS: "talos.fqdn",
InstanceID: "0",
}
mc2 := nocloud.MetadataConfig{
LocalHostname: "talos.fqdn",
InstanceID: "0",
InternalDNS: "talos.fqdn",
InstanceID: "0",
}

networkConfig, err := n.ParseMetadata(&m, st, &mc)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,3 +102,4 @@ metadata:
platform: nocloud
hostname: talos.fqdn
instanceId: "0"
internalDNS: talos.fqdn
Original file line number Diff line number Diff line change
Expand Up @@ -151,3 +151,4 @@ metadata:
platform: nocloud
hostname: talos.fqdn
instanceId: "0"
internalDNS: talos.fqdn
106 changes: 63 additions & 43 deletions pkg/machinery/api/resource/definitions/runtime/runtime.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit f4d7b9d

Please sign in to comment.