mirror of https://github.com/helm/helm
This adds install to the action package, and then fixes up a lot of testing. Signed-off-by: Matt Butcher <matt.butcher@microsoft.com>pull/5077/head
parent
2016109616
commit
bb53516fad
@ -0,0 +1,6 @@
|
|||||||
|
description: Empty testing chart
|
||||||
|
home: https://k8s.io/helm
|
||||||
|
name: empty
|
||||||
|
sources:
|
||||||
|
- https://github.com/kubernetes/helm
|
||||||
|
version: 0.1.0
|
@ -0,0 +1,3 @@
|
|||||||
|
#Empty
|
||||||
|
|
||||||
|
This space intentionally left blank.
|
@ -0,0 +1 @@
|
|||||||
|
# This file is intentionally blank
|
@ -0,0 +1 @@
|
|||||||
|
Name: my-empty
|
@ -0,0 +1,188 @@
|
|||||||
|
/*
|
||||||
|
Copyright The Helm Authors.
|
||||||
|
|
||||||
|
Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
you may not use this file except in compliance with the License.
|
||||||
|
You may obtain a copy of the License at
|
||||||
|
|
||||||
|
http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
|
||||||
|
Unless required by applicable law or agreed to in writing, software
|
||||||
|
distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
See the License for the specific language governing permissions and
|
||||||
|
limitations under the License.
|
||||||
|
*/
|
||||||
|
package action
|
||||||
|
|
||||||
|
import (
|
||||||
|
"flag"
|
||||||
|
"io"
|
||||||
|
"io/ioutil"
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/pkg/errors"
|
||||||
|
"k8s.io/client-go/kubernetes/fake"
|
||||||
|
"k8s.io/helm/pkg/chart"
|
||||||
|
"k8s.io/helm/pkg/hapi/release"
|
||||||
|
"k8s.io/helm/pkg/storage"
|
||||||
|
"k8s.io/helm/pkg/storage/driver"
|
||||||
|
"k8s.io/helm/pkg/tiller/environment"
|
||||||
|
)
|
||||||
|
|
||||||
|
var verbose = flag.Bool("test.log", false, "enable test logging")
|
||||||
|
|
||||||
|
func actionConfigFixture(t *testing.T) *Configuration {
|
||||||
|
t.Helper()
|
||||||
|
|
||||||
|
return &Configuration{
|
||||||
|
Releases: storage.Init(driver.NewMemory()),
|
||||||
|
KubeClient: &environment.PrintingKubeClient{Out: ioutil.Discard},
|
||||||
|
Discovery: fake.NewSimpleClientset().Discovery(),
|
||||||
|
Log: func(format string, v ...interface{}) {
|
||||||
|
t.Helper()
|
||||||
|
if *verbose {
|
||||||
|
t.Logf(format, v...)
|
||||||
|
}
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var manifestWithHook = `kind: ConfigMap
|
||||||
|
metadata:
|
||||||
|
name: test-cm
|
||||||
|
annotations:
|
||||||
|
"helm.sh/hook": post-install,pre-delete
|
||||||
|
data:
|
||||||
|
name: value`
|
||||||
|
|
||||||
|
var manifestWithTestHook = `kind: Pod
|
||||||
|
metadata:
|
||||||
|
name: finding-nemo,
|
||||||
|
annotations:
|
||||||
|
"helm.sh/hook": test-success
|
||||||
|
spec:
|
||||||
|
containers:
|
||||||
|
- name: nemo-test
|
||||||
|
image: fake-image
|
||||||
|
cmd: fake-command
|
||||||
|
`
|
||||||
|
|
||||||
|
type chartOptions struct {
|
||||||
|
*chart.Chart
|
||||||
|
}
|
||||||
|
|
||||||
|
type chartOption func(*chartOptions)
|
||||||
|
|
||||||
|
func buildChart(opts ...chartOption) *chart.Chart {
|
||||||
|
c := &chartOptions{
|
||||||
|
Chart: &chart.Chart{
|
||||||
|
// TODO: This should be more complete.
|
||||||
|
Metadata: &chart.Metadata{
|
||||||
|
Name: "hello",
|
||||||
|
},
|
||||||
|
// This adds a basic template and hooks.
|
||||||
|
Templates: []*chart.File{
|
||||||
|
{Name: "templates/hello", Data: []byte("hello: world")},
|
||||||
|
{Name: "templates/hooks", Data: []byte(manifestWithHook)},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, opt := range opts {
|
||||||
|
opt(c)
|
||||||
|
}
|
||||||
|
|
||||||
|
return c.Chart
|
||||||
|
}
|
||||||
|
|
||||||
|
func withNotes(notes string) chartOption {
|
||||||
|
return func(opts *chartOptions) {
|
||||||
|
opts.Templates = append(opts.Templates, &chart.File{
|
||||||
|
Name: "templates/NOTES.txt",
|
||||||
|
Data: []byte(notes),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func withDependency(dependencyOpts ...chartOption) chartOption {
|
||||||
|
return func(opts *chartOptions) {
|
||||||
|
opts.AddDependency(buildChart(dependencyOpts...))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func withSampleTemplates() chartOption {
|
||||||
|
return func(opts *chartOptions) {
|
||||||
|
sampleTemplates := []*chart.File{
|
||||||
|
// This adds basic templates and partials.
|
||||||
|
{Name: "templates/goodbye", Data: []byte("goodbye: world")},
|
||||||
|
{Name: "templates/empty", Data: []byte("")},
|
||||||
|
{Name: "templates/with-partials", Data: []byte(`hello: {{ template "_planet" . }}`)},
|
||||||
|
{Name: "templates/partials/_planet", Data: []byte(`{{define "_planet"}}Earth{{end}}`)},
|
||||||
|
}
|
||||||
|
opts.Templates = append(opts.Templates, sampleTemplates...)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func withKube(version string) chartOption {
|
||||||
|
return func(opts *chartOptions) {
|
||||||
|
opts.Metadata.KubeVersion = version
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// releaseStub creates a release stub, complete with the chartStub as its chart.
|
||||||
|
func releaseStub() *release.Release {
|
||||||
|
return namedReleaseStub("angry-panda", release.StatusDeployed)
|
||||||
|
}
|
||||||
|
|
||||||
|
func namedReleaseStub(name string, status release.ReleaseStatus) *release.Release {
|
||||||
|
now := time.Now()
|
||||||
|
return &release.Release{
|
||||||
|
Name: name,
|
||||||
|
Info: &release.Info{
|
||||||
|
FirstDeployed: now,
|
||||||
|
LastDeployed: now,
|
||||||
|
Status: status,
|
||||||
|
Description: "Named Release Stub",
|
||||||
|
},
|
||||||
|
Chart: buildChart(withSampleTemplates()),
|
||||||
|
Config: []byte(`name: value`),
|
||||||
|
Version: 1,
|
||||||
|
Hooks: []*release.Hook{
|
||||||
|
{
|
||||||
|
Name: "test-cm",
|
||||||
|
Kind: "ConfigMap",
|
||||||
|
Path: "test-cm",
|
||||||
|
Manifest: manifestWithHook,
|
||||||
|
Events: []release.HookEvent{
|
||||||
|
release.HookPostInstall,
|
||||||
|
release.HookPreDelete,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Name: "finding-nemo",
|
||||||
|
Kind: "Pod",
|
||||||
|
Path: "finding-nemo",
|
||||||
|
Manifest: manifestWithTestHook,
|
||||||
|
Events: []release.HookEvent{
|
||||||
|
release.HookReleaseTestSuccess,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func newHookFailingKubeClient() *hookFailingKubeClient {
|
||||||
|
return &hookFailingKubeClient{
|
||||||
|
PrintingKubeClient: environment.PrintingKubeClient{Out: ioutil.Discard},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
type hookFailingKubeClient struct {
|
||||||
|
environment.PrintingKubeClient
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *hookFailingKubeClient) WatchUntilReady(ns string, r io.Reader, timeout int64, shouldWait bool) error {
|
||||||
|
return errors.New("Failed watch")
|
||||||
|
}
|
@ -0,0 +1,435 @@
|
|||||||
|
/*
|
||||||
|
Copyright The Helm Authors.
|
||||||
|
|
||||||
|
Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
you may not use this file except in compliance with the License.
|
||||||
|
You may obtain a copy of the License at
|
||||||
|
|
||||||
|
http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
|
||||||
|
Unless required by applicable law or agreed to in writing, software
|
||||||
|
distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
See the License for the specific language governing permissions and
|
||||||
|
limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package action
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"path"
|
||||||
|
"sort"
|
||||||
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/pkg/errors"
|
||||||
|
"k8s.io/helm/pkg/chart"
|
||||||
|
"k8s.io/helm/pkg/chartutil"
|
||||||
|
"k8s.io/helm/pkg/engine"
|
||||||
|
"k8s.io/helm/pkg/hapi/release"
|
||||||
|
"k8s.io/helm/pkg/hooks"
|
||||||
|
"k8s.io/helm/pkg/releaseutil"
|
||||||
|
"k8s.io/helm/pkg/tiller"
|
||||||
|
"k8s.io/helm/pkg/version"
|
||||||
|
)
|
||||||
|
|
||||||
|
// releaseNameMaxLen is the maximum length of a release name.
|
||||||
|
//
|
||||||
|
// As of Kubernetes 1.4, the max limit on a name is 63 chars. We reserve 10 for
|
||||||
|
// charts to add data. Effectively, that gives us 53 chars.
|
||||||
|
// See https://github.com/kubernetes/helm/issues/1528
|
||||||
|
const releaseNameMaxLen = 53
|
||||||
|
|
||||||
|
// NOTESFILE_SUFFIX that we want to treat special. It goes through the templating engine
|
||||||
|
// but it's not a yaml file (resource) hence can't have hooks, etc. And the user actually
|
||||||
|
// wants to see this file after rendering in the status command. However, it must be a suffix
|
||||||
|
// since there can be filepath in front of it.
|
||||||
|
const notesFileSuffix = "NOTES.txt"
|
||||||
|
|
||||||
|
// Install performs an installation operation.
|
||||||
|
type Install struct {
|
||||||
|
cfg *Configuration
|
||||||
|
|
||||||
|
DryRun bool
|
||||||
|
DisableHooks bool
|
||||||
|
Replace bool
|
||||||
|
Wait bool
|
||||||
|
Devel bool
|
||||||
|
DepUp bool
|
||||||
|
Timeout int64
|
||||||
|
Namespace string
|
||||||
|
ReleaseName string
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewInstall creates a new Install object with the given configuration.
|
||||||
|
func NewInstall(cfg *Configuration) *Install {
|
||||||
|
return &Install{
|
||||||
|
cfg: cfg,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Run executes the installation
|
||||||
|
//
|
||||||
|
// If DryRun is set to true, this will prepare the release, but not install it
|
||||||
|
func (i *Install) Run(chrt *chart.Chart, rawValues []byte) (*release.Release, error) {
|
||||||
|
if err := i.availableName(); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
caps, err := i.cfg.capabilities()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
options := chartutil.ReleaseOptions{
|
||||||
|
Name: i.ReleaseName,
|
||||||
|
IsInstall: true,
|
||||||
|
}
|
||||||
|
valuesToRender, err := chartutil.ToRenderValues(chrt, rawValues, options, caps)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
rel := i.createRelease(chrt, rawValues)
|
||||||
|
var manifestDoc *bytes.Buffer
|
||||||
|
rel.Hooks, manifestDoc, rel.Info.Notes, err = i.renderResources(chrt, valuesToRender, caps.APIVersions)
|
||||||
|
// Even for errors, attach this if available
|
||||||
|
if manifestDoc != nil {
|
||||||
|
rel.Manifest = manifestDoc.String()
|
||||||
|
}
|
||||||
|
// Check error from render
|
||||||
|
if err != nil {
|
||||||
|
rel.SetStatus(release.StatusFailed, fmt.Sprintf("failed to render resource: %s", err.Error()))
|
||||||
|
rel.Version = 0 // Why do we do this?
|
||||||
|
return rel, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Mark this release as in-progress
|
||||||
|
rel.SetStatus(release.StatusPendingInstall, "Intiial install underway")
|
||||||
|
if err := i.validateManifest(manifestDoc); err != nil {
|
||||||
|
return rel, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Bail out here if it is a dry run
|
||||||
|
if i.DryRun {
|
||||||
|
rel.Info.Description = "Dry run complete"
|
||||||
|
return rel, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// If Replace is true, we need to supersede the last release.
|
||||||
|
if i.Replace {
|
||||||
|
if err := i.replaceRelease(rel); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Store the release in history before continuing (new in Helm 3). We always know
|
||||||
|
// that this is a create operation.
|
||||||
|
if err := i.cfg.Releases.Create(rel); err != nil {
|
||||||
|
// We could try to recover gracefully here, but since nothing has been installed
|
||||||
|
// yet, this is probably safer than trying to continue when we know storage is
|
||||||
|
// not working.
|
||||||
|
return rel, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// pre-install hooks
|
||||||
|
if !i.DisableHooks {
|
||||||
|
if err := i.execHook(rel.Hooks, hooks.PreInstall); err != nil {
|
||||||
|
rel.SetStatus(release.StatusFailed, "failed pre-install: "+err.Error())
|
||||||
|
i.replaceRelease(rel)
|
||||||
|
return rel, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// At this point, we can do the install. Note that before we were detecting whether to
|
||||||
|
// do an update, but it's not clear whether we WANT to do an update if the re-use is set
|
||||||
|
// to true, since that is basically an upgrade operation.
|
||||||
|
buf := bytes.NewBufferString(rel.Manifest)
|
||||||
|
if err := i.cfg.KubeClient.Create(i.Namespace, buf, i.Timeout, i.Wait); err != nil {
|
||||||
|
rel.SetStatus(release.StatusFailed, fmt.Sprintf("Release %q failed: %s", i.ReleaseName, err.Error()))
|
||||||
|
i.recordRelease(rel) // Ignore the error, since we have another error to deal with.
|
||||||
|
return rel, errors.Wrapf(err, "release %s failed", i.ReleaseName)
|
||||||
|
}
|
||||||
|
|
||||||
|
if !i.DisableHooks {
|
||||||
|
if err := i.execHook(rel.Hooks, hooks.PostInstall); err != nil {
|
||||||
|
rel.SetStatus(release.StatusFailed, "failed post-install: "+err.Error())
|
||||||
|
i.replaceRelease(rel)
|
||||||
|
return rel, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
rel.SetStatus(release.StatusDeployed, "Install complete")
|
||||||
|
|
||||||
|
// This is a tricky case. The release has been created, but the result
|
||||||
|
// cannot be recorded. The truest thing to tell the user is that the
|
||||||
|
// release was created. However, the user will not be able to do anything
|
||||||
|
// further with this release.
|
||||||
|
//
|
||||||
|
// One possible strategy would be to do a timed retry to see if we can get
|
||||||
|
// this stored in the future.
|
||||||
|
i.recordRelease(rel)
|
||||||
|
|
||||||
|
return rel, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// availableName tests whether a name is available
|
||||||
|
//
|
||||||
|
// Roughly, this will return an error if name is
|
||||||
|
//
|
||||||
|
// - empty
|
||||||
|
// - too long
|
||||||
|
// - already in use, and not deleted
|
||||||
|
// - used by a deleted release, and i.Replace is false
|
||||||
|
func (i *Install) availableName() error {
|
||||||
|
start := i.ReleaseName
|
||||||
|
if start == "" {
|
||||||
|
return errors.New("name is required")
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(start) > releaseNameMaxLen {
|
||||||
|
return errors.Errorf("release name %q exceeds max length of %d", start, releaseNameMaxLen)
|
||||||
|
}
|
||||||
|
|
||||||
|
h, err := i.cfg.Releases.History(start)
|
||||||
|
if err != nil || len(h) < 1 {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
releaseutil.Reverse(h, releaseutil.SortByRevision)
|
||||||
|
rel := h[0]
|
||||||
|
|
||||||
|
if st := rel.Info.Status; i.Replace && (st == release.StatusUninstalled || st == release.StatusFailed) {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
return errors.New("cannot re-use a name that is still in use")
|
||||||
|
}
|
||||||
|
|
||||||
|
// createRelease creates a new release object
|
||||||
|
func (i *Install) createRelease(chrt *chart.Chart, rawVals []byte) *release.Release {
|
||||||
|
ts := i.cfg.Now()
|
||||||
|
return &release.Release{
|
||||||
|
Name: i.ReleaseName,
|
||||||
|
Namespace: i.Namespace,
|
||||||
|
Chart: chrt,
|
||||||
|
Config: rawVals,
|
||||||
|
Info: &release.Info{
|
||||||
|
FirstDeployed: ts,
|
||||||
|
LastDeployed: ts,
|
||||||
|
Status: release.StatusUnknown,
|
||||||
|
},
|
||||||
|
Version: 1,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// recordRelease with an update operation in case reuse has been set.
|
||||||
|
func (i *Install) recordRelease(r *release.Release) error {
|
||||||
|
// This is a legacy function which has been reduced to a oneliner. Could probably
|
||||||
|
// refactor it out.
|
||||||
|
return i.cfg.Releases.Update(r)
|
||||||
|
}
|
||||||
|
|
||||||
|
// replaceRelease replaces an older release with this one
|
||||||
|
//
|
||||||
|
// This allows us to re-use names by superseding an existing release with a new one
|
||||||
|
func (i *Install) replaceRelease(rel *release.Release) error {
|
||||||
|
hist, err := i.cfg.Releases.History(rel.Name)
|
||||||
|
if err != nil || len(hist) == 0 {
|
||||||
|
// No releases exist for this name, so we can return early
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
releaseutil.Reverse(hist, releaseutil.SortByRevision)
|
||||||
|
last := hist[0]
|
||||||
|
|
||||||
|
// Update version to the next available
|
||||||
|
rel.Version = last.Version + 1
|
||||||
|
|
||||||
|
// Do not change the status of a failed release.
|
||||||
|
if last.Info.Status == release.StatusFailed {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// For any other status, mark it as superseded and store the old record
|
||||||
|
last.SetStatus(release.StatusSuperseded, "superseded by new release")
|
||||||
|
return i.recordRelease(last)
|
||||||
|
}
|
||||||
|
|
||||||
|
// renderResources renders the templates in a chart
|
||||||
|
func (i *Install) renderResources(ch *chart.Chart, values chartutil.Values, vs chartutil.VersionSet) ([]*release.Hook, *bytes.Buffer, string, error) {
|
||||||
|
hooks := []*release.Hook{}
|
||||||
|
buf := bytes.NewBuffer(nil)
|
||||||
|
// Guard to make sure Helm is at the right version to handle this chart.
|
||||||
|
sver := version.GetVersion()
|
||||||
|
if ch.Metadata.HelmVersion != "" &&
|
||||||
|
!version.IsCompatibleRange(ch.Metadata.HelmVersion, sver) {
|
||||||
|
return hooks, buf, "", errors.Errorf("chart incompatible with Helm %s", sver)
|
||||||
|
}
|
||||||
|
|
||||||
|
if ch.Metadata.KubeVersion != "" {
|
||||||
|
cap, _ := values["Capabilities"].(*chartutil.Capabilities)
|
||||||
|
gitVersion := cap.KubeVersion.String()
|
||||||
|
k8sVersion := strings.Split(gitVersion, "+")[0]
|
||||||
|
if !version.IsCompatibleRange(ch.Metadata.KubeVersion, k8sVersion) {
|
||||||
|
return hooks, buf, "", errors.Errorf("chart requires kubernetesVersion: %s which is incompatible with Kubernetes %s", ch.Metadata.KubeVersion, k8sVersion)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
files, err := engine.New().Render(ch, values)
|
||||||
|
if err != nil {
|
||||||
|
return hooks, buf, "", err
|
||||||
|
}
|
||||||
|
|
||||||
|
// NOTES.txt gets rendered like all the other files, but because it's not a hook nor a resource,
|
||||||
|
// pull it out of here into a separate file so that we can actually use the output of the rendered
|
||||||
|
// text file. We have to spin through this map because the file contains path information, so we
|
||||||
|
// look for terminating NOTES.txt. We also remove it from the files so that we don't have to skip
|
||||||
|
// it in the sortHooks.
|
||||||
|
notes := ""
|
||||||
|
for k, v := range files {
|
||||||
|
if strings.HasSuffix(k, notesFileSuffix) {
|
||||||
|
// Only apply the notes if it belongs to the parent chart
|
||||||
|
// Note: Do not use filePath.Join since it creates a path with \ which is not expected
|
||||||
|
if k == path.Join(ch.Name(), "templates", notesFileSuffix) {
|
||||||
|
notes = v
|
||||||
|
}
|
||||||
|
delete(files, k)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Sort hooks, manifests, and partials. Only hooks and manifests are returned,
|
||||||
|
// as partials are not used after renderer.Render. Empty manifests are also
|
||||||
|
// removed here.
|
||||||
|
// TODO: Can we migrate SortManifests out of pkg/tiller?
|
||||||
|
hooks, manifests, err := tiller.SortManifests(files, vs, tiller.InstallOrder)
|
||||||
|
if err != nil {
|
||||||
|
// By catching parse errors here, we can prevent bogus releases from going
|
||||||
|
// to Kubernetes.
|
||||||
|
//
|
||||||
|
// We return the files as a big blob of data to help the user debug parser
|
||||||
|
// errors.
|
||||||
|
b := bytes.NewBuffer(nil)
|
||||||
|
for name, content := range files {
|
||||||
|
if len(strings.TrimSpace(content)) == 0 {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
b.WriteString("\n---\n# Source: " + name + "\n")
|
||||||
|
b.WriteString(content)
|
||||||
|
}
|
||||||
|
return hooks, b, "", err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Aggregate all valid manifests into one big doc.
|
||||||
|
b := bytes.NewBuffer(nil)
|
||||||
|
for _, m := range manifests {
|
||||||
|
b.WriteString("\n---\n# Source: " + m.Name + "\n")
|
||||||
|
b.WriteString(m.Content)
|
||||||
|
}
|
||||||
|
|
||||||
|
return hooks, b, notes, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// validateManifest checks to see whether the given manifest is valid for the current Kubernetes
|
||||||
|
func (i *Install) validateManifest(manifest io.Reader) error {
|
||||||
|
_, err := i.cfg.KubeClient.BuildUnstructured(i.Namespace, manifest)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// execHook executes all of the hooks for the given hook event.
|
||||||
|
func (i *Install) execHook(hs []*release.Hook, hook string) error {
|
||||||
|
name := i.ReleaseName
|
||||||
|
namespace := i.Namespace
|
||||||
|
timeout := i.Timeout
|
||||||
|
executingHooks := []*release.Hook{}
|
||||||
|
|
||||||
|
for _, h := range hs {
|
||||||
|
for _, e := range h.Events {
|
||||||
|
if string(e) == hook {
|
||||||
|
executingHooks = append(executingHooks, h)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
sort.Sort(hookByWeight(executingHooks))
|
||||||
|
|
||||||
|
for _, h := range executingHooks {
|
||||||
|
if err := i.deleteHookByPolicy(h, hooks.BeforeHookCreation, hook); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
b := bytes.NewBufferString(h.Manifest)
|
||||||
|
if err := i.cfg.KubeClient.Create(namespace, b, timeout, false); err != nil {
|
||||||
|
return errors.Wrapf(err, "warning: Release %s %s %s failed", name, hook, h.Path)
|
||||||
|
}
|
||||||
|
b.Reset()
|
||||||
|
b.WriteString(h.Manifest)
|
||||||
|
|
||||||
|
if err := i.cfg.KubeClient.WatchUntilReady(namespace, b, timeout, false); err != nil {
|
||||||
|
// If a hook is failed, checkout the annotation of the hook to determine whether the hook should be deleted
|
||||||
|
// under failed condition. If so, then clear the corresponding resource object in the hook
|
||||||
|
if err := i.deleteHookByPolicy(h, hooks.HookFailed, hook); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// If all hooks are succeeded, checkout the annotation of each hook to determine whether the hook should be deleted
|
||||||
|
// under succeeded condition. If so, then clear the corresponding resource object in each hook
|
||||||
|
for _, h := range executingHooks {
|
||||||
|
if err := i.deleteHookByPolicy(h, hooks.HookSucceeded, hook); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
h.LastRun = time.Now()
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// deleteHookByPolicy deletes a hook if the hook policy instructs it to
|
||||||
|
func (i *Install) deleteHookByPolicy(h *release.Hook, policy, hook string) error {
|
||||||
|
b := bytes.NewBufferString(h.Manifest)
|
||||||
|
if hookHasDeletePolicy(h, policy) {
|
||||||
|
if errHookDelete := i.cfg.KubeClient.Delete(i.Namespace, b); errHookDelete != nil {
|
||||||
|
return errHookDelete
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// deletePolices represents a mapping between the key in the annotation for label deleting policy and its real meaning
|
||||||
|
// FIXME: Can we refactor this out?
|
||||||
|
var deletePolices = map[string]release.HookDeletePolicy{
|
||||||
|
hooks.HookSucceeded: release.HookSucceeded,
|
||||||
|
hooks.HookFailed: release.HookFailed,
|
||||||
|
hooks.BeforeHookCreation: release.HookBeforeHookCreation,
|
||||||
|
}
|
||||||
|
|
||||||
|
// hookHasDeletePolicy determines whether the defined hook deletion policy matches the hook deletion polices
|
||||||
|
// supported by helm. If so, mark the hook as one should be deleted.
|
||||||
|
func hookHasDeletePolicy(h *release.Hook, policy string) bool {
|
||||||
|
dp, ok := deletePolices[policy]
|
||||||
|
if !ok {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
for _, v := range h.DeletePolicies {
|
||||||
|
if dp == v {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
// hookByWeight is a sorter for hooks
|
||||||
|
type hookByWeight []*release.Hook
|
||||||
|
|
||||||
|
func (x hookByWeight) Len() int { return len(x) }
|
||||||
|
func (x hookByWeight) Swap(i, j int) { x[i], x[j] = x[j], x[i] }
|
||||||
|
func (x hookByWeight) Less(i, j int) bool {
|
||||||
|
if x[i].Weight == x[j].Weight {
|
||||||
|
return x[i].Name < x[j].Name
|
||||||
|
}
|
||||||
|
return x[i].Weight < x[j].Weight
|
||||||
|
}
|
@ -0,0 +1,216 @@
|
|||||||
|
/*
|
||||||
|
Copyright The Helm Authors.
|
||||||
|
|
||||||
|
Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
you may not use this file except in compliance with the License.
|
||||||
|
You may obtain a copy of the License at
|
||||||
|
|
||||||
|
http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
|
||||||
|
Unless required by applicable law or agreed to in writing, software
|
||||||
|
distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
See the License for the specific language governing permissions and
|
||||||
|
limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package action
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
|
||||||
|
"k8s.io/helm/pkg/hapi/release"
|
||||||
|
)
|
||||||
|
|
||||||
|
func installAction(t *testing.T) *Install {
|
||||||
|
config := actionConfigFixture(t)
|
||||||
|
instAction := NewInstall(config)
|
||||||
|
instAction.Namespace = "spaced"
|
||||||
|
instAction.ReleaseName = "test-install-release"
|
||||||
|
|
||||||
|
return instAction
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestInstallRelease(t *testing.T) {
|
||||||
|
is := assert.New(t)
|
||||||
|
instAction := installAction(t)
|
||||||
|
res, err := instAction.Run(buildChart(), []byte{})
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Failed install: %s", err)
|
||||||
|
}
|
||||||
|
is.Equal(res.Name, "test-install-release", "Expected release name.")
|
||||||
|
is.Equal(res.Namespace, "spaced")
|
||||||
|
|
||||||
|
rel, err := instAction.cfg.Releases.Get(res.Name, res.Version)
|
||||||
|
is.NoError(err)
|
||||||
|
|
||||||
|
is.Len(rel.Hooks, 1)
|
||||||
|
is.Equal(rel.Hooks[0].Manifest, manifestWithHook)
|
||||||
|
is.Equal(rel.Hooks[0].Events[0], release.HookPostInstall)
|
||||||
|
is.Equal(rel.Hooks[0].Events[1], release.HookPreDelete, "Expected event 0 is pre-delete")
|
||||||
|
|
||||||
|
is.NotEqual(len(res.Manifest), 0)
|
||||||
|
is.NotEqual(len(rel.Manifest), 0)
|
||||||
|
is.Contains(rel.Manifest, "---\n# Source: hello/templates/hello\nhello: world")
|
||||||
|
is.Equal(rel.Info.Description, "Install complete")
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestInstallRelease_NoName(t *testing.T) {
|
||||||
|
instAction := installAction(t)
|
||||||
|
instAction.ReleaseName = ""
|
||||||
|
_, err := instAction.Run(buildChart(), []byte{})
|
||||||
|
if err == nil {
|
||||||
|
t.Fatal("expected failure when no name is specified")
|
||||||
|
}
|
||||||
|
assert.Contains(t, err.Error(), "name is required")
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestInstallRelease_WithNotes(t *testing.T) {
|
||||||
|
is := assert.New(t)
|
||||||
|
instAction := installAction(t)
|
||||||
|
instAction.ReleaseName = "with-notes"
|
||||||
|
res, err := instAction.Run(buildChart(withNotes("note here")), []byte{})
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Failed install: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
is.Equal(res.Name, "with-notes")
|
||||||
|
is.Equal(res.Namespace, "spaced")
|
||||||
|
|
||||||
|
rel, err := instAction.cfg.Releases.Get(res.Name, res.Version)
|
||||||
|
is.NoError(err)
|
||||||
|
is.Len(rel.Hooks, 1)
|
||||||
|
is.Equal(rel.Hooks[0].Manifest, manifestWithHook)
|
||||||
|
is.Equal(rel.Hooks[0].Events[0], release.HookPostInstall)
|
||||||
|
is.Equal(rel.Hooks[0].Events[1], release.HookPreDelete, "Expected event 0 is pre-delete")
|
||||||
|
is.NotEqual(len(res.Manifest), 0)
|
||||||
|
is.NotEqual(len(rel.Manifest), 0)
|
||||||
|
is.Contains(rel.Manifest, "---\n# Source: hello/templates/hello\nhello: world")
|
||||||
|
is.Equal(rel.Info.Description, "Install complete")
|
||||||
|
|
||||||
|
is.Equal(rel.Info.Notes, "note here")
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestInstallRelease_WithNotesRendered(t *testing.T) {
|
||||||
|
is := assert.New(t)
|
||||||
|
instAction := installAction(t)
|
||||||
|
instAction.ReleaseName = "with-notes"
|
||||||
|
res, err := instAction.Run(buildChart(withNotes("got-{{.Release.Name}}")), []byte{})
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Failed install: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
rel, err := instAction.cfg.Releases.Get(res.Name, res.Version)
|
||||||
|
is.NoError(err)
|
||||||
|
|
||||||
|
expectedNotes := fmt.Sprintf("got-%s", res.Name)
|
||||||
|
is.Equal(expectedNotes, rel.Info.Notes)
|
||||||
|
is.Equal(rel.Info.Description, "Install complete")
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestInstallRelease_WithChartAndDependencyNotes(t *testing.T) {
|
||||||
|
// Regression: Make sure that the child's notes don't override the parent's
|
||||||
|
is := assert.New(t)
|
||||||
|
instAction := installAction(t)
|
||||||
|
instAction.ReleaseName = "with-notes"
|
||||||
|
res, err := instAction.Run(buildChart(
|
||||||
|
withNotes("parent"),
|
||||||
|
withDependency(withNotes("child"))), []byte{},
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Failed install: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
rel, err := instAction.cfg.Releases.Get(res.Name, res.Version)
|
||||||
|
is.Equal("with-notes", rel.Name)
|
||||||
|
is.NoError(err)
|
||||||
|
is.Equal("parent", rel.Info.Notes)
|
||||||
|
is.Equal(rel.Info.Description, "Install complete")
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestInstallRelease_DryRun(t *testing.T) {
|
||||||
|
is := assert.New(t)
|
||||||
|
instAction := installAction(t)
|
||||||
|
instAction.DryRun = true
|
||||||
|
res, err := instAction.Run(buildChart(withSampleTemplates()), []byte{})
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Failed install: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
is.Contains(res.Manifest, "---\n# Source: hello/templates/hello\nhello: world")
|
||||||
|
is.Contains(res.Manifest, "---\n# Source: hello/templates/goodbye\ngoodbye: world")
|
||||||
|
is.Contains(res.Manifest, "hello: Earth")
|
||||||
|
is.NotContains(res.Manifest, "hello: {{ template \"_planet\" . }}")
|
||||||
|
is.NotContains(res.Manifest, "empty")
|
||||||
|
|
||||||
|
_, err = instAction.cfg.Releases.Get(res.Name, res.Version)
|
||||||
|
is.Error(err)
|
||||||
|
is.Len(res.Hooks, 1)
|
||||||
|
is.True(res.Hooks[0].LastRun.IsZero(), "expect hook to not be marked as run")
|
||||||
|
is.Equal(res.Info.Description, "Dry run complete")
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestInstallRelease_NoHooks(t *testing.T) {
|
||||||
|
is := assert.New(t)
|
||||||
|
instAction := installAction(t)
|
||||||
|
instAction.DisableHooks = true
|
||||||
|
instAction.ReleaseName = "no-hooks"
|
||||||
|
instAction.cfg.Releases.Create(releaseStub())
|
||||||
|
|
||||||
|
res, err := instAction.Run(buildChart(), []byte{})
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Failed install: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
is.True(res.Hooks[0].LastRun.IsZero(), "hooks should not run with no-hooks")
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestInstallRelease_FailedHooks(t *testing.T) {
|
||||||
|
is := assert.New(t)
|
||||||
|
instAction := installAction(t)
|
||||||
|
instAction.ReleaseName = "failed-hooks"
|
||||||
|
instAction.cfg.KubeClient = newHookFailingKubeClient()
|
||||||
|
|
||||||
|
res, err := instAction.Run(buildChart(), []byte{})
|
||||||
|
is.Error(err)
|
||||||
|
is.Contains(res.Info.Description, "failed post-install")
|
||||||
|
is.Equal(res.Info.Status, release.StatusFailed)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestInstallRelease_ReplaceRelease(t *testing.T) {
|
||||||
|
is := assert.New(t)
|
||||||
|
instAction := installAction(t)
|
||||||
|
instAction.Replace = true
|
||||||
|
|
||||||
|
rel := releaseStub()
|
||||||
|
rel.Info.Status = release.StatusUninstalled
|
||||||
|
instAction.cfg.Releases.Create(rel)
|
||||||
|
instAction.ReleaseName = rel.Name
|
||||||
|
|
||||||
|
res, err := instAction.Run(buildChart(), []byte{})
|
||||||
|
is.NoError(err)
|
||||||
|
|
||||||
|
// This should have been auto-incremented
|
||||||
|
is.Equal(2, res.Version)
|
||||||
|
is.Equal(res.Name, rel.Name)
|
||||||
|
|
||||||
|
getres, err := instAction.cfg.Releases.Get(rel.Name, res.Version)
|
||||||
|
is.NoError(err)
|
||||||
|
is.Equal(getres.Info.Status, release.StatusDeployed)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestInstallRelease_KubeVersion(t *testing.T) {
|
||||||
|
is := assert.New(t)
|
||||||
|
instAction := installAction(t)
|
||||||
|
_, err := instAction.Run(buildChart(withKube(">=0.0.0")), []byte{})
|
||||||
|
is.NoError(err)
|
||||||
|
|
||||||
|
// This should fail for a few hundred years
|
||||||
|
instAction.ReleaseName = "should-fail"
|
||||||
|
_, err = instAction.Run(buildChart(withKube(">=99.0.0")), []byte{})
|
||||||
|
is.Error(err)
|
||||||
|
is.Contains(err.Error(), "chart requires kubernetesVersion")
|
||||||
|
}
|
@ -1,29 +0,0 @@
|
|||||||
/*
|
|
||||||
Copyright The Helm Authors.
|
|
||||||
|
|
||||||
Licensed under the Apache License, Version 2.0 (the "License");
|
|
||||||
you may not use this file except in compliance with the License.
|
|
||||||
You may obtain a copy of the License at
|
|
||||||
|
|
||||||
http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
|
|
||||||
Unless required by applicable law or agreed to in writing, software
|
|
||||||
distributed under the License is distributed on an "AS IS" BASIS,
|
|
||||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
||||||
See the License for the specific language governing permissions and
|
|
||||||
limitations under the License.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package tiller
|
|
||||||
|
|
||||||
// func TestRunReleaseTest(t *testing.T) {
|
|
||||||
// rs := rsFixture()
|
|
||||||
// rel := namedReleaseStub("nemo", release.Status_DEPLOYED)
|
|
||||||
// rs.env.Releases.Create(rel)
|
|
||||||
|
|
||||||
// req := &services.TestReleaseRequest{Name: "nemo", Timeout: 2}
|
|
||||||
// err := rs.RunReleaseTest(req, mockRunReleaseTestServer{})
|
|
||||||
// if err != nil {
|
|
||||||
// t.Fatalf("failed to run release tests on %s: %s", rel.Name, err)
|
|
||||||
// }
|
|
||||||
// }
|
|
Loading…
Reference in new issue