/
deployment.go
230 lines (205 loc) · 6.27 KB
/
deployment.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
/*
Copyright 2022 Cortex Labs, Inc.
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 k8s
import (
"context"
"time"
"github.com/cortexlabs/cortex/pkg/lib/errors"
kapps "k8s.io/api/apps/v1"
kcore "k8s.io/api/core/v1"
kerrors "k8s.io/apimachinery/pkg/api/errors"
kmeta "k8s.io/apimachinery/pkg/apis/meta/v1"
klabels "k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/util/intstr"
)
var _deploymentTypeMeta = kmeta.TypeMeta{
APIVersion: "apps/v1",
Kind: "Deployment",
}
type DeploymentSpec struct {
Name string
Replicas int32
PodSpec PodSpec
MaxSurge *string // Can be a percentage (e.g. 10%) or an absolute number (e.g. 2)
MaxUnavailable *string // Can be a percentage (e.g. 10%) or an absolute number (e.g. 2)
Selector map[string]string
Labels map[string]string
Annotations map[string]string
}
func Deployment(spec *DeploymentSpec) *kapps.Deployment {
if spec.PodSpec.Name == "" {
spec.PodSpec.Name = spec.Name
}
if spec.Selector == nil {
spec.Selector = spec.PodSpec.Labels
}
var maxSurge *intstr.IntOrString
if spec.MaxSurge != nil {
intStr := intstr.Parse(*spec.MaxSurge)
maxSurge = &intStr
}
var maxUnavailable *intstr.IntOrString
if spec.MaxUnavailable != nil {
intStr := intstr.Parse(*spec.MaxUnavailable)
maxUnavailable = &intStr
}
deployment := &kapps.Deployment{
TypeMeta: _deploymentTypeMeta,
ObjectMeta: kmeta.ObjectMeta{
Name: spec.Name,
Labels: spec.Labels,
Annotations: spec.Annotations,
},
Spec: kapps.DeploymentSpec{
Replicas: &spec.Replicas,
Strategy: kapps.DeploymentStrategy{
Type: kapps.RollingUpdateDeploymentStrategyType,
RollingUpdate: &kapps.RollingUpdateDeployment{
MaxSurge: maxSurge,
MaxUnavailable: maxUnavailable,
},
},
Template: kcore.PodTemplateSpec{
ObjectMeta: kmeta.ObjectMeta{
Name: spec.PodSpec.Name,
Labels: spec.PodSpec.Labels,
Annotations: spec.PodSpec.Annotations,
},
Spec: spec.PodSpec.K8sPodSpec,
},
Selector: &kmeta.LabelSelector{
MatchLabels: spec.Selector,
},
},
}
return deployment
}
func (c *Client) CreateDeployment(deployment *kapps.Deployment) (*kapps.Deployment, error) {
deployment.TypeMeta = _deploymentTypeMeta
deployment, err := c.deploymentClient.Create(context.Background(), deployment, kmeta.CreateOptions{})
if err != nil {
return nil, errors.WithStack(err)
}
return deployment, nil
}
func (c *Client) UpdateDeployment(deployment *kapps.Deployment) (*kapps.Deployment, error) {
deployment.TypeMeta = _deploymentTypeMeta
deployment, err := c.deploymentClient.Update(context.Background(), deployment, kmeta.UpdateOptions{})
if err != nil {
return nil, errors.WithStack(err)
}
return deployment, nil
}
func (c *Client) ApplyDeployment(deployment *kapps.Deployment) (*kapps.Deployment, error) {
existing, err := c.GetDeployment(deployment.Name)
if err != nil {
return nil, err
}
if existing == nil {
return c.CreateDeployment(deployment)
}
return c.UpdateDeployment(deployment)
}
func (c *Client) GetDeployment(name string) (*kapps.Deployment, error) {
deployment, err := c.deploymentClient.Get(context.Background(), name, kmeta.GetOptions{})
if err != nil {
if kerrors.IsNotFound(err) {
return nil, nil
}
return nil, errors.WithStack(err)
}
deployment.TypeMeta = _deploymentTypeMeta
return deployment, nil
}
func (c *Client) DeleteDeployment(name string) (bool, error) {
err := c.deploymentClient.Delete(context.Background(), name, _deleteOpts)
if err != nil {
if kerrors.IsNotFound(err) {
return false, nil
}
return false, errors.WithStack(err)
}
return true, nil
}
func (c *Client) ListDeployments(opts *kmeta.ListOptions) ([]kapps.Deployment, error) {
if opts == nil {
opts = &kmeta.ListOptions{}
}
deploymentList, err := c.deploymentClient.List(context.Background(), *opts)
if err != nil {
return nil, errors.WithStack(err)
}
for i := range deploymentList.Items {
deploymentList.Items[i].TypeMeta = _deploymentTypeMeta
}
return deploymentList.Items, nil
}
func (c *Client) ListDeploymentsByLabels(labels map[string]string) ([]kapps.Deployment, error) {
opts := &kmeta.ListOptions{
LabelSelector: klabels.SelectorFromSet(labels).String(),
}
return c.ListDeployments(opts)
}
func (c *Client) ListDeploymentsByLabel(labelKey string, labelValue string) ([]kapps.Deployment, error) {
return c.ListDeploymentsByLabels(map[string]string{labelKey: labelValue})
}
func (c *Client) ListDeploymentsWithLabelKeys(labelKeys ...string) ([]kapps.Deployment, error) {
opts := &kmeta.ListOptions{
LabelSelector: LabelExistsSelector(labelKeys...),
}
return c.ListDeployments(opts)
}
func DeploymentMap(deployments []kapps.Deployment) map[string]kapps.Deployment {
deploymentMap := map[string]kapps.Deployment{}
for _, deployment := range deployments {
deploymentMap[deployment.Name] = deployment
}
return deploymentMap
}
func DeploymentStartTime(deployment *kapps.Deployment) *time.Time {
if deployment == nil {
return nil
}
t := deployment.CreationTimestamp.Time
if t.IsZero() {
return nil
}
return &deployment.CreationTimestamp.Time
}
func DeploymentStrategiesMatch(s1, s2 kapps.DeploymentStrategy) bool {
if s1.Type != s2.Type {
return false
}
if s1.RollingUpdate == nil && s2.RollingUpdate == nil {
return true
}
if s1.RollingUpdate == nil || s2.RollingUpdate == nil {
return false
}
if !intOrStrPtrsMatch(s1.RollingUpdate.MaxUnavailable, s2.RollingUpdate.MaxUnavailable) {
return false
}
if !intOrStrPtrsMatch(s1.RollingUpdate.MaxSurge, s2.RollingUpdate.MaxSurge) {
return false
}
return true
}
func intOrStrPtrsMatch(intStr1, intStr2 *intstr.IntOrString) bool {
if intStr1 == nil && intStr2 == nil {
return true
}
if intStr1 == nil || intStr2 == nil {
return false
}
return (*intStr1).String() == (*intStr2).String()
}