/
daemonset.go
154 lines (134 loc) · 3.56 KB
/
daemonset.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
/*
@Version : 1.0
@Author : steven.wong
@Email : 'wangxk1991@gamil.com'
@Time : 2024/03/22 19:20:11
Desc :
*/
package kube
import (
"context"
"time"
appsv1 "k8s.io/api/apps/v1"
v1 "k8s.io/api/apps/v1"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
type DaemonSet struct {
*LinkInfo
*appsv1.DaemonSet
client *KubeClient
ctx context.Context
}
func NewDaemonSet(ctx context.Context) *DaemonSet {
if ctx == nil {
ctx = context.TODO()
}
return &DaemonSet{
DaemonSet: &appsv1.DaemonSet{
TypeMeta: metav1.TypeMeta{
Kind: "DaemonSet",
APIVersion: "apps/v1",
},
ObjectMeta: metav1.ObjectMeta{},
Spec: appsv1.DaemonSetSpec{},
},
LinkInfo: &LinkInfo{},
client: nil,
ctx: ctx,
}
}
func (d *DaemonSet) Link(region, config string) *DaemonSet {
d.Region = region
d.Config = config
d.client = NewKubeClient(region, config)
return d
}
func (d *DaemonSet) Metadata(name, namespace string) *DaemonSet {
d.Name, d.Namespace = name, namespace
return d
}
func (d *DaemonSet) Labels(labels map[string]string) *DaemonSet {
if d.DaemonSet.Labels == nil {
d.DaemonSet.Labels = make(map[string]string)
}
for k, v := range labels {
d.DaemonSet.Labels[k] = v
}
return d
}
func (d *DaemonSet) Annotations(annotations map[string]string) *DaemonSet {
if d.DaemonSet.Annotations == nil {
d.DaemonSet.Annotations = make(map[string]string)
}
for k, v := range annotations {
d.DaemonSet.Annotations[k] = v
}
return d
}
func (d *DaemonSet) Selector(labels map[string]string) *DaemonSet {
if d.DaemonSet.Spec.Selector == nil {
d.DaemonSet.Spec.Selector = &metav1.LabelSelector{}
}
d.DaemonSet.Spec.Selector = &metav1.LabelSelector{
MatchLabels: labels,
}
return d
}
func (d *DaemonSet) Template(pt *PodTemplate) *DaemonSet {
if pt == nil {
return d
}
d.DaemonSet.Spec.Template = pt.Template
return d
}
func (d *DaemonSet) Create() error {
daemonsets := d.client.AppsV1().DaemonSets(d.Namespace)
_, err := daemonsets.Create(d.ctx, d.DaemonSet, metav1.CreateOptions{})
return err
}
func (d *DaemonSet) Delete() error {
daemonsets := d.client.AppsV1().DaemonSets(d.Namespace)
return daemonsets.Delete(d.ctx, d.Name, metav1.DeleteOptions{})
}
func (d *DaemonSet) Update() error {
daemonsets := d.client.AppsV1().DaemonSets(d.Namespace)
_, err := daemonsets.Update(d.ctx, d.DaemonSet, metav1.UpdateOptions{})
return err
}
func (d *DaemonSet) Get() (*v1.DaemonSet, error) {
return d.client.AppsV1().DaemonSets(d.Namespace).
Get(d.ctx, d.Name, metav1.GetOptions{})
}
func (d *DaemonSet) CreateOrUpdate() error {
_, err := d.client.AppsV1().DaemonSets(d.Namespace).Get(d.ctx, d.Name, metav1.GetOptions{})
if err != nil {
if errors.IsNotFound(err) {
return d.Create()
}
return err
}
return d.Update()
}
func (d *DaemonSet) Empty() bool {
_, err := d.client.AppsV1().DaemonSets(d.Namespace).Get(d.ctx, d.Name, metav1.GetOptions{})
return errors.IsNotFound(err)
}
// implement the rollout method
func (d *DaemonSet) Rollout() error {
if d.DaemonSet.Annotations == nil {
d.DaemonSet.Annotations = make(map[string]string)
}
d.DaemonSet.Annotations["kubectl.kubernetes.io/restartedAt"] = time.Now().Local().Format(time.RFC3339)
_, err := d.client.AppsV1().DaemonSets(d.Namespace).Update(d.ctx, d.DaemonSet, metav1.UpdateOptions{})
return err
}
func (d *DaemonSet) Equal(keys []string) bool {
daemonSet, err := d.Get()
if err != nil && !errors.IsNotFound(err) {
}
if len(keys) == 0 {
keys = []string{"Spec"}
}
return ResourceEqual(d.DaemonSet.Spec, daemonSet.Spec, keys)
}