From 87c8e024ea0e9bb8165cdfd21b1329123c51d897 Mon Sep 17 00:00:00 2001 From: Lukasz Jakimczuk <39192420+ljakimczuk@users.noreply.github.com> Date: Thu, 1 Feb 2024 16:21:31 +0100 Subject: [PATCH] Removing provider flag (#451) * Removing hardcoded provider support * Changelog entry * Removing openstack, moving kinds to internal package, fixing tests * Adding AWSManagedCluster support * Adding two more managed kinds --- CHANGELOG.md | 8 + flag/service/provider/provider.go | 7 - flag/service/service.go | 2 - .../templates/configmap.yaml | 2 - helm/cluster-apps-operator/values.schema.json | 8 - helm/cluster-apps-operator/values.yaml | 3 - main.go | 2 - service/controller/cluster.go | 2 - .../resource/clusterconfigmap/desired.go | 41 ++--- .../resource/clusterconfigmap/desired_test.go | 148 ++++++++++++++++-- .../resource/clusterconfigmap/error.go | 9 ++ .../resource/clusterconfigmap/resource.go | 6 - .../resource/clusterconfigmap/types.go | 8 + .../resource/clustersecret/desired.go | 16 +- .../resource/clustersecret/openstack.go | 99 ------------ .../internal/infrastructure/infrastructure.go | 24 +++ service/service.go | 1 - 17 files changed, 204 insertions(+), 182 deletions(-) delete mode 100644 flag/service/provider/provider.go delete mode 100644 service/controller/resource/clustersecret/openstack.go create mode 100644 service/internal/infrastructure/infrastructure.go diff --git a/CHANGELOG.md b/CHANGELOG.md index 242d2536..a7704990 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +### Removed + +- Remove OpenStack support. + +### Changed + +- The provider information source of truth is changed from the configuration option to Cluster CRs. + ## [2.18.1] - 2024-01-29 ### Fixed diff --git a/flag/service/provider/provider.go b/flag/service/provider/provider.go deleted file mode 100644 index abddc0d1..00000000 --- a/flag/service/provider/provider.go +++ /dev/null @@ -1,7 +0,0 @@ -package provider - -// Provider is a data structure to hold provider specific configuration -// flags. -type Provider struct { - Kind string -} diff --git a/flag/service/service.go b/flag/service/service.go index 74c5fb4c..5a490af5 100644 --- a/flag/service/service.go +++ b/flag/service/service.go @@ -5,7 +5,6 @@ import ( "github.com/giantswarm/cluster-apps-operator/v2/flag/service/app" "github.com/giantswarm/cluster-apps-operator/v2/flag/service/image" - "github.com/giantswarm/cluster-apps-operator/v2/flag/service/provider" "github.com/giantswarm/cluster-apps-operator/v2/flag/service/proxy" "github.com/giantswarm/cluster-apps-operator/v2/flag/service/workload" ) @@ -15,7 +14,6 @@ type Service struct { App app.App Image image.Image Kubernetes kubernetes.Kubernetes - Provider provider.Provider Workload workload.Workload Proxy proxy.Proxy } diff --git a/helm/cluster-apps-operator/templates/configmap.yaml b/helm/cluster-apps-operator/templates/configmap.yaml index 84493c93..c4aeee92 100644 --- a/helm/cluster-apps-operator/templates/configmap.yaml +++ b/helm/cluster-apps-operator/templates/configmap.yaml @@ -28,8 +28,6 @@ data: caFile: '' crtFile: '' keyFile: '' - provider: - kind: {{ .Values.provider.kind }} proxy: noProxy: {{ .Values.proxy.noProxy }} http: {{ .Values.proxy.http }} diff --git a/helm/cluster-apps-operator/values.schema.json b/helm/cluster-apps-operator/values.schema.json index 0f7bf57a..cb8d3649 100644 --- a/helm/cluster-apps-operator/values.schema.json +++ b/helm/cluster-apps-operator/values.schema.json @@ -153,14 +153,6 @@ } } }, - "provider": { - "type": "object", - "properties": { - "kind": { - "type": "string" - } - } - }, "proxy": { "type": "object", "properties": { diff --git a/helm/cluster-apps-operator/values.yaml b/helm/cluster-apps-operator/values.yaml index 79b4a70c..146c04ba 100644 --- a/helm/cluster-apps-operator/values.yaml +++ b/helm/cluster-apps-operator/values.yaml @@ -10,9 +10,6 @@ baseDomain: "" managementClusterID: "" -provider: - kind: "" - proxy: noProxy: "" http: "" diff --git a/main.go b/main.go index 42c4fd8a..c88ea033 100644 --- a/main.go +++ b/main.go @@ -115,8 +115,6 @@ func mainE(ctx context.Context) error { daemonCommand.PersistentFlags().String(f.Service.Kubernetes.TLS.CrtFile, "", "Certificate file path to use to authenticate with Kubernetes.") daemonCommand.PersistentFlags().String(f.Service.Kubernetes.TLS.KeyFile, "", "Key file path to use to authenticate with Kubernetes.") - daemonCommand.PersistentFlags().String(f.Service.Provider.Kind, "", "Provider of management cluster this operator is running in. Used to determine provider-specific cluster values.") - daemonCommand.PersistentFlags().String(f.Service.Workload.Cluster.BaseDomain, "", "Cluster owner base domain.") daemonCommand.PersistentFlags().String(f.Service.Workload.Cluster.Calico.CIDR, "", "Prefix length for the CIDR block used by Calico.") daemonCommand.PersistentFlags().String(f.Service.Workload.Cluster.Calico.Subnet, "", "Network address for the CIDR block used by Calico.") diff --git a/service/controller/cluster.go b/service/controller/cluster.go index 05059999..57a1170f 100644 --- a/service/controller/cluster.go +++ b/service/controller/cluster.go @@ -37,7 +37,6 @@ type ClusterConfig struct { ClusterIPRange string DNSIP string ManagementClusterID string - Provider string RegistryDomain string Proxy proxy.Proxy } @@ -120,7 +119,6 @@ func newClusterResources(config ClusterConfig) ([]resource.Interface, error) { ClusterIPRange: config.ClusterIPRange, DNSIP: config.DNSIP, ManagementClusterID: config.ManagementClusterID, - Provider: config.Provider, RegistryDomain: config.RegistryDomain, Proxy: config.Proxy, } diff --git a/service/controller/resource/clusterconfigmap/desired.go b/service/controller/resource/clusterconfigmap/desired.go index 29fce0d9..4907cd3e 100644 --- a/service/controller/resource/clusterconfigmap/desired.go +++ b/service/controller/resource/clusterconfigmap/desired.go @@ -17,9 +17,9 @@ import ( "sigs.k8s.io/controller-runtime/pkg/client" "sigs.k8s.io/yaml" - capz "github.com/giantswarm/cluster-apps-operator/v2/api/capz/v1alpha4" "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" "github.com/giantswarm/cluster-apps-operator/v2/service/internal/podcidr" ) @@ -103,6 +103,7 @@ func (r *Resource) GetDesiredState(ctx context.Context, obj interface{}) ([]*cor } var ( + provider = "" // clusterCIDR is only used on azure. clusterCIDR = "" // gcpProject is only used on gcp. @@ -114,19 +115,10 @@ func (r *Resource) GetDesiredState(ctx context.Context, obj interface{}) ([]*cor { infrastructureRef := cr.Spec.InfrastructureRef if infrastructureRef != nil { - switch r.provider { - case "azure": - var azureCluster capz.AzureCluster - err = r.k8sClient.CtrlClient().Get(ctx, client.ObjectKey{Namespace: infrastructureRef.Namespace, Name: infrastructureRef.Name}, &azureCluster) - if err != nil { - return nil, microerror.Mask(err) - } + switch infrastructureRef.Kind { + case infra.AzureClusterKind, infra.AzureManagedClusterKind: + provider = infra.AzureClusterKindProvider - blocks := azureCluster.Spec.NetworkSpec.Vnet.CIDRBlocks - if len(blocks) > 0 { - clusterCIDR = blocks[0] - } - case "capz": capzCluster := &unstructured.Unstructured{} capzCluster.SetGroupVersionKind(schema.GroupVersionKind{ Group: infrastructureRef.GroupVersionKind().Group, @@ -154,8 +146,9 @@ func (r *Resource) GetDesiredState(ctx context.Context, obj interface{}) ([]*cor privateCluster = apiServerLbType == "Internal" - case "aws": - case "capa": + case infra.AWSClusterKind, infra.AWSManagedClusterKind: + provider = infra.AWSClusterKindProvider + awsCluster := &unstructured.Unstructured{} awsCluster.SetGroupVersionKind(schema.GroupVersionKind{ Group: infrastructureRef.GroupVersionKind().Group, @@ -176,9 +169,15 @@ func (r *Resource) GetDesiredState(ctx context.Context, obj interface{}) ([]*cor } privateCluster = annotationValue == annotation.AWSVPCModePrivate - case "cloud-director", "openstack", "vsphere": + case infra.VCDClusterKind: + provider = infra.VCDClusterKindProvider + privateCluster = !reflect.ValueOf(r.proxy).IsZero() + case infra.VSphereClusterKind: + provider = infra.VSphereClusterKindProvider privateCluster = !reflect.ValueOf(r.proxy).IsZero() - case "gcp": + case infra.GCPClusterKind, infra.GCPManagedClusterKind: + provider = infra.GCPClusterKindProvider + gcpCluster := &unstructured.Unstructured{} gcpCluster.SetGroupVersionKind(schema.GroupVersionKind{ Group: infrastructureRef.GroupVersionKind().Group, @@ -198,8 +197,10 @@ func (r *Resource) GetDesiredState(ctx context.Context, obj interface{}) ([]*cor return nil, fieldNotFoundOnInfrastructureTypeError } default: - r.logger.Debugf(ctx, "unable to extract infrastructure provider-specific clusterValues for cluster. Unsupported infrastructure kind %q", r.provider) + r.logger.Debugf(ctx, "unable to extract infrastructure provider-specific clusterValues for cluster. Unsupported infrastructure kind %q", infrastructureRef.Kind) } + } else { + return nil, microerror.Maskf(infrastructureRefNotFoundError, "%T.spec.infrastructureRef must not be empty", cr) } } @@ -209,7 +210,7 @@ func (r *Resource) GetDesiredState(ctx context.Context, obj interface{}) ([]*cor "workloadClusterID": key.ClusterID(&cr), }, "provider": map[string]interface{}{ - "kind": r.provider, + "kind": provider, }, "registry": map[string]interface{}{ "domain": r.registryDomain, @@ -263,7 +264,7 @@ func (r *Resource) GetDesiredState(ctx context.Context, obj interface{}) ([]*cor ClusterID: key.ClusterID(&cr), ClusterCIDR: clusterCIDR, GcpProject: gcpProject, - Provider: r.provider, + Provider: provider, } // disable boostrap mode and do not install CNI for EKS cluster diff --git a/service/controller/resource/clusterconfigmap/desired_test.go b/service/controller/resource/clusterconfigmap/desired_test.go index 00430922..d3223e16 100644 --- a/service/controller/resource/clusterconfigmap/desired_test.go +++ b/service/controller/resource/clusterconfigmap/desired_test.go @@ -102,7 +102,6 @@ func Test_ClusterValuesGCP(t *testing.T) { BaseDomain: "fadi.gigantic.io", ClusterIPRange: "10.0.0.0/16", DNSIP: "192.168.0.10", - Provider: "gcp", RegistryDomain: "quay.io/giantswarm", } resource, err := New(config) @@ -124,6 +123,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") if !cmData.BootstrapMode.Enabled { t.Fatal("bootstrap mode should be enabled") @@ -132,6 +132,14 @@ func Test_ClusterValuesGCP(t *testing.T) { if cmData.BootstrapMode.ApiServerPodPort != 6443 { t.Fatal("bootstrap mode should use 6443 on GCP") } + } else if strings.HasSuffix(configMap.Name, "-app-operator-values") { + cmData := &AppOperatorValuesConfig{} + err := yaml.Unmarshal([]byte(configMap.Data["values"]), cmData) + if err != nil { + t.Fatal(err) + } + + assertEquals(t, "gcp", cmData.Provider.Kind, "Wrong provider set in app-operator-values configmap") } } } @@ -143,6 +151,22 @@ func Test_ClusterValuesDNSIP(t *testing.T) { t.Fatal(err) } + gcpCluster := &unstructured.Unstructured{} + gcpCluster.Object = map[string]interface{}{ + "metadata": map[string]interface{}{ + "name": "test-cluster", + "namespace": "default", + }, + "spec": map[string]interface{}{ + "project": "12345", + }, + } + gcpCluster.SetGroupVersionKind(schema.GroupVersionKind{ + Group: "infrastructure.cluster.x-k8s.io", + Kind: "GCPCluster", + Version: "v1beta1", + }) + kubeadmControlPlane := &unstructured.Unstructured{} kubeadmControlPlane.Object = map[string]interface{}{ "metadata": map[string]interface{}{ @@ -178,6 +202,12 @@ func Test_ClusterValuesDNSIP(t *testing.T) { Name: "test-cluster", APIVersion: "controlplane.cluster.x-k8s.io/v1beta1", }, + InfrastructureRef: &corev1.ObjectReference{ + Kind: "GCPCluster", + Namespace: "default", + Name: "test-cluster", + APIVersion: "infrastructure.cluster.x-k8s.io/v1beta1", + }, }, } @@ -194,7 +224,7 @@ func Test_ClusterValuesDNSIP(t *testing.T) { fakeClient = k8sclienttest.NewClients(k8sclienttest.ClientsConfig{ CtrlClient: clientfake.NewClientBuilder(). - WithRuntimeObjects(kubeadmControlPlane, cluster). + WithRuntimeObjects(kubeadmControlPlane, gcpCluster, cluster). Build(), }) } @@ -206,7 +236,6 @@ func Test_ClusterValuesDNSIP(t *testing.T) { BaseDomain: "fadi.gigantic.io", ClusterIPRange: "10.0.0.0/16", DNSIP: "192.168.0.10", - Provider: "gcp", RegistryDomain: "quay.io/giantswarm", } resource, err := New(config) @@ -228,6 +257,15 @@ func Test_ClusterValuesDNSIP(t *testing.T) { } assertEquals(t, "172.16.0.10", cmData.ClusterDNSIP, "Wrong coredns service IP set in cluster-values configmap") assertEquals(t, "172.16.0.10", cmData.Cluster.Kubernetes.DNS["IP"], "Wrong coredns service IP set in cluster-values configmap") + assertEquals(t, "gcp", cmData.Provider, "Wrong provider set in cluster-values configmap") + } else if strings.HasSuffix(configMap.Name, "-app-operator-values") { + cmData := &AppOperatorValuesConfig{} + err := yaml.Unmarshal([]byte(configMap.Data["values"]), cmData) + if err != nil { + t.Fatal(err) + } + + assertEquals(t, "gcp", cmData.Provider.Kind, "Wrong provider set in app-operator-values configmap") } } } @@ -239,6 +277,22 @@ func Test_ClusterValuesDNSIPWhenServiceCidrIsNotSet(t *testing.T) { t.Fatal(err) } + gcpCluster := &unstructured.Unstructured{} + gcpCluster.Object = map[string]interface{}{ + "metadata": map[string]interface{}{ + "name": "test-cluster", + "namespace": "default", + }, + "spec": map[string]interface{}{ + "project": "12345", + }, + } + gcpCluster.SetGroupVersionKind(schema.GroupVersionKind{ + Group: "infrastructure.cluster.x-k8s.io", + Kind: "GCPCluster", + Version: "v1beta1", + }) + kubeadmControlPlane := &unstructured.Unstructured{} kubeadmControlPlane.Object = map[string]interface{}{ "metadata": map[string]interface{}{ @@ -265,6 +319,12 @@ func Test_ClusterValuesDNSIPWhenServiceCidrIsNotSet(t *testing.T) { Name: "test-cluster", APIVersion: "controlplane.cluster.x-k8s.io/v1beta1", }, + InfrastructureRef: &corev1.ObjectReference{ + Kind: "GCPCluster", + Namespace: "default", + Name: "test-cluster", + APIVersion: "infrastructure.cluster.x-k8s.io/v1beta1", + }, }, } @@ -281,7 +341,7 @@ func Test_ClusterValuesDNSIPWhenServiceCidrIsNotSet(t *testing.T) { fakeClient = k8sclienttest.NewClients(k8sclienttest.ClientsConfig{ CtrlClient: clientfake.NewClientBuilder(). - WithRuntimeObjects(kubeadmControlPlane, cluster). + WithRuntimeObjects(kubeadmControlPlane, gcpCluster, cluster). Build(), }) } @@ -293,7 +353,6 @@ func Test_ClusterValuesDNSIPWhenServiceCidrIsNotSet(t *testing.T) { BaseDomain: "fadi.gigantic.io", ClusterIPRange: "10.96.0.0/12", DNSIP: "10.96.0.10", - Provider: "gcp", RegistryDomain: "quay.io/giantswarm", } resource, err := New(config) @@ -315,28 +374,45 @@ func Test_ClusterValuesDNSIPWhenServiceCidrIsNotSet(t *testing.T) { } assertEquals(t, "10.96.0.10", cmData.ClusterDNSIP, "Wrong coredns service IP set in cluster-values configmap") assertEquals(t, "10.96.0.10", cmData.Cluster.Kubernetes.DNS["IP"], "Wrong coredns service IP set in cluster-values configmap") + assertEquals(t, "gcp", cmData.Provider, "Wrong provider set in cluster-values configmap") + } else if strings.HasSuffix(configMap.Name, "-app-operator-values") { + cmData := &AppOperatorValuesConfig{} + err := yaml.Unmarshal([]byte(configMap.Data["values"]), cmData) + if err != nil { + t.Fatal(err) + } + + assertEquals(t, "gcp", cmData.Provider.Kind, "Wrong provider set in app-operator-values configmap") } } } func Test_ClusterValuesGCPProjectOnlyAddedOnGCP(t *testing.T) { - podCidrConfig := podcidr.Config{InstallationCIDR: "10.0.0.0/16"} + podCidrConfig := podcidr.Config{InstallationCIDR: "10.200.0.0/24"} podCidr, err := podcidr.New(podCidrConfig) if err != nil { t.Fatal(err) } - openstackCluster := &unstructured.Unstructured{} - openstackCluster.Object = map[string]interface{}{ + capzCluster := &unstructured.Unstructured{} + capzCluster.Object = map[string]interface{}{ "metadata": map[string]interface{}{ "name": "test-cluster", "namespace": "default", }, - "spec": map[string]interface{}{}, + "spec": map[string]interface{}{ + "resourceGroup": "group1", + "subscriptionID": "143d9c06-6015-4a4a-a4f9-74a664207db7", + "networkSpec": map[string]interface{}{ + "apiServerLB": map[string]interface{}{ + "type": "Public", + }, + }, + }, } - openstackCluster.SetGroupVersionKind(schema.GroupVersionKind{ + capzCluster.SetGroupVersionKind(schema.GroupVersionKind{ Group: "infrastructure.cluster.x-k8s.io", - Kind: "OpenstackCluster", + Kind: "AzureCluster", Version: "v1beta1", }) @@ -344,14 +420,30 @@ func Test_ClusterValuesGCPProjectOnlyAddedOnGCP(t *testing.T) { ObjectMeta: metav1.ObjectMeta{ Name: "test-cluster", Namespace: "default", + Labels: map[string]string{ + capi.ClusterLabelName: "test-cluster", + }, }, Spec: capi.ClusterSpec{ InfrastructureRef: &corev1.ObjectReference{ - Kind: "OpenstackCluster", + Kind: "AzureCluster", Namespace: "default", Name: "test-cluster", APIVersion: "infrastructure.cluster.x-k8s.io/v1beta1", }, + ClusterNetwork: &capi.ClusterNetwork{ + ServiceDomain: "cluster.local", + Services: &capi.NetworkRanges{ + CIDRBlocks: []string{ + "172.31.0.0/16", + }, + }, + Pods: &capi.NetworkRanges{ + CIDRBlocks: []string{ + "192.168.0.0/16", + }, + }, + }, }, } @@ -368,7 +460,7 @@ func Test_ClusterValuesGCPProjectOnlyAddedOnGCP(t *testing.T) { fakeClient = k8sclienttest.NewClients(k8sclienttest.ClientsConfig{ CtrlClient: clientfake.NewClientBuilder(). - WithRuntimeObjects(openstackCluster, cluster). + WithRuntimeObjects(capzCluster, cluster). Build(), }) } @@ -380,7 +472,6 @@ func Test_ClusterValuesGCPProjectOnlyAddedOnGCP(t *testing.T) { BaseDomain: "fadi.gigantic.io", ClusterIPRange: "10.96.0.0/12", DNSIP: "10.96.0.10", - Provider: "vsphere", RegistryDomain: "quay.io/giantswarm", } resource, err := New(config) @@ -403,6 +494,15 @@ func Test_ClusterValuesGCPProjectOnlyAddedOnGCP(t *testing.T) { assertEquals(t, "", cmData.GcpProject, "GCPProject is only set when using gcp") assertEquals(t, "10.96.0.10", cmData.ClusterDNSIP, "Wrong coredns service IP set in cluster-values configmap") assertEquals(t, "10.96.0.10", cmData.Cluster.Kubernetes.DNS["IP"], "Wrong coredns service IP set in cluster-values configmap") + assertEquals(t, "capz", cmData.Provider, "Wrong provider set in cluster-values configmap") + } else if strings.HasSuffix(configMap.Name, "-app-operator-values") { + cmData := &AppOperatorValuesConfig{} + err := yaml.Unmarshal([]byte(configMap.Data["values"]), cmData) + if err != nil { + t.Fatal(err) + } + + assertEquals(t, "capz", cmData.Provider.Kind, "Wrong provider set in app-operator-values configmap") } } } @@ -492,7 +592,6 @@ func Test_ClusterValuesCAPZ(t *testing.T) { BaseDomain: "azuretest.gigantic.io", ClusterIPRange: "10.200.0.0/24", DNSIP: "172.31.0.10", - Provider: "capz", RegistryDomain: "quay.io/giantswarm", } resource, err := New(config) @@ -513,6 +612,7 @@ func Test_ClusterValuesCAPZ(t *testing.T) { t.Fatal(err) } 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") if !cmData.BootstrapMode.Enabled { t.Fatal("bootstrap mode should be enabled") @@ -521,6 +621,14 @@ func Test_ClusterValuesCAPZ(t *testing.T) { if cmData.BootstrapMode.ApiServerPodPort != 6443 { t.Fatal("bootstrap mode should use 6443 on CAPZ") } + } else if strings.HasSuffix(configMap.Name, "-app-operator-values") { + cmData := &AppOperatorValuesConfig{} + err := yaml.Unmarshal([]byte(configMap.Data["values"]), cmData) + if err != nil { + t.Fatal(err) + } + + assertEquals(t, "capz", cmData.Provider.Kind, "Wrong provider set in app-operator-values configmap") } } } @@ -610,7 +718,6 @@ func Test_ClusterValuesPrivateCAPZ(t *testing.T) { BaseDomain: "azuretest.gigantic.io", ClusterIPRange: "10.200.0.0/24", DNSIP: "172.31.0.10", - Provider: "capz", RegistryDomain: "quay.io/giantswarm", } resource, err := New(config) @@ -633,6 +740,7 @@ func Test_ClusterValuesPrivateCAPZ(t *testing.T) { assertEquals(t, "test-cluster.azuretest.gigantic.io", cmData.BaseDomain, "Wrong baseDomain set in cluster-values configmap") assertEquals(t, "", *cmData.ExternalDNSIP, "Wrong externalDNSIP set in cluster-values configmap for a private cluster") assertEquals(t, "true", strconv.FormatBool(cmData.Cluster.Private), "Wrong cluster.private set in cluster-values configmap for a private cluster") + assertEquals(t, "capz", cmData.Provider, "Wrong provider set in app-operator-values configmap") if !cmData.BootstrapMode.Enabled { t.Fatal("bootstrap mode should be enabled") @@ -641,6 +749,14 @@ func Test_ClusterValuesPrivateCAPZ(t *testing.T) { if cmData.BootstrapMode.ApiServerPodPort != 6443 { t.Fatal("bootstrap mode should use 6443 on CAPZ") } + } else if strings.HasSuffix(configMap.Name, "-app-operator-values") { + cmData := &AppOperatorValuesConfig{} + err := yaml.Unmarshal([]byte(configMap.Data["values"]), cmData) + if err != nil { + t.Fatal(err) + } + + assertEquals(t, "capz", cmData.Provider.Kind, "Wrong provider set in app-operator-values configmap") } } } diff --git a/service/controller/resource/clusterconfigmap/error.go b/service/controller/resource/clusterconfigmap/error.go index f6cd8b0b..096202cf 100644 --- a/service/controller/resource/clusterconfigmap/error.go +++ b/service/controller/resource/clusterconfigmap/error.go @@ -2,6 +2,15 @@ package clusterconfigmap import "github.com/giantswarm/microerror" +var infrastructureRefNotFoundError = µerror.Error{ + Kind: "infrastructureRefNotFoundError", +} + +// IsInvalidConfig asserts invalidConfigError. +func IsInfrastructureRefNotFoundError(err error) bool { + return microerror.Cause(err) == infrastructureRefNotFoundError +} + var invalidConfigError = µerror.Error{ Kind: "invalidConfigError", } diff --git a/service/controller/resource/clusterconfigmap/resource.go b/service/controller/resource/clusterconfigmap/resource.go index 19b2aac5..e7c49e98 100644 --- a/service/controller/resource/clusterconfigmap/resource.go +++ b/service/controller/resource/clusterconfigmap/resource.go @@ -27,7 +27,6 @@ type Config struct { ClusterIPRange string DNSIP string ManagementClusterID string - Provider string RegistryDomain string Proxy proxy.Proxy } @@ -44,7 +43,6 @@ type Resource struct { // dnsIP is the 10th IP within the `clusterIPRange` CIDR, that will be used for the coredns `Service`. dnsIP string managementClusterID string - provider string registryDomain string proxy proxy.Proxy } @@ -72,9 +70,6 @@ func New(config Config) (*Resource, error) { if config.DNSIP == "" { return nil, microerror.Maskf(invalidConfigError, "%T.DNSIP must not be empty", config) } - if config.Provider == "" { - return nil, microerror.Maskf(invalidConfigError, "%T.Provider must not be empty", config) - } if config.RegistryDomain == "" { return nil, microerror.Maskf(invalidConfigError, "%T.RegistryDomain must not be empty", config) } @@ -88,7 +83,6 @@ func New(config Config) (*Resource, error) { clusterIPRange: config.ClusterIPRange, dnsIP: config.DNSIP, managementClusterID: config.ManagementClusterID, - provider: config.Provider, registryDomain: config.RegistryDomain, proxy: config.Proxy, } diff --git a/service/controller/resource/clusterconfigmap/types.go b/service/controller/resource/clusterconfigmap/types.go index 147b7226..f34eab58 100644 --- a/service/controller/resource/clusterconfigmap/types.go +++ b/service/controller/resource/clusterconfigmap/types.go @@ -1,5 +1,13 @@ package clusterconfigmap +type AppOperatorProvider struct { + Kind string `json:"kind"` +} + +type AppOperatorValuesConfig struct { + Provider AppOperatorProvider `json:"provider"` +} + type ChartOperatorBootstrapMode struct { Enabled bool `json:"enabled"` ApiServerPodPort int32 `json:"apiServerPodPort"` diff --git a/service/controller/resource/clustersecret/desired.go b/service/controller/resource/clustersecret/desired.go index 760d56ae..82b0f2e5 100644 --- a/service/controller/resource/clustersecret/desired.go +++ b/service/controller/resource/clustersecret/desired.go @@ -20,10 +20,9 @@ import ( capi "sigs.k8s.io/cluster-api/api/v1beta1" capvcd "github.com/giantswarm/cluster-apps-operator/v2/api/capvcd/v1beta1" - - capo "github.com/giantswarm/cluster-apps-operator/v2/api/capo/v1alpha4" "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" ) const ( @@ -59,18 +58,7 @@ func (r *Resource) GetDesiredState(ctx context.Context, obj interface{}) ([]*cor infrastructureRef := cr.Spec.InfrastructureRef if infrastructureRef != nil { switch infrastructureRef.Kind { - case "OpenStackCluster": - var infraCluster capo.OpenStackCluster - err = r.k8sClient.CtrlClient().Get(ctx, client.ObjectKey{Namespace: infrastructureRef.Namespace, Name: infrastructureRef.Name}, &infraCluster) - if err != nil { - return nil, microerror.Mask(err) - } - - values["cloudConfig"], err = r.generateOpenStackCloudConfig(ctx, infraCluster) - if err != nil { - return nil, microerror.Mask(err) - } - case "VCDCluster": + case infra.VCDClusterKind: var infraCluster capvcd.VCDCluster err = r.k8sClient.CtrlClient().Get(ctx, client.ObjectKey{Namespace: infrastructureRef.Namespace, Name: infrastructureRef.Name}, &infraCluster) if err != nil { diff --git a/service/controller/resource/clustersecret/openstack.go b/service/controller/resource/clustersecret/openstack.go deleted file mode 100644 index ad211886..00000000 --- a/service/controller/resource/clustersecret/openstack.go +++ /dev/null @@ -1,99 +0,0 @@ -package clustersecret - -import ( - "context" - - "github.com/giantswarm/microerror" - corev1 "k8s.io/api/core/v1" - "sigs.k8s.io/controller-runtime/pkg/client" - "sigs.k8s.io/yaml" - - capo "github.com/giantswarm/cluster-apps-operator/v2/api/capo/v1alpha4" -) - -type openStackClouds struct { - Clouds map[string]openStackCloudConfig `json:"clouds"` -} - -type openStackCloudConfigAuth struct { - AuthURL string `json:"auth_url"` - Username string `json:"username"` - Password string `json:"password"` - UserDomainName string `json:"user_domain_name"` - ProjectID string `json:"project_id"` -} - -type openStackCloudConfig struct { - Auth openStackCloudConfigAuth `json:"auth"` - Verify bool `json:"verify"` - RegionName string `json:"region_name"` - Interface string `json:"interface"` - IdentityAPIVersion int `json:"identity_api_version"` -} - -func (r *Resource) generateOpenStackCloudConfig(ctx context.Context, cluster capo.OpenStackCluster) (map[string]interface{}, error) { - if cluster.Spec.IdentityRef == nil || cluster.Spec.IdentityRef.Name == "" || cluster.Spec.IdentityRef.Kind != "Secret" { - return nil, microerror.Mask(invalidConfigError) - } - - var cloudConfigSecret corev1.Secret - err := r.k8sClient.CtrlClient().Get(ctx, client.ObjectKey{Namespace: cluster.Namespace, Name: cluster.Spec.IdentityRef.Name}, &cloudConfigSecret) - if err != nil { - return nil, microerror.Mask(err) - } - - cloudsYAML, ok := cloudConfigSecret.Data["clouds.yaml"] - if !ok { - return nil, microerror.Mask(invalidConfigError) - } - - var clouds openStackClouds - err = yaml.Unmarshal(cloudsYAML, &clouds) - if err != nil { - return nil, microerror.Mask(err) - } - - cloudConfig, ok := clouds.Clouds["openstack"] - if !ok { - return nil, microerror.Mask(invalidConfigError) - } - - var networkID, subnetID, floatingNetworkID, publicNetworkName string - - if cluster.Status.Network != nil { - networkID = cluster.Status.Network.ID - subnetID = cluster.Status.Network.Subnet.ID - } - if cluster.Status.ExternalNetwork != nil { - floatingNetworkID = cluster.Status.ExternalNetwork.ID - publicNetworkName = cluster.Status.ExternalNetwork.Name - } - - authURL := cloudConfig.Auth.AuthURL - username := cloudConfig.Auth.Username - password := cloudConfig.Auth.Password - tenantID := cloudConfig.Auth.ProjectID - domainName := cloudConfig.Auth.UserDomainName - region := cloudConfig.RegionName - - return map[string]interface{}{ - "global": map[string]interface{}{ - "auth-url": authURL, - "username": username, - "password": password, - "tenant-id": tenantID, - "domain-name": domainName, - "region": region, - }, - "networking": map[string]interface{}{ - "ipv6-support-disabled": true, - "public-network-name": publicNetworkName, - }, - "loadBalancer": map[string]interface{}{ - "internal-lb": false, - "floating-network-id": floatingNetworkID, - "network-id": networkID, - "subnet-id": subnetID, - }, - }, nil -} diff --git a/service/internal/infrastructure/infrastructure.go b/service/internal/infrastructure/infrastructure.go new file mode 100644 index 00000000..f416b9a3 --- /dev/null +++ b/service/internal/infrastructure/infrastructure.go @@ -0,0 +1,24 @@ +package provider + +const ( + AWSClusterKind = "AWSCluster" + AWSClusterKindProvider = "capa" + + AWSManagedClusterKind = "AWSManagedCluster" + + AzureClusterKind = "AzureCluster" + AzureClusterKindProvider = "capz" + + AzureManagedClusterKind = "AzureManagedCluster" + + VCDClusterKind = "VCDCluster" + VCDClusterKindProvider = "cloud-director" + + VSphereClusterKind = "VSphereCluster" + VSphereClusterKindProvider = "vsphere" + + GCPClusterKind = "GCPCluster" + GCPClusterKindProvider = "gcp" + + GCPManagedClusterKind = "GCPManagedCluster" +) diff --git a/service/service.go b/service/service.go index a656180b..b8240941 100644 --- a/service/service.go +++ b/service/service.go @@ -150,7 +150,6 @@ func New(config Config) (*Service, error) { ClusterIPRange: clusterIPRange, DNSIP: dnsIP, ManagementClusterID: config.Viper.GetString(config.Flag.Service.Workload.Cluster.Owner), - Provider: config.Viper.GetString(config.Flag.Service.Provider.Kind), Proxy: proxy.Proxy{ HttpProxy: config.Viper.GetString(config.Flag.Service.Proxy.HttpProxy), HttpsProxy: config.Viper.GetString(config.Flag.Service.Proxy.HttpsProxy),