-
Notifications
You must be signed in to change notification settings - Fork 2
/
replicationcontroller.go
151 lines (130 loc) · 4.55 KB
/
replicationcontroller.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
package k8s
import (
"context"
"github.com/dbunion/com/scheduler"
v1 "k8s.io/api/core/v1"
meta_v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/yaml"
)
// ReplicationControllerClient replication controller client
type ReplicationControllerClient struct {
apiClient *Client
}
//newReplicationControllerClient ...
func newReplicationControllerClient(apiClient *Client) *ReplicationControllerClient {
return &ReplicationControllerClient{
apiClient: apiClient,
}
}
// convertToRC - convert k8s's Pod to Pod
func convertToRC(n *v1.ReplicationController) *scheduler.RC {
if n == nil {
return nil
}
rc := &scheduler.RC{
Version: n.APIVersion,
Name: n.Name,
Namespace: n.Namespace,
Labels: n.Labels,
Spec: scheduler.RCSpec{
Replicas: *n.Spec.Replicas,
MinReadySeconds: n.Spec.MinReadySeconds,
Selector: n.Spec.Selector,
Template: *convertToPodTemplateSpec(n.Spec.Template),
},
Status: scheduler.RCStatus{
Replicas: n.Status.ReadyReplicas,
FullyLabeledReplicas: n.Status.FullyLabeledReplicas,
ReadyReplicas: n.Status.ReadyReplicas,
AvailableReplicas: n.Status.AvailableReplicas,
ObservedGeneration: n.Status.ObservedGeneration,
},
YAML: nil,
}
return rc
}
// Get - query ReplicationControllers info
func (c *ReplicationControllerClient) Get(ctx context.Context, namespace string, param *scheduler.RC) (*scheduler.RC, error) {
n, err := c.apiClient.clientSet.CoreV1().ReplicationControllers(namespace).Get(ctx, param.Name, meta_v1.GetOptions{})
if err != nil {
return nil, err
}
return convertToRC(n), nil
}
// List - query ReplicationControllers list
func (c *ReplicationControllerClient) List(ctx context.Context, namespace string, options scheduler.Options) ([]*scheduler.RC, error) {
list, err := c.apiClient.clientSet.CoreV1().ReplicationControllers(namespace).List(ctx, convertToListOptions(options))
if err != nil {
return nil, err
}
podList := make([]*scheduler.RC, 0)
for i := 0; i < len(list.Items); i++ {
podList = append(podList, convertToRC(&list.Items[i]))
}
return podList, err
}
// Create - create new ReplicationControllers
func (c *ReplicationControllerClient) Create(ctx context.Context, param *scheduler.RC, options scheduler.Options) error {
template := convertPodTemplateSpecToK8sPodTemplateSpec(param.Spec.Template)
req := &v1.ReplicationController{
TypeMeta: meta_v1.TypeMeta{
Kind: "Pod",
APIVersion: "v1",
},
ObjectMeta: meta_v1.ObjectMeta{
Name: param.Name,
Labels: param.Labels,
},
Spec: v1.ReplicationControllerSpec{
Replicas: ¶m.Spec.Replicas,
MinReadySeconds: param.Spec.MinReadySeconds,
Selector: param.Spec.Selector,
Template: &template,
},
}
_, err := c.apiClient.clientSet.CoreV1().ReplicationControllers(param.Namespace).Create(ctx, req, meta_v1.CreateOptions{})
if err != nil {
return err
}
return nil
}
// CreateWithYaml - create new ReplicationControllers with yaml
func (c *ReplicationControllerClient) CreateWithYaml(ctx context.Context, param *scheduler.RC, options scheduler.Options) error {
var req v1.ReplicationController
if err := yaml.Unmarshal(param.YAML, &req); err != nil {
return err
}
_, err := c.apiClient.clientSet.CoreV1().ReplicationControllers(param.Namespace).Create(ctx, &req, convertToCreateOptions(options))
if err != nil {
return err
}
return nil
}
// Update - update ReplicationControllers content
func (c *ReplicationControllerClient) Update(ctx context.Context, param *scheduler.RC) error {
req, err := c.apiClient.clientSet.CoreV1().ReplicationControllers(param.Namespace).Get(ctx, param.Name, meta_v1.GetOptions{})
if err != nil {
return err
}
// update fields
req.Labels = param.Labels
_, err = c.apiClient.clientSet.CoreV1().ReplicationControllers(param.Namespace).Update(ctx, req, meta_v1.UpdateOptions{})
if err != nil {
return err
}
return nil
}
// Delete - delete ReplicationControllers map
func (c *ReplicationControllerClient) Delete(ctx context.Context, param *scheduler.RC, options scheduler.Options) error {
op := convertToDeleteOptions(options)
return c.apiClient.clientSet.CoreV1().ReplicationControllers(param.Namespace).Delete(ctx, param.Name, op)
}
// Watch - watch RC change
func (c *ReplicationControllerClient) Watch(ctx context.Context, param *scheduler.RC, options scheduler.Options) (scheduler.Interface, error) {
op := convertToListOptions(options)
w, err := c.apiClient.clientSet.CoreV1().ReplicationControllers(param.Namespace).Watch(ctx, op)
if err != nil {
return nil, err
}
return NewWatcher(w), nil
}