Skip to content

Commit

Permalink
imagePullsecret injection, per-cluster env variables to global
Browse files Browse the repository at this point in the history
  • Loading branch information
Jancis committed Oct 4, 2024
1 parent 18d8b07 commit b6364f0
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 21 deletions.
13 changes: 13 additions & 0 deletions cmd/ciImageLogin.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,19 @@ Available flags and environment variables:
aksSPAppID, _ := cmd.Flags().GetString("aks-sp-app-id")
aksSPPass, _ := cmd.Flags().GetString("aks-sp-password")

// Rewrite per-cluster environment variables
cluster_id := os.Getenv("SILTA_CLUSTER_ID")
if len(cluster_id) > 0 {
// iterate all environment variables, search for keys with "<${SILTA_CLUSTER_ID}>_" prefix
// and rewrite them to the corresponding keys without the prefix
for _, e := range os.Environ() {
pair := strings.SplitN(e, "=", 2)
if strings.HasPrefix(pair[0], cluster_id+"_") {
os.Setenv(strings.TrimPrefix(pair[0], cluster_id+"_"), pair[1])
}
}
}

// Use environment variables as fallback
if useEnv {
if len(gcpKeyJson) == 0 {
Expand Down
58 changes: 40 additions & 18 deletions cmd/ciReleaseDeploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ import (

"github.com/spf13/cobra"
"github.com/wunderio/silta-cli/internal/common"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
v1core "k8s.io/api/core/v1"
v1meta "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/clientcmd"
)
Expand Down Expand Up @@ -145,26 +146,46 @@ var ciReleaseDeployCmd = &cobra.Command{
chartVersionOverride = fmt.Sprintf("--version '%s'", chartVersion)
}

// TODO: Create namespace if it doesn't exist
// & tag the namespace if it isn't already tagged.
// TODO: Rewrite
if !debug {
// Connect to the cluster
homeDir, err := os.UserHomeDir()
if err != nil {
log.Fatalf("cannot read user home dir")
}
kubeConfigPath := homeDir + "/.kube/config"

command := fmt.Sprintf(`
# Deployed chart version
NAMESPACE='%s'
//k8s go client init logic
config, err := clientcmd.BuildConfigFromFlags("", kubeConfigPath)
if err != nil {
log.Fatalf("cannot read kubeConfig from path: %s", err)
}
clientset, err := kubernetes.NewForConfig(config)
if err != nil {
log.Fatalf("cannot initialize k8s client: %s", err)
}

# Create the namespace if it doesn't exist.
if ! kubectl get namespace "$NAMESPACE" &>/dev/null ; then
kubectl create namespace "$NAMESPACE"
fi
// Create namespace if it doesn't exist
_, err = clientset.CoreV1().Namespaces().Get(context.TODO(), namespace, v1meta.GetOptions{})
if err != nil {
_, err = clientset.CoreV1().Namespaces().Create(context.TODO(), &v1core.Namespace{
ObjectMeta: v1meta.ObjectMeta{
Name: namespace,
},
}, v1meta.CreateOptions{})
if err != nil {
log.Fatalf("cannot create namespace: %s", err)
}
}
}

# Tag the namespace if it isn't already tagged.
if ! kubectl get namespace -l name=$NAMESPACE --no-headers 2>/dev/null | grep $NAMESPACE &>/dev/null ; then
kubectl label namespace "$NAMESPACE" "name=$NAMESPACE" --overwrite
fi
// Add imagePullsecret to release values if IMAGE_PULL_SECRET is set
imagePullSecret := os.Getenv("IMAGE_PULL_SECRET")
if len(imagePullSecret) > 0 {
helmFlags = fmt.Sprintf("%s --set imagePullSecret='%s'", helmFlags, imagePullSecret)
}

`, namespace)
pipedExec(command, "", "ERROR: ", debug)
// TODO: fix this
command := ""

if !debug {
// Add helm repositories
Expand Down Expand Up @@ -493,6 +514,7 @@ var ciReleaseDeployCmd = &cobra.Command{
referenceDataOverride := ""
if !debug {

// Connect to the cluster
homeDir, err := os.UserHomeDir()
if err != nil {
log.Fatalf("cannot read user home dir")
Expand All @@ -512,7 +534,7 @@ var ciReleaseDeployCmd = &cobra.Command{
// PVC name can be either "*-reference-data" or "*-reference", so we need to check both
// Unless we parse and merge configuration yaml files, we can't know the exact name of the PVC
// Check all pvc's in the namespace and see if any of them match the pattern
pvcs, err := clientset.CoreV1().PersistentVolumeClaims(namespace).List(context.TODO(), v1.ListOptions{})
pvcs, err := clientset.CoreV1().PersistentVolumeClaims(namespace).List(context.TODO(), v1meta.ListOptions{})
if err != nil {
log.Fatalf("cannot get persistent volume claims: %s", err)
}
Expand Down
8 changes: 5 additions & 3 deletions cmd/ciReleaseDiff.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,9 +131,11 @@ var ciReleaseDiffCmd = &cobra.Command{
chartVersionOverride = fmt.Sprintf("--version '%s'", chartVersion)
}

// TODO: Create namespace if it doesn't exist
// & tag the namespace if it isn't already tagged.
// TODO: Rewrite
// Add imagePullsecret to release values if IMAGE_PULL_SECRET is set
imagePullSecret := os.Getenv("IMAGE_PULL_SECRET")
if len(imagePullSecret) > 0 {
helmFlags = fmt.Sprintf("%s --set imagePullSecret='%s'", helmFlags, imagePullSecret)
}

if !debug {
// Add helm repositories
Expand Down

0 comments on commit b6364f0

Please sign in to comment.