-
Notifications
You must be signed in to change notification settings - Fork 328
/
cluster.go
195 lines (170 loc) · 6.72 KB
/
cluster.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
/*
* Tencent is pleased to support the open source community by making TKEStack
* available.
*
* Copyright (C) 2012-2019 Tencent. All Rights Reserved.
*
* 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
*
* https://opensource.org/licenses/Apache-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 OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/
package validation
import (
"context"
"errors"
"fmt"
"strings"
"time"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/util/validation"
"k8s.io/apimachinery/pkg/util/validation/field"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"tkestack.io/tke/api/platform"
"tkestack.io/tke/pkg/platform/provider/util/mark"
"tkestack.io/tke/pkg/platform/types"
"tkestack.io/tke/pkg/spec"
"tkestack.io/tke/pkg/util/apiclient"
utilvalidation "tkestack.io/tke/pkg/util/validation"
)
// ValidateCluster validates a given Cluster.
func ValidateCluster(ctx context.Context, cluster *types.Cluster) field.ErrorList {
allErrs := ValidateClusterAddresses(cluster.Status.Addresses, field.NewPath("status", "addresses"))
if cluster.Spec.ClusterCredentialRef != nil {
allErrs = append(allErrs, ValidateClusterCredentialRef(ctx, cluster, field.NewPath("spec", "clusterCredentialRef"))...)
client, err := cluster.Clientset()
if err != nil {
allErrs = append(allErrs, field.Invalid(field.NewPath("name"), cluster.Name, fmt.Sprintf("get clientset error: %v", err)))
}
if cluster.Status.Phase == platform.ClusterInitializing {
allErrs = append(allErrs, ValidateClusterMark(ctx, cluster.Name, field.NewPath("name"), client)...)
allErrs = append(allErrs, ValidateClusterVersion(field.NewPath("name"), client)...)
}
}
return allErrs
}
// ValidateClusterAddresses validates a given ClusterAddresses.
func ValidateClusterAddresses(addresses []platform.ClusterAddress, fldPath *field.Path) field.ErrorList {
allErrs := field.ErrorList{}
if len(addresses) == 0 {
allErrs = append(allErrs, field.Required(field.NewPath("status", "addresses"), "must specify at least one obj access address"))
} else {
for i, address := range addresses {
fldPath := fldPath.Index(i)
allErrs = utilvalidation.ValidateEnum(address.Type, fldPath.Child("type"), []interface{}{
platform.AddressAdvertise,
platform.AddressReal,
})
if address.Host == "" {
allErrs = append(allErrs, field.Required(fldPath.Child("host"), "must specify host"))
}
for _, msg := range validation.IsValidPortNum(int(address.Port)) {
allErrs = append(allErrs, field.Invalid(fldPath.Child("port"), address.Port, msg))
}
if address.Path != "" && !strings.HasPrefix(address.Path, "/") {
allErrs = append(allErrs, field.Invalid(fldPath.Child("path"), address.Path, "must start by `/`"))
}
url := fmt.Sprintf("https://%s:%d", address.Host, address.Port)
if address.Path != "" {
url = fmt.Sprintf("%s%s", url, address.Path)
}
err := utilvalidation.IsValiadURL(url, 5*time.Second)
if err != nil {
allErrs = append(allErrs, field.Invalid(fldPath, address, err.Error()))
}
}
}
return allErrs
}
// ValidateClusterCredentialRef validates cluster.Spec.ClusterCredentialRef.
func ValidateClusterCredentialRef(ctx context.Context, cluster *types.Cluster, fldPath *field.Path) field.ErrorList {
allErrs := field.ErrorList{}
credential := cluster.ClusterCredential
if credential.ClientCert == nil && credential.ClientKey != nil ||
credential.ClientCert != nil && credential.ClientKey == nil {
allErrs = append(allErrs, field.Required(fldPath.Child("clientCert"),
"`clientCert` and `clientKey` must provide togther"))
}
host, err := cluster.Host()
if err != nil {
allErrs = append(allErrs, field.Invalid(field.NewPath("clusterName"), credential.ClusterName, err.Error()))
} else {
restConfig := &rest.Config{
Host: host,
Timeout: 5 * time.Second,
}
if credential.CACert != nil {
restConfig.CAData = credential.CACert
if err = utilvalidation.ValidateRESTConfig(ctx, restConfig); err != nil {
if status := apierrors.APIStatus(nil); !errors.As(err, &status) {
allErrs = append(allErrs, field.Invalid(field.NewPath("caCert"), "", err.Error()))
}
}
} else {
restConfig.Insecure = true
}
if credential.Token != nil {
config := rest.CopyConfig(restConfig)
config.BearerToken = *credential.Token
if err = utilvalidation.ValidateRESTConfig(ctx, config); err != nil {
if apierrors.IsUnauthorized(err) {
allErrs = append(allErrs, field.Invalid(field.NewPath("token"), *credential.Token, err.Error()))
} else {
allErrs = append(allErrs, field.InternalError(field.NewPath("token"), err))
}
}
}
if credential.ClientCert != nil && credential != nil {
config := rest.CopyConfig(restConfig)
config.TLSClientConfig.CertData = credential.ClientCert
config.TLSClientConfig.KeyData = credential.ClientKey
if err = utilvalidation.ValidateRESTConfig(ctx, config); err != nil {
if apierrors.IsUnauthorized(err) {
allErrs = append(allErrs, field.Invalid(field.NewPath("clientCert"), "", err.Error()))
} else {
allErrs = append(allErrs, field.InternalError(field.NewPath("clientCert"), err))
}
}
}
}
return allErrs
}
// ValidateClusterMark validates a given cluster had imported already.
func ValidateClusterMark(ctx context.Context, clusterName string, fldPath *field.Path, client kubernetes.Interface) field.ErrorList {
allErrs := field.ErrorList{}
_, err := mark.Get(ctx, client)
if err != nil {
if !apierrors.IsNotFound(err) {
allErrs = append(allErrs, field.InternalError(fldPath, err))
}
} else {
allErrs = append(allErrs, field.Invalid(fldPath, clusterName,
fmt.Sprintf("can't imported same cluster, you can use `kubectl -n%s delete configmap %s`", mark.Namespace, mark.Name)))
}
return allErrs
}
// ValidateClusterVersion validates a given cluster's version.
func ValidateClusterVersion(fldPath *field.Path, client kubernetes.Interface) field.ErrorList {
allErrs := field.ErrorList{}
v, err := apiclient.GetClusterVersion(client)
if err != nil {
allErrs = append(allErrs, field.InternalError(fldPath, err))
return allErrs
}
result, err := apiclient.CheckVersion(v, spec.K8sVersionConstraint)
if err != nil {
allErrs = append(allErrs, field.InternalError(fldPath, err))
return allErrs
}
if !result {
allErrs = append(allErrs, field.Invalid(fldPath, v, fmt.Sprintf("cluster version must %s", spec.K8sVersionConstraint)))
}
return allErrs
}