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 Sep 24, 2019
1 parent 4955bcf commit a2171e0
Show file tree
Hide file tree
Showing 7 changed files with 118 additions and 6 deletions.
5 changes: 3 additions & 2 deletions Dockerfile-alpine3.9
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
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
2 changes: 1 addition & 1 deletion pkg/helm/helm.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
4 changes: 3 additions & 1 deletion pkg/helm/tiller.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import (
"github.com/romana/rlog"
)

const TillerPath = "tiller"

// TillerOptions
type TillerOptions struct {
Namespace string
Expand All @@ -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 = "/"

Expand Down
105 changes: 105 additions & 0 deletions pkg/helm/werf.go
Original file line number Diff line number Diff line change
@@ -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
}
4 changes: 2 additions & 2 deletions pkg/module_manager/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit a2171e0

Please sign in to comment.