-
Notifications
You must be signed in to change notification settings - Fork 25
/
objects.go
184 lines (165 loc) · 5.04 KB
/
objects.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
package controllers
import (
"context"
"fmt"
addonmgrv1alpha1 "github.com/keikoproj/addon-manager/api/addon/v1alpha1"
"sigs.k8s.io/controller-runtime/pkg/client"
appsv1 "k8s.io/api/apps/v1"
batchv1 "k8s.io/api/batch/v1"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/labels"
)
func ObserveService(cli client.Client, namespace string, selector labels.Selector) ([]addonmgrv1alpha1.ObjectStatus, error) {
services := &corev1.ServiceList{}
err := cli.List(context.TODO(), services, &client.ListOptions{
LabelSelector: selector,
Namespace: namespace,
})
if err != nil {
return nil, fmt.Errorf("failed to list services %v", err)
}
res := []addonmgrv1alpha1.ObjectStatus{}
for _, service := range services.Items {
if service.ObjectMeta.Namespace == namespace {
res = append(res, addonmgrv1alpha1.ObjectStatus{
Kind: "Service",
Group: "",
Name: service.GetName(),
Link: service.GetSelfLink(),
})
}
}
return res, nil
}
func ObserveJob(cli client.Client, namespace string, selector labels.Selector) ([]addonmgrv1alpha1.ObjectStatus, error) {
jobs := &batchv1.JobList{}
err := cli.List(context.TODO(), jobs, &client.ListOptions{
LabelSelector: selector,
Namespace: namespace,
})
if err != nil {
return nil, fmt.Errorf("failed to list cronjobs %v", err)
}
res := []addonmgrv1alpha1.ObjectStatus{}
for _, job := range jobs.Items {
res = append(res, addonmgrv1alpha1.ObjectStatus{
Kind: "Job",
Group: "batch/v1",
Name: job.GetName(),
Link: job.GetSelfLink(),
})
}
return res, nil
}
func ObserveCronJob(cli client.Client, namespace string, selector labels.Selector) ([]addonmgrv1alpha1.ObjectStatus, error) {
cronJobs := &batchv1.CronJobList{}
err := cli.List(context.TODO(), cronJobs, &client.ListOptions{
LabelSelector: selector,
Namespace: namespace,
})
if err != nil {
return nil, fmt.Errorf("failed to list cronjobs %v", err)
}
res := []addonmgrv1alpha1.ObjectStatus{}
for _, cronJob := range cronJobs.Items {
res = append(res, addonmgrv1alpha1.ObjectStatus{
Kind: "CronJob",
Group: "batch/v1",
Name: cronJob.GetName(),
Link: cronJob.GetSelfLink(),
})
}
return res, nil
}
func ObserveDeployment(cli client.Client, namespace string, selector labels.Selector) ([]addonmgrv1alpha1.ObjectStatus, error) {
deployments := &appsv1.DeploymentList{}
err := cli.List(context.TODO(), deployments, &client.ListOptions{
LabelSelector: selector})
if err != nil {
return nil, err
}
res := []addonmgrv1alpha1.ObjectStatus{}
for _, deployment := range deployments.Items {
res = append(res, addonmgrv1alpha1.ObjectStatus{
Kind: "Deployment",
Group: "apps/v1",
Name: deployment.GetName(),
Link: deployment.GetSelfLink(),
})
}
return res, nil
}
func ObserveDaemonSet(cli client.Client, namespace string, selector labels.Selector) ([]addonmgrv1alpha1.ObjectStatus, error) {
daemonSets := &appsv1.DaemonSetList{}
err := cli.List(context.TODO(), daemonSets, &client.ListOptions{
LabelSelector: selector})
if err != nil {
return nil, err
}
res := []addonmgrv1alpha1.ObjectStatus{}
for _, deployment := range daemonSets.Items {
res = append(res, addonmgrv1alpha1.ObjectStatus{
Kind: "DaemonSet",
Group: "apps/v1",
Name: deployment.GetName(),
Link: deployment.GetSelfLink(),
})
}
return res, nil
}
func ObserveReplicaSet(cli client.Client, namespace string, selector labels.Selector) ([]addonmgrv1alpha1.ObjectStatus, error) {
replicaSets := &appsv1.ReplicaSetList{}
err := cli.List(context.TODO(), replicaSets, &client.ListOptions{
LabelSelector: selector})
if err != nil {
return nil, err
}
res := []addonmgrv1alpha1.ObjectStatus{}
for _, replicaSet := range replicaSets.Items {
res = append(res, addonmgrv1alpha1.ObjectStatus{
Kind: "ReplicaSe",
Group: "apps/v1",
Name: replicaSet.GetName(),
Link: replicaSet.GetSelfLink(),
})
}
return res, nil
}
func ObserveStatefulSet(cli client.Client, name string, selector labels.Selector) ([]addonmgrv1alpha1.ObjectStatus, error) {
statefulSets := &appsv1.StatefulSetList{}
err := cli.List(context.TODO(), statefulSets, &client.ListOptions{
LabelSelector: selector})
if err != nil {
return nil, err
}
res := []addonmgrv1alpha1.ObjectStatus{}
for _, statefulSet := range statefulSets.Items {
res = append(res, addonmgrv1alpha1.ObjectStatus{
Kind: "StatefulSet",
Group: "apps/v1",
Name: statefulSet.GetName(),
Link: statefulSet.GetSelfLink(),
})
}
return res, nil
}
func ObserveNamespace(cli client.Client, name string, selector labels.Selector) ([]addonmgrv1alpha1.ObjectStatus, error) {
namespaces := &corev1.NamespaceList{}
err := cli.List(context.TODO(), namespaces, &client.ListOptions{
LabelSelector: selector})
if err != nil {
return nil, err
}
res := []addonmgrv1alpha1.ObjectStatus{}
for _, namespace := range namespaces.Items {
if namespace.Name == name {
res = append(res, addonmgrv1alpha1.ObjectStatus{
Kind: "Namespace",
Group: "",
Name: namespace.GetName(),
Link: namespace.GetSelfLink(),
})
}
}
return res, nil
}