Skip to content

Commit

Permalink
Merge pull request uyuni-project#464 from cbosdo/k8s-auth
Browse files Browse the repository at this point in the history
Factorize the code adding the SCC credentials
  • Loading branch information
deneb-alpha authored Oct 9, 2024
2 parents b63f1e4 + 9aa69a1 commit dfc1610
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 26 deletions.
11 changes: 3 additions & 8 deletions mgradm/cmd/install/kubernetes/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 3 additions & 8 deletions mgradm/cmd/migrate/kubernetes/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 3 additions & 8 deletions mgrpxy/cmd/install/kubernetes/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
19 changes: 17 additions & 2 deletions shared/kubernetes/kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit dfc1610

Please sign in to comment.