/
controller.go
333 lines (301 loc) · 10.7 KB
/
controller.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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
/*
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 configuredservice
import (
"context"
"github.com/go-logr/logr"
meshv1alpha1 "github.com/symcn/mesh-operator/api/v1alpha1"
"github.com/symcn/mesh-operator/pkg/option"
networkingv1beta1 "istio.io/client-go/pkg/apis/networking/v1beta1"
"k8s.io/apimachinery/pkg/api/errors"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
"k8s.io/klog"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/event"
"sigs.k8s.io/controller-runtime/pkg/handler"
"sigs.k8s.io/controller-runtime/pkg/predicate"
"sigs.k8s.io/controller-runtime/pkg/source"
)
// Reconciler reconciles a ConfiguredService object
type Reconciler struct {
client.Client
Log logr.Logger
Scheme *runtime.Scheme
Opt *option.ControllerOption
MeshConfig *meshv1alpha1.MeshConfig
}
// +kubebuilder:rbac:groups="",resources=*,verbs=get;list;watch;create;update;
// +kubebuilder:rbac:groups=apps,resources=*,verbs=get;list;watch;create;update;
// +kubebuilder:rbac:groups=mesh.symcn.com,resources=*,verbs=get;list;watch;create;update;patch;delete
// +kubebuilder:rbac:groups=networking.istio.io,resources=*,verbs=get;list;watch;create;update;patch;delete
// Reconcile ...
func (r *Reconciler) Reconcile(req ctrl.Request) (ctrl.Result, error) {
klog.Infof("Reconciling ConfiguredService: %s/%s", req.Namespace, req.Name)
ctx := context.TODO()
// Fetch the ConfiguredService instance
instance := &meshv1alpha1.ConfiguredService{}
err := r.Get(ctx, req.NamespacedName, instance)
if err != nil {
if errors.IsNotFound(err) {
// Request foundect not found, could have been deleted after reconcile req.
// Owned foundects are automatically garbage collected. For additional cleanup logic use finalizers.
// Return and don't requeue
klog.V(6).Infof("Can't found ConfiguredService[%s/%s], skip", req.Namespace, req.Name)
return ctrl.Result{}, nil
}
// Error reading the foundect - requeue the req.
return ctrl.Result{}, err
}
// Fetch the MeshConfig
err = r.getMeshConfig(ctx)
if err != nil {
klog.Errorf("Get cluster MeshConfig[%s/%s] error: %+v",
r.Opt.MeshConfigNamespace, r.Opt.MeshConfigName, err)
return ctrl.Result{}, err
}
// TODO Set finalizers
// deleteAmcFinalizer := "appmeshconfig.finalizers"
// if instance.ObjectMeta.DeletionTimestamp.IsZero() {
// // The object is not being deleted, so if it does not have our finalizer,
// // then lets add the finalizer and update the object. This is equivalent
// // registering our finalizer.
// if !containsString(instance.ObjectMeta.Finalizers, deleteAmcFinalizer) {
// instance.ObjectMeta.Finalizers = append(instance.ObjectMeta.Finalizers, deleteAmcFinalizer)
// if err := r.Update(ctx, instance); err != nil {
// return ctrl.Result{}, err
// }
// }
// } else {
// // The object is being deleted
// if containsString(instance.ObjectMeta.Finalizers, deleteAmcFinalizer) {
// // our finalizer is present, so lets handle any external dependency
// if err := r.deleteAmc(ctx, instance); err != nil {
// // if fail to delete the external dependency here, return with error
// // so that it can be retried
// return ctrl.Result{}, err
// }
// instance.ObjectMeta.Finalizers = removeString(instance.ObjectMeta.Finalizers, deleteAmcFinalizer)
// if err := r.Update(ctx, instance); err != nil {
// return ctrl.Result{}, err
// }
// }
// // Stop reconciliation as the item is being deleted
// return ctrl.Result{}, nil
// }
// Distribute Istio Config
if err := r.reconcileSidecar(ctx, instance); err != nil {
return ctrl.Result{}, err
}
if err := r.reconcileWorkloadEntry(ctx, instance); err != nil {
return ctrl.Result{}, err
}
if err := r.reconcileServiceEntry(ctx, instance); err != nil {
return ctrl.Result{}, err
}
sc := &meshv1alpha1.ServiceConfig{}
err = r.Get(ctx, types.NamespacedName{Name: instance.Name, Namespace: instance.Namespace}, sc)
if apierrors.IsNotFound(err) {
if err := r.reconcileDestinationRule(ctx, instance); err != nil {
return ctrl.Result{}, err
}
if err := r.reconcileVirtualService(ctx, instance); err != nil {
return ctrl.Result{}, err
}
}
// Reconcile AppMeshConfig
// klog.Infof("Reconcile AppMeshConfig[%s/%s]", req.Namespace, req.Name)
// err = r.reconcileAmc(ctx, instance)
// if err != nil {
// klog.Errorf("%s/%s create AppMeshConfig failed, err: %+v", req.Namespace, req.Name, err)
// }
klog.Infof("End Reconciliation, ConfiguredService: %s/%s.", req.Namespace, req.Name)
return ctrl.Result{}, nil
}
func (r *Reconciler) getMeshConfig(ctx context.Context) error {
meshConfig := &meshv1alpha1.MeshConfig{}
err := r.Get(
ctx,
types.NamespacedName{
Namespace: r.Opt.MeshConfigNamespace,
Name: r.Opt.MeshConfigName,
},
meshConfig,
)
if err != nil {
return err
}
r.MeshConfig = meshConfig
klog.V(6).Infof("Get cluster MeshConfig: %+v", meshConfig)
return nil
}
// func (r *Reconciler) reconcileAmc(ctx context.Context, cs *meshv1alpha1.ConfiguredService) error {
// name, ok := cs.Labels["app"]
// if !ok {
// klog.Infof("Can not found app label in ConfiguredService[%s], skip create AppMeshConfig.", cs.Name)
// return nil
// }
// found := &meshv1alpha1.AppMeshConfig{}
// err := r.Get(ctx, types.NamespacedName{Namespace: cs.Namespace, Name: name}, found)
// if err != nil {
// if errors.IsNotFound(err) {
// klog.Infof("Can't found AppMeshConfig[%s/%s], create...", cs.Namespace, name)
// amc := r.buildAmc(cs, name)
// err = r.Create(ctx, amc)
// if err != nil {
// klog.Errorf("Create AppMeshConfig[%s/%s] error: %+v", cs.Namespace, name, err)
// return err
// }
// return nil
// }
// klog.Errorf("Get AppMeshConfig[%s/%s] error: %+v", cs.Namespace, name, err)
// return err
// }
// err = r.Update(ctx, r.updateAmc(found, cs))
// if err != nil {
// klog.Errorf("Update AppMeshConfig[%s/%s] error: %+v", cs.Namespace, name, err)
// return err
// }
// return nil
// }
// func (r *Reconciler) buildAmc(cs *meshv1alpha1.ConfiguredService, name string) *meshv1alpha1.AppMeshConfig {
// return &meshv1alpha1.AppMeshConfig{
// ObjectMeta: v1.ObjectMeta{
// Name: name,
// Namespace: cs.Namespace,
// // Labels: map[string]string{"": ""},
// },
// Spec: meshv1alpha1.AppMeshConfigSpec{
// Services: []*meshv1alpha1.Service{{
// Name: cs.Name,
// OriginalName: cs.Spec.OriginalName,
// Ports: cs.Spec.Ports,
// Instances: cs.Spec.Instances,
// Policy: cs.Spec.Policy,
// Subsets: cs.Spec.Subsets,
// }},
// },
// }
// }
// func (r *Reconciler) updateAmc(amc *meshv1alpha1.AppMeshConfig, cs *meshv1alpha1.ConfiguredService) *meshv1alpha1.AppMeshConfig {
// found := false
// for _, svc := range amc.Spec.Services {
// if svc.Name == cs.Name {
// found = true
// svc.Subsets = cs.Spec.Subsets
// svc.Ports = cs.Spec.Ports
// svc.Instances = cs.Spec.Instances
// svc.Policy = cs.Spec.Policy
// svc.OriginalName = cs.Spec.OriginalName
// }
// }
// if !found {
// amc.Spec.Services = append(amc.Spec.Services, &meshv1alpha1.Service{
// Name: cs.Name,
// OriginalName: cs.Spec.OriginalName,
// Ports: cs.Spec.Ports,
// Instances: cs.Spec.Instances,
// Policy: cs.Spec.Policy,
// Subsets: cs.Spec.Subsets,
// })
// }
// return amc
// }
// func (r *Reconciler) deleteAmc(ctx context.Context, cs *meshv1alpha1.ConfiguredService) error {
// name, ok := cs.Labels["app"]
// if !ok {
// klog.Infof("Can not found app label in ConfiguredService[%s], skip delete AppMeshConfig.", cs.Name)
// return nil
// }
// found := &meshv1alpha1.AppMeshConfig{}
// err := r.Get(ctx, types.NamespacedName{Namespace: cs.Namespace, Name: name}, found)
// if err != nil {
// if errors.IsNotFound(err) {
// klog.Infof("Can't found AppMeshConfig[%s/%s] when deleting it", cs.Namespace, name)
// return nil
// }
// klog.Errorf("Get AppMeshConfig[%s/%s] error: %+v", cs.Namespace, name, err)
// return err
// }
// for _, svc := range found.Spec.Services {
// if len(found.Spec.Services) == 0 {
// if err := r.Delete(ctx, found); err != nil {
// klog.Errorf("Delete AppMeshConfig[%s] error: %+v", name, err)
// return err
// }
// return nil
// }
// if svc.Name == cs.Name {
// continue
// }
// found.Spec.Services = append(found.Spec.Services, svc)
// }
// if err := r.Update(ctx, found); err != nil {
// klog.Errorf("Update AppMeshConfig[%s] error: %+v", name, err)
// return err
// }
// return nil
// }
// Helper functions to check and remove string from a slice of strings.
// func containsString(slice []string, s string) bool {
// for _, item := range slice {
// if item == s {
// return true
// }
// }
// return false
// }
// func removeString(slice []string, s string) (result []string) {
// for _, item := range slice {
// if item == s {
// continue
// }
// result = append(result, item)
// }
// return
// }
// SetupWithManager ...
// ignored create event of istio configs
func (r *Reconciler) SetupWithManager(mgr ctrl.Manager) error {
builder := ctrl.NewControllerManagedBy(mgr).For(&meshv1alpha1.ConfiguredService{})
if r.Opt.WatchIstioCRD {
builder.
Watches(
&source.Kind{Type: &networkingv1beta1.WorkloadEntry{}},
&handler.EnqueueRequestForOwner{IsController: true, OwnerType: &meshv1alpha1.ConfiguredService{}},
).
Watches(
&source.Kind{Type: &networkingv1beta1.ServiceEntry{}},
&handler.EnqueueRequestForOwner{IsController: true, OwnerType: &meshv1alpha1.ConfiguredService{}},
).
Watches(
&source.Kind{Type: &networkingv1beta1.VirtualService{}},
&handler.EnqueueRequestForOwner{IsController: true, OwnerType: &meshv1alpha1.ConfiguredService{}},
).
Watches(
&source.Kind{Type: &networkingv1beta1.DestinationRule{}},
&handler.EnqueueRequestForOwner{IsController: true, OwnerType: &meshv1alpha1.ConfiguredService{}},
).
WithEventFilter(predicate.Funcs{
CreateFunc: func(e event.CreateEvent) bool {
_, ok := e.Object.(*meshv1alpha1.ConfiguredService)
if ok {
return true
}
return false
},
})
}
return builder.Complete(r)
}