-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdaemonset.go
153 lines (130 loc) · 4.36 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
package k8s
import (
"context"
"github.com/dbunion/com/scheduler"
v1 "k8s.io/api/apps/v1"
meta_v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/yaml"
)
// DaemonSetClient ...
type DaemonSetClient struct {
apiClient *Client
}
// newDaemonSetClient - new statefulSet client
func newDaemonSetClient(apiClient *Client) *DaemonSetClient {
return &DaemonSetClient{
apiClient: apiClient,
}
}
// convertToDaemonSet - convert k8s's DaemonSet to DaemonSet
func convertToDaemonSet(n *v1.DaemonSet) *scheduler.DaemonSet {
if n == nil {
return nil
}
daeset := &scheduler.DaemonSet{
Version: n.APIVersion,
Name: n.Name,
Namespace: n.Namespace,
Labels: n.Labels,
Spec: scheduler.DaemonSetSpec{
Selector: n.Spec.Selector.MatchLabels,
MinReadySeconds: n.Spec.MinReadySeconds,
},
Status: scheduler.DaemonSetStatus{
CurrentNumberScheduled: n.Status.CurrentNumberScheduled,
NumberMisscheduled: n.Status.NumberMisscheduled,
DesiredNumberScheduled: n.Status.DesiredNumberScheduled,
NumberReady: n.Status.NumberReady,
NumberAvailable: n.Status.NumberAvailable,
NumberUnavailable: n.Status.NumberUnavailable,
},
}
return daeset
}
// Get - query DaemonSets info
func (c *DaemonSetClient) Get(ctx context.Context, namespace string, param *scheduler.DaemonSet) (*scheduler.DaemonSet, error) {
n, err := c.apiClient.clientSet.AppsV1().DaemonSets(namespace).Get(ctx, param.Name, meta_v1.GetOptions{})
if err != nil {
return nil, err
}
return convertToDaemonSet(n), nil
}
// List - query DaemonSets list
func (c *DaemonSetClient) List(ctx context.Context, namespace string, options scheduler.Options) ([]*scheduler.DaemonSet, error) {
list, err := c.apiClient.clientSet.AppsV1().DaemonSets(namespace).List(ctx, convertToListOptions(options))
if err != nil {
return nil, err
}
stsList := make([]*scheduler.DaemonSet, 0)
for i := 0; i < len(list.Items); i++ {
stsList = append(stsList, convertToDaemonSet(&list.Items[i]))
}
return stsList, err
}
// Create - create new DaemonSets
func (c *DaemonSetClient) Create(ctx context.Context, param *scheduler.DaemonSet, options scheduler.Options) error {
req := &v1.DaemonSet{
TypeMeta: meta_v1.TypeMeta{
Kind: "DaemonSet",
APIVersion: param.Version,
},
ObjectMeta: meta_v1.ObjectMeta{
Name: param.Name,
Labels: param.Labels,
},
Spec: v1.DaemonSetSpec{
Selector: &meta_v1.LabelSelector{
MatchLabels: param.Spec.Selector,
},
Template: convertPodTemplateSpecToK8sPodTemplateSpec(param.Spec.Template),
UpdateStrategy: v1.DaemonSetUpdateStrategy{},
MinReadySeconds: 0,
RevisionHistoryLimit: nil,
},
}
_, err := c.apiClient.clientSet.AppsV1().DaemonSets(param.Namespace).Create(ctx, req, meta_v1.CreateOptions{})
if err != nil {
return err
}
return nil
}
// CreateWithYaml - create new daemonSet map with yalm
func (c *DaemonSetClient) CreateWithYaml(ctx context.Context, param *scheduler.DaemonSet, options scheduler.Options) error {
var req v1.DaemonSet
if err := yaml.Unmarshal(param.YAML, &req); err != nil {
return err
}
_, err := c.apiClient.clientSet.AppsV1().DaemonSets(param.Namespace).Create(ctx, &req, convertToCreateOptions(options))
if err != nil {
return err
}
return nil
}
// Update - update DaemonSets content
func (c *DaemonSetClient) Update(ctx context.Context, param *scheduler.DaemonSet) error {
req, err := c.apiClient.clientSet.AppsV1().DaemonSets(param.Namespace).Get(ctx, param.Name, meta_v1.GetOptions{})
if err != nil {
return err
}
// update fields
req.Labels = param.Labels
_, err = c.apiClient.clientSet.AppsV1().DaemonSets(param.Namespace).Update(ctx, req, meta_v1.UpdateOptions{})
if err != nil {
return err
}
return nil
}
// Delete - delete DaemonSets map
func (c *DaemonSetClient) Delete(ctx context.Context, param *scheduler.DaemonSet, options scheduler.Options) error {
op := convertToDeleteOptions(options)
return c.apiClient.clientSet.AppsV1().DaemonSets(param.Namespace).Delete(ctx, param.Name, op)
}
// Watch - watch DaemonSets change
func (c *DaemonSetClient) Watch(ctx context.Context, param *scheduler.DaemonSet, options scheduler.Options) (scheduler.Interface, error) {
op := convertToListOptions(options)
w, err := c.apiClient.clientSet.AppsV1().DaemonSets(param.Namespace).Watch(ctx, op)
if err != nil {
return nil, err
}
return NewWatcher(w), nil
}