/
kyma_synchronization_context.go
264 lines (225 loc) · 8.39 KB
/
kyma_synchronization_context.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
package remote
import (
"bytes"
"context"
"errors"
"fmt"
apicorev1 "k8s.io/api/core/v1"
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
"k8s.io/apimachinery/pkg/api/meta"
apimetav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/json"
"k8s.io/client-go/rest"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
"github.com/kyma-project/lifecycle-manager/api/shared"
"github.com/kyma-project/lifecycle-manager/api/v1beta2"
"github.com/kyma-project/lifecycle-manager/pkg/adapter"
"github.com/kyma-project/lifecycle-manager/pkg/util"
)
type ClientFunc func() *rest.Config
var (
//nolint:gochecknoglobals // used for testing
LocalClient ClientFunc
ErrNotFoundAndKCPKymaUnderDeleting = errors.New("not found and kcp kyma under deleting")
)
type KymaSynchronizationContext struct {
ControlPlaneClient Client
RuntimeClient Client
}
func InitializeKymaSynchronizationContext(ctx context.Context, kcp Client, cache *ClientCache,
kyma *v1beta2.Kyma, syncNamespace string,
) (*KymaSynchronizationContext, error) {
strategyValue, found := kyma.Annotations[shared.SyncStrategyAnnotation]
syncStrategy := v1beta2.SyncStrategyLocalSecret
if found && strategyValue == v1beta2.SyncStrategyLocalClient {
syncStrategy = v1beta2.SyncStrategyLocalClient
}
skr, err := NewClientLookup(kcp, cache, v1beta2.SyncStrategy(syncStrategy)).
Lookup(ctx, client.ObjectKeyFromObject(kyma))
if err != nil {
return nil, err
}
sync := &KymaSynchronizationContext{
ControlPlaneClient: kcp,
RuntimeClient: skr,
}
if err := sync.ensureRemoteNamespaceExists(ctx, syncNamespace); err != nil {
return nil, err
}
return sync, nil
}
func (c *KymaSynchronizationContext) GetRemotelySyncedKyma(
ctx context.Context, remoteSyncNamespace string,
) (*v1beta2.Kyma, error) {
remoteKyma := &v1beta2.Kyma{}
remoteKyma.Name = shared.DefaultRemoteKymaName
remoteKyma.Namespace = remoteSyncNamespace
if err := c.RuntimeClient.Get(ctx, client.ObjectKeyFromObject(remoteKyma), remoteKyma); err != nil {
return remoteKyma, fmt.Errorf("failed to get remote kyma: %w", err)
}
return remoteKyma, nil
}
func RemoveFinalizersFromRemoteKyma(
ctx context.Context, remoteSyncNamespace string,
) error {
syncContext, err := SyncContextFromContext(ctx)
if err != nil {
return fmt.Errorf("failed to get syncContext: %w", err)
}
remoteKyma, err := syncContext.GetRemotelySyncedKyma(ctx, remoteSyncNamespace)
if err != nil {
return err
}
for _, finalizer := range remoteKyma.Finalizers {
controllerutil.RemoveFinalizer(remoteKyma, finalizer)
}
err = syncContext.RuntimeClient.Update(ctx, remoteKyma)
if err != nil {
return fmt.Errorf("failed to update remote kyma when removing finalizers: %w", err)
}
return nil
}
func DeleteRemotelySyncedKyma(
ctx context.Context, remoteSyncNamespace string,
) error {
syncContext, err := SyncContextFromContext(ctx)
if err != nil {
return fmt.Errorf("failed to get syncContext: %w", err)
}
remoteKyma, err := syncContext.GetRemotelySyncedKyma(ctx, remoteSyncNamespace)
if err != nil {
return err
}
err = syncContext.RuntimeClient.Delete(ctx, remoteKyma)
if err != nil {
return fmt.Errorf("failed to delete remote kyma: %w", err)
}
return nil
}
// ensureRemoteNamespaceExists tries to ensure existence of a namespace for synchronization based on
// name of controlPlaneKyma.namespace in this order.
func (c *KymaSynchronizationContext) ensureRemoteNamespaceExists(ctx context.Context, syncNamespace string) error {
namespace := &apicorev1.Namespace{
ObjectMeta: apimetav1.ObjectMeta{
Name: syncNamespace,
Labels: map[string]string{
shared.ManagedBy: shared.OperatorName,
"istio-injection": "enabled",
"namespaces.warden.kyma-project.io/validate": "enabled",
},
},
// setting explicit type meta is required for SSA on Namespaces
TypeMeta: apimetav1.TypeMeta{APIVersion: "v1", Kind: "Namespace"},
}
var buf bytes.Buffer
if err := json.NewEncoder(&buf).Encode(namespace); err != nil {
return fmt.Errorf("failed to encode namespace: %w", err)
}
patch := client.RawPatch(types.ApplyPatchType, buf.Bytes())
force := true
fieldManager := "kyma-sync-context"
if err := c.RuntimeClient.Patch(
ctx, namespace, patch, &client.PatchOptions{Force: &force, FieldManager: fieldManager},
); err != nil {
return fmt.Errorf("failed to ensure remote namespace exists: %w", err)
}
return nil
}
func (c *KymaSynchronizationContext) CreateOrUpdateCRD(ctx context.Context, plural string) error {
crd := &apiextensionsv1.CustomResourceDefinition{}
crdFromRuntime := &apiextensionsv1.CustomResourceDefinition{}
var err error
err = c.ControlPlaneClient.Get(
ctx, client.ObjectKey{
// this object name is derived from the plural and is the default kustomize value for crd namings, if the CRD
// name changes, this also has to be adjusted here. We can think of making this configurable later
Name: fmt.Sprintf("%s.%s", plural, v1beta2.GroupVersion.Group),
}, crd,
)
if err != nil {
return fmt.Errorf("failed to get kyma CRDs on kcp: %w", err)
}
err = c.RuntimeClient.Get(
ctx, client.ObjectKey{
Name: fmt.Sprintf("%s.%s", plural, v1beta2.GroupVersion.Group),
}, crdFromRuntime,
)
if util.IsNotFound(err) || !ContainsLatestVersion(crdFromRuntime, v1beta2.GroupVersion.Version) {
return PatchCRD(ctx, c.RuntimeClient, crd)
}
if err != nil {
return fmt.Errorf("failed to get kyma CRDs on remote: %w", err)
}
return nil
}
func (c *KymaSynchronizationContext) CreateOrFetchRemoteKyma(
ctx context.Context, kyma *v1beta2.Kyma, remoteSyncNamespace string,
) (*v1beta2.Kyma, error) {
recorder := adapter.RecorderFromContext(ctx)
remoteKyma, err := c.GetRemotelySyncedKyma(ctx, remoteSyncNamespace)
if meta.IsNoMatchError(err) || CRDNotFoundErr(err) {
if err := c.CreateOrUpdateCRD(ctx, shared.KymaKind.Plural()); err != nil {
return nil, err
}
recorder.Event(kyma, "Normal", "CRDInstallation", "CRDs were installed to SKR")
}
if util.IsNotFound(err) {
if !kyma.DeletionTimestamp.IsZero() {
return nil, ErrNotFoundAndKCPKymaUnderDeleting
}
kyma.Spec.DeepCopyInto(&remoteKyma.Spec)
err = c.RuntimeClient.Create(ctx, remoteKyma)
if err != nil {
return nil, fmt.Errorf("failed to create remote kyma: %w", err)
}
recorder.Event(kyma, "Normal", "RemoteInstallation", "Kyma was installed to SKR")
} else if err != nil {
return nil, fmt.Errorf("failed to fetch remote kyma: %w", err)
}
return remoteKyma, nil
}
func (c *KymaSynchronizationContext) SynchronizeRemoteKyma(
ctx context.Context,
controlPlaneKyma, remoteKyma *v1beta2.Kyma,
) error {
if !remoteKyma.GetDeletionTimestamp().IsZero() {
return nil
}
recorder := adapter.RecorderFromContext(ctx)
c.SyncWatcherLabelsAnnotations(controlPlaneKyma, remoteKyma)
if err := c.RuntimeClient.Update(ctx, remoteKyma); err != nil {
recorder.Event(controlPlaneKyma, "Warning", err.Error(), "could not synchronise runtime kyma "+
"spec, watcher labels and annotations")
return fmt.Errorf("failed to synchronise runtime kyma: %w", err)
}
remoteKyma.Status = controlPlaneKyma.Status
if err := c.RuntimeClient.Status().Update(ctx, remoteKyma); err != nil {
recorder.Event(controlPlaneKyma, "Warning", err.Error(), "could not update runtime kyma status")
return fmt.Errorf("failed to update runtime kyma status: %w", err)
}
return nil
}
// ReplaceModules replaces modules specification from control plane Kyma with Remote Kyma specifications.
func ReplaceModules(
controlPlaneKyma *v1beta2.Kyma,
remoteKyma *v1beta2.Kyma,
) {
controlPlaneKyma.Spec.Modules = []v1beta2.Module{}
controlPlaneKyma.Spec.Modules = append(controlPlaneKyma.Spec.Modules, remoteKyma.Spec.Modules...)
controlPlaneKyma.Spec.Channel = remoteKyma.Spec.Channel
}
// SyncWatcherLabelsAnnotations inserts labels into the given KymaCR, which are needed to ensure
// a working e2e-flow for the runtime-watcher.
func (c *KymaSynchronizationContext) SyncWatcherLabelsAnnotations(controlPlaneKyma, remoteKyma *v1beta2.Kyma) {
if remoteKyma.Labels == nil {
remoteKyma.Labels = make(map[string]string)
}
remoteKyma.Labels[shared.WatchedByLabel] = shared.OperatorName
if remoteKyma.Annotations == nil {
remoteKyma.Annotations = make(map[string]string)
}
remoteKyma.Annotations[shared.OwnedByAnnotation] = fmt.Sprintf(shared.OwnedByFormat,
controlPlaneKyma.GetNamespace(), controlPlaneKyma.GetName())
}