Skip to content

Commit

Permalink
Fixing not set operation.CloudProvider when provisioner is managing…
Browse files Browse the repository at this point in the history
… cluster (#1515)

* initial fix with tests adjusted

* tests adjusted
  • Loading branch information
jaroslaw-pieszka authored Dec 2, 2024
1 parent a9ef93b commit ac4567f
Show file tree
Hide file tree
Showing 3 changed files with 157 additions and 29 deletions.
15 changes: 11 additions & 4 deletions cmd/broker/provisioning_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ func TestProvisioning_HappyPathAWS(t *testing.T) {

suite.AssertKymaResourceExists(opID)
suite.AssertKymaAnnotationExists(opID, "compass-runtime-id-for-migration")
suite.AssertKymaLabelsExist(opID, map[string]string{"kyma-project.io/region": "eu-central-1"})
suite.AssertKymaLabelsExist(opID, map[string]string{"kyma-project.io/region": "eu-central-1", "kyma-project.io/provider": "AWS"})
suite.AssertKymaLabelNotExists(opID, "kyma-project.io/platform-region")
}

Expand Down Expand Up @@ -335,7 +335,7 @@ func TestProvisioning_HappyPathSapConvergedCloud(t *testing.T) {

suite.AssertKymaResourceExists(opID)
suite.AssertKymaAnnotationExists(opID, "compass-runtime-id-for-migration")
suite.AssertKymaLabelsExist(opID, map[string]string{"kyma-project.io/region": "eu-de-1"})
suite.AssertKymaLabelsExist(opID, map[string]string{"kyma-project.io/region": "eu-de-1", "kyma-project.io/provider": "SapConvergedCloud"})
})

t.Run("should fail for invalid platform region - invalid platform region", func(t *testing.T) {
Expand Down Expand Up @@ -431,7 +431,8 @@ func TestProvisioning_Preview(t *testing.T) {

suite.AssertKymaResourceExists(opID)
suite.AssertKymaLabelsExist(opID, map[string]string{
"kyma-project.io/region": "eu-central-1",
"kyma-project.io/region": "eu-central-1",
"kyma-project.io/provider": "AWS",
})
suite.AssertKymaLabelNotExists(opID, "kyma-project.io/platform-region")
}
Expand Down Expand Up @@ -597,6 +598,7 @@ func TestProvisioning_AzureWithEURestrictedAccessHappyFlow(t *testing.T) {
suite.AssertAzureRegion("switzerlandnorth")
suite.AssertKymaLabelsExist(opID, map[string]string{
"kyma-project.io/region": "switzerlandnorth",
"kyma-project.io/provider": "Azure",
"kyma-project.io/platform-region": "cf-ch20"})
}

Expand Down Expand Up @@ -633,6 +635,7 @@ func TestProvisioning_AzureWithEURestrictedAccessDefaultRegion(t *testing.T) {
suite.AssertAzureRegion("switzerlandnorth")
suite.AssertKymaLabelsExist(opID, map[string]string{
"kyma-project.io/region": "switzerlandnorth",
"kyma-project.io/provider": "Azure",
"kyma-project.io/platform-region": "cf-ch20"})
}

Expand Down Expand Up @@ -669,6 +672,7 @@ func TestProvisioning_AWSWithEURestrictedAccessHappyFlow(t *testing.T) {
suite.AssertAWSRegionAndZone("eu-central-1")
suite.AssertKymaLabelsExist(opID, map[string]string{
"kyma-project.io/region": "eu-central-1",
"kyma-project.io/provider": "AWS",
"kyma-project.io/platform-region": "cf-eu11"})

}
Expand Down Expand Up @@ -706,6 +710,7 @@ func TestProvisioning_AWSWithEURestrictedAccessDefaultRegion(t *testing.T) {
suite.AssertAWSRegionAndZone("eu-central-1")
suite.AssertKymaLabelsExist(opID, map[string]string{
"kyma-project.io/region": "eu-central-1",
"kyma-project.io/provider": "AWS",
"kyma-project.io/platform-region": "cf-eu11"})

}
Expand Down Expand Up @@ -742,7 +747,8 @@ func TestProvisioning_TrialWithEmptyRegion(t *testing.T) {
// then
suite.AssertAWSRegionAndZone("eu-west-1")
suite.AssertKymaLabelsExist(opID, map[string]string{
"kyma-project.io/region": "eu-west-1"})
"kyma-project.io/provider": "AWS",
"kyma-project.io/region": "eu-west-1"})
suite.AssertKymaLabelNotExists(opID, "kyma-project.io/platform-region")

}
Expand Down Expand Up @@ -896,6 +902,7 @@ func TestProvisioning_TrialAtEU(t *testing.T) {
suite.AssertAWSRegionAndZone("eu-central-1")
suite.AssertKymaLabelsExist(opID, map[string]string{
"kyma-project.io/region": "eu-central-1",
"kyma-project.io/provider": "AWS",
"kyma-project.io/platform-region": "cf-eu11",
})

Expand Down
165 changes: 141 additions & 24 deletions internal/process/provisioning/apply_kyma_step_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,61 @@ func TestCreatingKymaResource(t *testing.T) {
err = cli.List(context.Background(), &aList)
require.NoError(t, err)
assert.Equal(t, 1, len(aList.Items))
assertLabelsExistsForExternalKymaResource(t, aList.Items[0])
expectedLabels := map[string]string{
"kyma-project.io/instance-id": "inst-id",
"kyma-project.io/runtime-id": "runtime-inst-id",
"kyma-project.io/global-account-id": "e8f7ec0a-0cd6-41f0-905d-5d1efa9fb6c4",
"kyma-project.io/subaccount-id": "SA-op-id",
"kyma-project.io/shoot-name": "Shoot-inst-id",
"kyma-project.io/platform-region": "westeurope",
"operator.kyma-project.io/kyma-name": "runtime-inst-id",
"kyma-project.io/broker-plan-id": "4deee563-e5ec-4731-b9b1-53b42d855f0c",
"kyma-project.io/broker-plan-name": "azure",
"operator.kyma-project.io/managed-by": "lifecycle-manager",
"kyma-project.io/provider": "Test"}

assertLabelsExistsForExternalKymaResource(t, expectedLabels, aList.Items[0])

_, _, err = svc.Run(operation, logrus.New())
require.NoError(t, err)
}

func TestCreatingKymaResourceWithCloudProviderInOperation(t *testing.T) {
// given
operation, cli := fixOperationForApplyKymaResource(t)
operation.CloudProvider = "Azure"
*operation.ProvisioningParameters.ErsContext.LicenseType = "CUSTOMER"
storage := storage.NewMemoryStorage()
err := storage.Operations().InsertOperation(operation)
require.NoError(t, err)
svc := NewApplyKymaStep(storage.Operations(), cli)

// when
_, backoff, err := svc.Run(operation, logrus.New())

// then
require.NoError(t, err)
require.Zero(t, backoff)
aList := unstructured.UnstructuredList{}
aList.SetGroupVersionKind(schema.GroupVersionKind{Group: "operator.kyma-project.io", Version: "v1beta2", Kind: "KymaList"})

err = cli.List(context.Background(), &aList)
require.NoError(t, err)
assert.Equal(t, 1, len(aList.Items))
expectedLabels := map[string]string{
"kyma-project.io/instance-id": "inst-id",
"kyma-project.io/runtime-id": "runtime-inst-id",
"kyma-project.io/global-account-id": "e8f7ec0a-0cd6-41f0-905d-5d1efa9fb6c4",
"kyma-project.io/subaccount-id": "SA-op-id",
"kyma-project.io/shoot-name": "Shoot-inst-id",
"kyma-project.io/platform-region": "westeurope",
"operator.kyma-project.io/kyma-name": "runtime-inst-id",
"kyma-project.io/broker-plan-id": "4deee563-e5ec-4731-b9b1-53b42d855f0c",
"kyma-project.io/broker-plan-name": "azure",
"operator.kyma-project.io/managed-by": "lifecycle-manager",
"kyma-project.io/provider": "Azure"}

assertLabelsExistsForExternalKymaResource(t, expectedLabels, aList.Items[0])

_, _, err = svc.Run(operation, logrus.New())
require.NoError(t, err)
Expand All @@ -87,7 +141,19 @@ func TestCreatingInternalKymaResource(t *testing.T) {
err = cli.List(context.Background(), &aList)
require.NoError(t, err)
assert.Equal(t, 1, len(aList.Items))
assertLabelsExistsForInternalKymaResource(t, aList.Items[0])
expectedLabels := map[string]string{
"kyma-project.io/instance-id": "inst-id",
"kyma-project.io/runtime-id": "runtime-inst-id",
"kyma-project.io/global-account-id": "e8f7ec0a-0cd6-41f0-905d-5d1efa9fb6c4",
"kyma-project.io/subaccount-id": "SA-op-id",
"kyma-project.io/shoot-name": "Shoot-inst-id",
"kyma-project.io/platform-region": "westeurope",
"operator.kyma-project.io/kyma-name": "runtime-inst-id",
"kyma-project.io/broker-plan-id": "4deee563-e5ec-4731-b9b1-53b42d855f0c",
"kyma-project.io/broker-plan-name": "azure",
"operator.kyma-project.io/managed-by": "lifecycle-manager",
"kyma-project.io/provider": "Test"}
assertLabelsExistsForInternalKymaResource(t, expectedLabels, aList.Items[0])

assertCompassRuntimeIdAnnotationExists(t, aList.Items[0])
_, _, err = svc.Run(operation, logrus.New())
Expand Down Expand Up @@ -115,7 +181,19 @@ func TestCreatingInternalKymaResource(t *testing.T) {
err = cli.List(context.Background(), &aList)
require.NoError(t, err)
assert.Equal(t, 1, len(aList.Items))
assertLabelsExistsForInternalKymaResource(t, aList.Items[0])
expectedLabels := map[string]string{
"kyma-project.io/instance-id": "inst-id",
"kyma-project.io/runtime-id": "runtime-inst-id",
"kyma-project.io/global-account-id": "e8f7ec0a-0cd6-41f0-905d-5d1efa9fb6c4",
"kyma-project.io/subaccount-id": "SA-op-id",
"kyma-project.io/shoot-name": "Shoot-inst-id",
"kyma-project.io/platform-region": "westeurope",
"operator.kyma-project.io/kyma-name": "runtime-inst-id",
"kyma-project.io/broker-plan-id": "4deee563-e5ec-4731-b9b1-53b42d855f0c",
"kyma-project.io/broker-plan-name": "azure",
"operator.kyma-project.io/managed-by": "lifecycle-manager",
"kyma-project.io/provider": "Test"}
assertLabelsExistsForInternalKymaResource(t, expectedLabels, aList.Items[0])

assertCompassRuntimeIdAnnotationNotExists(t, aList.Items[0])
_, _, err = svc.Run(operation, logrus.New())
Expand Down Expand Up @@ -145,7 +223,19 @@ func TestCreatingKymaResource_UseNamespaceFromTimeOfCreationNotTemplate(t *testi
err = cli.List(context.Background(), &aList)
require.NoError(t, err)
assert.Equal(t, 1, len(aList.Items))
assertLabelsExistsForExternalKymaResource(t, aList.Items[0])
expectedLabels := map[string]string{
"kyma-project.io/instance-id": "inst-id",
"kyma-project.io/runtime-id": "runtime-inst-id",
"kyma-project.io/global-account-id": "e8f7ec0a-0cd6-41f0-905d-5d1efa9fb6c4",
"kyma-project.io/subaccount-id": "SA-op-id",
"kyma-project.io/shoot-name": "Shoot-inst-id",
"kyma-project.io/platform-region": "westeurope",
"operator.kyma-project.io/kyma-name": "runtime-inst-id",
"kyma-project.io/broker-plan-id": "4deee563-e5ec-4731-b9b1-53b42d855f0c",
"kyma-project.io/broker-plan-name": "azure",
"operator.kyma-project.io/managed-by": "lifecycle-manager",
"kyma-project.io/provider": "Test"}
assertLabelsExistsForExternalKymaResource(t, expectedLabels, aList.Items[0])

_, _, err = svc.Run(operation, logrus.New())
require.NoError(t, err)
Expand Down Expand Up @@ -173,7 +263,19 @@ func TestCreatingInternalKymaResource_UseNamespaceFromTimeOfCreationNotTemplate(
err = cli.List(context.Background(), &aList)
require.NoError(t, err)
assert.Equal(t, 1, len(aList.Items))
assertLabelsExistsForInternalKymaResource(t, aList.Items[0])
expectedLabels := map[string]string{
"kyma-project.io/instance-id": "inst-id",
"kyma-project.io/runtime-id": "runtime-inst-id",
"kyma-project.io/global-account-id": "e8f7ec0a-0cd6-41f0-905d-5d1efa9fb6c4",
"kyma-project.io/subaccount-id": "SA-op-id",
"kyma-project.io/shoot-name": "Shoot-inst-id",
"kyma-project.io/platform-region": "westeurope",
"operator.kyma-project.io/kyma-name": "runtime-inst-id",
"kyma-project.io/broker-plan-id": "4deee563-e5ec-4731-b9b1-53b42d855f0c",
"kyma-project.io/broker-plan-name": "azure",
"operator.kyma-project.io/managed-by": "lifecycle-manager",
"kyma-project.io/provider": "Test"}
assertLabelsExistsForInternalKymaResource(t, expectedLabels, aList.Items[0])

_, _, err = svc.Run(operation, logrus.New())
require.NoError(t, err)
Expand Down Expand Up @@ -213,7 +315,20 @@ func TestUpdatinglKymaResourceIfExists(t *testing.T) {
err = cli.List(context.Background(), &aList)
require.NoError(t, err)
assert.Equal(t, 1, len(aList.Items))
assertLabelsExistsForExternalKymaResource(t, aList.Items[0])
expectedLabels := map[string]string{
"kyma-project.io/instance-id": "inst-id",
"kyma-project.io/runtime-id": "runtime-inst-id",
"kyma-project.io/global-account-id": "e8f7ec0a-0cd6-41f0-905d-5d1efa9fb6c4",
"kyma-project.io/subaccount-id": "SA-op-id",
"kyma-project.io/shoot-name": "Shoot-inst-id",
"kyma-project.io/platform-region": "westeurope",
"operator.kyma-project.io/kyma-name": "runtime-inst-id",
"kyma-project.io/broker-plan-id": "4deee563-e5ec-4731-b9b1-53b42d855f0c",
"kyma-project.io/broker-plan-name": "azure",
"operator.kyma-project.io/managed-by": "lifecycle-manager",
"kyma-project.io/provider": "Test"}

assertLabelsExistsForExternalKymaResource(t, expectedLabels, aList.Items[0])
}

func TestUpdatinInternalKymaResourceIfExists(t *testing.T) {
Expand Down Expand Up @@ -248,32 +363,34 @@ func TestUpdatinInternalKymaResourceIfExists(t *testing.T) {
err = cli.List(context.Background(), &aList)
require.NoError(t, err)
assert.Equal(t, 1, len(aList.Items))
assertLabelsExistsForInternalKymaResource(t, aList.Items[0])
expectedLabels := map[string]string{
"kyma-project.io/instance-id": "inst-id",
"kyma-project.io/runtime-id": "runtime-inst-id",
"kyma-project.io/global-account-id": "e8f7ec0a-0cd6-41f0-905d-5d1efa9fb6c4",
"kyma-project.io/subaccount-id": "SA-op-id",
"kyma-project.io/shoot-name": "Shoot-inst-id",
"kyma-project.io/platform-region": "westeurope",
"operator.kyma-project.io/kyma-name": "runtime-inst-id",
"kyma-project.io/broker-plan-id": "4deee563-e5ec-4731-b9b1-53b42d855f0c",
"kyma-project.io/broker-plan-name": "azure",
"operator.kyma-project.io/managed-by": "lifecycle-manager",
"kyma-project.io/provider": "Test"}

assertLabelsExistsForInternalKymaResource(t, expectedLabels, aList.Items[0])
}

func assertLabelsExists(t *testing.T, obj unstructured.Unstructured) {
func assertLabelsExists(t *testing.T, expectedLabels map[string]string, obj unstructured.Unstructured) {
keys := make([]string, 0, len(obj.GetLabels()))
for k := range obj.GetLabels() {
keys = append(keys, k)
}

assert.Subset(t, keys, []string{
"kyma-project.io/instance-id",
"kyma-project.io/runtime-id",
"kyma-project.io/global-account-id",
"kyma-project.io/subaccount-id",
"kyma-project.io/shoot-name",
"kyma-project.io/platform-region",
"operator.kyma-project.io/kyma-name",
"kyma-project.io/broker-plan-id",
"kyma-project.io/broker-plan-name",
"operator.kyma-project.io/managed-by",
"kyma-project.io/provider"})
assert.Subset(t, obj.GetLabels(), expectedLabels)
}

func assertLabelsExistsForInternalKymaResource(t *testing.T, obj unstructured.Unstructured) {
func assertLabelsExistsForInternalKymaResource(t *testing.T, expectedLabels map[string]string, obj unstructured.Unstructured) {
assert.Contains(t, obj.GetLabels(), "operator.kyma-project.io/internal")
assertLabelsExists(t, obj)
assertLabelsExists(t, expectedLabels, obj)
}

func assertCompassRuntimeIdAnnotationExists(t *testing.T, obj unstructured.Unstructured) {
Expand All @@ -286,9 +403,9 @@ func assertCompassRuntimeIdAnnotationNotExists(t *testing.T, obj unstructured.Un
assert.NotContains(t, obj.GetAnnotations(), "compass-runtime-id-for-migration")
}

func assertLabelsExistsForExternalKymaResource(t *testing.T, obj unstructured.Unstructured) {
func assertLabelsExistsForExternalKymaResource(t *testing.T, expectedLabels map[string]string, obj unstructured.Unstructured) {
assert.NotContains(t, obj.GetLabels(), "operator.kyma-project.io/internal")
assertLabelsExists(t, obj)
assertLabelsExists(t, expectedLabels, obj)
}

func fixOperationForApplyKymaResource(t *testing.T) (internal.Operation, client.Client) {
Expand Down
6 changes: 5 additions & 1 deletion internal/process/steps/lifecycle_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,11 @@ func ApplyLabelsAndAnnotationsForLM(object client.Object, operation internal.Ope

l[customresources.RegionLabel] = operation.Region
l[customresources.ManagedByLabel] = "lifecycle-manager"
l[customresources.CloudProviderLabel] = operation.CloudProvider
if operation.CloudProvider != "" {
l[customresources.CloudProviderLabel] = operation.CloudProvider
} else { // fallback to inputCreator that will be removed when provisioner is decommissioned
l[customresources.CloudProviderLabel] = string(operation.InputCreator.Provider())
}

if isKymaResourceInternal(operation) {
l[customresources.InternalLabel] = "true"
Expand Down

0 comments on commit ac4567f

Please sign in to comment.