forked from knative/pkg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
request.go
170 lines (147 loc) · 6.08 KB
/
request.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
167
168
169
170
/*
Copyright 2019 The Knative Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package gke
import (
"errors"
container "google.golang.org/api/container/v1beta1"
)
const defaultGKEVersion = "latest"
// Request contains all settings collected for cluster creation
type Request struct {
// Project: name of the gcloud project for the cluster
Project string
// GKEVersion: GKE version of the cluster, default to be latest if not provided
GKEVersion string
// ReleaseChannel: GKE release channel. Only one of GKEVersion or ReleaseChannel can be
// specified at a time.
// https://cloud.google.com/kubernetes-engine/docs/concepts/release-channels
ReleaseChannel string
// ClusterName: name of the cluster
ClusterName string
// MinNodes: the minimum number of nodes of the cluster
MinNodes int64
// MaxNodes: the maximum number of nodes of the cluster
MaxNodes int64
// NodeType: node type of the cluster, e.g. e2-standard-4, e2-standard-8
NodeType string
// Region: region of the cluster, e.g. us-west1, us-central1
Region string
// Zone: default is none, must be provided together with region
Zone string
// Addons: cluster addons to be added to cluster, such as istio
Addons []string
// EnableWorkloadIdentity: whether to enable Workload Identity for this cluster
EnableWorkloadIdentity bool
// ServiceAccount: service account that will be used on this cluster
ServiceAccount string
}
// DeepCopy will make a deepcopy of the request struct.
func (r *Request) DeepCopy() *Request {
return &Request{
Project: r.Project,
GKEVersion: r.GKEVersion,
ReleaseChannel: r.ReleaseChannel,
ClusterName: r.ClusterName,
MinNodes: r.MinNodes,
MaxNodes: r.MaxNodes,
NodeType: r.NodeType,
Region: r.Region,
Zone: r.Zone,
Addons: r.Addons,
EnableWorkloadIdentity: r.EnableWorkloadIdentity,
ServiceAccount: r.ServiceAccount,
}
}
// NewCreateClusterRequest returns a new CreateClusterRequest that can be used in gcloud SDK.
func NewCreateClusterRequest(request *Request) (*container.CreateClusterRequest, error) {
if request.ClusterName == "" {
return nil, errors.New("cluster name cannot be empty")
}
if request.MinNodes <= 0 {
return nil, errors.New("min nodes must be larger than 1")
}
if request.MinNodes > request.MaxNodes {
return nil, errors.New("min nodes cannot be larger than max nodes")
}
if request.NodeType == "" {
return nil, errors.New("node type cannot be empty")
}
if request.EnableWorkloadIdentity && request.Project == "" {
return nil, errors.New("project cannot be empty if you want Workload Identity")
}
if request.GKEVersion != "" && request.ReleaseChannel != "" {
return nil, errors.New("can only specify one of GKE version or release channel (not both)")
}
ccr := &container.CreateClusterRequest{
Cluster: &container.Cluster{
NodePools: []*container.NodePool{
{
Name: "default-pool",
InitialNodeCount: request.MinNodes,
Autoscaling: &container.NodePoolAutoscaling{
Enabled: true,
MinNodeCount: request.MinNodes,
MaxNodeCount: request.MaxNodes,
},
Config: &container.NodeConfig{
MachineType: request.NodeType,
// The set of Google API scopes to be made available on all
// of the node VMs under the "default" service account.
// If unspecified, no scopes are added, unless Cloud Logging or
// Cloud Monitoring are enabled, in which case their required
// scopes will be added.
// `https://www.googleapis.com/auth/devstorage.read_only` is required
// for communicating with **gcr.io**, and it's included in cloud-platform scope.
// TODO(chizhg): give more fine granular scope based on the actual needs.
OauthScopes: []string{container.CloudPlatformScope},
},
},
},
Name: request.ClusterName,
// Installing addons after cluster creation takes at least 5
// minutes, so install addons as part of cluster creation, which
// doesn't seem to add much time on top of cluster creation
AddonsConfig: GetAddonsConfig(request.Addons),
// Equivalent to --enable-basic-auth, so that user:pass can be
// later on retrieved for setting up cluster roles. Use the
// default username from gcloud command, the password will be
// automatically generated by GKE SDK
MasterAuth: &container.MasterAuth{Username: "admin"},
},
}
if request.EnableWorkloadIdentity {
// Equivalent to --identity-namespace=[PROJECT_ID].svc.id.goog, then
// we can configure a Kubernetes service account to act as a Google
// service account.
ccr.Cluster.WorkloadIdentityConfig = &container.WorkloadIdentityConfig{
IdentityNamespace: request.Project + ".svc.id.goog",
}
}
if request.ServiceAccount != "" {
// The Google Cloud Platform Service Account to be used by the node VMs.
// If a service account is specified, the cloud-platform and userinfo.email scopes are used.
// If no Service Account is specified, the project default service account is used.
ccr.Cluster.NodePools[0].Config.ServiceAccount = request.ServiceAccount
}
// Manage the GKE cluster version. Only one of initial cluster version or release channel can be specified.
if request.ReleaseChannel != "" {
ccr.Cluster.ReleaseChannel = &container.ReleaseChannel{Channel: request.ReleaseChannel}
} else if request.GKEVersion != "" {
ccr.Cluster.InitialClusterVersion = request.GKEVersion
} else {
// The default cluster version is not latest, has to explicitly
// set it as "latest"
ccr.Cluster.InitialClusterVersion = defaultGKEVersion
}
return ccr, nil
}