From 9aa69a1a1ee1273ec7f4a7794452261a5420332d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Bosdonnat?= Date: Wed, 9 Oct 2024 09:26:33 +0200 Subject: [PATCH] Factorize the code adding the SCC credentials --- mgradm/cmd/install/kubernetes/utils.go | 11 +++-------- mgradm/cmd/migrate/kubernetes/utils.go | 11 +++-------- mgrpxy/cmd/install/kubernetes/utils.go | 11 +++-------- shared/kubernetes/kubernetes.go | 19 +++++++++++++++++-- 4 files changed, 26 insertions(+), 26 deletions(-) diff --git a/mgradm/cmd/install/kubernetes/utils.go b/mgradm/cmd/install/kubernetes/utils.go index 4640213c5..b7b024872 100644 --- a/mgradm/cmd/install/kubernetes/utils.go +++ b/mgradm/cmd/install/kubernetes/utils.go @@ -69,14 +69,9 @@ func installForKubernetes(globalFlags *types.GlobalFlags, helmArgs = append(helmArgs, sslArgs...) // Create a secret using SCC credentials if any are provided - if flags.Scc.User != "" && flags.Scc.Password != "" { - secretName := "scc-credentials" - if err := shared_kubernetes.CreateDockerSecret( - flags.Helm.Uyuni.Namespace, secretName, "registry.suse.com", flags.Scc.User, flags.Scc.Password, - ); err != nil { - return err - } - helmArgs = append(helmArgs, "--set", "registrySecret="+secretName) + helmArgs, err = shared_kubernetes.AddSccSecret(helmArgs, flags.Helm.Uyuni.Namespace, &flags.Scc) + if err != nil { + return err } // Deploy Uyuni and wait for it to be up diff --git a/mgradm/cmd/migrate/kubernetes/utils.go b/mgradm/cmd/migrate/kubernetes/utils.go index 6e8d47d4a..b2bc33279 100644 --- a/mgradm/cmd/migrate/kubernetes/utils.go +++ b/mgradm/cmd/migrate/kubernetes/utils.go @@ -80,14 +80,9 @@ func migrateToKubernetes( helmArgs := []string{} // Create a secret using SCC credentials if any are provided - if flags.Scc.User != "" && flags.Scc.Password != "" { - secretName := "scc-credentials" - if err := shared_kubernetes.CreateDockerSecret( - flags.Helm.Uyuni.Namespace, secretName, "registry.suse.com", flags.Scc.User, flags.Scc.Password, - ); err != nil { - return err - } - helmArgs = append(helmArgs, "--set", "registrySecret="+secretName) + helmArgs, err = shared_kubernetes.AddSccSecret(helmArgs, flags.Helm.Uyuni.Namespace, &flags.Scc) + if err != nil { + return err } // Deploy for running migration command diff --git a/mgrpxy/cmd/install/kubernetes/utils.go b/mgrpxy/cmd/install/kubernetes/utils.go index 137396ac7..64f01ffeb 100644 --- a/mgrpxy/cmd/install/kubernetes/utils.go +++ b/mgrpxy/cmd/install/kubernetes/utils.go @@ -57,14 +57,9 @@ func installForKubernetes(globalFlags *types.GlobalFlags, } helmArgs := []string{"--set", "ingress=" + clusterInfos.Ingress} - if flags.Scc.User != "" && flags.Scc.Password != "" { - secretName := "scc-credentials" - if err := shared_kubernetes.CreateDockerSecret( - flags.Helm.Proxy.Namespace, secretName, "registry.suse.com", flags.Scc.User, flags.Scc.Password, - ); err != nil { - return err - } - helmArgs = append(helmArgs, "--set", "registrySecret="+secretName) + helmArgs, err = shared_kubernetes.AddSccSecret(helmArgs, flags.Helm.Proxy.Namespace, &flags.Scc) + if err != nil { + return err } // Install the uyuni proxy helm chart diff --git a/shared/kubernetes/kubernetes.go b/shared/kubernetes/kubernetes.go index 3e90d2996..00878be60 100644 --- a/shared/kubernetes/kubernetes.go +++ b/shared/kubernetes/kubernetes.go @@ -14,6 +14,7 @@ import ( "github.com/rs/zerolog" "github.com/rs/zerolog/log" . "github.com/uyuni-project/uyuni-tools/shared/l10n" + "github.com/uyuni-project/uyuni-tools/shared/types" "github.com/uyuni-project/uyuni-tools/shared/utils" ) @@ -153,8 +154,8 @@ func GetSecret(secretName string, filter string) (string, error) { return string(decoded), nil } -// CreateDockerSecret creates a secret of docker type to authenticate registries. -func CreateDockerSecret(namespace string, name string, registry string, username string, password string) error { +// createDockerSecret creates a secret of docker type to authenticate registries. +func createDockerSecret(namespace string, name string, registry string, username string, password string) error { authString := fmt.Sprintf("%s:%s", username, password) auth := base64.StdEncoding.EncodeToString([]byte(authString)) configjson := fmt.Sprintf( @@ -191,6 +192,20 @@ data: return nil } +// AddSccSecret creates a secret holding the SCC credentials and adds it to the helm args. +func AddSccSecret(helmArgs []string, namespace string, scc *types.SCCCredentials) ([]string, error) { + if scc.User != "" && scc.Password != "" { + secretName := "scc-credentials" + if err := createDockerSecret( + namespace, secretName, "registry.suse.com", scc.User, scc.Password, + ); err != nil { + return helmArgs, err + } + helmArgs = append(helmArgs, "--set", "registrySecret="+secretName) + } + return helmArgs, nil +} + // GetDeploymentImagePullSecret returns the name of the image pull secret of a deployment. // // This assumes only one secret is defined on the deployment.