This repository has been archived by the owner on Mar 28, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 741
/
Copy pathcluster.go
216 lines (183 loc) · 7.62 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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
// Copyright 2016 The etcd-operator 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 v1beta2
import (
"errors"
"strings"
"k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
const (
defaultRepository = "quay.io/coreos/etcd"
DefaultEtcdVersion = "3.2.13"
)
var (
// TODO: move validation code into separate package.
ErrBackupUnsetRestoreSet = errors.New("spec: backup policy must be set if restore policy is set")
)
// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
// EtcdClusterList is a list of etcd clusters.
type EtcdClusterList struct {
metav1.TypeMeta `json:",inline"`
// Standard list metadata
// More info: http://releases.k8s.io/HEAD/docs/devel/api-conventions.md#metadata
metav1.ListMeta `json:"metadata,omitempty"`
Items []EtcdCluster `json:"items"`
}
// +genclient
// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
type EtcdCluster struct {
metav1.TypeMeta `json:",inline"`
metav1.ObjectMeta `json:"metadata,omitempty"`
Spec ClusterSpec `json:"spec"`
Status ClusterStatus `json:"status"`
}
func (c *EtcdCluster) AsOwner() metav1.OwnerReference {
trueVar := true
return metav1.OwnerReference{
APIVersion: SchemeGroupVersion.String(),
Kind: EtcdClusterResourceKind,
Name: c.Name,
UID: c.UID,
Controller: &trueVar,
}
}
type ClusterSpec struct {
// Size is the expected size of the etcd cluster.
// The etcd-operator will eventually make the size of the running
// cluster equal to the expected size.
// The vaild range of the size is from 1 to 7.
Size int `json:"size"`
// Repository is the name of the repository that hosts
// etcd container images. It should be direct clone of the repository in official
// release:
// https://github.com/coreos/etcd/releases
// That means, it should have exact same tags and the same meaning for the tags.
//
// By default, it is `quay.io/coreos/etcd`.
Repository string `json:"repository,omitempty"`
// Version is the expected version of the etcd cluster.
// The etcd-operator will eventually make the etcd cluster version
// equal to the expected version.
//
// The version must follow the [semver]( http://semver.org) format, for example "3.2.13".
// Only etcd released versions are supported: https://github.com/coreos/etcd/releases
//
// If version is not set, default is "3.2.13".
Version string `json:"version,omitempty"`
// Paused is to pause the control of the operator for the etcd cluster.
Paused bool `json:"paused,omitempty"`
// Pod defines the policy to create pod for the etcd pod.
//
// Updating Pod does not take effect on any existing etcd pods.
Pod *PodPolicy `json:"pod,omitempty"`
// etcd cluster TLS configuration
TLS *TLSPolicy `json:"TLS,omitempty"`
}
// PodPolicy defines the policy to create pod for the etcd container.
type PodPolicy struct {
// Labels specifies the labels to attach to pods the operator creates for the
// etcd cluster.
// "app" and "etcd_*" labels are reserved for the internal use of the etcd operator.
// Do not overwrite them.
Labels map[string]string `json:"labels,omitempty"`
// NodeSelector specifies a map of key-value pairs. For the pod to be eligible
// to run on a node, the node must have each of the indicated key-value pairs as
// labels.
NodeSelector map[string]string `json:"nodeSelector,omitempty"`
// The scheduling constraints on etcd pods.
Affinity *v1.Affinity `json:"affinity,omitempty"`
// **DEPRECATED**. Use Affinity instead.
AntiAffinity bool `json:"antiAffinity,omitempty"`
// Resources is the resource requirements for the etcd container.
// This field cannot be updated once the cluster is created.
Resources v1.ResourceRequirements `json:"resources,omitempty"`
// Tolerations specifies the pod's tolerations.
Tolerations []v1.Toleration `json:"tolerations,omitempty"`
// List of environment variables to set in the etcd container.
// This is used to configure etcd process. etcd cluster cannot be created, when
// bad environement variables are provided. Do not overwrite any flags used to
// bootstrap the cluster (for example `--initial-cluster` flag).
// This field cannot be updated.
EtcdEnv []v1.EnvVar `json:"etcdEnv,omitempty"`
// PersistentVolumeClaimSpec is the spec to describe PVC for the etcd container
// This field is optional. If no PVC spec, etcd container will use emptyDir as volume
// Note. This feature is in alpha stage. It is currently only used as non-stable storage,
// not the stable storage. Future work need to make it used as stable storage.
PersistentVolumeClaimSpec *v1.PersistentVolumeClaimSpec `json:"persistentVolumeClaimSpec,omitempty"`
// Annotations specifies the annotations to attach to pods the operator creates for the
// etcd cluster.
// The "etcd.version" annotation is reserved for the internal use of the etcd operator.
Annotations map[string]string `json:"annotations,omitempty"`
// busybox init container image. default is busybox:1.28.0-glibc
// busybox:latest uses uclibc which contains a bug that sometimes prevents name resolution
// More info: https://github.com/docker-library/busybox/issues/27
BusyboxImage string `json:"busyboxImage,omitempty"`
// SecurityContext specifies the security context for the entire pod
// More info: https://kubernetes.io/docs/tasks/configure-pod-container/security-context
SecurityContext *v1.PodSecurityContext `json:"securityContext,omitempty"`
// DNSTimeoutInSecond is the maximum allowed time for the init container of the etcd pod to
// reverse DNS lookup its IP given the hostname.
// The default is to wait indefinitely and has a vaule of 0.
DNSTimeoutInSecond int64 `json:"DNSTimeoutInSecond,omitempty"`
// ClusterDomain is the cluster domain to use for member URLs E.g.
// '.cluster.local'.
// The default is to not set a cluster domain explicitly.
ClusterDomain string `json:"ClusterDomain"`
}
// TODO: move this to initializer
func (c *ClusterSpec) Validate() error {
if c.TLS != nil {
if err := c.TLS.Validate(); err != nil {
return err
}
}
if c.Pod != nil {
for k := range c.Pod.Labels {
if k == "app" || strings.HasPrefix(k, "etcd_") {
return errors.New("spec: pod labels contains reserved label")
}
}
}
return nil
}
// SetDefaults cleans up user passed spec, e.g. defaulting, transforming fields.
// TODO: move this to initializer
func (e *EtcdCluster) SetDefaults() {
c := &e.Spec
if len(c.Repository) == 0 {
c.Repository = defaultRepository
}
if len(c.Version) == 0 {
c.Version = DefaultEtcdVersion
}
c.Version = strings.TrimLeft(c.Version, "v")
// convert PodPolicy.AntiAffinity to Pod.Affinity.PodAntiAffinity
// TODO: Remove this once PodPolicy.AntiAffinity is removed
if c.Pod != nil && c.Pod.AntiAffinity && c.Pod.Affinity == nil {
c.Pod.Affinity = &v1.Affinity{
PodAntiAffinity: &v1.PodAntiAffinity{
RequiredDuringSchedulingIgnoredDuringExecution: []v1.PodAffinityTerm{
{
// set anti-affinity to the etcd pods that belongs to the same cluster
LabelSelector: &metav1.LabelSelector{MatchLabels: map[string]string{
"etcd_cluster": e.Name,
}},
TopologyKey: "kubernetes.io/hostname",
},
},
},
}
}
}