From f5a1044aecf3e451125ce89156fccaed91b3aa61 Mon Sep 17 00:00:00 2001 From: Pieszka Date: Tue, 17 Sep 2024 13:15:21 +0200 Subject: [PATCH] logic but doubts remain --- cmd/broker/deprovisioning.go | 2 +- .../process/deprovisioning/remove_runtime.go | 21 ++++++++++++++----- .../deprovisioning/remove_runtime_test.go | 16 +++++++------- internal/process/update/upgrade_shoot_step.go | 2 +- 4 files changed, 27 insertions(+), 14 deletions(-) diff --git a/cmd/broker/deprovisioning.go b/cmd/broker/deprovisioning.go index 9af1928ca4..952173d488 100644 --- a/cmd/broker/deprovisioning.go +++ b/cmd/broker/deprovisioning.go @@ -56,7 +56,7 @@ func NewDeprovisioningProcessingQueue(ctx context.Context, workersAmount int, de step: deprovisioning.NewCheckGardenerClusterDeletedStep(db.Operations(), cli), }, { - step: deprovisioning.NewRemoveRuntimeStep(db.Operations(), db.Instances(), provisionerClient, cfg.Provisioner.DeprovisioningTimeout, cfg.Broker.KimConfig), + step: deprovisioning.NewRemoveRuntimeStep(db.Operations(), db.Instances(), provisionerClient, cli, cfg.Provisioner.DeprovisioningTimeout), }, { step: deprovisioning.NewCheckRuntimeRemovalStep(db.Operations(), db.Instances(), provisionerClient, cfg.Provisioner.DeprovisioningTimeout), diff --git a/internal/process/deprovisioning/remove_runtime.go b/internal/process/deprovisioning/remove_runtime.go index 7057a755d1..1e643b58ee 100644 --- a/internal/process/deprovisioning/remove_runtime.go +++ b/internal/process/deprovisioning/remove_runtime.go @@ -1,7 +1,11 @@ package deprovisioning import ( + "context" "fmt" + imv1 "github.com/kyma-project/infrastructure-manager/api/v1" + "k8s.io/apimachinery/pkg/api/errors" + "sigs.k8s.io/controller-runtime/pkg/client" "time" "github.com/kyma-project/kyma-environment-broker/internal/storage/dberr" @@ -20,17 +24,17 @@ type RemoveRuntimeStep struct { operationManager *process.OperationManager instanceStorage storage.Instances provisionerClient provisioner.Client + k8sClient client.Client provisionerTimeout time.Duration - kimConfig broker.KimConfig } -func NewRemoveRuntimeStep(os storage.Operations, is storage.Instances, cli provisioner.Client, provisionerTimeout time.Duration, kimConfig broker.KimConfig) *RemoveRuntimeStep { +func NewRemoveRuntimeStep(os storage.Operations, is storage.Instances, cli provisioner.Client, k8sClient client.Client, provisionerTimeout time.Duration) *RemoveRuntimeStep { return &RemoveRuntimeStep{ operationManager: process.NewOperationManager(os), instanceStorage: is, provisionerClient: cli, provisionerTimeout: provisionerTimeout, - kimConfig: kimConfig, + k8sClient: k8sClient, } } @@ -39,8 +43,15 @@ func (s *RemoveRuntimeStep) Name() string { } func (s *RemoveRuntimeStep) Run(operation internal.Operation, log logrus.FieldLogger) (internal.Operation, time.Duration, error) { - if s.kimConfig.IsDrivenByKimOnly(broker.PlanNamesMapping[operation.ProvisioningParameters.PlanID]) { - log.Infof("Only KIM is driving the process for plan %s, skipping", broker.PlanNamesMapping[operation.ProvisioningParameters.PlanID]) + + var runtime = imv1.Runtime{} + err := s.k8sClient.Get(context.Background(), client.ObjectKey{Name: operation.GetRuntimeResourceName(), Namespace: operation.GetRuntimeResourceNamespace()}, &runtime) + if err != nil && !errors.IsNotFound(err) { + log.Warnf("Unable to read runtime: %s", err) + return s.operationManager.RetryOperation(operation, err.Error(), err, 5*time.Second, 1*time.Minute, log) + } + if !(errors.IsNotFound(err) || runtime.IsControlledByProvisioner()) { + log.Infof("Skipping the step because the runtime %s/%s is not controlled by the provisioner", operation.GetRuntimeResourceName(), operation.GetRuntimeResourceName()) return operation, 0, nil } diff --git a/internal/process/deprovisioning/remove_runtime_test.go b/internal/process/deprovisioning/remove_runtime_test.go index 13bdff3cc5..c423f311c8 100644 --- a/internal/process/deprovisioning/remove_runtime_test.go +++ b/internal/process/deprovisioning/remove_runtime_test.go @@ -1,11 +1,13 @@ package deprovisioning import ( + imv1 "github.com/kyma-project/infrastructure-manager/api/v1" + "k8s.io/client-go/kubernetes/scheme" + + "sigs.k8s.io/controller-runtime/pkg/client/fake" "testing" "time" - "github.com/kyma-project/kyma-environment-broker/internal/broker" - "github.com/kyma-project/kyma-environment-broker/internal/fixture" provisionerAutomock "github.com/kyma-project/kyma-environment-broker/internal/provisioner/automock" "github.com/kyma-project/kyma-environment-broker/internal/storage" @@ -16,16 +18,16 @@ import ( func TestRemoveRuntimeStep_Run(t *testing.T) { t.Run("Should repeat process when deprovisioning call to provisioner succeeded", func(t *testing.T) { // given + err := imv1.AddToScheme(scheme.Scheme) + assert.NoError(t, err) log := logrus.New() memoryStorage := storage.NewMemoryStorage() + kcpClient := fake.NewClientBuilder().WithRuntimeObjects(fixRuntimeResource("runtime-id", "kcp-system")).Build() - kimConfig := broker.KimConfig{ - Enabled: false, - } operation := fixture.FixDeprovisioningOperation(fixOperationID, fixInstanceID) operation.GlobalAccountID = fixGlobalAccountID operation.RuntimeID = fixRuntimeID - err := memoryStorage.Operations().InsertDeprovisioningOperation(operation) + err = memoryStorage.Operations().InsertDeprovisioningOperation(operation) assert.NoError(t, err) err = memoryStorage.Instances().Insert(fixInstanceRuntimeStatus()) @@ -34,7 +36,7 @@ func TestRemoveRuntimeStep_Run(t *testing.T) { provisionerClient := &provisionerAutomock.Client{} provisionerClient.On("DeprovisionRuntime", fixGlobalAccountID, fixRuntimeID).Return(fixProvisionerOperationID, nil) - step := NewRemoveRuntimeStep(memoryStorage.Operations(), memoryStorage.Instances(), provisionerClient, time.Minute, kimConfig) + step := NewRemoveRuntimeStep(memoryStorage.Operations(), memoryStorage.Instances(), provisionerClient, kcpClient, time.Minute) // when entry := log.WithFields(logrus.Fields{"step": "TEST"}) diff --git a/internal/process/update/upgrade_shoot_step.go b/internal/process/update/upgrade_shoot_step.go index 505b11e4d2..124a169541 100644 --- a/internal/process/update/upgrade_shoot_step.go +++ b/internal/process/update/upgrade_shoot_step.go @@ -62,7 +62,7 @@ func (s *UpgradeShootStep) Run(operation internal.Operation, log logrus.FieldLog return s.operationManager.RetryOperation(operation, err.Error(), err, 5*time.Second, 1*time.Minute, log) } if !runtime.IsControlledByProvisioner() { - log.Infof("Skipping provisioning because the runtime is not controlled by the provisioner") + log.Infof("Skipping because the runtime is not controlled by the provisioner") return operation, 0, nil }