Skip to content

Commit

Permalink
Add subscriptionID to cluster-values configmap for CAPZ (#476)
Browse files Browse the repository at this point in the history
  • Loading branch information
erkanerol authored Mar 14, 2024
1 parent 2190fd1 commit c1c941a
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 9 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
38 changes: 32 additions & 6 deletions service/controller/resource/clusterconfigmap/desired.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"
Expand Down Expand Up @@ -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 {
Expand All @@ -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:
Expand Down Expand Up @@ -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,
Expand Down
8 changes: 5 additions & 3 deletions service/controller/resource/clusterconfigmap/desired_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"
)

Expand Down Expand Up @@ -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")
}
Expand Down Expand Up @@ -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")
Expand Down
2 changes: 2 additions & 0 deletions service/controller/resource/clusterconfigmap/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,6 @@ type ClusterValuesConfig struct {
Provider string `json:"provider"`
GcpProject string `json:"gcpProject"`
ChartOperator ChartOperatorConfig `json:"chartOperator"`

AzureSubscriptionID string `json:"subscriptionID"`
}

0 comments on commit c1c941a

Please sign in to comment.