|
| 1 | +package landscaper |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "time" |
| 7 | + |
| 8 | + "github.com/openmcp-project/controller-utils/pkg/collections" |
| 9 | + "github.com/openmcp-project/controller-utils/pkg/logging" |
| 10 | + |
| 11 | + commonapi "github.com/openmcp-project/openmcp-operator/api/common" |
| 12 | + openmcpls "github.com/openmcp-project/service-provider-landscaper/api/v1alpha1" |
| 13 | + |
| 14 | + apierrors "k8s.io/apimachinery/pkg/api/errors" |
| 15 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 16 | + ctrl "sigs.k8s.io/controller-runtime" |
| 17 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 18 | + |
| 19 | + cconst "github.com/openmcp-project/mcp-operator/api/constants" |
| 20 | + openmcpv1alpha1 "github.com/openmcp-project/mcp-operator/api/core/v1alpha1" |
| 21 | + openmcperrors "github.com/openmcp-project/mcp-operator/api/errors" |
| 22 | + "github.com/openmcp-project/mcp-operator/internal/utils/components" |
| 23 | +) |
| 24 | + |
| 25 | +func (r *LandscaperConnector) v2HandleCreateOrUpdate(ctx context.Context, ls *openmcpv1alpha1.Landscaper) (ctrl.Result, bool, []openmcpv1alpha1.ComponentCondition, openmcperrors.ReasonableError) { |
| 26 | + lsv2 := &openmcpls.Landscaper{} |
| 27 | + lsv2.SetName(ls.Name) |
| 28 | + lsv2.SetNamespace(ls.Namespace) |
| 29 | + if _, err := ctrl.CreateOrUpdate(ctx, r.CrateClient, lsv2, func() error { |
| 30 | + if lsv2.Labels == nil { |
| 31 | + lsv2.Labels = map[string]string{} |
| 32 | + } |
| 33 | + lsv2.Labels[openmcpv1alpha1.V1MCPReferenceLabelName] = ls.Name |
| 34 | + lsv2.Labels[openmcpv1alpha1.V1MCPReferenceLabelNamespace] = ls.Namespace |
| 35 | + |
| 36 | + return nil |
| 37 | + }); err != nil { |
| 38 | + return ctrl.Result{}, false, nil, openmcperrors.WithReason(fmt.Errorf("error creating or updating Landscaper v2 resource: %w", err), cconst.ReasonCrateClusterInteractionProblem) |
| 39 | + } |
| 40 | + |
| 41 | + ready := lsv2.Status.Phase == commonapi.StatusPhaseReady && lsv2.Status.ObservedGeneration == lsv2.Generation |
| 42 | + cons := collections.ProjectSlice(lsv2.Status.Conditions, func(v2con metav1.Condition) openmcpv1alpha1.ComponentCondition { |
| 43 | + return components.NewCondition("LSv2_"+v2con.Type, components.ComponentConditionStatusFromMetav1ConditionStatus(v2con.Status), v2con.Reason, v2con.Message) |
| 44 | + }) |
| 45 | + |
| 46 | + return ctrl.Result{}, ready, cons, nil |
| 47 | +} |
| 48 | + |
| 49 | +func (r *LandscaperConnector) v2HandleDelete(ctx context.Context, ls *openmcpv1alpha1.Landscaper) (ctrl.Result, bool, []openmcpv1alpha1.ComponentCondition, openmcperrors.ReasonableError) { |
| 50 | + log := logging.FromContextOrPanic(ctx) |
| 51 | + |
| 52 | + lsv2 := &openmcpls.Landscaper{} |
| 53 | + lsv2.SetName(ls.Name) |
| 54 | + lsv2.SetNamespace(ls.Namespace) |
| 55 | + if err := r.CrateClient.Get(ctx, client.ObjectKeyFromObject(lsv2), lsv2); err != nil { |
| 56 | + if !apierrors.IsNotFound(err) { |
| 57 | + return ctrl.Result{}, false, nil, openmcperrors.WithReason(fmt.Errorf("error getting Landscaper v2 resource: %w", err), cconst.ReasonCrateClusterInteractionProblem) |
| 58 | + } |
| 59 | + lsv2 = nil |
| 60 | + } |
| 61 | + |
| 62 | + if lsv2 != nil { |
| 63 | + if lsv2.DeletionTimestamp.IsZero() { |
| 64 | + log.Info("Deleting Landscaper v2 resource", "resourceName", lsv2.Name, "resourceNamespace", lsv2.Namespace) |
| 65 | + if err := r.CrateClient.Delete(ctx, lsv2); err != nil { |
| 66 | + return ctrl.Result{}, false, nil, openmcperrors.WithReason(fmt.Errorf("error deleting Landscaper v2 resource: %w", err), cconst.ReasonCrateClusterInteractionProblem) |
| 67 | + } |
| 68 | + } else { |
| 69 | + log.Info("Waiting for Landscaper v2 resource to be deleted", "resourceName", lsv2.Name, "resourceNamespace", lsv2.Namespace) |
| 70 | + } |
| 71 | + |
| 72 | + cons := collections.ProjectSlice(lsv2.Status.Conditions, func(v2con metav1.Condition) openmcpv1alpha1.ComponentCondition { |
| 73 | + return components.NewCondition("LSv2_"+v2con.Type, components.ComponentConditionStatusFromMetav1ConditionStatus(v2con.Status), v2con.Reason, v2con.Message) |
| 74 | + }) |
| 75 | + return ctrl.Result{RequeueAfter: 30 * time.Second}, false, cons, nil |
| 76 | + } |
| 77 | + |
| 78 | + log.Info("Landscaper v2 resource deleted", "resourceName", ls.Name, "resourceNamespace", ls.Namespace) |
| 79 | + return ctrl.Result{}, true, nil, nil |
| 80 | +} |
0 commit comments