Skip to content
Permalink
Browse files

Merge pull request #76 from bookingcom/juliogreff/validate-deployment…

…-name

Validate Deployment name when installing a chart
  • Loading branch information...
parhamdoustdar committed Apr 15, 2019
2 parents f4fe1e6 + f858974 commit 91bc21e0a7195924c55fc87a7274a0fb0bc7e1bb
@@ -4,6 +4,7 @@ import (
"fmt"
"reflect"
"sort"
"strings"

"github.com/golang/glog"
appsv1 "k8s.io/api/apps/v1"
@@ -332,6 +333,24 @@ func (i *Installer) installManifests(
return NewDecodeManifestError("error decoding manifest: %s", err)
}

// We need the Deployment in the chart to have a unique name,
// meaning that two different Releases need to generate two
// different Deployments. Otherwise, we try to overwrite a
// previous Deployment, and that fails with a "field is
// immutable" error.
if deployment, ok := decodedObj.(*appsv1.Deployment); ok {
deploymentName := deployment.ObjectMeta.Name
releaseName := i.Release.ObjectMeta.Name
if !strings.Contains(deploymentName, releaseName) {
return controller.NewInvalidChartError(
fmt.Sprintf("Deployment %q has invalid name."+
" The name of the Deployment should be"+
" templated with {{.Release.Name}}.",
deploymentName),
)
}
}

// Here we keep a counter of Services that have the lb label. This will
// be used later on to determine whether or not an invalid error should
// be returned to the caller.
@@ -18,6 +18,7 @@ import (
kubetesting "k8s.io/client-go/testing"

shipper "github.com/bookingcom/shipper/pkg/apis/shipper/v1alpha1"
"github.com/bookingcom/shipper/pkg/controller"
"github.com/bookingcom/shipper/pkg/controller/janitor"
shippertesting "github.com/bookingcom/shipper/pkg/testing"
)
@@ -281,6 +282,35 @@ func TestInstallerChartTarballBrokenService(t *testing.T) {
}
}

// TestInstallerChartTarballInvalidDeploymentName tests if the installation
// process fails when the release contains a deployment that doesn't have a
// name templated with the release's name.
func TestInstallerChartTarballInvalidDeploymentName(t *testing.T) {
cluster := buildCluster("minikube-a")

// there is a reviews-api-invalid-tarball.tgz in testdata which contains invalid deployment and service templates
release := buildRelease("0.0.1", "reviews-api", "0", "deadbeef", "reviews-api")
release.Spec.Environment.Chart.Version = "invalid-deployment-name"

it := buildInstallationTarget(release, "reviews-api", "reviews-api", []string{cluster.Name})
installer := newInstaller(release, it)

clientsPerCluster, _, fakeDynamicClientBuilder, _ := initializeClients(apiResourceList, nil, objectsPerClusterMap{cluster.Name: []runtime.Object{}})

fakePair := clientsPerCluster[cluster.Name]

restConfig := &rest.Config{}

err := installer.installRelease(cluster, fakePair.fakeClient, restConfig, fakeDynamicClientBuilder)
if err == nil {
t.Fatal("installRelease should fail, invalid deployment name")
}

if _, ok := err.(controller.InvalidChartError); !ok {
t.Fatalf("installRelease should fail with InvalidChartError, got %v instead", err)
}
}

// TestInstallerBrokenChartContents tests if the installation process fails when the
// release contains a valid chart tarball with invalid K8s object templates.
func TestInstallerBrokenChartContents(t *testing.T) {

0 comments on commit 91bc21e

Please sign in to comment.
You can’t perform that action at this time.