Skip to content

Commit

Permalink
Adjust for canary
Browse files Browse the repository at this point in the history
  • Loading branch information
koncar committed Jun 3, 2022
1 parent 8906f86 commit 0ae4ce6
Show file tree
Hide file tree
Showing 8 changed files with 63 additions and 46 deletions.
32 changes: 18 additions & 14 deletions internal/chart/application_chart.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,11 +233,11 @@ appVersion: {{ .AppVersion }}

// ChartConfig contains data used to render the helm chart's "Chart.yaml" file.
type ChartConfig struct {
Version string
Description string
AppName string
AppVersion string
DeploymentVersion int
Version string
Description string
AppName string
AppVersion string
DeploymentVersions []int
}

// NewChartConfig returns a ChartConfig instance based on the given application.
Expand All @@ -248,20 +248,24 @@ func NewChartConfig(app ketchv1.App) ChartConfig {
version = *app.Spec.Version
}
return ChartConfig{
Version: chartVersion,
Description: app.Spec.Description,
AppName: app.Name,
AppVersion: version,
DeploymentVersion: targetDeploymentVersion(app),
Version: chartVersion,
Description: app.Spec.Description,
AppName: app.Name,
AppVersion: version,
DeploymentVersions: deploymentVersions(app),
}
}

// targetDeploymentVersion is last deployment in app deployments
func targetDeploymentVersion(appCR ketchv1.App) int {
// targetDeploymentVersion is all deployment version in given app
func deploymentVersions(appCR ketchv1.App) []int {
if len(appCR.Spec.Deployments) == 0 {
return 0
return []int{}
}
return int(appCR.Spec.Deployments[len(appCR.Spec.Deployments)-1].Version)
versions := make([]int, len(appCR.Spec.Deployments))
for i, d := range appCR.Spec.Deployments {
versions[i] = int(d.Version)
}
return versions
}

func (config ChartConfig) render() ([]byte, error) {
Expand Down
6 changes: 3 additions & 3 deletions internal/chart/application_chart_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -394,9 +394,9 @@ func TestNewApplicationChart(t *testing.T) {
actualFilename := filepath.Join(chartDirectory, fmt.Sprintf("%s.output.yaml", tt.wantYamlsFilename))

chartConfig := ChartConfig{
Version: "0.0.1",
AppName: tt.application.Name,
DeploymentVersion: targetDeploymentVersion(*tt.application),
Version: "0.0.1",
AppName: tt.application.Name,
DeploymentVersions: deploymentVersions(*tt.application),
}

client := HelmClient{cfg: &action.Configuration{KubeClient: &fake.PrintingKubeClient{}, Releases: storage.Init(driver.NewMemory())}, namespace: tt.framework.Spec.NamespaceName, c: clientfake.NewClientBuilder().Build()}
Expand Down
10 changes: 5 additions & 5 deletions internal/chart/chartutils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ import (

func TestBufferedFiles(t *testing.T) {
chartConfig := ChartConfig{
Version: "v0.0.1",
Description: "test config",
AppName: "test app",
AppVersion: "v1",
DeploymentVersion: 1,
Version: "v0.0.1",
Description: "test config",
AppName: "test app",
AppVersion: "v1",
DeploymentVersions: []int{1},
}
templates := map[string]string{
"test.yaml": "name: {{ App.spec.name }}",
Expand Down
18 changes: 10 additions & 8 deletions internal/chart/helm_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,11 @@ func (c HelmClient) UpdateChart(tv TemplateValuer, config ChartConfig, opts ...I
clientInstall.ReleaseName = appName
clientInstall.Namespace = c.namespace
clientInstall.PostRenderer = &postRender{
namespace: c.namespace,
cli: c.c,
appName: config.AppName,
deploymentVersion: config.DeploymentVersion,
log: c.log,
cli: c.c,
namespace: c.namespace,
appName: config.AppName,
deploymentVersions: config.DeploymentVersions,
}
for _, opt := range opts {
opt(clientInstall)
Expand All @@ -121,10 +122,11 @@ func (c HelmClient) UpdateChart(tv TemplateValuer, config ChartConfig, opts ...I
// Let's set it to minimal to disable "helm rollback".
updateClient.MaxHistory = 1
updateClient.PostRenderer = &postRender{
namespace: c.namespace,
cli: c.c,
appName: config.AppName,
deploymentVersion: config.DeploymentVersion,
cli: c.c,
log: c.log,
namespace: c.namespace,
appName: config.AppName,
deploymentVersions: config.DeploymentVersions,
}
shouldUpdate, err := c.isHelmChartStatusActionable(c.statusFunc, appName, helmStatusActionMapUpdate)
if err != nil || !shouldUpdate {
Expand Down
10 changes: 5 additions & 5 deletions internal/chart/job_chart.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,11 @@ func NewJobChartConfig(job ketchv1.Job) ChartConfig {
version = job.Spec.Version
}
return ChartConfig{
Version: chartVersion,
Description: job.Spec.Description,
AppName: job.Spec.Name,
AppVersion: version,
DeploymentVersion: int(job.ObjectMeta.Generation),
Version: chartVersion,
Description: job.Spec.Description,
AppName: job.Spec.Name,
AppVersion: version,
DeploymentVersions: []int{int(job.ObjectMeta.Generation)},
}
}

Expand Down
10 changes: 5 additions & 5 deletions internal/chart/job_chart_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,11 @@ func TestNewJobChart(t *testing.T) {

func TestNewJobChartConfig(t *testing.T) {
expected := ChartConfig{
Version: fmt.Sprintf("v0.0.%v", testJob.ObjectMeta.Generation),
Description: testJob.Spec.Description,
AppName: testJob.Spec.Name,
AppVersion: testJob.Spec.Version,
DeploymentVersion: int(testJob.ObjectMeta.Generation),
Version: fmt.Sprintf("v0.0.%v", testJob.ObjectMeta.Generation),
Description: testJob.Spec.Description,
AppName: testJob.Spec.Name,
AppVersion: testJob.Spec.Version,
DeploymentVersions: []int{int(testJob.ObjectMeta.Generation)},
}
chartConfig := NewJobChartConfig(*testJob)
require.Equal(t, expected, chartConfig)
Expand Down
20 changes: 15 additions & 5 deletions internal/chart/post_render.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"strings"

"github.com/go-logr/logr"
"helm.sh/helm/v3/pkg/postrender"
v1 "k8s.io/api/core/v1"
"sigs.k8s.io/controller-runtime/pkg/client"
Expand All @@ -17,10 +18,12 @@ import (
var _ postrender.PostRenderer = &postRender{}

type postRender struct {
cli client.Client
appName string
deploymentVersion int
namespace string
log logr.Logger
cli client.Client

appName string
deploymentVersions []int
namespace string
}

func (p *postRender) Run(renderedManifests *bytes.Buffer) (modifiedManifests *bytes.Buffer, err error) {
Expand All @@ -33,11 +36,18 @@ func (p *postRender) Run(renderedManifests *bytes.Buffer) (modifiedManifests *by
finalBuffer := renderedManifests
for _, cm := range configMapList.Items {
fwPatch := strings.HasSuffix(cm.Name, "-postrender")
appPatch := strings.HasPrefix(cm.Name, fmt.Sprintf("%s-%d-app-post-render", p.appName, p.deploymentVersion))
var appPatch bool
for _, dv := range p.deploymentVersions {
appPatch = strings.HasPrefix(cm.Name, fmt.Sprintf("%s-%d-app-post-render", p.appName, dv))
if appPatch {
break
}
}

if !fwPatch && !appPatch {
continue
}
p.log.Info(fmt.Sprintf("including post renderer patch: appPatch: %t, fwPatch %t, %s ", appPatch, fwPatch, cm.Name))

fs := filesys.MakeFsInMemory()
localPath := p.localPath(fwPatch, p.appName)
Expand Down
3 changes: 2 additions & 1 deletion internal/chart/post_render_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/controller-runtime/pkg/client/fake"
"sigs.k8s.io/controller-runtime/pkg/log"
)

//go:embed testdata/render_yamls/prerendered-manifests.yaml
Expand Down Expand Up @@ -82,8 +83,8 @@ func TestPostRenderRun(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
client := fake.NewClientBuilder()
client.WithObjects(&tt.configmap)

pr := postRender{
log: log.NullLogger{},
namespace: "fake",
cli: client.Build(),
}
Expand Down

0 comments on commit 0ae4ce6

Please sign in to comment.