/
k8s_daemonset.go
146 lines (130 loc) · 3.85 KB
/
k8s_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
package k8s
import (
"context"
"fmt"
"github.com/sirupsen/logrus"
appv1 "k8s.io/api/apps/v1"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
// DaemonSetExists checks if a daemonset exists.
func DaemonSetExists(namespace, name string) (bool, error) {
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()
if !IsInitialized() {
return false, fmt.Errorf("knuu is not initialized")
}
_, err := Clientset().AppsV1().DaemonSets(namespace).Get(ctx, name, metav1.GetOptions{})
if err != nil {
if isNotFound(err) {
return false, nil
}
return false, fmt.Errorf("error getting daemonset %s: %w", name, err)
}
return true, nil
}
// GetDaemonSet retrieves a daemonset.
func GetDaemonSet(namespace, name string) (*appv1.DaemonSet, error) {
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()
if !IsInitialized() {
return nil, fmt.Errorf("knuu is not initialized")
}
ds, err := Clientset().AppsV1().DaemonSets(namespace).Get(ctx, name, metav1.GetOptions{})
if err != nil {
return nil, fmt.Errorf("error getting daemonset %s: %w", name, err)
}
return ds, nil
}
// CreateDaemonSet creates a new daemonset.
func CreateDaemonSet(
namespace,
name string,
labels map[string]string,
initContainers []v1.Container,
containers []v1.Container,
) (*appv1.DaemonSet, error) {
ds, err := prepareDaemonSet(namespace, name, labels, initContainers, containers)
if err != nil {
return nil, err
}
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()
if !IsInitialized() {
return nil, fmt.Errorf("knuu is not initialized")
}
created, err := Clientset().AppsV1().DaemonSets(namespace).Create(ctx, ds, metav1.CreateOptions{})
if err != nil {
return nil, fmt.Errorf("error creating daemonset %s: %w", name, err)
}
logrus.Debugf("DaemonSet %s created in namespace %s", name, namespace)
return created, nil
}
// UpdateDaemonSet updates an existing daemonset.
func UpdateDaemonSet(
namespace,
name string,
labels map[string]string,
initContainers []v1.Container,
containers []v1.Container,
) (*appv1.DaemonSet, error) {
ds, err := prepareDaemonSet(namespace, name, labels, initContainers, containers)
if err != nil {
return nil, err
}
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()
if !IsInitialized() {
return nil, fmt.Errorf("knuu is not initialized")
}
updated, err := Clientset().AppsV1().DaemonSets(namespace).Update(ctx, ds, metav1.UpdateOptions{})
if err != nil {
return nil, fmt.Errorf("error updating daemonset %s: %w", name, err)
}
logrus.Debugf("DaemonSet %s updated in namespace %s", name, namespace)
return updated, nil
}
// DeleteDaemonSet deletes an existing daemonset.
func DeleteDaemonSet(namespace, name string) error {
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()
if !IsInitialized() {
return fmt.Errorf("knuu is not initialized")
}
if err := Clientset().AppsV1().DaemonSets(namespace).Delete(ctx, name, metav1.DeleteOptions{}); err != nil {
return fmt.Errorf("error deleting daemonset %s: %w", name, err)
}
logrus.Debugf("DaemonSet %s deleted in namespace %s", name, namespace)
return nil
}
// prepareService constructs a new Service object with the specified parameters.
func prepareDaemonSet(
namespace,
name string,
labels map[string]string,
initContainers,
containers []v1.Container,
) (*appv1.DaemonSet, error) {
ds := &appv1.DaemonSet{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: namespace,
Labels: labels,
},
Spec: appv1.DaemonSetSpec{
Selector: &metav1.LabelSelector{
MatchLabels: labels,
},
Template: v1.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{
Labels: labels,
},
Spec: v1.PodSpec{
InitContainers: initContainers,
Containers: containers,
},
},
},
}
return ds, nil
}