Skip to content

Commit 524f83d

Browse files
committed
feat: use official Go SDK to fetch GCP instance metadata
This enables fetching of hostname as well. Signed-off-by: Andrey Smirnov <andrey.smirnov@talos-systems.com>
1 parent d2a7e08 commit 524f83d

File tree

3 files changed

+30
-111
lines changed

3 files changed

+30
-111
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ require (
2929
)
3030

3131
require (
32+
cloud.google.com/go v0.99.0
3233
github.com/AlekSi/pointer v1.2.0
3334
github.com/BurntSushi/toml v0.4.1
3435
github.com/beevik/ntp v0.3.0
@@ -123,7 +124,6 @@ require (
123124
)
124125

125126
require (
126-
cloud.google.com/go v0.99.0 // indirect
127127
cloud.google.com/go/storage v1.10.0 // indirect
128128
github.com/Azure/go-ansiterm v0.0.0-20210617225240-d185dfc1b5a1 // indirect
129129
github.com/Azure/go-autorest v14.2.0+incompatible // indirect

internal/app/machined/pkg/runtime/v1alpha1/platform/gcp/gcp.go

Lines changed: 29 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -6,24 +6,14 @@ package gcp
66

77
import (
88
"context"
9-
"encoding/json"
10-
"log"
119
"net"
10+
"strings"
1211

12+
"cloud.google.com/go/compute/metadata"
1313
"github.com/talos-systems/go-procfs/procfs"
1414

1515
"github.com/talos-systems/talos/internal/app/machined/pkg/runtime"
1616
"github.com/talos-systems/talos/internal/app/machined/pkg/runtime/v1alpha1/platform/errors"
17-
"github.com/talos-systems/talos/pkg/download"
18-
)
19-
20-
// Ref: https://cloud.google.com/compute/docs/storing-retrieving-metadata
21-
// ex, curl -H "Metadata-Flavor: Google" 'http://169.254.169.254/computeMetadata/v1/instance/network-interfaces/?recursive=true'
22-
const (
23-
// GCUserDataEndpoint is the local metadata endpoint inside of DO.
24-
GCUserDataEndpoint = "http://metadata.google.internal/computeMetadata/v1/instance/attributes/user-data"
25-
// GCExternalIPEndpoint displays all external addresses associated with the instance.
26-
GCExternalIPEndpoint = "http://metadata.google.internal/computeMetadata/v1/instance/network-interfaces/?recursive=true"
2717
)
2818

2919
// GCP is the concrete type that implements the platform.Platform interface.
@@ -36,17 +26,32 @@ func (g *GCP) Name() string {
3626

3727
// Configuration implements the platform.Platform interface.
3828
func (g *GCP) Configuration(ctx context.Context) ([]byte, error) {
39-
log.Printf("fetching machine config from: %q", GCUserDataEndpoint)
29+
userdata, err := metadata.InstanceAttributeValue("user-data")
30+
if err != nil {
31+
if _, ok := err.(metadata.NotDefinedError); ok {
32+
return nil, errors.ErrNoConfigSource
33+
}
4034

41-
return download.Download(ctx, GCUserDataEndpoint,
42-
download.WithHeaders(map[string]string{"Metadata-Flavor": "Google"}),
43-
download.WithErrorOnNotFound(errors.ErrNoConfigSource),
44-
download.WithErrorOnEmptyResponse(errors.ErrNoConfigSource))
35+
return nil, err
36+
}
37+
38+
userdata = strings.TrimSpace(userdata)
39+
40+
if userdata == "" {
41+
return nil, errors.ErrNoConfigSource
42+
}
43+
44+
return []byte(userdata), nil
4545
}
4646

4747
// Hostname implements the platform.Platform interface.
4848
func (g *GCP) Hostname(context.Context) (hostname []byte, err error) {
49-
return nil, nil
49+
host, err := metadata.Hostname()
50+
if err != nil {
51+
return nil, err
52+
}
53+
54+
return []byte(host), nil
5055
}
5156

5257
// Mode implements the platform.Platform interface.
@@ -56,33 +61,17 @@ func (g *GCP) Mode() runtime.Mode {
5661

5762
// ExternalIPs implements the runtime.Platform interface.
5863
func (g *GCP) ExternalIPs(ctx context.Context) (addrs []net.IP, err error) {
59-
log.Printf("fetching externalIP from: %q", GCExternalIPEndpoint)
60-
61-
metadataNetworkConfig, err := download.Download(ctx, GCExternalIPEndpoint,
62-
download.WithHeaders(map[string]string{"Metadata-Flavor": "Google"}),
63-
download.WithErrorOnNotFound(errors.ErrNoExternalIPs),
64-
download.WithErrorOnEmptyResponse(errors.ErrNoExternalIPs))
64+
extIP, err := metadata.ExternalIP()
6565
if err != nil {
66-
return nil, err
67-
}
68-
69-
type metadata []struct {
70-
AccessConfigs []struct {
71-
ExternalIP string `json:"externalIp"`
72-
} `json:"accessConfigs"`
73-
}
66+
if _, ok := err.(metadata.NotDefinedError); ok {
67+
return nil, nil
68+
}
7469

75-
m := metadata{}
76-
if err = json.Unmarshal(metadataNetworkConfig, &m); err != nil {
7770
return nil, err
7871
}
7972

80-
for _, networkInterface := range m {
81-
for _, accessConfig := range networkInterface.AccessConfigs {
82-
if addr := net.ParseIP(accessConfig.ExternalIP); addr != nil {
83-
addrs = append(addrs, addr)
84-
}
85-
}
73+
if addr := net.ParseIP(extIP); addr != nil {
74+
addrs = append(addrs, addr)
8675
}
8776

8877
return addrs, nil

internal/app/machined/pkg/runtime/v1alpha1/platform/gcp/gcp_test.go

Lines changed: 0 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -12,73 +12,3 @@ func TestEmpty(t *testing.T) {
1212
// please remove it once any unit-test is added
1313
// for this package
1414
}
15-
16-
// TODO use this mock data for tests
17-
/*
18-
brad@instance-1:~$ curl -H "Metadata-Flavor: Google" 'http://169.254.169.254/computeMetadata/v1/instance/?recursive=true' | jq '.'
19-
% Total % Received % Xferd Average Speed Time Time Time Current
20-
Dload Upload Total Spent Left Speed
21-
100 1825 100 1825 0 0 285k 0 --:--:-- --:--:-- --:--:-- 297k
22-
{
23-
"attributes": {
24-
"ssh-keys": ""
25-
},
26-
"cpuPlatform": "Intel Haswell",
27-
"description": "",
28-
"disks": [
29-
{
30-
"deviceName": "instance-1",
31-
"index": 0,
32-
"mode": "READ_WRITE",
33-
"type": "PERSISTENT"
34-
}
35-
],
36-
"guestAttributes": {},
37-
"hostname": "instance-1.c.talos-testbed.internal",
38-
"id": 7413733082653629000,
39-
"image": "projects/debian-cloud/global/images/debian-9-stretch-v20190916",
40-
"licenses": [
41-
{
42-
"id": "1000205"
43-
}
44-
],
45-
"machineType": "projects/381598048798/machineTypes/f1-micro",
46-
"maintenanceEvent": "NONE",
47-
"name": "instance-1",
48-
"networkInterfaces": [
49-
{
50-
"accessConfigs": [
51-
{
52-
"externalIp": "35.239.151.17",
53-
"type": "ONE_TO_ONE_NAT"
54-
}
55-
],
56-
"dnsServers": [
57-
"169.254.169.254"
58-
],
59-
"forwardedIps": [],
60-
"gateway": "10.128.0.1",
61-
"ip": "10.128.15.237",
62-
"ipAliases": [],
63-
"mac": "42:01:0a:80:0f:ed",
64-
"mtu": 1460,
65-
"network": "projects/381598048798/networks/default",
66-
"subnetmask": "255.255.240.0",
67-
"targetInstanceIps": []
68-
}
69-
],
70-
"preempted": "FALSE",
71-
"remainingCpuTime": -1,
72-
"scheduling": {
73-
"automaticRestart": "TRUE",
74-
"onHostMaintenance": "MIGRATE",
75-
"preemptible": "FALSE"
76-
},
77-
"serviceAccounts": {},
78-
"tags": [],
79-
"virtualClock": {
80-
"driftToken": "0"
81-
},
82-
"zone": "projects/381598048798/zones/us-central1-f"
83-
}
84-
*/

0 commit comments

Comments
 (0)