Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
Signed-off-by: Xieql <[email protected]>
  • Loading branch information
Xieql committed Sep 13, 2023
1 parent af78bc5 commit d8903f5
Show file tree
Hide file tree
Showing 14 changed files with 51 additions and 33 deletions.
24 changes: 23 additions & 1 deletion pkg/fleet-manager/fleet_plugin_backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
Expand Down Expand Up @@ -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
}
2 changes: 1 addition & 1 deletion pkg/fleet-manager/manifests/plugin.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
26 changes: 15 additions & 11 deletions pkg/fleet-manager/plugin/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package plugin

import (
"encoding/json"
"fmt"
"io/fs"
"strings"

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
}
}

Expand Down
13 changes: 8 additions & 5 deletions pkg/fleet-manager/plugin/plugin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -315,6 +316,7 @@ func TestRenderVelero(t *testing.T) {
SecretName: "backup-secret",
},
},
newSecretName: "kurator-velero-s3",
},
{
name: "custom-values",
Expand Down Expand Up @@ -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",
},
}

Expand All @@ -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)
Expand Down
6 changes: 2 additions & 4 deletions pkg/fleet-manager/plugin/testdata/backup/custom-values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -67,7 +66,6 @@ spec:
- mountPath: /target
name: plugins
snapshotsEnabled: false

interval: 1m0s
install:
createNamespace: true
Expand Down
5 changes: 2 additions & 3 deletions pkg/fleet-manager/plugin/testdata/backup/default.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -65,7 +65,6 @@ spec:
- mountPath: /target
name: plugins
snapshotsEnabled: false

interval: 1m0s
install:
createNamespace: true
Expand Down
1 change: 0 additions & 1 deletion pkg/fleet-manager/plugin/testdata/grafana/default.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ spec:
fullnameOverride: grafana
service:
type: LoadBalancer

interval: 1m0s
install:
createNamespace: true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ spec:
fullnameOverride: grafana
service:
type: LoadBalancer

interval: 1m0s
install:
createNamespace: true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ spec:
podSecuritySeverity: medium
podSecurityStandard: baseline
validationFailureAction: Audit

interval: 1m0s
install:
createNamespace: true
Expand Down
1 change: 0 additions & 1 deletion pkg/fleet-manager/plugin/testdata/kyverno/default.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ spec:
name: "kyverno-cluster1"
values:
fullnameOverride: kyverno

interval: 1m0s
install:
createNamespace: true
Expand Down
1 change: 0 additions & 1 deletion pkg/fleet-manager/plugin/testdata/prometheus/default.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ spec:
secretName: thanos-objstore
service:
type: LoadBalancer

interval: 1m0s
install:
createNamespace: true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ spec:
secretName: thanos-objstore
service:
type: LoadBalancer

interval: 1m0s
install:
createNamespace: true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ spec:
enabled: false
storegateway:
enabled: true

interval: 1m0s
install:
createNamespace: true
Expand Down
1 change: 0 additions & 1 deletion pkg/fleet-manager/plugin/testdata/thanos/default.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ spec:
enabled: false
storegateway:
enabled: true

interval: 1m0s
install:
createNamespace: true
Expand Down

0 comments on commit d8903f5

Please sign in to comment.