From d8903f50f043a7f531ce99894e41b3c669336a73 Mon Sep 17 00:00:00 2001 From: Xieql Date: Wed, 13 Sep 2023 10:36:57 +0800 Subject: [PATCH] update Signed-off-by: Xieql --- pkg/fleet-manager/fleet_plugin_backup.go | 24 ++++++++++++++++- pkg/fleet-manager/manifests/plugin.tpl | 2 +- pkg/fleet-manager/plugin/plugin.go | 26 +++++++++++-------- pkg/fleet-manager/plugin/plugin_test.go | 13 ++++++---- .../plugin/testdata/backup/custom-values.yaml | 6 ++--- .../plugin/testdata/backup/default.yaml | 5 ++-- .../plugin/testdata/grafana/default.yaml | 1 - .../testdata/grafana/with-datasource.yaml | 1 - .../testdata/kyverno-policies/default.yaml | 1 - .../plugin/testdata/kyverno/default.yaml | 1 - .../plugin/testdata/prometheus/default.yaml | 1 - .../testdata/prometheus/with-values.yaml | 1 - .../plugin/testdata/thanos/custom-values.yaml | 1 - .../plugin/testdata/thanos/default.yaml | 1 - 14 files changed, 51 insertions(+), 33 deletions(-) diff --git a/pkg/fleet-manager/fleet_plugin_backup.go b/pkg/fleet-manager/fleet_plugin_backup.go index cd20df520..4d2ed6667 100644 --- a/pkg/fleet-manager/fleet_plugin_backup.go +++ b/pkg/fleet-manager/fleet_plugin_backup.go @@ -66,7 +66,6 @@ func (f *FleetManager) reconcileBackupPlugin(ctx context.Context, fleet *v1alpha // handle provider-specific details objStoreProvider := veleroCfg.Storage.Location.Provider // newSecret is a variable used to store the newly created secret object which contains the necessary credentials for the object storage provider. The specific structure and content of the secret vary depending on the provider. - // providerValues is a map that stores default configurations associated with the specific provider. These configurations are necessary for the proper functioning of the Velero tool with the provider. Currently, this includes configurations for initContainers. newSecret, err := f.getProviderDetails(ctx, veleroCfg.Storage.SecretName, objStoreProvider, fleetNN) if err != nil { return nil, ctrl.Result{}, err @@ -85,6 +84,10 @@ func (f *FleetManager) reconcileBackupPlugin(ctx context.Context, fleet *v1alpha return nil, ctrl.Result{}, err } + if err := createNewSecretInFleetCluster(cluster, newSecret); err != nil { + return nil, ctrl.Result{}, err + } + // apply Velero helm resources veleroResources, err := util.PatchResources(b) if err != nil { @@ -175,3 +178,22 @@ func getObjStoreCredentials(ctx context.Context, client client.Client, namespace return accessKey, secretKey, nil } + +// createNewSecretInFleetCluster creates a new secret in the specified fleet cluster. +// It takes a fleetCluster instance and a pre-built corev1.Secret instance as parameters. +// It uses the kube client from the fleetCluster instance to create the new secret in the respective cluster. +func createNewSecretInFleetCluster(cluster *fleetCluster, newSecret *corev1.Secret) error { + // Get the kubeclient.Interface instance + kubeClient := cluster.client.KubeClient() + + // Get the namespace of the secret + namespace := newSecret.Namespace + + // Create the new secret + _, err := kubeClient.CoreV1().Secrets(namespace).Create(context.TODO(), newSecret, metav1.CreateOptions{}) + if err != nil { + return err + } + + return nil +} diff --git a/pkg/fleet-manager/manifests/plugin.tpl b/pkg/fleet-manager/manifests/plugin.tpl index fd9b82bf1..c57641f88 100644 --- a/pkg/fleet-manager/manifests/plugin.tpl +++ b/pkg/fleet-manager/manifests/plugin.tpl @@ -47,7 +47,7 @@ spec: name: "{{ .ResourceName }}" {{- if or .Chart.Values .Values }} values: - {{- merge .Values .Chart.Values | toYaml | nindent 4 }} + {{- merge .Values .Chart.Values | toYaml | trim | nindent 4 }} {{- end }} interval: 1m0s install: diff --git a/pkg/fleet-manager/plugin/plugin.go b/pkg/fleet-manager/plugin/plugin.go index 74fe89a58..9de76447b 100644 --- a/pkg/fleet-manager/plugin/plugin.go +++ b/pkg/fleet-manager/plugin/plugin.go @@ -18,6 +18,7 @@ package plugin import ( "encoding/json" + "fmt" "io/fs" "strings" @@ -243,11 +244,12 @@ func RenderVelero( // get default values defaultValues := c.Values + // providerValues is a map that stores default configurations associated with the specific provider. These configurations are necessary for the proper functioning of the Velero tool with the provider. Currently, this includes configurations for initContainers. + providerValues, err := getProviderValues(backupCfg.Storage.Location.Provider) if err != nil { return nil, err } // add providerValues to default values - providerValues := getProviderValues(backupCfg.Storage.Location.Provider) defaultValues = transform.MergeMaps(defaultValues, providerValues) // get custom values @@ -320,18 +322,20 @@ func toMap(args apiextensionsv1.JSON) (map[string]interface{}, error) { return m, nil } -func getProviderValues(provider string) map[string]interface{} { +// getProviderValues return the map that stores default configurations associated with the specific provider. +// The provider parameter can be one of the following values: "aws", "huaweicloud", "gcp", "azure". +func getProviderValues(provider string) (map[string]interface{}, error) { switch provider { - case "AWS": - return buildAWSProviderValues() - case "HuaWeiCloud": - return buildHuaWeiCloudProviderValues() - case "GCP": - return buildGCPProviderValues() - case "Azure": - return buildAzureProviderValues() + case "aws": + return buildAWSProviderValues(), nil + case "huaweicloud": + return buildHuaWeiCloudProviderValues(), nil + case "gcp": + return buildGCPProviderValues(), nil + case "azure": + return buildAzureProviderValues(), nil default: - return nil + return nil, fmt.Errorf("unknown objStoreProvider: %v", provider) } } diff --git a/pkg/fleet-manager/plugin/plugin_test.go b/pkg/fleet-manager/plugin/plugin_test.go index 369ac1a73..232b2ea3c 100644 --- a/pkg/fleet-manager/plugin/plugin_test.go +++ b/pkg/fleet-manager/plugin/plugin_test.go @@ -287,10 +287,11 @@ func TestRenderPrometheus(t *testing.T) { func TestRenderVelero(t *testing.T) { cases := []struct { - name string - fleet types.NamespacedName - ref *metav1.OwnerReference - in *v1alpha1.BackupConfig + name string + fleet types.NamespacedName + ref *metav1.OwnerReference + in *v1alpha1.BackupConfig + newSecretName string }{ { name: "default", @@ -315,6 +316,7 @@ func TestRenderVelero(t *testing.T) { SecretName: "backup-secret", }, }, + newSecretName: "kurator-velero-s3", }, { name: "custom-values", @@ -342,6 +344,7 @@ func TestRenderVelero(t *testing.T) { Raw: []byte("{\"image\": {\n \"repository\": \"velero/velero\",\n \"tag\": \"v1.10.1\",\n \"pullPolicy\": \"IfNotPresent\"\n}}"), }, }, + newSecretName: "kurator-velero-s3", }, } @@ -351,7 +354,7 @@ func TestRenderVelero(t *testing.T) { Name: "cluster1", SecretName: "cluster1", SecretKey: "kubeconfig.yaml", - }, tc.in, "xxx") + }, tc.in, tc.newSecretName) assert.NoError(t, err) getExpected, err := getExpected("backup", tc.name) diff --git a/pkg/fleet-manager/plugin/testdata/backup/custom-values.yaml b/pkg/fleet-manager/plugin/testdata/backup/custom-values.yaml index 491c21d61..696d79bdd 100644 --- a/pkg/fleet-manager/plugin/testdata/backup/custom-values.yaml +++ b/pkg/fleet-manager/plugin/testdata/backup/custom-values.yaml @@ -51,9 +51,8 @@ spec: s3Url: http://x.x.x.x:x provider: aws credentials: - secretContents: - "useSecret": true, - "existingSecret": kurator-velero-s3, + existingSecret: kurator-velero-s3 + useSecret: true defaultVolumesToFsBackup: true deployNodeAgent: true image: @@ -67,7 +66,6 @@ spec: - mountPath: /target name: plugins snapshotsEnabled: false - interval: 1m0s install: createNamespace: true diff --git a/pkg/fleet-manager/plugin/testdata/backup/default.yaml b/pkg/fleet-manager/plugin/testdata/backup/default.yaml index 401475d89..4d44cf28b 100644 --- a/pkg/fleet-manager/plugin/testdata/backup/default.yaml +++ b/pkg/fleet-manager/plugin/testdata/backup/default.yaml @@ -51,8 +51,8 @@ spec: s3Url: http://x.x.x.x:x provider: aws credentials: - "useSecret": true, - "existingSecret": kurator-velero-s3, + existingSecret: kurator-velero-s3 + useSecret: true defaultVolumesToFsBackup: true deployNodeAgent: true image: @@ -65,7 +65,6 @@ spec: - mountPath: /target name: plugins snapshotsEnabled: false - interval: 1m0s install: createNamespace: true diff --git a/pkg/fleet-manager/plugin/testdata/grafana/default.yaml b/pkg/fleet-manager/plugin/testdata/grafana/default.yaml index f091ce8a1..dd1bf3af5 100644 --- a/pkg/fleet-manager/plugin/testdata/grafana/default.yaml +++ b/pkg/fleet-manager/plugin/testdata/grafana/default.yaml @@ -47,7 +47,6 @@ spec: fullnameOverride: grafana service: type: LoadBalancer - interval: 1m0s install: createNamespace: true diff --git a/pkg/fleet-manager/plugin/testdata/grafana/with-datasource.yaml b/pkg/fleet-manager/plugin/testdata/grafana/with-datasource.yaml index 3177a2a17..b15a5faea 100644 --- a/pkg/fleet-manager/plugin/testdata/grafana/with-datasource.yaml +++ b/pkg/fleet-manager/plugin/testdata/grafana/with-datasource.yaml @@ -56,7 +56,6 @@ spec: fullnameOverride: grafana service: type: LoadBalancer - interval: 1m0s install: createNamespace: true diff --git a/pkg/fleet-manager/plugin/testdata/kyverno-policies/default.yaml b/pkg/fleet-manager/plugin/testdata/kyverno-policies/default.yaml index 5c554e106..056d80065 100644 --- a/pkg/fleet-manager/plugin/testdata/kyverno-policies/default.yaml +++ b/pkg/fleet-manager/plugin/testdata/kyverno-policies/default.yaml @@ -46,7 +46,6 @@ spec: podSecuritySeverity: medium podSecurityStandard: baseline validationFailureAction: Audit - interval: 1m0s install: createNamespace: true diff --git a/pkg/fleet-manager/plugin/testdata/kyverno/default.yaml b/pkg/fleet-manager/plugin/testdata/kyverno/default.yaml index e93acc504..0a56f3ada 100644 --- a/pkg/fleet-manager/plugin/testdata/kyverno/default.yaml +++ b/pkg/fleet-manager/plugin/testdata/kyverno/default.yaml @@ -43,7 +43,6 @@ spec: name: "kyverno-cluster1" values: fullnameOverride: kyverno - interval: 1m0s install: createNamespace: true diff --git a/pkg/fleet-manager/plugin/testdata/prometheus/default.yaml b/pkg/fleet-manager/plugin/testdata/prometheus/default.yaml index 3491ec179..62135a79a 100644 --- a/pkg/fleet-manager/plugin/testdata/prometheus/default.yaml +++ b/pkg/fleet-manager/plugin/testdata/prometheus/default.yaml @@ -69,7 +69,6 @@ spec: secretName: thanos-objstore service: type: LoadBalancer - interval: 1m0s install: createNamespace: true diff --git a/pkg/fleet-manager/plugin/testdata/prometheus/with-values.yaml b/pkg/fleet-manager/plugin/testdata/prometheus/with-values.yaml index 4c39c7c69..96b6a36d2 100644 --- a/pkg/fleet-manager/plugin/testdata/prometheus/with-values.yaml +++ b/pkg/fleet-manager/plugin/testdata/prometheus/with-values.yaml @@ -60,7 +60,6 @@ spec: secretName: thanos-objstore service: type: LoadBalancer - interval: 1m0s install: createNamespace: true diff --git a/pkg/fleet-manager/plugin/testdata/thanos/custom-values.yaml b/pkg/fleet-manager/plugin/testdata/thanos/custom-values.yaml index c9c708cc8..a067776e5 100644 --- a/pkg/fleet-manager/plugin/testdata/thanos/custom-values.yaml +++ b/pkg/fleet-manager/plugin/testdata/thanos/custom-values.yaml @@ -52,7 +52,6 @@ spec: enabled: false storegateway: enabled: true - interval: 1m0s install: createNamespace: true diff --git a/pkg/fleet-manager/plugin/testdata/thanos/default.yaml b/pkg/fleet-manager/plugin/testdata/thanos/default.yaml index 032988a1d..80683e3b0 100644 --- a/pkg/fleet-manager/plugin/testdata/thanos/default.yaml +++ b/pkg/fleet-manager/plugin/testdata/thanos/default.yaml @@ -51,7 +51,6 @@ spec: enabled: false storegateway: enabled: true - interval: 1m0s install: createNamespace: true