-
Notifications
You must be signed in to change notification settings - Fork 163
/
kubernetes.go
317 lines (263 loc) · 9.47 KB
/
kubernetes.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
// Copyright © 2018 Banzai Cloud
//
// 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 cluster
import (
"encoding/base64"
"strings"
"emperror.dev/errors"
"github.com/sirupsen/logrus"
"k8s.io/client-go/kubernetes"
"github.com/banzaicloud/pipeline/internal/global"
"github.com/banzaicloud/pipeline/internal/providers/kubernetes/kubernetesadapter"
"github.com/banzaicloud/pipeline/internal/secret/secrettype"
pkgCluster "github.com/banzaicloud/pipeline/pkg/cluster"
"github.com/banzaicloud/pipeline/pkg/k8sclient"
"github.com/banzaicloud/pipeline/src/model"
"github.com/banzaicloud/pipeline/src/secret"
"github.com/banzaicloud/pipeline/src/secret/verify"
)
const RBAC_API_VERSION = "rbac.authorization.k8s.io"
// CreateKubernetesClusterFromRequest creates ClusterModel struct from the request
func CreateKubernetesClusterFromRequest(request *pkgCluster.CreateClusterRequest, orgId uint, userId uint) (*KubeCluster, error) {
cluster := KubeCluster{
log: log.WithField("cluster", request.Name),
}
cluster.modelCluster = &model.ClusterModel{
Name: request.Name,
Location: request.Location,
Cloud: request.Cloud,
OrganizationId: orgId,
CreatedBy: userId,
SecretId: request.SecretId,
Distribution: pkgCluster.Unknown,
Kubernetes: kubernetesadapter.KubernetesClusterModel{
Metadata: request.Properties.CreateClusterKubernetes.Metadata,
},
}
return &cluster, nil
}
// KubeCluster struct for Build your own cluster
type KubeCluster struct {
modelCluster *model.ClusterModel
k8sConfig []byte
APIEndpoint string
log logrus.FieldLogger
CommonClusterBase
}
// CreateCluster creates a new cluster
func (c *KubeCluster) CreateCluster() error {
kubeConfig, err := c.GetK8sConfig()
if err != nil {
return errors.WrapIf(err, "couldn't get Kubernetes config")
}
client, err := k8sclient.NewClientFromKubeConfig(kubeConfig)
if err != nil {
return errors.WrapIf(err, "couldn't create Kubernetes client")
}
c.modelCluster.RbacEnabled, err = c.isRBACEnabled(client)
if err != nil {
return errors.WrapIf(err, "couldn't determine if RBAC is enabled on the cluster")
}
if c.modelCluster.RbacEnabled {
c.log.Info("rbac is enabled on the cluster")
} else {
c.log.Info("rbac is not enabled on the cluster")
}
return nil
}
// Persist save the cluster model
// Deprecated: Do not use.
func (c *KubeCluster) Persist() error {
return errors.WrapIf(c.modelCluster.Save(), "failed to persist cluster")
}
// DownloadK8sConfig downloads the kubeconfig file from cloud
func (c *KubeCluster) DownloadK8sConfig() ([]byte, error) {
s, err := c.GetSecretWithValidation()
if err != nil {
return nil, err
}
c.k8sConfig, err = base64.StdEncoding.DecodeString(s.Values[secrettype.K8SConfig])
return c.k8sConfig, err
}
// GetName returns the name of the cluster
func (c *KubeCluster) GetName() string {
return c.modelCluster.Name
}
// GetCloud returns the cloud type of the cluster
func (c *KubeCluster) GetCloud() string {
return pkgCluster.Kubernetes
}
// GetDistribution returns the distribution type of the cluster
func (c *KubeCluster) GetDistribution() string {
return c.modelCluster.Distribution
}
// GetStatus gets cluster status
func (c *KubeCluster) GetStatus() (*pkgCluster.GetClusterStatusResponse, error) {
if len(c.modelCluster.Location) == 0 {
c.log.Debug("Empty location.. reload from db")
// reload from db
db := global.DB()
db.Find(&c.modelCluster, model.ClusterModel{ID: c.GetID()})
}
return &pkgCluster.GetClusterStatusResponse{
Status: c.modelCluster.Status,
StatusMessage: c.modelCluster.StatusMessage,
Name: c.GetName(),
Location: c.modelCluster.Location,
Cloud: pkgCluster.Kubernetes,
Distribution: c.modelCluster.Distribution,
ResourceID: c.modelCluster.ID,
CreatorBaseFields: *NewCreatorBaseFields(c.modelCluster.CreatedAt, c.modelCluster.CreatedBy),
NodePools: nil,
Region: c.modelCluster.Location,
StartedAt: c.modelCluster.StartedAt,
}, nil
}
// DeleteCluster deletes cluster from cloud, in this case no delete function
func (c *KubeCluster) DeleteCluster() error {
return nil
}
// UpdateNodePools updates nodes pools of a cluster
func (c *KubeCluster) UpdateNodePools(request *pkgCluster.UpdateNodePoolsRequest, userId uint) error {
return nil
}
// UpdateCluster updates cluster in cloud, in this case no update function
func (c *KubeCluster) UpdateCluster(updateRequest *pkgCluster.UpdateClusterRequest, _ uint) error {
return nil
}
// GetID returns the specified cluster id
func (c *KubeCluster) GetID() uint {
return c.modelCluster.ID
}
func (c *KubeCluster) GetUID() string {
return c.modelCluster.UID
}
// GetSecretId returns the specified secret id
func (c *KubeCluster) GetSecretId() string {
return c.modelCluster.SecretId
}
// GetSshSecretId returns the specified ssh secret id
func (c *KubeCluster) GetSshSecretId() string {
return c.modelCluster.SshSecretId
}
// SaveSshSecretId saves the ssh secret id to database
func (c *KubeCluster) SaveSshSecretId(sshSecretId string) error {
return c.modelCluster.UpdateSshSecret(sshSecretId)
}
// GetModel returns the whole clusterModel
func (c *KubeCluster) GetModel() *model.ClusterModel {
return c.modelCluster
}
// CheckEqualityToUpdate validates the update request, in this case no update function
func (c *KubeCluster) CheckEqualityToUpdate(*pkgCluster.UpdateClusterRequest) error {
return nil
}
// AddDefaultsToUpdate adds defaults to update request, in this case no update function
func (c *KubeCluster) AddDefaultsToUpdate(*pkgCluster.UpdateClusterRequest) {
}
// GetAPIEndpoint returns the Kubernetes Api endpoint
func (c *KubeCluster) GetAPIEndpoint() (string, error) {
if c.APIEndpoint != "" {
return c.APIEndpoint, nil
}
secretItem, err := c.GetSecretWithValidation()
if err != nil {
return "", err
}
config, err := base64.StdEncoding.DecodeString(secretItem.Values[secrettype.K8SConfig])
if err != nil {
return "", err
}
return pkgCluster.GetAPIEndpointFromKubeconfig(config)
}
// DeleteFromDatabase deletes model from the database
func (c *KubeCluster) DeleteFromDatabase() error {
return c.modelCluster.Delete()
}
// GetOrganizationId returns the specified organization id
func (c *KubeCluster) GetOrganizationId() uint {
return c.modelCluster.OrganizationId
}
// GetLocation gets where the cluster is.
func (c *KubeCluster) GetLocation() string {
return c.modelCluster.Location
}
// CreateKubernetesClusterFromModel converts ClusterModel to KubeCluster
func CreateKubernetesClusterFromModel(clusterModel *model.ClusterModel) (*KubeCluster, error) {
kubeCluster := KubeCluster{
modelCluster: clusterModel,
}
return &kubeCluster, nil
}
// SetStatus sets the cluster's status
func (c *KubeCluster) SetStatus(status, statusMessage string) error {
return c.modelCluster.UpdateStatus(status, statusMessage)
}
// NodePoolExists returns true if node pool with nodePoolName exists
func (c *KubeCluster) NodePoolExists(nodePoolName string) bool {
return false
}
// IsReady checks if the cluster is running according to the cloud provider.
func (c *KubeCluster) IsReady() (bool, error) {
return true, nil
}
// ValidateCreationFields validates all field
func (c *KubeCluster) ValidateCreationFields(r *pkgCluster.CreateClusterRequest) error {
sir, err := getSecret(c.GetOrganizationId(), r.SecretId)
if err != nil {
return errors.WrapIfWithDetails(err, "secret not found", "secretID", r.SecretId)
}
return verify.CreateKubeConfigSecretVerifier(sir.Values).VerifySecret()
}
// GetSecretWithValidation returns secret from vault
func (c *KubeCluster) GetSecretWithValidation() (*secret.SecretItemResponse, error) {
return c.CommonClusterBase.getSecret(c)
}
// SaveConfigSecretId saves the config secret id in database
func (c *KubeCluster) SaveConfigSecretId(configSecretId string) error {
return c.modelCluster.UpdateConfigSecret(configSecretId)
}
// GetConfigSecretId return config secret id
func (c *KubeCluster) GetConfigSecretId() string {
return c.modelCluster.ConfigSecretId
}
// GetK8sConfig returns the Kubernetes config
func (c *KubeCluster) GetK8sConfig() ([]byte, error) {
return c.DownloadK8sConfig()
}
// GetK8sUserConfig returns the Kubernetes config
func (c *KubeCluster) GetK8sUserConfig() ([]byte, error) {
return c.GetK8sConfig()
}
// RbacEnabled returns true if rbac enabled on the cluster
func (c *KubeCluster) RbacEnabled() bool {
return c.modelCluster.RbacEnabled
}
// isRBACEnabled determines if RBAC is enabled on the Kubernetes cluster by investigating if list of
// api versions enabled on the API server contains 'rbac`
func (c *KubeCluster) isRBACEnabled(client *kubernetes.Clientset) (bool, error) {
apiGroups, err := client.ServerGroups()
if err != nil {
return false, errors.WrapIf(err, "couldn't retrieve Kubernetes API groups")
}
if apiGroups == nil {
return false, errors.New("no API groups found")
}
for _, g := range apiGroups.Groups {
if strings.Contains(strings.ToLower(g.Name), RBAC_API_VERSION) {
return true, nil
}
}
return false, nil
}