167 lines
3.8 KiB
Go
167 lines
3.8 KiB
Go
// Package main implements the IntegraCI deploy-k8s Dagger module.
|
|
//
|
|
// Deploys services to Kubernetes via Helm + ArgoCD sync.
|
|
//
|
|
// Usage:
|
|
//
|
|
// dagger call deploy --chart ./helm/my-service --values ./values-staging.yaml --namespace staging
|
|
package main
|
|
|
|
import (
|
|
"context"
|
|
"dagger/deploy-k-8-s/internal/dagger"
|
|
"fmt"
|
|
)
|
|
|
|
// DeployK8s is the IntegraCI Kubernetes deployment module.
|
|
type DeployK8s struct{}
|
|
|
|
// DeployResult contains the output of a deployment.
|
|
type DeployResult struct {
|
|
Success bool `json:"success"`
|
|
Namespace string `json:"namespace"`
|
|
Release string `json:"release"`
|
|
Revision string `json:"revision,omitempty"`
|
|
Message string `json:"message"`
|
|
}
|
|
|
|
// Deploy runs Helm upgrade/install to deploy a service.
|
|
func (m *DeployK8s) Deploy(
|
|
ctx context.Context,
|
|
// Helm chart directory
|
|
chart *dagger.Directory,
|
|
// Values file
|
|
// +optional
|
|
values *dagger.File,
|
|
// Release name
|
|
// +optional
|
|
// +default="app"
|
|
release string,
|
|
// Target namespace
|
|
// +optional
|
|
// +default="default"
|
|
namespace string,
|
|
// Container image to deploy
|
|
// +optional
|
|
image string,
|
|
// Image tag
|
|
// +optional
|
|
// +default="latest"
|
|
tag string,
|
|
// Dry run mode
|
|
// +optional
|
|
dryRun bool,
|
|
// Kubeconfig file used to authenticate against the cluster
|
|
// +optional
|
|
kubeconfig *dagger.Secret,
|
|
// kube-context to target within the kubeconfig
|
|
// +optional
|
|
kubeContext string,
|
|
) (*DeployResult, error) {
|
|
helmArgs := []string{
|
|
"upgrade", "--install", release,
|
|
"/chart",
|
|
"--namespace", namespace,
|
|
"--create-namespace",
|
|
"--wait",
|
|
"--timeout", "5m",
|
|
}
|
|
|
|
if image != "" {
|
|
helmArgs = append(helmArgs, "--set", fmt.Sprintf("image.repository=%s", image))
|
|
}
|
|
if tag != "" {
|
|
helmArgs = append(helmArgs, "--set", fmt.Sprintf("image.tag=%s", tag))
|
|
}
|
|
if dryRun {
|
|
helmArgs = append(helmArgs, "--dry-run")
|
|
}
|
|
|
|
container := dag.Container().
|
|
From("alpine/helm:3.16").
|
|
WithDirectory("/chart", chart)
|
|
|
|
if values != nil {
|
|
container = container.WithFile("/values.yaml", values)
|
|
helmArgs = append(helmArgs, "-f", "/values.yaml")
|
|
}
|
|
if kubeconfig != nil {
|
|
container = container.
|
|
WithMountedSecret("/kube/config", kubeconfig).
|
|
WithEnvVariable("KUBECONFIG", "/kube/config")
|
|
}
|
|
if kubeContext != "" {
|
|
helmArgs = append(helmArgs, "--kube-context", kubeContext)
|
|
}
|
|
|
|
out, err := container.WithExec(helmArgs).Stdout(ctx)
|
|
if err != nil {
|
|
return &DeployResult{
|
|
Success: false,
|
|
Namespace: namespace,
|
|
Release: release,
|
|
Message: fmt.Sprintf("Helm deploy failed: %v", err),
|
|
}, nil
|
|
}
|
|
|
|
return &DeployResult{
|
|
Success: true,
|
|
Namespace: namespace,
|
|
Release: release,
|
|
Message: out,
|
|
}, nil
|
|
}
|
|
|
|
// Rollback performs a Helm rollback to a previous revision.
|
|
func (m *DeployK8s) Rollback(
|
|
ctx context.Context,
|
|
// Release name
|
|
release string,
|
|
// Target namespace
|
|
namespace string,
|
|
// Revision to rollback to (0 = previous)
|
|
// +optional
|
|
// +default=0
|
|
revision int,
|
|
// Kubeconfig file used to authenticate against the cluster
|
|
// +optional
|
|
kubeconfig *dagger.Secret,
|
|
// kube-context to target within the kubeconfig
|
|
// +optional
|
|
kubeContext string,
|
|
) (*DeployResult, error) {
|
|
args := []string{"rollback", release, "--namespace", namespace, "--wait"}
|
|
if revision > 0 {
|
|
args = append(args, fmt.Sprintf("%d", revision))
|
|
}
|
|
if kubeContext != "" {
|
|
args = append(args, "--kube-context", kubeContext)
|
|
}
|
|
|
|
container := dag.Container().
|
|
From("alpine/helm:3.16")
|
|
|
|
if kubeconfig != nil {
|
|
container = container.
|
|
WithMountedSecret("/kube/config", kubeconfig).
|
|
WithEnvVariable("KUBECONFIG", "/kube/config")
|
|
}
|
|
|
|
out, err := container.WithExec(args).Stdout(ctx)
|
|
if err != nil {
|
|
return &DeployResult{
|
|
Success: false,
|
|
Namespace: namespace,
|
|
Release: release,
|
|
Message: fmt.Sprintf("Rollback failed: %v", err),
|
|
}, nil
|
|
}
|
|
|
|
return &DeployResult{
|
|
Success: true,
|
|
Namespace: namespace,
|
|
Release: release,
|
|
Message: out,
|
|
}, nil
|
|
}
|