-
Notifications
You must be signed in to change notification settings - Fork 5
/
apply_f.go
261 lines (255 loc) · 7.94 KB
/
apply_f.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
package k8s
import (
"bytes"
"context"
"io/ioutil"
"regexp"
"github.com/forbearing/k8s/clusterrole"
"github.com/forbearing/k8s/clusterrolebinding"
"github.com/forbearing/k8s/configmap"
"github.com/forbearing/k8s/cronjob"
"github.com/forbearing/k8s/daemonset"
"github.com/forbearing/k8s/deployment"
"github.com/forbearing/k8s/ingress"
"github.com/forbearing/k8s/ingressclass"
"github.com/forbearing/k8s/job"
"github.com/forbearing/k8s/namespace"
"github.com/forbearing/k8s/networkpolicy"
"github.com/forbearing/k8s/persistentvolume"
"github.com/forbearing/k8s/persistentvolumeclaim"
"github.com/forbearing/k8s/pod"
"github.com/forbearing/k8s/replicaset"
"github.com/forbearing/k8s/replicationcontroller"
"github.com/forbearing/k8s/role"
"github.com/forbearing/k8s/rolebinding"
"github.com/forbearing/k8s/secret"
"github.com/forbearing/k8s/service"
"github.com/forbearing/k8s/serviceaccount"
"github.com/forbearing/k8s/statefulset"
"github.com/forbearing/k8s/storageclass"
appsv1 "k8s.io/api/apps/v1"
batchv1 "k8s.io/api/batch/v1"
corev1 "k8s.io/api/core/v1"
networking "k8s.io/api/networking/v1"
rbacv1 "k8s.io/api/rbac/v1"
storagev1 "k8s.io/api/storage/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/kubernetes/scheme"
"k8s.io/klog"
)
// ApplyF work like "kubectl apply -f filename.yaml",
func ApplyF(ctx context.Context, kubeconfig, filename string) error {
k8sResourceFile, err := ioutil.ReadFile(filename)
if err != nil {
return err
}
// remove all comments in the yaml file.
removeComments := regexp.MustCompile(`#.*`)
k8sResourceFile = removeComments.ReplaceAll(k8sResourceFile, []byte(""))
// split yaml file by "---"
k8sResourceItems := bytes.Split(k8sResourceFile, []byte("---"))
for _, k8sResource := range k8sResourceItems {
// ignore empty line
if len(bytes.TrimSpace(k8sResource)) == 0 {
continue
}
object, err := Decode(k8sResource)
if err != nil {
klog.V(4).Infof("Decode error: %v", err)
continue
}
switch object.(type) {
case *corev1.Namespace:
handler, err := namespace.New(ctx, kubeconfig)
if err != nil {
return err
}
if ns, err := handler.Apply(k8sResource); err != nil {
klog.V(4).Infof("apply namespace %q failed: %v\n", ns.Name, err)
}
case *corev1.Service:
handler, err := service.New(ctx, kubeconfig, "")
if err != nil {
return err
}
if svc, err := handler.Apply(k8sResource); err != nil {
klog.V(4).Infof("apply service %q failed: %v\n", svc.Name, err)
}
case *corev1.ConfigMap:
handler, err := configmap.New(ctx, kubeconfig, "")
if err != nil {
return err
}
if cm, err := handler.Apply(k8sResource); err != nil {
klog.V(4).Infof("apply configmap %q failed: %v\n", cm.Name, err)
}
case *corev1.Secret:
handler, err := secret.New(ctx, kubeconfig, "")
if err != nil {
return err
}
if sec, err := handler.Apply(k8sResource); err != nil {
klog.V(4).Infof("apply secret %q failed: %v\n", sec.Name, err)
}
case *corev1.ServiceAccount:
handler, err := serviceaccount.New(ctx, kubeconfig, "")
if err != nil {
return err
}
if sa, err := handler.Apply(k8sResource); err != nil {
klog.V(4).Infof("apply serviceaccount %q failed: %v\n", sa.Name, err)
}
case *corev1.Pod:
handler, err := pod.New(ctx, kubeconfig, "")
if err != nil {
return err
}
if p, err := handler.Apply(k8sResource); err != nil {
klog.V(4).Infof("apply pod %q failed: %v\n", p.Name, err)
}
case *corev1.PersistentVolume:
handler, err := persistentvolume.New(ctx, kubeconfig)
if err != nil {
return err
}
if pv, err := handler.Apply(k8sResource); err != nil {
klog.V(4).Infof("apply persistentvolume %q failed: %v\n", pv.Name, err)
}
case *corev1.PersistentVolumeClaim:
handler, err := persistentvolumeclaim.New(ctx, kubeconfig, "")
if err != nil {
return err
}
if pvc, err := handler.Apply(k8sResource); err != nil {
klog.V(4).Infof("apply persistentvolumeclaim %q failed: %v\n", pvc.Name, err)
}
case *appsv1.Deployment:
handler, err := deployment.New(ctx, kubeconfig, "")
if err != nil {
return err
}
if deploy, err := handler.Apply(k8sResource); err != nil {
klog.V(4).Infof("apply deployment %q failed: %v\n", deploy.Name, err)
}
case *appsv1.StatefulSet:
handler, err := statefulset.New(ctx, kubeconfig, "")
if err != nil {
return err
}
if sts, err := handler.Apply(k8sResource); err != nil {
klog.V(4).Infof("apply statefulset %q failed: %v\n", sts.Name, err)
}
case *appsv1.DaemonSet:
handler, err := daemonset.New(ctx, kubeconfig, "")
if err != nil {
return err
}
if ds, err := handler.Apply(k8sResource); err != nil {
klog.V(4).Infof("apply daemonset %q failed: %v\n", ds.Name, err)
}
case *networking.Ingress:
handler, err := ingress.New(ctx, kubeconfig, "")
if err != nil {
return err
}
if ing, err := handler.Apply(k8sResource); err != nil {
klog.V(4).Infof("apply ingress %q failed: %v\n", ing.Name, err)
}
case *networking.IngressClass:
handler, err := ingressclass.New(ctx, kubeconfig)
if err != nil {
return err
}
if ingc, err := handler.Apply(k8sResource); err != nil {
klog.V(4).Infof("apply ingressclass %q failed: %v\n", ingc.Name, err)
}
case *networking.NetworkPolicy:
handler, err := networkpolicy.New(ctx, kubeconfig, "")
if err != nil {
return err
}
if netpol, err := handler.Apply(k8sResource); err != nil {
klog.V(4).Infof("apply networkpolicy %q failed: %v\n", netpol.Name, err)
}
case *batchv1.Job:
handler, err := job.New(ctx, kubeconfig, "")
if err != nil {
return err
}
if j, err := handler.Apply(k8sResource); err != nil {
klog.V(4).Infof("apply job %q failed: %v\n", j.Name, err)
}
case *batchv1.CronJob:
handler, err := cronjob.New(ctx, kubeconfig, "")
if err != nil {
return err
}
if cj, err := handler.Apply(k8sResource); err != nil {
klog.V(4).Infof("apply cronjob %q failed: %v\n", cj.Name, err)
}
case *rbacv1.Role:
handler, err := role.New(ctx, kubeconfig, "")
if err != nil {
return err
}
if r, err := handler.Apply(k8sResource); err != nil {
klog.V(4).Infof("apply role %q failed: %v\n", r.Name, err)
}
case *rbacv1.RoleBinding:
handler, err := rolebinding.New(ctx, kubeconfig, "")
if err != nil {
return err
}
if rb, err := handler.Apply(k8sResource); err != nil {
klog.V(4).Infof("apply rolebinding %q failed: %v\n", rb.Name, err)
}
case *rbacv1.ClusterRole:
handler, err := clusterrole.New(ctx, kubeconfig)
if err != nil {
return err
}
if cr, err := handler.Apply(k8sResource); err != nil {
klog.V(4).Infof("apply clusterrole %q failed: %v\n", cr.Name, err)
}
case *rbacv1.ClusterRoleBinding:
handler, err := clusterrolebinding.New(ctx, kubeconfig)
if err != nil {
return err
}
if crb, err := handler.Apply(k8sResource); err != nil {
klog.V(4).Infof("apply clusterrolebinding %q failed: %v\n", crb.Name, err)
}
case *corev1.ReplicationController:
handler, err := replicationcontroller.New(ctx, kubeconfig, "")
if err != nil {
return err
}
if rc, err := handler.Apply(k8sResource); err != nil {
klog.V(4).Infof("apply replicationcontroller %q failed: %v\n", rc.Name, err)
}
case *appsv1.ReplicaSet:
handler, err := replicaset.New(ctx, kubeconfig, "")
if err != nil {
return err
}
if rs, err := handler.Apply(k8sResource); err != nil {
klog.V(4).Infof("apply replicaset %q failed: %v\n", rs.Name, err)
}
case *storagev1.StorageClass:
handler, err := storageclass.New(ctx, kubeconfig)
if err != nil {
return err
}
if sc, err := handler.Apply(k8sResource); err != nil {
klog.V(4).Infof("apply storageclass %q failed: %v\n", sc.Name, err)
}
default:
}
}
return nil
}
func Decode(data []byte) (runtime.Object, error) {
decode := scheme.Codecs.UniversalDeserializer().Decode
object, _, err := decode(data, nil, nil)
return object, err
}