-
Notifications
You must be signed in to change notification settings - Fork 0
/
containerizedworkload_controller.go
235 lines (210 loc) · 9.23 KB
/
containerizedworkload_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
/*
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 containerizedworkload
import (
"context"
"fmt"
"github.com/pkg/errors"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/types"
"strings"
cpv1alpha1 "github.com/crossplane/crossplane-runtime/apis/core/v1alpha1"
"github.com/crossplane/crossplane-runtime/pkg/event"
"github.com/crossplane/crossplane-runtime/pkg/logging"
"github.com/go-logr/logr"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/runtime"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/builder"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/predicate"
"github.com/xishengcai/oam/apis/core/v1alpha2"
"github.com/xishengcai/oam/pkg/controller"
"github.com/xishengcai/oam/pkg/oam/util"
)
// Reconcile error strings.
const (
errRenderWorkload = "cannot render workload"
errApplyChildResource= "cannot apply the childResource"
errRenderService = "cannot render service"
errApplyService = "cannot apply the service"
errGcService = "cannot gc the service"
errApplyConfigMap = "cannot apply the configMap"
)
// Setup adds a controller that reconciles ContainerizedWorkload.
func Setup(mgr ctrl.Manager, args controller.Args, log logging.Logger) error {
r := Reconciler{
Client: mgr.GetClient(),
log: ctrl.Log.WithName("ContainerizedWorkload"),
record: event.NewAPIRecorder(mgr.GetEventRecorderFor("ContainerizedWorkload")),
Scheme: mgr.GetScheme(),
}
return r.SetupWithManager(mgr)
}
// Reconciler reconciles a ContainerizedWorkload object
type Reconciler struct {
client.Client
log logr.Logger
record event.Recorder
Scheme *runtime.Scheme
childResource []string
}
// Reconcile reconciles a ContainerizedWorkload object
// +kubebuilder:rbac:groups=core.oam.dev,resources=containerizedworkloads,verbs=get;list;watch
// +kubebuilder:rbac:groups=core.oam.dev,resources=containerizedworkloads/status,verbs=get;update;patch
// +kubebuilder:rbac:groups=apps,resources=deployments,verbs=get;list;watch;create;update;patch;delete
// +kubebuilder:rbac:groups=core,resources=services,verbs=get;list;watch;create;update;patch;delete
// +kubebuilder:rbac:groups=core,resources=configmaps,verbs=get;list;watch;create;update;patch;delete
func (r *Reconciler) Reconcile(req ctrl.Request) (ctrl.Result, error) {
ctx := context.Background()
log := r.log.WithValues("containerizedworkload", req.NamespacedName)
log.Info("Reconcile container workload")
var workload v1alpha2.ContainerizedWorkload
if err := r.Get(ctx, req.NamespacedName, &workload); err != nil {
if apierrors.IsNotFound(err) {
log.Info("Container workload is deleted")
}
return ctrl.Result{}, client.IgnoreNotFound(err)
}
//log.Info("Get the workload", "apiVersion", workload.APIVersion, "kind", workload.Kind)
// find the resource object to record the event to, default is the parent appConfig.
eventObj, err := util.LocateParentAppConfig(ctx, r.Client, &workload)
if eventObj == nil {
// fallback to workload itself
log.Error(err, "workload", workload.Name)
eventObj = &workload
}
// applicationConfiguration write label by workload child define
if workload.Labels == nil {
log.Info("label is nil, default child resource deployment")
}
//r.childResource = childResourceValue
workload.Status.Resources = nil
applyOpts := []client.PatchOption{client.ForceOwnership, client.FieldOwner(workload.GetUID())}
childObject, err := r.renderChildResource(&workload)
if err != nil {
log.Error(err, "Failed to render a deployment")
r.record.Event(eventObj, event.Warning(errRenderWorkload, err))
return util.ReconcileWaitResult,
util.PatchCondition(ctx, r, &workload, cpv1alpha1.ReconcileError(errors.Wrap(err, errRenderWorkload)))
}
//server side apply, only the fields we set are touched
if err := r.Patch(ctx, childObject, client.Apply, applyOpts...); err != nil {
log.Error(err, "Failed to apply to a deployment")
r.record.Event(eventObj, event.Warning(errApplyChildResource, err))
return util.ReconcileWaitResult,
util.PatchCondition(ctx, r, &workload, cpv1alpha1.ReconcileError(errors.Wrap(err, errApplyChildResource)))
}
var uid types.UID
switch workload.Labels[util.LabelKeyChildResource] {
case util.KindDeployment:
uid = childObject.(*appsv1.Deployment).UID
case util.KindStatefulSet:
uid = childObject.(*appsv1.StatefulSet).UID
}
workload.Status.Resources = append(workload.Status.Resources,
cpv1alpha1.TypedReference{
APIVersion: childObject.GetObjectKind().GroupVersionKind().GroupVersion().String(),
Kind: childObject.GetObjectKind().GroupVersionKind().Kind,
Name: workload.Name,
UID: uid,
},
)
r.record.Event(eventObj, event.Normal("childResource created",
fmt.Sprintf("Workload `%s` successfully server side patched a childResource", workload.Name)))
// garbage collect the deployment that we created but not needed
if err := r.cleanupResources(ctx, &workload, util.KindDeployment, uid); err != nil {
log.Error(err, "Failed to clean up resources")
r.record.Event(eventObj, event.Warning(errApplyChildResource, err))
}
// configMap
configMapApplyOpts := []client.PatchOption{client.ForceOwnership, client.FieldOwner(workload.GetUID())}
configMaps, err := r.renderConfigMaps(ctx, &workload)
if err != nil {
log.Error(err, "Failed to render configMap")
r.record.Event(eventObj, event.Warning(errRenderWorkload, err))
return util.ReconcileWaitResult,
util.PatchCondition(ctx, r, &workload, cpv1alpha1.ReconcileError(errors.Wrap(err, errRenderWorkload)))
}
for _, cm := range configMaps {
if err := r.Patch(ctx, cm, client.Apply, configMapApplyOpts...); err != nil {
log.Error(err, "Failed to apply a configMap")
r.record.Event(eventObj, event.Warning(errApplyConfigMap, err))
return util.ReconcileWaitResult,
util.PatchCondition(ctx, r, &workload, cpv1alpha1.ReconcileError(errors.Wrap(err, errApplyConfigMap)))
}
r.record.Event(eventObj, event.Normal("ConfigMap created",
fmt.Sprintf("Workload `%s` successfully server side patched a configmap `%s`",
workload.Name, cm.Name)))
// record the new deployment, new service
workload.Status.Resources = append(workload.Status.Resources,
cpv1alpha1.TypedReference{
APIVersion: cm.GetObjectKind().GroupVersionKind().GroupVersion().String(),
Kind: cm.GetObjectKind().GroupVersionKind().Kind,
Name: cm.GetName(),
UID: cm.UID,
},
)
}
// service
service, err := r.renderService(ctx, &workload, childObject)
if err != nil {
r.record.Event(eventObj, event.Warning(errRenderService, err))
return util.ReconcileWaitResult,
util.PatchCondition(ctx, r, &workload, cpv1alpha1.ReconcileError(errors.Wrap(err, errRenderService)))
}
// PointToGrayName is gray workload
if service != nil{
// server side apply the service
if err := r.Patch(ctx, service, client.Apply, applyOpts...); err != nil {
log.Error(err, "Failed to apply a service")
r.record.Event(eventObj, event.Warning(errApplyService, err))
return util.ReconcileWaitResult,
util.PatchCondition(ctx, r, &workload, cpv1alpha1.ReconcileError(errors.Wrap(err, errApplyService)))
}
//r.record.Event(eventObj, event.Normal("Service created",
// fmt.Sprintf("Workload `%s` successfully server side patched a service `%s`",
// workload.Name, service.Name)))
// garbage collect the service/deployments that we created but not needed
if err := r.cleanupResources(ctx, &workload, serviceKind, service.UID); err != nil {
log.Error(err, "Failed to clean up resources")
r.record.Event(eventObj, event.Warning(errGcService, err))
}
// record the new deployment, new service
workload.Status.Resources = append(workload.Status.Resources,
cpv1alpha1.TypedReference{
APIVersion: service.GetObjectKind().GroupVersionKind().GroupVersion().String(),
Kind: service.GetObjectKind().GroupVersionKind().Kind,
Name: service.GetName(),
UID: service.UID,
},
)
}
if err := r.Status().Update(ctx, &workload); err != nil {
return util.ReconcileWaitResult, err
}
return ctrl.Result{}, util.PatchCondition(ctx, r, &workload, cpv1alpha1.ReconcileSuccess())
}
// SetupWithManager setups up k8s controller.
func (r *Reconciler) SetupWithManager(mgr ctrl.Manager) error {
src := &v1alpha2.ContainerizedWorkload{}
name := "oam/" + strings.ToLower(v1alpha2.ContainerizedWorkloadKind)
return ctrl.NewControllerManagedBy(mgr).
Named(name).
For(src).
Owns(&appsv1.Deployment{}, builder.WithPredicates(predicate.GenerationChangedPredicate{})).
Owns(&appsv1.StatefulSet{}, builder.WithPredicates(predicate.GenerationChangedPredicate{})).
Owns(&corev1.Service{}, builder.WithPredicates(predicate.GenerationChangedPredicate{})).
Owns(&corev1.ConfigMap{}, builder.WithPredicates(predicate.GenerationChangedPredicate{})).
Complete(r)
}