@@ -6,24 +6,14 @@ package gcp
6
6
7
7
import (
8
8
"context"
9
- "encoding/json"
10
- "log"
11
9
"net"
10
+ "strings"
12
11
12
+ "cloud.google.com/go/compute/metadata"
13
13
"github.com/talos-systems/go-procfs/procfs"
14
14
15
15
"github.com/talos-systems/talos/internal/app/machined/pkg/runtime"
16
16
"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"
27
17
)
28
18
29
19
// GCP is the concrete type that implements the platform.Platform interface.
@@ -36,17 +26,32 @@ func (g *GCP) Name() string {
36
26
37
27
// Configuration implements the platform.Platform interface.
38
28
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
+ }
40
34
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
45
45
}
46
46
47
47
// Hostname implements the platform.Platform interface.
48
48
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
50
55
}
51
56
52
57
// Mode implements the platform.Platform interface.
@@ -56,33 +61,17 @@ func (g *GCP) Mode() runtime.Mode {
56
61
57
62
// ExternalIPs implements the runtime.Platform interface.
58
63
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 ()
65
65
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
+ }
74
69
75
- m := metadata {}
76
- if err = json .Unmarshal (metadataNetworkConfig , & m ); err != nil {
77
70
return nil , err
78
71
}
79
72
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 )
86
75
}
87
76
88
77
return addrs , nil
0 commit comments