generated from SAP/repository-template
-
Notifications
You must be signed in to change notification settings - Fork 1
/
target.go
80 lines (67 loc) · 2.58 KB
/
target.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
/*
SPDX-FileCopyrightText: 2023 SAP SE or an SAP affiliate company and component-operator-runtime contributors
SPDX-License-Identifier: Apache-2.0
*/
package component
import (
"context"
"github.com/pkg/errors"
"github.com/sap/component-operator-runtime/internal/cluster"
"github.com/sap/component-operator-runtime/pkg/manifests"
"github.com/sap/component-operator-runtime/pkg/reconciler"
)
type reconcileTarget[T Component] struct {
reconciler *reconciler.Reconciler
reconcilerName string
reconcilerId string
client cluster.Client
resourceGenerator manifests.Generator
}
func newReconcileTarget[T Component](reconcilerName string, reconcilerId string, clnt cluster.Client, resourceGenerator manifests.Generator, options reconciler.ReconcilerOptions) *reconcileTarget[T] {
return &reconcileTarget[T]{
reconcilerName: reconcilerName,
reconcilerId: reconcilerId,
reconciler: reconciler.NewReconciler(reconcilerName, clnt, options),
client: clnt,
resourceGenerator: resourceGenerator,
}
}
func (t *reconcileTarget[T]) Apply(ctx context.Context, component T) (bool, error) {
//log := log.FromContext(ctx)
namespace := ""
name := ""
if placementConfiguration, ok := assertPlacementConfiguration(component); ok {
namespace = placementConfiguration.GetDeploymentNamespace()
name = placementConfiguration.GetDeploymentName()
}
if namespace == "" {
namespace = component.GetNamespace()
}
if name == "" {
name = component.GetName()
}
ownerId := t.reconcilerId + "/" + component.GetNamespace() + "/" + component.GetName()
status := component.GetStatus()
componentDigest := calculateComponentDigest(component)
// TODO: enhance ctx with local client
generateCtx := newContext(ctx).
WithReconcilerName(t.reconcilerName).
WithClient(t.client).
WithComponent(component).
WithComponentDigest(componentDigest)
objects, err := t.resourceGenerator.Generate(generateCtx, namespace, name, component.GetSpec())
if err != nil {
return false, errors.Wrap(err, "error rendering manifests")
}
return t.reconciler.Apply(ctx, &status.Inventory, objects, namespace, ownerId, component.GetGeneration())
}
func (t *reconcileTarget[T]) Delete(ctx context.Context, component T) (bool, error) {
// log := log.FromContext(ctx)
status := component.GetStatus()
return t.reconciler.Delete(ctx, &status.Inventory)
}
func (t *reconcileTarget[T]) IsDeletionAllowed(ctx context.Context, component T) (bool, string, error) {
// log := log.FromContext(ctx)
status := component.GetStatus()
return t.reconciler.IsDeletionAllowed(ctx, &status.Inventory)
}