diff --git a/CHANGELOG.md b/CHANGELOG.md index a7fd15ca..da57a089 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +### Added + +- Add `subscriptionID` to cluster-values configmap for CAPZ so that apps in default-apps bundle can consume it automatically. + ## [2.20.2] - 2024-03-06 ### Fixed diff --git a/service/controller/resource/clusterconfigmap/desired.go b/service/controller/resource/clusterconfigmap/desired.go index 0a8a5839..abae078a 100644 --- a/service/controller/resource/clusterconfigmap/desired.go +++ b/service/controller/resource/clusterconfigmap/desired.go @@ -5,10 +5,6 @@ import ( "fmt" "reflect" - "github.com/giantswarm/k8smetadata/pkg/annotation" - "github.com/giantswarm/k8smetadata/pkg/label" - "github.com/giantswarm/microerror" - "github.com/giantswarm/operatorkit/v7/pkg/controller/context/resourcecanceledcontext" corev1 "k8s.io/api/core/v1" apierrors "k8s.io/apimachinery/pkg/api/errors" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" @@ -17,6 +13,11 @@ import ( "sigs.k8s.io/controller-runtime/pkg/client" "sigs.k8s.io/yaml" + "github.com/giantswarm/k8smetadata/pkg/annotation" + "github.com/giantswarm/k8smetadata/pkg/label" + "github.com/giantswarm/microerror" + "github.com/giantswarm/operatorkit/v7/pkg/controller/context/resourcecanceledcontext" + "github.com/giantswarm/cluster-apps-operator/v2/pkg/project" "github.com/giantswarm/cluster-apps-operator/v2/service/controller/key" infra "github.com/giantswarm/cluster-apps-operator/v2/service/internal/infrastructure" @@ -110,6 +111,9 @@ func (r *Resource) GetDesiredState(ctx context.Context, obj interface{}) ([]*cor // gcpProject is only used on gcp. gcpProject = "" gcpProjectFound bool + + azureSubscriptionID = "" + azureSubscriptionIDFound bool ) privateCluster, err := privatecluster.IsPrivateCluster(ctx, r.logger, r.k8sClient.CtrlClient(), cr) if err != nil { @@ -120,8 +124,29 @@ func (r *Resource) GetDesiredState(ctx context.Context, obj interface{}) ([]*cor infrastructureRef := cr.Spec.InfrastructureRef if infrastructureRef != nil { switch infrastructureRef.Kind { - case infra.AzureClusterKind, infra.AzureManagedClusterKind: + case infra.AzureManagedClusterKind: provider = infra.AzureClusterKindProvider + case infra.AzureClusterKind: + provider = infra.AzureClusterKindProvider + + capzCluster := &unstructured.Unstructured{} + capzCluster.SetGroupVersionKind(schema.GroupVersionKind{ + Group: infrastructureRef.GroupVersionKind().Group, + Kind: infrastructureRef.Kind, + Version: infrastructureRef.GroupVersionKind().Version, + }) + err = r.k8sClient.CtrlClient().Get(ctx, client.ObjectKey{ + Namespace: cr.Namespace, + Name: infrastructureRef.Name, + }, capzCluster) + if err != nil { + return nil, microerror.Mask(err) + } + + azureSubscriptionID, azureSubscriptionIDFound, err = unstructured.NestedString(capzCluster.Object, []string{"spec", "subscriptionID"}...) + if err != nil || !azureSubscriptionIDFound { + return nil, fieldNotFoundOnInfrastructureTypeError + } case infra.AWSClusterKind, infra.AWSManagedClusterKind: provider = infra.AWSClusterKindProvider case infra.VCDClusterKind: @@ -201,7 +226,8 @@ func (r *Resource) GetDesiredState(ctx context.Context, obj interface{}) ([]*cor } clusterValues := ClusterValuesConfig{ - BaseDomain: key.BaseDomain(&cr, r.baseDomain), + AzureSubscriptionID: azureSubscriptionID, + BaseDomain: key.BaseDomain(&cr, r.baseDomain), BootstrapMode: ChartOperatorBootstrapMode{ Enabled: true, ApiServerPodPort: 6443, diff --git a/service/controller/resource/clusterconfigmap/desired_test.go b/service/controller/resource/clusterconfigmap/desired_test.go index d3223e16..c94c4def 100644 --- a/service/controller/resource/clusterconfigmap/desired_test.go +++ b/service/controller/resource/clusterconfigmap/desired_test.go @@ -6,8 +6,6 @@ import ( "strings" "testing" - "github.com/giantswarm/k8sclient/v7/pkg/k8sclienttest" - "github.com/giantswarm/micrologger/microloggertest" corev1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" @@ -18,6 +16,9 @@ import ( clientfake "sigs.k8s.io/controller-runtime/pkg/client/fake" "sigs.k8s.io/yaml" + "github.com/giantswarm/k8sclient/v7/pkg/k8sclienttest" + "github.com/giantswarm/micrologger/microloggertest" + "github.com/giantswarm/cluster-apps-operator/v2/service/internal/podcidr" ) @@ -124,7 +125,7 @@ func Test_ClusterValuesGCP(t *testing.T) { assertEquals(t, "test-cluster.fadi.gigantic.io", cmData.BaseDomain, "Wrong baseDomain set in cluster-values configmap") assertEquals(t, "12345", cmData.GcpProject, "Wrong gcpProject set in cluster-values configmap") assertEquals(t, "gcp", cmData.Provider, "Wrong provider set in cluster-values configmap") - + assertEquals(t, "", cmData.AzureSubscriptionID, "AzureSubscriptionID should be empty for non-CAPZ clusters") if !cmData.BootstrapMode.Enabled { t.Fatal("bootstrap mode should be enabled") } @@ -613,6 +614,7 @@ func Test_ClusterValuesCAPZ(t *testing.T) { } assertEquals(t, "test-cluster.azuretest.gigantic.io", cmData.BaseDomain, "Wrong baseDomain set in cluster-values configmap") assertEquals(t, "capz", cmData.Provider, "Wrong provider set in cluster-values configmap") + assertEquals(t, "143d9c06-6015-4a4a-a4f9-74a664207db7", cmData.AzureSubscriptionID, "Wrong AzureSubscriptionID set in cluster-values configmap") if !cmData.BootstrapMode.Enabled { t.Fatal("bootstrap mode should be enabled") diff --git a/service/controller/resource/clusterconfigmap/types.go b/service/controller/resource/clusterconfigmap/types.go index f34eab58..b8300850 100644 --- a/service/controller/resource/clusterconfigmap/types.go +++ b/service/controller/resource/clusterconfigmap/types.go @@ -43,4 +43,6 @@ type ClusterValuesConfig struct { Provider string `json:"provider"` GcpProject string `json:"gcpProject"` ChartOperator ChartOperatorConfig `json:"chartOperator"` + + AzureSubscriptionID string `json:"subscriptionID"` }