-
Notifications
You must be signed in to change notification settings - Fork 50
/
gke.go
166 lines (143 loc) · 4.8 KB
/
gke.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package resolve
import (
"io/ioutil"
"net/http"
"strings"
jxcore "github.com/jenkins-x/jx-api/v4/pkg/apis/core/v4beta1"
"github.com/jenkins-x/jx-helpers/v3/pkg/gitclient"
"github.com/jenkins-x/jx-helpers/v3/pkg/httphelpers"
"github.com/jenkins-x/jx-helpers/v3/pkg/stringhelpers"
"github.com/jenkins-x/jx-helpers/v3/pkg/termcolor"
"github.com/jenkins-x/jx-logging/v3/pkg/log"
"github.com/pkg/errors"
)
const (
// GKEMetadataEndpoint default Google metadata endpoint.
// See https://cloud.google.com/compute/docs/storing-retrieving-metadata#querying
GKEMetadataEndpoint = "http://metadata.google.internal/computeMetadata/v1/"
// paths in the REST API...
// GKEPathProjectID metadata endpoint path to the project ID string
GKEPathProjectID = "project/project-id"
// GKEPathProjectNumber metadata endpoint path to the project number
GKEPathProjectNumber = "project/numeric-project-id"
// GKEPathClusterName metadata endpoint path to the cluster name
GKEPathClusterName = "instance/attributes/cluster-name"
// GKEPathClusterLocation metadata endpoint path to the cluster location
GKEPathClusterLocation = "instance/attributes/cluster-location"
)
// GKEConfig the GKE specific configuration
type GKEConfig struct {
MetadataEndpoint string
}
// ResolveGKE resolves any missing GKE metadata
func (o *Options) ResolveGKE() error {
cluster := &o.requirements.Spec.Cluster
if cluster.GKEConfig == nil {
cluster.GKEConfig = &jxcore.GKEConfig{}
}
projectID := cluster.ProjectID
clusterName := cluster.ClusterName
projectNumber := cluster.GKEConfig.ProjectNumber
region := cluster.Region
zone := cluster.Zone
location := region
if location == "" {
location = zone
}
if projectID != "" && clusterName != "" && projectNumber != "" && (zone != "" || region != "") {
o.logGKEMetadata()
return nil
}
if !o.NoInClusterCheck && !IsInCluster() {
log.Logger().Warnf("cannot default GKE metadata as this command is not running inside the cluster")
return nil
}
log.Logger().Infof("resolving missing GKE project and cluster metadata from endpoint %s", o.getGKEMetadataEndpoint())
var err error
if projectID == "" {
cluster.ProjectID, err = o.getGKEMetadata(GKEPathProjectID)
if err != nil {
return err
}
}
if projectNumber == "" {
cluster.GKEConfig.ProjectNumber, err = o.getGKEMetadata(GKEPathProjectNumber)
if err != nil {
return err
}
}
if clusterName == "" {
cluster.ClusterName, err = o.getGKEMetadata(GKEPathClusterName)
if err != nil {
return err
}
}
if location == "" {
location, err := o.getGKEMetadata(GKEPathClusterLocation)
if err != nil {
return err
}
// TODO lets assume a zone for now
cluster.Zone = location
}
err = o.requirements.SaveConfig(o.requirementsFileName)
if err != nil {
return errors.Wrapf(err, "failed to save modified requirements file: %s", o.requirementsFileName)
}
log.Logger().Infof("resolved GKE project and cluster metadata and modified file %s", termcolor.ColorInfo(o.requirementsFileName))
o.logGKEMetadata()
if o.NoCommit {
return nil
}
gitter := o.GitClient()
_, err = gitter.Command(o.Dir, "add", "*")
if err != nil {
return errors.Wrapf(err, "failed to add to git")
}
err = gitclient.CommitIfChanges(gitter, o.Dir, "chore: default GKE project, cluster and location metadata")
if err != nil {
return errors.Wrapf(err, "failed to git commit the changes to the GKE project, cluster and location")
}
return nil
}
func (o *Options) getGKEMetadata(path string) (string, error) {
ep := o.getGKEMetadataEndpoint()
u := stringhelpers.UrlJoin(ep, path)
client := httphelpers.GetClient()
req, err := http.NewRequest("GET", u, nil)
if err != nil {
return "", errors.Wrapf(err, "failed to create http request for %s", u)
}
req.Header.Add("Metadata-Flavor", "Google")
resp, err := client.Do(req)
if err != nil {
if resp != nil {
return "", errors.Wrapf(err, "failed to GET endpoint %s with status %s", u, resp.Status)
}
return "", errors.Wrapf(err, "failed to GET endpoint %s", u)
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return "", errors.Wrapf(err, "failed to read response from %s", u)
}
return strings.TrimSpace(string(body)), nil
}
func (o *Options) getGKEMetadataEndpoint() string {
if o.GKEConfig.MetadataEndpoint == "" {
o.GKEConfig.MetadataEndpoint = GKEMetadataEndpoint
}
return o.GKEConfig.MetadataEndpoint
}
func (o *Options) logGKEMetadata() {
cluster := &o.requirements.Spec.Cluster
info := termcolor.ColorInfo
log.Logger().Infof("GKE project: %s", info(cluster.ProjectID))
log.Logger().Infof("project number: %s", info(cluster.GKEConfig.ProjectNumber))
log.Logger().Infof("cluster name: %s", info(cluster.ClusterName))
if cluster.Region != "" {
log.Logger().Infof("region: %s", info(cluster.Region))
} else {
log.Logger().Infof("zone: %s", info(cluster.Zone))
}
}