@@ -11,7 +11,8 @@ import (
1111
1212 "github.com/spf13/cobra"
1313 "github.com/wunderio/silta-cli/internal/common"
14- v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
14+ v1core "k8s.io/api/core/v1"
15+ v1meta "k8s.io/apimachinery/pkg/apis/meta/v1"
1516 "k8s.io/client-go/kubernetes"
1617 "k8s.io/client-go/tools/clientcmd"
1718)
@@ -25,6 +26,9 @@ var ciReleaseDeployCmd = &cobra.Command{
2526 * Chart allows prepending extra configuration (to helm --values line) via
2627 "SILTA_<chart_name>_CONFIG_VALUES" environment variable. It has to be a
2728 base64 encoded string of a silta configuration yaml file.
29+
30+ * If IMAGE_PULL_SECRET is set (base64 encoded), it will be added to the
31+ release values as imagePullSecret.
2832 ` ,
2933 Run : func (cmd * cobra.Command , args []string ) {
3034
@@ -145,26 +149,43 @@ var ciReleaseDeployCmd = &cobra.Command{
145149 chartVersionOverride = fmt .Sprintf ("--version '%s'" , chartVersion )
146150 }
147151
148- // TODO: Create namespace if it doesn't exist
149- // & tag the namespace if it isn't already tagged.
150- // TODO: Rewrite
151-
152- command := fmt . Sprintf ( `
153- # Deployed chart version
154- NAMESPACE='%s'
152+ if ! debug {
153+ // Connect to the cluster
154+ homeDir , err := os . UserHomeDir ()
155+ if err != nil {
156+ log . Fatalf ( "cannot read user home dir" )
157+ }
158+ kubeConfigPath := homeDir + "/.kube/config"
155159
156- # Create the namespace if it doesn't exist.
157- if ! kubectl get namespace "$NAMESPACE" &>/dev/null ; then
158- kubectl create namespace "$NAMESPACE"
159- fi
160+ //k8s go client init logic
161+ config , err := clientcmd .BuildConfigFromFlags ("" , kubeConfigPath )
162+ if err != nil {
163+ log .Fatalf ("cannot read kubeConfig from path: %s" , err )
164+ }
165+ clientset , err := kubernetes .NewForConfig (config )
166+ if err != nil {
167+ log .Fatalf ("cannot initialize k8s client: %s" , err )
168+ }
160169
161- # Tag the namespace if it isn't already tagged.
162- if ! kubectl get namespace -l name=$NAMESPACE --no-headers 2>/dev/null | grep $NAMESPACE &>/dev/null ; then
163- kubectl label namespace "$NAMESPACE" "name=$NAMESPACE" --overwrite
164- fi
170+ // Create namespace if it doesn't exist
171+ _ , err = clientset .CoreV1 ().Namespaces ().Get (context .TODO (), namespace , v1meta.GetOptions {})
172+ if err != nil {
173+ _ , err = clientset .CoreV1 ().Namespaces ().Create (context .TODO (), & v1core.Namespace {
174+ ObjectMeta : v1meta.ObjectMeta {
175+ Name : namespace ,
176+ },
177+ }, v1meta.CreateOptions {})
178+ if err != nil {
179+ log .Fatalf ("cannot create namespace: %s" , err )
180+ }
181+ }
182+ }
165183
166- ` , namespace )
167- pipedExec (command , "" , "ERROR: " , debug )
184+ // Add imagePullsecret to release values if IMAGE_PULL_SECRET is set
185+ imagePullSecret := os .Getenv ("IMAGE_PULL_SECRET" )
186+ if len (imagePullSecret ) > 0 {
187+ helmFlags = fmt .Sprintf ("%s --set imagePullSecret='%s'" , helmFlags , imagePullSecret )
188+ }
168189
169190 if ! debug {
170191 // Add helm repositories
@@ -180,6 +201,8 @@ var ciReleaseDeployCmd = &cobra.Command{
180201 exec .Command ("bash" , "-c" , command ).Run ()
181202 }
182203
204+ command := ""
205+
183206 if chartName == "simple" || strings .HasSuffix (chartName , "/simple" ) {
184207
185208 if len (nginxImageUrl ) == 0 {
@@ -493,6 +516,7 @@ var ciReleaseDeployCmd = &cobra.Command{
493516 referenceDataOverride := ""
494517 if ! debug {
495518
519+ // Connect to the cluster
496520 homeDir , err := os .UserHomeDir ()
497521 if err != nil {
498522 log .Fatalf ("cannot read user home dir" )
@@ -512,7 +536,7 @@ var ciReleaseDeployCmd = &cobra.Command{
512536 // PVC name can be either "*-reference-data" or "*-reference", so we need to check both
513537 // Unless we parse and merge configuration yaml files, we can't know the exact name of the PVC
514538 // Check all pvc's in the namespace and see if any of them match the pattern
515- pvcs , err := clientset .CoreV1 ().PersistentVolumeClaims (namespace ).List (context .TODO (), v1 .ListOptions {})
539+ pvcs , err := clientset .CoreV1 ().PersistentVolumeClaims (namespace ).List (context .TODO (), v1meta .ListOptions {})
516540 if err != nil {
517541 log .Fatalf ("cannot get persistent volume claims: %s" , err )
518542 }
0 commit comments