/
cluster.go
172 lines (143 loc) · 6.87 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
// Copyright 2019 Shanghai JingDuo Information Technology co., Ltd.
//
// 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 api
import (
"fmt"
"strings"
"k8s.io/apimachinery/pkg/util/validation"
"github.com/kpaas-io/kpaas/pkg/utils/validator"
)
type (
Cluster struct {
ShortName string `json:"shortName" binding:"required" minLength:"1" maxLength:"20"`
Name string `json:"name" binding:"required"`
KubeAPIServerConnectType KubeAPIServerConnectType `json:"kubeAPIServerConnectType" binding:"required" enums:"firstMasterIP,keepalived,loadbalancer"` // kube-apiserver connect type
VIP string `json:"vip,omitempty" maxLength:"15"` // keepalived listen virtual ip
NetInterfaceName string `json:"netInterfaceName,omitempty" maxLength:"30"` // keepalived listen net interface name
LoadbalancerIP string `json:"loadbalancerIP,omitempty" maxLength:"15"` // kube-apiserver loadbalancer ip when kubeAPIServerConnectType is loadbalancer required
LoadbalancerPort uint16 `json:"loadbalancerPort,omitempty" minimum:"1" maximum:"65535"` // kube-apiserver loadbalancer port when kubeAPIServerConnectType is loadbalancer required
NodePortMinimum uint16 `json:"nodePortMinimum" minimum:"1" default:"30000"`
NodePortMaximum uint16 `json:"nodePortMaximum" maximum:"65535" default:"32767"`
Labels []Label `json:"labels"`
Annotations []Annotation `json:"annotations"`
}
KubeAPIServerConnectType string
Label struct {
Key string `json:"key" binding:"required" minimum:"1" maximum:"253"`
Value string `json:"value" binding:"required" minimum:"1"`
}
Annotation struct {
Key string `json:"key" binding:"required" minimum:"1" maximum:"253"`
Value string `json:"value" binding:"required" minimum:"1"`
}
)
const (
KubeAPIServerConnectTypeFirstMasterIP KubeAPIServerConnectType = "firstMasterIP"
KubeAPIServerConnectTypeKeepalived KubeAPIServerConnectType = "keepalived"
KubeAPIServerConnectTypeLoadBalancer KubeAPIServerConnectType = "loadbalancer"
ClusterNameLengthLimit = 30
ClusterShortNameLengthLimit = 20
ClusterIPLengthLimit = 15
ClusterNetInterfaceLengthLimit = 30
ClusterLoadbalancerPortMinimum = 1
ClusterLoadbalancerPortMaximum = 65535
ClusterNodePortMinimum = 1
ClusterNodePortMaximum = 65535
LabelKeyLengthLimit = 253
AnnotationKeyLengthLimit = 253
DefaultClusterNodePortMinimum = 30000
DefaultClusterNodePortMaximum = 32767
)
func (cluster *Cluster) Validate() error {
wrapper := validator.NewWrapper(
validator.ValidateString(cluster.Name, "name", validator.ItemNotEmptyLimit, ClusterNameLengthLimit),
validator.ValidateString(cluster.ShortName, "shortName", validator.ItemNotEmptyLimit, ClusterShortNameLengthLimit),
validator.ValidateStringOptions(string(cluster.KubeAPIServerConnectType),
"kubeAPIServerConnectType",
[]string{string(KubeAPIServerConnectTypeFirstMasterIP), string(KubeAPIServerConnectTypeKeepalived), string(KubeAPIServerConnectTypeLoadBalancer)}),
)
if cluster.NodePortMinimum > 0 {
wrapper.AddValidateFunc(
validator.ValidateIntRange(int(cluster.NodePortMinimum), "nodePortMinimum", ClusterNodePortMinimum, ClusterNodePortMaximum),
)
}
if cluster.NodePortMaximum > 0 {
wrapper.AddValidateFunc(
validator.ValidateIntRange(int(cluster.NodePortMaximum), "nodePortMaximum", ClusterNodePortMinimum, ClusterNodePortMaximum),
)
}
if cluster.NodePortMinimum > 0 && cluster.NodePortMaximum > 0 {
wrapper.AddValidateFunc(
func() error {
if cluster.NodePortMinimum > cluster.NodePortMaximum {
return fmt.Errorf("nodePortMinimum must be not larger than nodePortMaximum")
}
return nil
},
)
}
switch cluster.KubeAPIServerConnectType {
case KubeAPIServerConnectTypeKeepalived:
wrapper.AddValidateFunc(
validator.ValidateString(cluster.VIP, "vip", validator.ItemNotEmptyLimit, ClusterIPLengthLimit),
validator.ValidateIP(cluster.VIP, "vip"),
validator.ValidateString(cluster.NetInterfaceName, "netInterfaceName", validator.ItemNotEmptyLimit, ClusterNetInterfaceLengthLimit),
)
case KubeAPIServerConnectTypeLoadBalancer:
wrapper.AddValidateFunc(
validator.ValidateString(cluster.LoadbalancerIP, "loadbalancerIP", validator.ItemNotEmptyLimit, ClusterIPLengthLimit),
validator.ValidateIP(cluster.LoadbalancerIP, "loadbalancerIP"),
validator.ValidateIntRange(int(cluster.LoadbalancerPort), "loadbalancerPort", ClusterLoadbalancerPortMinimum, ClusterLoadbalancerPortMaximum),
)
}
for _, label := range cluster.Labels {
wrapper.AddValidateFunc(
func() error {
return label.Validate()
},
)
}
for _, annotation := range cluster.Annotations {
wrapper.AddValidateFunc(
func() error {
return annotation.Validate()
},
)
}
return wrapper.Validate()
}
func (label *Label) Validate() error {
return validator.NewWrapper(
validator.ValidateString(label.Key, "key", validator.ItemNotEmptyLimit, LabelKeyLengthLimit),
ValidateStringFunctionReturnErrorMessages(validation.IsQualifiedName, label.Key, "label.key"),
validator.ValidateString(label.Value, "value", validator.ItemNotEmptyLimit, validator.ItemNoLimit),
ValidateStringFunctionReturnErrorMessages(validation.IsValidLabelValue, label.Value, "label.value"),
).Validate()
}
func (annotation *Annotation) Validate() error {
return validator.NewWrapper(
validator.ValidateString(annotation.Key, "key", validator.ItemNotEmptyLimit, AnnotationKeyLengthLimit),
ValidateStringFunctionReturnErrorMessages(validation.IsQualifiedName, annotation.Key, "annotation.key"),
validator.ValidateString(annotation.Value, "value", validator.ItemNotEmptyLimit, validator.ItemNoLimit),
).Validate()
}
func ValidateStringFunctionReturnErrorMessages(function func(string) []string, value, keyName string) validator.ValidateFunc {
return func() error {
errorMessages := validation.IsQualifiedName(value)
if len(errorMessages) > 0 {
return fmt.Errorf("%s %s", keyName, strings.Join(errorMessages, ", "))
}
return nil
}
}