This repository has been archived by the owner on Jun 19, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 82
/
helper.go
160 lines (145 loc) · 5.28 KB
/
helper.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
/*
Copyright 2020 Google LLC
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package reconciler
import (
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/equality"
apierrs "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/kubernetes"
appsv1listers "k8s.io/client-go/listers/apps/v1"
corev1listers "k8s.io/client-go/listers/core/v1"
"k8s.io/client-go/tools/cache"
"k8s.io/client-go/tools/record"
)
const (
deploymentCreated = "DeploymentCreated"
deploymentUpdated = "DeploymentUpdated"
serviceCreated = "ServiceCreated"
serviceUpdated = "ServiceUpdated"
configMapCreated = "ConfigMapCreated"
configMapUpdated = "ConfigMapUpdated"
)
type ServiceReconciler struct {
KubeClient kubernetes.Interface
ServiceLister corev1listers.ServiceLister
EndpointsLister corev1listers.EndpointsLister
Recorder record.EventRecorder
}
type DeploymentReconciler struct {
KubeClient kubernetes.Interface
Lister appsv1listers.DeploymentLister
Recorder record.EventRecorder
}
type ConfigMapReconciler struct {
KubeClient kubernetes.Interface
Lister corev1listers.ConfigMapLister
Recorder record.EventRecorder
}
// ReconcileDeployment reconciles the K8s Deployment 'd'.
func (r *DeploymentReconciler) ReconcileDeployment(obj runtime.Object, d *appsv1.Deployment) (*appsv1.Deployment, error) {
current, err := r.Lister.Deployments(d.Namespace).Get(d.Name)
if apierrs.IsNotFound(err) {
current, err = r.KubeClient.AppsV1().Deployments(d.Namespace).Create(d)
if apierrs.IsAlreadyExists(err) {
return current, nil
}
if err == nil {
r.Recorder.Eventf(obj, corev1.EventTypeNormal, deploymentCreated, "Created deployment %s/%s", d.Namespace, d.Name)
}
return current, err
}
if err != nil {
return nil, err
}
if !equality.Semantic.DeepDerivative(d.Spec, current.Spec) {
// Don't modify the informers copy.
desired := current.DeepCopy()
desired.Spec = d.Spec
d, err := r.KubeClient.AppsV1().Deployments(desired.Namespace).Update(desired)
if err == nil {
r.Recorder.Eventf(obj, corev1.EventTypeNormal, deploymentUpdated, "Updated deployment %s/%s", d.Namespace, d.Name)
}
return d, err
}
return current, err
}
// ReconcileService reconciles the K8s Service 'svc'.
func (r *ServiceReconciler) ReconcileService(obj runtime.Object, svc *corev1.Service) (*corev1.Endpoints, error) {
current, err := r.ServiceLister.Services(svc.Namespace).Get(svc.Name)
if apierrs.IsNotFound(err) {
current, err = r.KubeClient.CoreV1().Services(svc.Namespace).Create(svc)
if err == nil {
r.Recorder.Eventf(obj, corev1.EventTypeNormal, serviceCreated, "Created service %s/%s", svc.Namespace, svc.Name)
}
if err == nil || apierrs.IsAlreadyExists(err) {
return r.EndpointsLister.Endpoints(svc.Namespace).Get(svc.Name)
}
return nil, err
}
if err != nil {
return nil, err
}
// spec.clusterIP is immutable and is set on existing services. If we don't set this to the same value, we will
// encounter an error while updating.
svc.Spec.ClusterIP = current.Spec.ClusterIP
if !equality.Semantic.DeepDerivative(svc.Spec, current.Spec) {
// Don't modify the informers copy.
desired := current.DeepCopy()
desired.Spec = svc.Spec
current, err = r.KubeClient.CoreV1().Services(current.Namespace).Update(desired)
if err == nil {
r.Recorder.Eventf(obj, corev1.EventTypeNormal, serviceUpdated, "Updated service %s/%s", svc.Namespace, svc.Name)
}
if err != nil {
return nil, err
}
}
return r.EndpointsLister.Endpoints(svc.Namespace).Get(svc.Name)
}
// ReconcileConfigMap reconciles the K8s ConfigMao 'cm'.
func (r *ConfigMapReconciler) ReconcileConfigMap(obj runtime.Object, cm *corev1.ConfigMap, handlers ...cache.ResourceEventHandlerFuncs) (*corev1.ConfigMap, error) {
current, err := r.Lister.ConfigMaps(cm.Namespace).Get(cm.Name)
if apierrs.IsNotFound(err) {
current, err = r.KubeClient.CoreV1().ConfigMaps(cm.Namespace).Create(cm)
if apierrs.IsAlreadyExists(err) {
return current, nil
}
if err == nil {
r.Recorder.Eventf(obj, corev1.EventTypeNormal, configMapCreated, "Created configmap %s/%s", cm.Namespace, cm.Name)
for _, h := range handlers {
h.OnAdd(current)
}
}
return current, err
}
if err != nil {
return nil, err
}
if !equality.Semantic.DeepEqual(cm.BinaryData, current.BinaryData) || !equality.Semantic.DeepEqual(cm.Data, current.Data) {
// Don't modify the informers copy.
desired := current.DeepCopy()
desired.Data = cm.Data
desired.BinaryData = cm.BinaryData
res, err := r.KubeClient.CoreV1().ConfigMaps(desired.Namespace).Update(desired)
if err == nil {
r.Recorder.Eventf(obj, corev1.EventTypeNormal, configMapUpdated, "Updated configmap %s/%s", res.Namespace, res.Name)
for _, h := range handlers {
h.OnUpdate(current, desired)
}
}
return res, err
}
return current, err
}