From 1cf7854d3c18f7ae1e4e36415770eb8291259673 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 | 3 +- go.sum | 1 + pkg/app/app.go | 3 ++ pkg/helm/helm.go | 6 ++- pkg/helm/werf.go | 101 +++++++++++++++++++++++++++++++++++ pkg/module_manager/module.go | 4 +- 6 files changed, 113 insertions(+), 5 deletions(-) create mode 100644 pkg/helm/werf.go diff --git a/Dockerfile-alpine3.9 b/Dockerfile-alpine3.9 index 9857b283..a99d12cd 100644 --- a/Dockerfile-alpine3.9 +++ b/Dockerfile-alpine3.9 @@ -13,7 +13,8 @@ RUN apk --no-cache add ca-certificates jq bash && \ 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.12/werf-linux-amd64-v1.0.4-beta.12 -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 aaaadf03..db17d527 100644 --- a/go.sum +++ b/go.sum @@ -35,6 +35,7 @@ github.com/flant/shell-operator v1.0.0-beta.5.0.20191014100941-a02a093d4a71 h1:q github.com/flant/shell-operator v1.0.0-beta.5.0.20191014100941-a02a093d4a71/go.mod h1:KQ6PubnYxbkW8BoAXfWVra5dGuMSrec/G4ztFQEC2jU= github.com/flant/shell-operator v1.0.0-beta.5.0.20191023115920-4f35920cc42f h1:UwikOFWT3eCWIRYNugKHdZPa19mhIMEd1VWtNXoNpos= github.com/flant/shell-operator v1.0.0-beta.5.0.20191023115920-4f35920cc42f/go.mod h1:KQ6PubnYxbkW8BoAXfWVra5dGuMSrec/G4ztFQEC2jU= +github.com/flant/shell-operator v1.0.0-beta.5.0.20191023161047-91e8e7a8683e h1:TndP4g22Ww+AZxsipjg6qIl/unZReraHa68dLX08bZo= github.com/flant/shell-operator v1.0.0-beta.5.0.20191023161047-91e8e7a8683e/go.mod h1:KQ6PubnYxbkW8BoAXfWVra5dGuMSrec/G4ztFQEC2jU= github.com/fsnotify/fsnotify v1.4.7 h1:IXs+QLmnXW2CcXuY+8Mzv/fWEsPGWxqefPtCP5CnV9I= github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo= 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 66a4f77a..696b8387 100644 --- a/pkg/helm/helm.go +++ b/pkg/helm/helm.go @@ -14,10 +14,12 @@ import ( metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" kblabels "k8s.io/apimachinery/pkg/labels" - "github.com/flant/addon-operator/pkg/app" - "github.com/flant/addon-operator/pkg/utils" "github.com/flant/shell-operator/pkg/executor" "github.com/flant/shell-operator/pkg/kube" + + "github.com/flant/addon-operator/pkg/app" + "github.com/flant/addon-operator/pkg/utils" + ) const HelmPath = "helm" diff --git a/pkg/helm/werf.go b/pkg/helm/werf.go new file mode 100644 index 00000000..c454dd82 --- /dev/null +++ b/pkg/helm/werf.go @@ -0,0 +1,101 @@ +package helm + +import ( + "bytes" + "fmt" + "os" + "os/exec" + "strings" + + log "github.com/sirupsen/logrus" + + "github.com/flant/shell-operator/pkg/executor" + + "github.com/flant/addon-operator/pkg/app" +) + +const WerfPath = "werf" + +type WerfClient interface { + DeployChart(releaseName string, chart string, valuesPaths []string, setValues []string, namespace string) error +} + +// WerfOptions +// FIXME is this needed? +type WerfOptions struct { + HelmReleaseStorageNamespace string + Namespace string +} + +type werfClient struct { + Options WerfOptions + LogEntry *log.Entry +} + +// werfClient implements WerfClient +var _ WerfClient = &werfClient{} + +func NewWerfClient(logEntry *log.Entry, opts WerfOptions) WerfClient { + return &werfClient{ + LogEntry: logEntry.WithField("operator.component", "werf"), + 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) + + w.LogEntry.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) + } + w.LogEntry.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 +} + +// 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) + 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 c21b00e1..b084578d 100644 --- a/pkg/module_manager/module.go +++ b/pkg/module_manager/module.go @@ -215,11 +215,11 @@ func (m *Module) runHelmInstall() error { if doRelease { logEntry.Debugf("helm release '%s' checksum '%s': installing/upgrading release", helmReleaseName, checksum) - return helmClient.UpgradeRelease( + werfCl := helm.NewWerfClient(logEntry, helm.WerfOptions{}) + return werfCl.DeployChart( helmReleaseName, runChartPath, []string{valuesPath}, []string{fmt.Sprintf("_addonOperatorModuleChecksum=%s", checksum)}, - //helm.Client.TillerNamespace(), app.Namespace, ) } else {