Created
February 11, 2023 01:23
-
-
Save gmlewis/a88ba51d2589d88f1a55960fd260404b to your computer and use it in GitHub Desktop.
Work in progress - perform "helm upgrade" to deploy chart to Azure dev cluster
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package common | |
import ( | |
"context" | |
"fmt" | |
"log" | |
"os" | |
"strings" | |
"dagger.io/dagger" | |
) | |
const ( | |
azureClusterNameEnvVar = "AZURE_CLUSTER_NAME" | |
// The KUBE_CONFIG secret environment variable is generated using this command on Linux: | |
// $ base64 -w 0 < ~/.kube/config | |
kubeConfigEnvVar = "KUBE_CONFIG" | |
) | |
// HelmUpgrade runs "helm upgrade" on the provided container. | |
func (c *Client) HelmUpgrade(ctx context.Context, golang *dagger.Container, helmService, helmChartPath, helmChartValuesFilename, helmChartNamespace string) (err error) { | |
if !strings.HasSuffix(helmChartPath, "/") { | |
helmChartPath += "/" | |
} | |
golang, err = c.setupKubectl(ctx, golang) | |
if err != nil { | |
return err | |
} | |
golang, err = c.setupHelm(ctx, golang) | |
if err != nil { | |
return err | |
} | |
helmUpgrade := fmt.Sprintf(`helm upgrade %v "%[2]v" -f "%[2]v%[3]v" -n "%[4]v"`, helmService, helmChartPath, helmChartValuesFilename, helmChartNamespace) | |
golang = golang.WithExec([]string{"sh", "-c", helmUpgrade}) | |
out, err := golang.Stdout(ctx) | |
if err != nil { | |
return err | |
} | |
log.Printf("%v", out) | |
return nil | |
} | |
func (c *Client) setupKubectl(ctx context.Context, golang *dagger.Container) (*dagger.Container, error) { | |
azureClusterName := os.Getenv(azureClusterNameEnvVar) | |
// home := os.Getenv("HOME") | |
// kubeDir := filepath.Join(home, ".kube") | |
// kubeConfig := filepath.Join(kubeDir, "config") | |
getKubectl := `curl -LO "https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl"` | |
getKubelogin := `wget -q https://github.com/Azure/kubelogin/releases/download/v0.0.26/kubelogin-linux-amd64.zip` | |
getAz := `curl -sL https://aka.ms/InstallAzureCLIDeb | bash` | |
golang = golang.WithEnvVariable(kubeConfigEnvVar, os.Getenv(kubeConfigEnvVar)). | |
WithExec([]string{"sh", "-c", getKubectl}). | |
WithExec([]string{"sh", "-c", "chmod 755 kubectl && mv kubectl /usr/local/bin"}). | |
WithExec([]string{"which", "kubectl"}). | |
// WithExec([]string{"sh", "-c", "mkdir -p " + kubeDir}). | |
// WithExec([]string{"sh", "-c", makeKubeConfig}). | |
WithExec([]string{"sh", "-c", getKubelogin}). | |
WithExec([]string{"sh", "-c", "apt-get update && apt-get install zip -y"}). | |
WithExec([]string{"sh", "-c", "unzip kubelogin-linux-amd64.zip"}). | |
WithExec([]string{"sh", "-c", "mv bin/linux_amd64/kubelogin /usr/local/bin"}). | |
WithExec([]string{"sh", "-c", getAz}). | |
WithExec([]string{"sh", "-c", fmt.Sprintf("az aks get-credentials -n %v -g Development", azureClusterName)}). | |
WithExec([]string{"sh", "-c", "kubelogin convert-kubeconfig -l azurecli"}). | |
WithExec([]string{"sh", "-c", "kubectl config get-contexts"}). | |
WithExec([]string{"sh", "-c", fmt.Sprintf("kubectl config use-context %v", azureClusterName)}) | |
out, err := golang.Stdout(ctx) | |
if err != nil { | |
return nil, err | |
} | |
log.Printf("%v", out) | |
return golang, nil | |
} | |
func (c *Client) setupHelm(ctx context.Context, golang *dagger.Container) (*dagger.Container, error) { | |
getHelm := "curl https://raw.githubusercontent.com/helm/helm/main/scripts/get-helm-3 | bash" | |
golang = golang.WithExec([]string{"sh", "-c", getHelm}). | |
WithExec([]string{"which", "helm"}) | |
out, err := golang.Stdout(ctx) | |
if err != nil { | |
return nil, err | |
} | |
log.Printf("%v", out) | |
return golang, nil | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment