diff --git a/cmd/broker/provisioning.go b/cmd/broker/provisioning.go index 4b26784568..1d8d4c3598 100644 --- a/cmd/broker/provisioning.go +++ b/cmd/broker/provisioning.go @@ -87,7 +87,7 @@ func NewProvisioningProcessingQueue(ctx context.Context, provisionManager *proce { condition: provisioning.SkipForOwnClusterPlan, stage: createRuntimeStageName, - step: provisioning.NewCreateRuntimeWithoutKymaStep(db.Operations(), db.RuntimeStates(), db.Instances(), provisionerClient), + step: provisioning.NewCreateRuntimeWithoutKymaStep(db.Operations(), db.RuntimeStates(), db.Instances(), provisionerClient, cfg.Broker.KimConfig), }, { stage: createRuntimeStageName, diff --git a/internal/process/provisioning/create_runtime_without_kyma_step.go b/internal/process/provisioning/create_runtime_without_kyma_step.go index f5dc9c87cb..fb72179203 100644 --- a/internal/process/provisioning/create_runtime_without_kyma_step.go +++ b/internal/process/provisioning/create_runtime_without_kyma_step.go @@ -4,6 +4,9 @@ import ( "fmt" "time" + "github.com/kyma-project/kyma-environment-broker/internal/broker" + "github.com/kyma-project/kyma-environment-broker/internal/kim" + "github.com/kyma-project/control-plane/components/provisioner/pkg/gqlschema" "github.com/kyma-project/kyma-environment-broker/internal" kebError "github.com/kyma-project/kyma-environment-broker/internal/error" @@ -27,14 +30,16 @@ type CreateRuntimeWithoutKymaStep struct { instanceStorage storage.Instances runtimeStateStorage storage.RuntimeStates provisionerClient provisioner.Client + kimConfig kim.Config } -func NewCreateRuntimeWithoutKymaStep(os storage.Operations, runtimeStorage storage.RuntimeStates, is storage.Instances, cli provisioner.Client) *CreateRuntimeWithoutKymaStep { +func NewCreateRuntimeWithoutKymaStep(os storage.Operations, runtimeStorage storage.RuntimeStates, is storage.Instances, cli provisioner.Client, kimConfig kim.Config) *CreateRuntimeWithoutKymaStep { return &CreateRuntimeWithoutKymaStep{ operationManager: process.NewOperationManager(os), instanceStorage: is, provisionerClient: cli, runtimeStateStorage: runtimeStorage, + kimConfig: kimConfig, } } @@ -43,6 +48,11 @@ func (s *CreateRuntimeWithoutKymaStep) Name() string { } func (s *CreateRuntimeWithoutKymaStep) Run(operation internal.Operation, log logrus.FieldLogger) (internal.Operation, time.Duration, error) { + if s.kimConfig.IsDrivenByKimOnly(broker.PlanNamesMapping[operation.ProvisioningParameters.PlanID]) { + log.Infof("KIM is driving the process for plan %s, skipping", broker.PlanNamesMapping[operation.ProvisioningParameters.PlanID]) + return operation, 0, nil + } + if operation.RuntimeID != "" { log.Infof("RuntimeID already set %s, skipping", operation.RuntimeID) return operation, 0, nil diff --git a/internal/process/provisioning/create_runtime_without_kyma_test.go b/internal/process/provisioning/create_runtime_without_kyma_test.go index e81aa5aad0..9877263a5c 100644 --- a/internal/process/provisioning/create_runtime_without_kyma_test.go +++ b/internal/process/provisioning/create_runtime_without_kyma_test.go @@ -5,6 +5,8 @@ import ( "reflect" "testing" + "github.com/kyma-project/kyma-environment-broker/internal/kim" + "github.com/kyma-project/control-plane/components/provisioner/pkg/gqlschema" "github.com/kyma-project/kyma-environment-broker/internal/broker" "github.com/kyma-project/kyma-environment-broker/internal/provider" @@ -30,6 +32,10 @@ func TestCreateRuntimeWithoutKyma_Run(t *testing.T) { err = memoryStorage.Instances().Insert(fixInstance()) assert.NoError(t, err) + kimConfig := kim.Config{ + Enabled: false, + } + disabled := false provisionerInput := fixProvisionerInput(disabled, false) @@ -47,7 +53,7 @@ func TestCreateRuntimeWithoutKyma_Run(t *testing.T) { RuntimeID: ptr.String(runtimeID), }, nil) - step := NewCreateRuntimeWithoutKymaStep(memoryStorage.Operations(), memoryStorage.RuntimeStates(), memoryStorage.Instances(), provisionerClient) + step := NewCreateRuntimeWithoutKymaStep(memoryStorage.Operations(), memoryStorage.RuntimeStates(), memoryStorage.Instances(), provisionerClient, kimConfig) // when entry := log.WithFields(logrus.Fields{"step": "TEST"}) @@ -63,6 +69,54 @@ func TestCreateRuntimeWithoutKyma_Run(t *testing.T) { assert.Equal(t, instance.RuntimeID, runtimeID) } +func TestCreateRuntimeWithoutKyma_SkipForKIM(t *testing.T) { + // given + log := logrus.New() + memoryStorage := storage.NewMemoryStorage() + + operation := fixOperationCreateRuntime(t, broker.GCPPlanID, "europe-west3") + operation.ShootDomain = "kyma.org" + err := memoryStorage.Operations().InsertOperation(operation) + assert.NoError(t, err) + + err = memoryStorage.Instances().Insert(fixInstance()) + assert.NoError(t, err) + + kimConfig := kim.Config{ + Enabled: true, + Plans: []string{"gcp"}, + KimOnlyPlans: []string{"gcp"}, + } + + disabled := false + provisionerInput := fixProvisionerInput(disabled, false) + + provisionerClient := &provisionerAutomock.Client{} + provisionerClient.On("ProvisionRuntime", globalAccountID, subAccountID, mock.MatchedBy( + func(input gqlschema.ProvisionRuntimeInput) bool { + return reflect.DeepEqual(input.RuntimeInput.Labels, provisionerInput.RuntimeInput.Labels) && + input.KymaConfig == nil && reflect.DeepEqual(input.ClusterConfig, provisionerInput.ClusterConfig) + }, + )).Return(gqlschema.OperationStatus{ + ID: ptr.String(provisionerOperationID), + Operation: "", + State: "", + Message: nil, + RuntimeID: ptr.String(runtimeID), + }, nil) + + step := NewCreateRuntimeWithoutKymaStep(memoryStorage.Operations(), memoryStorage.RuntimeStates(), memoryStorage.Instances(), provisionerClient, kimConfig) + + // when + entry := log.WithFields(logrus.Fields{"step": "TEST"}) + operation, repeat, err := step.Run(operation, entry) + + // then + assert.NoError(t, err) + assert.Zero(t, repeat) + assert.Empty(t, operation.ProvisionerOperationID) +} + func TestCreateRuntimeWithoutKyma_RunWithEuAccess(t *testing.T) { // given log := logrus.New() @@ -76,6 +130,10 @@ func TestCreateRuntimeWithoutKyma_RunWithEuAccess(t *testing.T) { err = memoryStorage.Instances().Insert(fixInstance()) assert.NoError(t, err) + kimConfig := kim.Config{ + Enabled: false, + } + disabled := false provisionerInput := fixProvisionerInput(disabled, true) @@ -93,7 +151,7 @@ func TestCreateRuntimeWithoutKyma_RunWithEuAccess(t *testing.T) { RuntimeID: ptr.String(runtimeID), }, nil) - step := NewCreateRuntimeWithoutKymaStep(memoryStorage.Operations(), memoryStorage.RuntimeStates(), memoryStorage.Instances(), provisionerClient) + step := NewCreateRuntimeWithoutKymaStep(memoryStorage.Operations(), memoryStorage.RuntimeStates(), memoryStorage.Instances(), provisionerClient, kimConfig) // when entry := log.WithFields(logrus.Fields{"step": "TEST"}) @@ -184,13 +242,17 @@ func TestCreateRuntimeWithoutKymaStep_RunWithBadRequestError(t *testing.T) { err := memoryStorage.Operations().InsertOperation(operation) assert.NoError(t, err) + kimConfig := kim.Config{ + Enabled: false, + } + err = memoryStorage.Instances().Insert(fixInstance()) assert.NoError(t, err) provisionerClient := &provisionerAutomock.Client{} provisionerClient.On("ProvisionRuntime", globalAccountID, subAccountID, mock.Anything).Return(gqlschema.OperationStatus{}, fmt.Errorf("some permanent error")) - step := NewCreateRuntimeWithoutKymaStep(memoryStorage.Operations(), memoryStorage.RuntimeStates(), memoryStorage.Instances(), provisionerClient) + step := NewCreateRuntimeWithoutKymaStep(memoryStorage.Operations(), memoryStorage.RuntimeStates(), memoryStorage.Instances(), provisionerClient, kimConfig) // when entry := log.WithFields(logrus.Fields{"step": "TEST"}) diff --git a/internal/process/steps/runtime_resource.go b/internal/process/steps/runtime_resource.go index bef0b158c5..1c58458d78 100644 --- a/internal/process/steps/runtime_resource.go +++ b/internal/process/steps/runtime_resource.go @@ -40,22 +40,8 @@ func (_ *checkRuntimeResource) Name() string { } func (s *checkRuntimeResource) Run(operation internal.Operation, log logrus.FieldLogger) (internal.Operation, time.Duration, error) { - if !s.kimConfig.IsEnabledForPlan(broker.PlanNamesMapping[operation.ProvisioningParameters.PlanID]) { - if !s.kimConfig.Enabled { - log.Infof("KIM is not enabled, skipping") - return operation, 0, nil - } - log.Infof("KIM is not enabled for plan %s, skipping", broker.PlanNamesMapping[operation.ProvisioningParameters.PlanID]) - return operation, 0, nil - } - - if s.kimConfig.ViewOnly { - log.Infof("Provisioner is controlling provisioning process, skipping") - return operation, 0, nil - } - - if s.kimConfig.DryRun { - log.Infof("KIM integration in dry-run mode, skipping") + if !s.kimConfig.IsDrivenByKim(broker.PlanNamesMapping[operation.ProvisioningParameters.PlanID]) { + log.Infof("Only provisioner is controlling provisioning process, skipping") return operation, 0, nil } diff --git a/internal/process/steps/runtime_resource_test.go b/internal/process/steps/runtime_resource_test.go index c1fa32f5e9..9f9a3efdad 100644 --- a/internal/process/steps/runtime_resource_test.go +++ b/internal/process/steps/runtime_resource_test.go @@ -104,12 +104,11 @@ func TestCheckRuntimeResource_RunWhenNotReady_Retry(t *testing.T) { } func fixKimConfigForAzure() kim.Config { - kimConfig := kim.Config{ + return kim.Config{ Enabled: true, Plans: []string{"azure"}, ViewOnly: false, } - return kimConfig } func createRuntime(state imv1.State) imv1.Runtime {