From a2171e04378bf060788e8c479538aa2be20bab50 Mon Sep 17 00:00:00 2001 From: Ivan Mikheykin Date: Tue, 24 Sep 2019 20:29:14 +0300 Subject: [PATCH] feat: use werf to install modules' charts --- Dockerfile-alpine3.9 | 5 +- go.sum | 1 + pkg/app/app.go | 3 + pkg/helm/helm.go | 2 +- pkg/helm/tiller.go | 4 +- pkg/helm/werf.go | 105 +++++++++++++++++++++++++++++++++++ pkg/module_manager/module.go | 4 +- 7 files changed, 118 insertions(+), 6 deletions(-) create mode 100644 pkg/helm/werf.go diff --git a/Dockerfile-alpine3.9 b/Dockerfile-alpine3.9 index 6d213584..c56bc511 100644 --- a/Dockerfile-alpine3.9 +++ b/Dockerfile-alpine3.9 @@ -9,11 +9,12 @@ FROM alpine:3.9 RUN apk --no-cache add ca-certificates jq bash && \ wget https://storage.googleapis.com/kubernetes-release/release/v1.13.5/bin/linux/amd64/kubectl -O /bin/kubectl && \ chmod +x /bin/kubectl && \ - wget https://storage.googleapis.com/kubernetes-helm/helm-v2.12.1-linux-amd64.tar.gz -O /helm.tgz && \ + wget https://storage.googleapis.com/kubernetes-helm/helm-v2.14.3-linux-amd64.tar.gz -O /helm.tgz && \ tar -z -x -C /bin -f /helm.tgz --strip-components=1 linux-amd64/helm linux-amd64/tiller && \ rm -f /helm.tgz && \ helm init --client-only && \ - mkdir /hooks + wget https://dl.bintray.com/flant/werf/v1.0.4-beta.5/werf-linux-amd64-v1.0.4-beta.5 -O /bin/werf && \ + chmod +x /bin/werf COPY --from=0 /addon-operator/addon-operator / WORKDIR / ENV MODULES_DIR /modules diff --git a/go.sum b/go.sum index c6cf9d63..354850f0 100644 --- a/go.sum +++ b/go.sum @@ -28,6 +28,7 @@ github.com/flant/shell-operator v1.0.0-beta.5 h1:4KYhM2YmMIWlwTiFNVzhFeoo6EPpNUy github.com/flant/shell-operator v1.0.0-beta.5/go.mod h1:qUqjq76as7qJn8BBMqCYQ+sqXBOmtk56AYY6BQQvUfA= github.com/flant/shell-operator v1.0.0-beta.5.0.20190917065053-86a4b2a7a3ae h1:mtSLQERuqx7ayExSgyO07yyVF2prG2Vkg7fCRsrl1L8= github.com/flant/shell-operator v1.0.0-beta.5.0.20190917065053-86a4b2a7a3ae/go.mod h1:qUqjq76as7qJn8BBMqCYQ+sqXBOmtk56AYY6BQQvUfA= +github.com/flant/shell-operator v1.0.0-beta.5.0.20190923140739-5f7d9cca9885 h1:V/GLcxnepIwrunJx8M8Pr1kjg9hAMJYo4YRoF1EcBdw= github.com/flant/shell-operator v1.0.0-beta.5.0.20190923140739-5f7d9cca9885/go.mod h1:qUqjq76as7qJn8BBMqCYQ+sqXBOmtk56AYY6BQQvUfA= github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo= github.com/ghodss/yaml v1.0.0 h1:wQHKEahhL6wmXdzwWG11gIVCkOv05bNOh+Rxn0yngAk= diff --git a/pkg/app/app.go b/pkg/app/app.go index 956c1918..7a8f0ada 100644 --- a/pkg/app/app.go +++ b/pkg/app/app.go @@ -23,6 +23,9 @@ var TillerProbeListenAddress = "127.0.0.1" var TillerProbeListenPort int32 = 44435 var TillerMaxHistory = 0 +var WerfTillerNamespace = "" +var WerfArgs = "" + var ConfigMapName = "addon-operator" var ValuesChecksumsAnnotation = "addon-operator/values-checksums" var TasksQueueDumpFilePath = "/tmp/addon-operator-tasks-queue" diff --git a/pkg/helm/helm.go b/pkg/helm/helm.go index 345e5d93..f2a2ca42 100644 --- a/pkg/helm/helm.go +++ b/pkg/helm/helm.go @@ -226,7 +226,7 @@ func (helm *CliHelm) UpgradeRelease(releaseName string, chart string, valuesPath if err != nil { return fmt.Errorf("helm upgrade failed: %s:\n%s %s", err, stdout, stderr) } - rlog.Infof("Helm upgrade for release '%s' with chart '%s' in namespace '%s' successful:\n%s\n%s", releaseName, chart, namespace, stdout, stderr) + rlog.Infof("Helm upgrade for release '%s' with chart '%s' in namespace '%s' was successful:\n%s\n%s", releaseName, chart, namespace, stdout, stderr) return nil } diff --git a/pkg/helm/tiller.go b/pkg/helm/tiller.go index aff3ecdd..beb50354 100644 --- a/pkg/helm/tiller.go +++ b/pkg/helm/tiller.go @@ -11,6 +11,8 @@ import ( "github.com/romana/rlog" ) +const TillerPath = "tiller" + // TillerOptions type TillerOptions struct { Namespace string @@ -35,7 +37,7 @@ func InitTillerProcess(options TillerOptions) error { fmt.Sprintf("%s:%d", options.ProbeListenAddress, options.ProbeListenPort), } - tillerCmd := exec.Command("/bin/tiller", args...) + tillerCmd := exec.Command(TillerPath, args...) tillerCmd.Env = append(os.Environ(), env...) tillerCmd.Dir = "/" diff --git a/pkg/helm/werf.go b/pkg/helm/werf.go new file mode 100644 index 00000000..1f2c47ab --- /dev/null +++ b/pkg/helm/werf.go @@ -0,0 +1,105 @@ +package helm + +import ( + "bytes" + "fmt" + "os" + "os/exec" + "strings" + + "github.com/romana/rlog" + + "github.com/flant/shell-operator/pkg/executor" + + "github.com/flant/addon-operator/pkg/app" +) + +const WerfPath = "werf" + +// WerfOptions +// FIXME is this needed? +type WerfOptions struct { + HelmReleaseStorageNamespace string + Namespace string +} + +type WerfClient interface { + DeployChart(releaseName string, chart string, valuesPaths []string, setValues []string, namespace string) error +} + +type werfClient struct { + Options WerfOptions +} + +// werfClient implements WerfClient +var _ WerfClient = &werfClient{} + +func NewWerfClient(opts WerfOptions) WerfClient { + return &werfClient{ + Options: opts, + } +} + +func (w *werfClient) DeployChart(releaseName string, chart string, valuesPaths []string, setValues []string, namespace string) error { + args := make([]string, 0) + args = append(args, "helm") + args = append(args, "deploy-chart") + + ns := namespace + if app.WerfTillerNamespace != "" { + ns = app.WerfTillerNamespace + } + args = append(args, "--namespace") + args = append(args, ns) + args = append(args, "--helm-release-storage-namespace") + args = append(args, ns) + + for _, valuesPath := range valuesPaths { + args = append(args, "--values") + args = append(args, valuesPath) + } + + for _, setValue := range setValues { + args = append(args, "--set") + args = append(args, setValue) + } + + args = append(args, chart) + args = append(args, releaseName) + + rlog.Infof("Running werf helm deploy-chart for release '%s' with chart '%s' in namespace '%s' ...", releaseName, chart, ns) + stdout, stderr, err := w.Run(args...) + if err != nil { + return fmt.Errorf("werf helm deploy-chart failed: %s:\n%s %s", err, stdout, stderr) + } + rlog.Infof("werf helm deploy-chart for release '%s' with chart '%s' in namespace '%s' was successful:\n%s\n%s", releaseName, chart, ns, stdout, stderr) + + return nil +} + + +func (w *werfClient) TillerNamespace() string { + //return h.tillerNamespace + return w.Options.Namespace +} + +// Cmd starts Helm with specified arguments. +// Sets the TILLER_NAMESPACE environment variable before starting, because Addon-operator works with its own Tiller. +func (w *werfClient) Run(args ...string) (stdout string, stderr string, err error) { + cmd := exec.Command(WerfPath, args...) + cmd.Env = os.Environ() + if app.WerfTillerNamespace != "" { + cmd.Env = append(cmd.Env, fmt.Sprintf("TILLER_NAMESPACE=%s", app.WerfTillerNamespace)) + } + + var stdoutBuf bytes.Buffer + cmd.Stdout = &stdoutBuf + var stderrBuf bytes.Buffer + cmd.Stderr = &stderrBuf + + err = executor.Run(cmd, true) + stdout = strings.TrimSpace(stdoutBuf.String()) + stderr = strings.TrimSpace(stderrBuf.String()) + + return +} diff --git a/pkg/module_manager/module.go b/pkg/module_manager/module.go index e84609d0..78f01d0b 100644 --- a/pkg/module_manager/module.go +++ b/pkg/module_manager/module.go @@ -200,11 +200,11 @@ func (m *Module) runHelmInstall() error { if doRelease { rlog.Debugf("MODULE_RUN '%s': helm release '%s' checksum '%s': installing/upgrading release", m.Name, helmReleaseName, checksum) - return helm.Client.UpgradeRelease( + werfCl := helm.NewWerfClient(helm.WerfOptions{}) + return werfCl.DeployChart( helmReleaseName, runChartPath, []string{valuesPath}, []string{fmt.Sprintf("_addonOperatorModuleChecksum=%s", checksum)}, - //helm.Client.TillerNamespace(), app.Namespace, ) } else {