This repository has been archived by the owner on Nov 1, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Copy pathresourcekinds.go
272 lines (223 loc) · 7.95 KB
/
resourcekinds.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
package kubernetes
import (
"fmt"
apiapps "k8s.io/api/apps/v1beta1"
apibatch "k8s.io/api/batch/v1beta1"
apiv1 "k8s.io/api/core/v1"
apiext "k8s.io/api/extensions/v1beta1"
meta_v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"github.com/weaveworks/flux"
"github.com/weaveworks/flux/cluster"
"github.com/weaveworks/flux/image"
"github.com/weaveworks/flux/resource"
)
/////////////////////////////////////////////////////////////////////////////
// Kind registry
type resourceKind interface {
getPodController(c *Cluster, namespace, name string) (podController, error)
getPodControllers(c *Cluster, namespace string) ([]podController, error)
}
var (
resourceKinds = make(map[string]resourceKind)
)
func init() {
resourceKinds["cronjob"] = &cronJobKind{}
resourceKinds["daemonset"] = &daemonSetKind{}
resourceKinds["deployment"] = &deploymentKind{}
resourceKinds["statefulset"] = &statefulSetKind{}
}
type podController struct {
apiVersion string
kind string
name string
status string
podTemplate apiv1.PodTemplateSpec
apiObject interface{}
}
func (pc podController) toClusterController(resourceID flux.ResourceID) cluster.Controller {
var clusterContainers []resource.Container
var excuse string
for _, container := range pc.podTemplate.Spec.Containers {
ref, err := image.ParseRef(container.Image)
if err != nil {
clusterContainers = nil
excuse = err.Error()
break
}
clusterContainers = append(clusterContainers, resource.Container{Name: container.Name, Image: ref})
}
return cluster.Controller{
ID: resourceID,
Status: pc.status,
Containers: cluster.ContainersOrExcuse{Containers: clusterContainers, Excuse: excuse},
}
}
func (pc podController) GetNamespace() string {
objectMeta := pc.apiObject.(namespacedLabeled)
return objectMeta.GetNamespace()
}
func (pc podController) GetLabels() map[string]string {
objectMeta := pc.apiObject.(namespacedLabeled)
return objectMeta.GetLabels()
}
/////////////////////////////////////////////////////////////////////////////
// extensions/v1beta1 Deployment
type deploymentKind struct{}
func (dk *deploymentKind) getPodController(c *Cluster, namespace, name string) (podController, error) {
deployment, err := c.client.Deployments(namespace).Get(name, meta_v1.GetOptions{})
if err != nil {
return podController{}, err
}
return makeDeploymentPodController(deployment), nil
}
func (dk *deploymentKind) getPodControllers(c *Cluster, namespace string) ([]podController, error) {
deployments, err := c.client.Deployments(namespace).List(meta_v1.ListOptions{})
if err != nil {
return nil, err
}
var podControllers []podController
for i := range deployments.Items {
podControllers = append(podControllers, makeDeploymentPodController(&deployments.Items[i]))
}
return podControllers, nil
}
func makeDeploymentPodController(deployment *apiext.Deployment) podController {
var status string
objectMeta, deploymentStatus := deployment.ObjectMeta, deployment.Status
if deploymentStatus.ObservedGeneration >= objectMeta.Generation {
// the definition has been updated; now let's see about the replicas
updated, wanted := deploymentStatus.UpdatedReplicas, *deployment.Spec.Replicas
if updated == wanted {
status = StatusReady
} else {
status = fmt.Sprintf("%d out of %d updated", updated, wanted)
}
} else {
status = StatusUpdating
}
return podController{
apiVersion: "extensions/v1beta1",
kind: "Deployment",
name: deployment.ObjectMeta.Name,
status: status,
podTemplate: deployment.Spec.Template,
apiObject: deployment}
}
/////////////////////////////////////////////////////////////////////////////
// extensions/v1beta daemonset
type daemonSetKind struct{}
func (dk *daemonSetKind) getPodController(c *Cluster, namespace, name string) (podController, error) {
daemonSet, err := c.client.DaemonSets(namespace).Get(name, meta_v1.GetOptions{})
if err != nil {
return podController{}, err
}
return makeDaemonSetPodController(daemonSet), nil
}
func (dk *daemonSetKind) getPodControllers(c *Cluster, namespace string) ([]podController, error) {
daemonSets, err := c.client.DaemonSets(namespace).List(meta_v1.ListOptions{})
if err != nil {
return nil, err
}
var podControllers []podController
for i, _ := range daemonSets.Items {
podControllers = append(podControllers, makeDaemonSetPodController(&daemonSets.Items[i]))
}
return podControllers, nil
}
func makeDaemonSetPodController(daemonSet *apiext.DaemonSet) podController {
var status string
objectMeta, daemonSetStatus := daemonSet.ObjectMeta, daemonSet.Status
if daemonSetStatus.ObservedGeneration >= objectMeta.Generation {
// the definition has been updated; now let's see about the replicas
updated, wanted := daemonSetStatus.UpdatedNumberScheduled, daemonSetStatus.DesiredNumberScheduled
if updated == wanted {
status = StatusReady
} else {
status = fmt.Sprintf("%d out of %d updated", updated, wanted)
}
} else {
status = StatusUpdating
}
return podController{
apiVersion: "extensions/v1beta1",
kind: "DaemonSet",
name: daemonSet.ObjectMeta.Name,
status: status,
podTemplate: daemonSet.Spec.Template,
apiObject: daemonSet}
}
/////////////////////////////////////////////////////////////////////////////
// apps/v1beta1 StatefulSet
type statefulSetKind struct{}
func (dk *statefulSetKind) getPodController(c *Cluster, namespace, name string) (podController, error) {
statefulSet, err := c.client.StatefulSets(namespace).Get(name, meta_v1.GetOptions{})
if err != nil {
return podController{}, err
}
return makeStatefulSetPodController(statefulSet), nil
}
func (dk *statefulSetKind) getPodControllers(c *Cluster, namespace string) ([]podController, error) {
statefulSets, err := c.client.StatefulSets(namespace).List(meta_v1.ListOptions{})
if err != nil {
return nil, err
}
var podControllers []podController
for i, _ := range statefulSets.Items {
podControllers = append(podControllers, makeStatefulSetPodController(&statefulSets.Items[i]))
}
return podControllers, nil
}
func makeStatefulSetPodController(statefulSet *apiapps.StatefulSet) podController {
var status string
objectMeta, statefulSetStatus := statefulSet.ObjectMeta, statefulSet.Status
if *statefulSetStatus.ObservedGeneration >= objectMeta.Generation {
// the definition has been updated; now let's see about the replicas
updated, wanted := statefulSetStatus.UpdatedReplicas, *statefulSet.Spec.Replicas
if updated == wanted {
status = StatusReady
} else {
status = fmt.Sprintf("%d out of %d updated", updated, wanted)
}
} else {
status = StatusUpdating
}
return podController{
apiVersion: "apps/v1beta1",
kind: "StatefulSet",
name: statefulSet.ObjectMeta.Name,
status: status,
podTemplate: statefulSet.Spec.Template,
apiObject: statefulSet}
}
/////////////////////////////////////////////////////////////////////////////
// batch/v1beta1 CronJob
type cronJobKind struct{}
func (dk *cronJobKind) getPodController(c *Cluster, namespace, name string) (podController, error) {
cronJob, err := c.client.CronJobs(namespace).Get(name, meta_v1.GetOptions{})
if err != nil {
return podController{}, err
}
return makeCronJobPodController(cronJob), nil
}
func (dk *cronJobKind) getPodControllers(c *Cluster, namespace string) ([]podController, error) {
cronJobs, err := c.client.CronJobs(namespace).List(meta_v1.ListOptions{})
if err != nil {
return nil, err
}
var podControllers []podController
for i, _ := range cronJobs.Items {
podControllers = append(podControllers, makeCronJobPodController(&cronJobs.Items[i]))
}
return podControllers, nil
}
func makeCronJobPodController(cronJob *apibatch.CronJob) podController {
return podController{
apiVersion: "batch/v1beta1",
kind: "CronJob",
name: cronJob.ObjectMeta.Name,
status: StatusReady,
podTemplate: cronJob.Spec.JobTemplate.Spec.Template,
apiObject: cronJob}
}
/////////////////////////////////////////////////////////////////////////////
//