/
controller.go
177 lines (153 loc) · 7.73 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
/*
Copyright 2022.
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 keptnworkload
import (
"context"
"fmt"
"reflect"
"time"
"github.com/go-logr/logr"
apilifecycle "github.com/keptn/lifecycle-toolkit/lifecycle-operator/apis/lifecycle/v1"
apicommon "github.com/keptn/lifecycle-toolkit/lifecycle-operator/apis/lifecycle/v1/common"
operatorcommon "github.com/keptn/lifecycle-toolkit/lifecycle-operator/common"
controllercommon "github.com/keptn/lifecycle-toolkit/lifecycle-operator/controllers/common"
"github.com/keptn/lifecycle-toolkit/lifecycle-operator/controllers/common/eventsender"
"github.com/keptn/lifecycle-toolkit/lifecycle-operator/controllers/common/telemetry"
controllererrors "github.com/keptn/lifecycle-toolkit/lifecycle-operator/controllers/errors"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/propagation"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
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/controller/controllerutil"
"sigs.k8s.io/controller-runtime/pkg/predicate"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
)
// KeptnWorkloadReconciler reconciles a KeptnWorkload object
type KeptnWorkloadReconciler struct {
client.Client
Scheme *runtime.Scheme
EventSender eventsender.IEvent
Log logr.Logger
TracerFactory telemetry.TracerFactory
}
// +kubebuilder:rbac:groups=lifecycle.keptn.sh,resources=keptnworkloads,verbs=get;list;watch;create;update;patch;delete
// +kubebuilder:rbac:groups=lifecycle.keptn.sh,resources=keptnworkloads/status,verbs=get;update;patch
// +kubebuilder:rbac:groups=lifecycle.keptn.sh,resources=keptnworkloads/finalizers,verbs=update
// +kubebuilder:rbac:groups=lifecycle.keptn.sh,resources=keptnworkloadversions,verbs=get;list;watch;create;update;patch;delete
// +kubebuilder:rbac:groups=lifecycle.keptn.sh,resources=keptnworkloadversions/status,verbs=get;update;patch
// +kubebuilder:rbac:groups=lifecycle.keptn.sh,resources=keptnworkloadversions/finalizers,verbs=update
// Reconcile is part of the main kubernetes reconciliation loop which aims to
// move the current state of the cluster closer to the desired state.
// TODO(user): Modify the Reconcile function to compare the state specified by
// the KeptnWorkload object against the actual cluster state, and then
// perform operations to make the cluster state reflect the state specified by
// the user.
//
// For more details, check Reconcile and its Result here:
// - https://pkg.go.dev/sigs.k8s.io/controller-runtime@v0.12.2/pkg/reconcile
func (r *KeptnWorkloadReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
requestInfo := controllercommon.GetRequestInfo(req)
r.Log.Info("Searching for workload", "requestInfo", requestInfo)
workload := &apilifecycle.KeptnWorkload{}
err := r.Get(ctx, req.NamespacedName, workload)
if errors.IsNotFound(err) {
return reconcile.Result{}, nil
}
if err != nil {
return reconcile.Result{}, fmt.Errorf(controllererrors.ErrCannotRetrieveWorkloadMsg, err)
}
traceContextCarrier := propagation.MapCarrier(workload.Annotations)
ctx = otel.GetTextMapPropagator().Extract(ctx, traceContextCarrier)
r.Log.Info("Reconciling Keptn Workload", "workload", workload.Name, "requestInfo", requestInfo)
workloadVersion := &apilifecycle.KeptnWorkloadVersion{}
workloadVersionName := operatorcommon.CreateResourceName(apicommon.MaxK8sObjectLength, apicommon.MinKeptnNameLen, workload.Name, workload.Spec.Version)
// Try to find the workload instance
err = r.Get(ctx, types.NamespacedName{Namespace: workload.Namespace, Name: workloadVersionName}, workloadVersion)
if client.IgnoreNotFound(err) != nil {
r.Log.Error(err, "could not get WorkloadVersion", "requestInfo", requestInfo)
return ctrl.Result{RequeueAfter: 10 * time.Second}, err
} else if errors.IsNotFound(err) {
// If the workload instance does not exist, create it
workloadVersion, err := r.createWorkloadVersion(ctx, workload)
if err != nil {
return reconcile.Result{}, err
}
err = r.Client.Create(ctx, workloadVersion)
if err != nil {
r.Log.Error(err, "could not create WorkloadVersion", "requestInfo", requestInfo)
r.EventSender.Emit(apicommon.PhaseCreateWorkloadVersion, "Warning", workloadVersion, apicommon.PhaseStateFailed, "could not create KeptnWorkloadVersion ", workloadVersion.Spec.Version)
return ctrl.Result{RequeueAfter: 10 * time.Second}, err
}
workload.Status.CurrentVersion = workload.Spec.Version
if err := r.Client.Status().Update(ctx, workload); err != nil {
r.Log.Error(err, "could not update Current Version of Workload")
return ctrl.Result{}, err
}
} else if !reflect.DeepEqual(workloadVersion.Spec.KeptnWorkloadSpec, workload.Spec) {
r.Log.Info("updating spec of KeptnWorkloadVersion", "requestInfo", requestInfo, "workloadVersion", workloadVersion.Name)
workloadVersion.Spec.KeptnWorkloadSpec = workload.Spec
if err := r.Client.Update(ctx, workloadVersion); err != nil {
r.Log.Error(err, "could not update spec of Workload", "requestInfo", requestInfo)
// requeue to try again in case of an unexpected error
return ctrl.Result{RequeueAfter: 10 * time.Second}, err
}
}
return ctrl.Result{}, nil
}
// SetupWithManager sets up the controller with the Manager.
func (r *KeptnWorkloadReconciler) SetupWithManager(mgr ctrl.Manager) error {
if err := mgr.GetFieldIndexer().IndexField(context.Background(), &apilifecycle.KeptnWorkload{}, "spec.app", func(rawObj client.Object) []string {
workload := rawObj.(*apilifecycle.KeptnWorkload)
return []string{workload.Spec.AppName}
}); err != nil {
return err
}
return ctrl.NewControllerManagedBy(mgr).
For(&apilifecycle.KeptnWorkload{}, builder.WithPredicates(predicate.GenerationChangedPredicate{})).
Complete(r)
}
func (r *KeptnWorkloadReconciler) createWorkloadVersion(ctx context.Context, workload *apilifecycle.KeptnWorkload) (*apilifecycle.KeptnWorkloadVersion, error) {
// create TraceContext
// follow up with a Keptn propagator that JSON-encoded the OTel map into our own key
traceContextCarrier := propagation.MapCarrier{}
otel.GetTextMapPropagator().Inject(ctx, traceContextCarrier)
previousVersion := ""
if workload.Spec.Version != workload.Status.CurrentVersion {
previousVersion = workload.Status.CurrentVersion
}
workloadVersion := generateWorkloadVersion(previousVersion, traceContextCarrier, workload)
err := controllerutil.SetControllerReference(workload, &workloadVersion, r.Scheme)
if err != nil {
r.Log.Error(err, "could not set controller reference for WorkloadVersion: "+workloadVersion.Name)
}
return &workloadVersion, err
}
func generateWorkloadVersion(previousVersion string, traceContextCarrier map[string]string, w *apilifecycle.KeptnWorkload) apilifecycle.KeptnWorkloadVersion {
return apilifecycle.KeptnWorkloadVersion{
ObjectMeta: metav1.ObjectMeta{
Annotations: traceContextCarrier,
Name: operatorcommon.CreateResourceName(apicommon.MaxK8sObjectLength, apicommon.MinKeptnNameLen, w.Name, w.Spec.Version),
Namespace: w.Namespace,
},
Spec: apilifecycle.KeptnWorkloadVersionSpec{
KeptnWorkloadSpec: w.Spec,
WorkloadName: w.Name,
PreviousVersion: previousVersion,
},
}
}