generated from datewu/project-abc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeploy_crud.go
140 lines (130 loc) · 3.59 KB
/
deploy_crud.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
package k8s
import (
"context"
"errors"
"time"
"github.com/datewu/gtea/jsonlog"
apps_v1 "k8s.io/api/apps/v1"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
func newDBio(d *apps_v1.Deployment) *Bio {
bio := &Bio{
Name: d.Name,
}
containes := d.Spec.Template.Spec.Containers
cs := make([]*ConBio, len(containes))
for i, c := range containes {
cs[i] = &ConBio{
Name: c.Name,
Image: c.Image,
Pull: string(c.ImagePullPolicy),
}
}
bio.Containers = cs
return bio
}
// GetDBio get deployment Bio
func GetDBio(ns, name string) (*Bio, error) {
opts := v1.GetOptions{}
ctx := context.Background()
d, err := classicalClientSet.AppsV1().Deployments(ns).Get(ctx, name, opts)
if err != nil {
return nil, err
}
return newDBio(d), nil
}
// ListDeployWithLabels list deployment by label
func ListDeployWithLabels(ns, label string) ([]apps_v1.Deployment, error) {
ctx := context.Background()
opts := v1.ListOptions{LabelSelector: label}
deploys, err := classicalClientSet.AppsV1().Deployments(ns).List(ctx, opts)
if err != nil {
return nil, err
}
return deploys.Items, nil
}
// ListBios list deployment bios
func ListBios(ns string) ([]*Bio, error) {
ctx := context.Background()
opts := v1.ListOptions{}
deploys, err := classicalClientSet.AppsV1().Deployments(ns).List(ctx, opts)
if err != nil {
return nil, err
}
its := deploys.Items
res := make([]*Bio, len(its))
for i, d := range its {
res[i] = newDBio(&d)
}
return res, nil
}
// SetDeployImgWithLabel ...
func SetDeployImgWithLabel(id *ContainerPath, label ...string) error {
return setDeployImg(id, label...)
}
// SetDeployImg ...
func SetDeployImg(id *ContainerPath) error {
return setDeployImg(id)
}
func setDeployImg(id *ContainerPath, labels ...string) error {
ctx := context.Background()
opts := v1.GetOptions{}
d, err := classicalClientSet.AppsV1().Deployments(id.Ns).Get(ctx, id.Name, opts)
if err != nil {
jsonlog.Err(err, map[string]any{"name": id.Name, "msg": "get deploy failed"})
return err
}
if labels != nil {
ls := d.GetLabels()
if err = checkLabels(ls, labels); err != nil {
return err
}
}
cpy := d.DeepCopy()
found := false
for i, c := range cpy.Spec.Template.Spec.Containers {
if c.Name == id.CName {
jsonlog.Info("got new image tag", map[string]any{"deploy": id.Name, "image": id.Img})
cpy.Spec.Template.Spec.Containers[i].Image = id.Img
found = true
break
}
}
if !found {
fErr := errors.New("cannot find container")
jsonlog.Err(fErr, map[string]any{"deploy": id.Name, "image": id.Img, "container": id.CName})
return fErr
}
uOpts := v1.UpdateOptions{}
if id.Name != "set-img" {
zero := int32(0)
cpy.Spec.Replicas = &zero
}
_, err = classicalClientSet.AppsV1().Deployments(id.Ns).Update(ctx, cpy, uOpts)
if err != nil {
jsonlog.Err(err, map[string]any{"deploy": id.Name, "image": id.Img, "msg": "update deploy failed"})
return err
}
go func() {
if id.Name == "set-img" {
jsonlog.Debug("no scale deploy back replics for set-img")
return
}
time.Sleep(3 * time.Second)
a, rerr := classicalClientSet.AppsV1().Deployments(id.Ns).Get(ctx, id.Name, opts)
if rerr != nil {
jsonlog.Err(err, map[string]any{"name": id.Name, "msg": "get deploy failed"})
return
}
acpy := a.DeepCopy()
acpy.Spec.Replicas = d.Spec.Replicas
jsonlog.Debug("going to scale deploy back replics",
map[string]any{"*replicas": *d.Spec.Replicas, "replicas": d.Spec.Replicas})
_, rerr = classicalClientSet.AppsV1().Deployments(id.Ns).Update(ctx, acpy, uOpts)
if rerr != nil {
jsonlog.Err(err, map[string]any{"name": id.Name, "msg": "scale deploy failed"})
return
}
}()
return nil
}