-
Notifications
You must be signed in to change notification settings - Fork 0
/
cmap.go
177 lines (139 loc) · 5.64 KB
/
cmap.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
package function
import (
"context"
"fmt"
"reflect"
corev1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/runtime/schema"
"knative.dev/pkg/kmeta"
"knative.dev/pkg/reconciler"
"knative.dev/pkg/tracker"
commonv1alpha1 "github.com/zeiss/typhoon/pkg/apis/common/v1alpha1"
"github.com/zeiss/typhoon/pkg/apis/extensions/v1alpha1"
common "github.com/zeiss/typhoon/pkg/reconciler"
"github.com/zeiss/typhoon/pkg/reconciler/event"
"github.com/zeiss/typhoon/pkg/reconciler/resource"
"github.com/zeiss/typhoon/pkg/reconciler/skip"
)
const functionNameLabel = "extensions.typhoon.zeiss.com/function"
const codeCmapDataKey = "code"
// appInstanceLabel is a unique name identifying the instance of an application.
// See Kubernetes recommended labels
// https://kubernetes.io/docs/concepts/overview/working-with-objects/common-labels/
const appInstanceLabel = "app.kubernetes.io/instance"
var configMapGVK = corev1.SchemeGroupVersion.WithKind("ConfigMap")
func (r *Reconciler) reconcileConfigmap(ctx context.Context) error {
if skip.Skip(ctx) {
return nil
}
f := commonv1alpha1.ReconcilableFromContext(ctx).(*v1alpha1.Function)
status := &f.Status
desiredCmap := newCodeConfigMap(f)
currentCmap, err := r.getOrCreateCodeConfigMap(ctx, desiredCmap)
if err != nil {
status.MarkConfigMapUnavailable(v1alpha1.FunctionReasonFailedSync, fmt.Sprintf(
"Failed to get or create code ConfigMap: %s", err))
return err
}
if currentCmap, err = r.syncCodeConfigMap(ctx, currentCmap, desiredCmap); err != nil {
status.MarkConfigMapUnavailable(v1alpha1.FunctionReasonFailedSync, fmt.Sprintf(
"Failed to synchronize code ConfigMap: %s", err))
return fmt.Errorf("synchronizing code ConfigMap: %w", err)
}
cmRef := tracker.Reference{
APIVersion: configMapGVK.GroupVersion().String(),
Kind: configMapGVK.Kind,
Name: currentCmap.Name,
Namespace: currentCmap.Namespace,
}
if err := r.tracker.TrackReference(cmRef, f); err != nil {
return fmt.Errorf("tracking changes to code ConfigMap: %w", err)
}
status.MarkConfigMapAvailable(currentCmap.Name, currentCmap.ResourceVersion)
return nil
}
// getOrCreateCodeConfigMap returns the existing code ConfigMap for a given
// Function instance, or creates it if it is missing.
func (r *Reconciler) getOrCreateCodeConfigMap(ctx context.Context,
desiredCmap *corev1.ConfigMap,
) (*corev1.ConfigMap, error) {
rcl := commonv1alpha1.ReconcilableFromContext(ctx)
cmap, err := r.findCodeConfigMap(rcl, metav1.GetControllerOfNoCopy(desiredCmap))
switch {
case apierrors.IsNotFound(err):
cmap, err = r.cmCli(desiredCmap.Namespace).Create(ctx, desiredCmap, metav1.CreateOptions{})
if err != nil {
return nil, reconciler.NewEvent(corev1.EventTypeWarning, "FailedConfigMapCreate",
"Failed to create code ConfigMap %q: %s", desiredCmap.Name, err)
}
event.Normal(ctx, "CreateConfigMap", "Created code ConfigMap %q", cmap.Name)
case err != nil:
return nil, fmt.Errorf("getting code ConfigMap from cache: %w", err)
}
return cmap, nil
}
// syncCodeConfigMap synchronizes the desired state of a Function's code
// ConfigMap against its current state in the running cluster.
func (r *Reconciler) syncCodeConfigMap(ctx context.Context,
currentCmap, desiredCmap *corev1.ConfigMap,
) (*corev1.ConfigMap, error) {
if reflect.DeepEqual(desiredCmap.Data, currentCmap.Data) {
return currentCmap, nil
}
// resourceVersion must be returned to the API server unmodified for
// optimistic concurrency, as per Kubernetes API conventions
desiredCmap.ResourceVersion = currentCmap.ResourceVersion
cmap, err := r.cmCli(desiredCmap.Namespace).Update(ctx, desiredCmap, metav1.UpdateOptions{})
if err != nil {
return nil, reconciler.NewEvent(corev1.EventTypeWarning, "FailedConfigMapUpdate",
"Failed to update code ConfigMap %q: %s", desiredCmap.Name, err)
}
event.Normal(ctx, "UpdateConfigMap", "Updated code ConfigMap %q", cmap.Name)
return cmap, nil
}
// findCodeConfigMap returns the ConfigMap containing the code of the given
// Function instance if it exists.
func (r *Reconciler) findCodeConfigMap(rcl commonv1alpha1.Reconcilable,
owner *metav1.OwnerReference,
) (*corev1.ConfigMap, error) {
ls := common.CommonObjectLabels(rcl)
// the combination of standard labels {name,instance} is unique
// and immutable for single-tenant components
ls[appInstanceLabel] = rcl.GetName()
sel := labels.SelectorFromValidatedSet(ls)
cmaps, err := r.cmLister(rcl.GetNamespace()).List(sel)
if err != nil {
return nil, err
}
for _, cmap := range cmaps {
cmapOwner := metav1.GetControllerOfNoCopy(cmap)
if cmapOwner.UID == owner.UID {
return cmap, nil
}
}
gr := corev1.Resource("configmaps")
return nil, newNotFoundForSelector(gr, sel)
}
// newNotFoundForSelector returns an error which indicates that no object of
// the type matching the given GroupResource was found for the given label
// selector.
func newNotFoundForSelector(gr schema.GroupResource, sel labels.Selector) *apierrors.StatusError {
err := apierrors.NewNotFound(gr, "")
err.ErrStatus.Message = fmt.Sprint(gr, " not found for selector ", sel)
return err
}
// newCodeConfigMap returns a ConfigMap object containing the code of the given Function.
func newCodeConfigMap(f *v1alpha1.Function) *corev1.ConfigMap {
ns := f.Namespace
name := f.Name
return resource.NewConfigMap(ns, kmeta.ChildName(common.ComponentName(f)+"-code-", name),
resource.Controller(f),
resource.Labels(common.CommonObjectLabels(f)),
resource.Label(appInstanceLabel, name),
resource.Label(functionNameLabel, name),
resource.Data(codeCmapDataKey, f.Spec.Code),
)
}