Skip to content

Commit

Permalink
feat: use werf to install modules' charts
Browse files Browse the repository at this point in the history
  • Loading branch information
diafour committed Oct 30, 2019
1 parent 03576e7 commit 1cf7854
Show file tree
Hide file tree
Showing 6 changed files with 113 additions and 5 deletions.
3 changes: 2 additions & 1 deletion Dockerfile-alpine3.9
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
3 changes: 3 additions & 0 deletions pkg/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
6 changes: 4 additions & 2 deletions pkg/helm/helm.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
101 changes: 101 additions & 0 deletions pkg/helm/werf.go
Original file line number Diff line number Diff line change
@@ -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
}
4 changes: 2 additions & 2 deletions pkg/module_manager/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 1cf7854

Please sign in to comment.