Skip to content
This repository has been archived by the owner on Mar 24, 2023. It is now read-only.

Commit

Permalink
Support templates in kustomize base path attribute
Browse files Browse the repository at this point in the history
  • Loading branch information
marccampbell committed Jan 26, 2019
1 parent fadb947 commit f4dad1d
Show file tree
Hide file tree
Showing 2 changed files with 149 additions and 9 deletions.
26 changes: 17 additions & 9 deletions pkg/lifecycle/daemon/routes_navcycle_getstep.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,6 @@ import (
)

func (d *NavcycleRoutes) getStep(c *gin.Context) {
debug := level.Debug(log.With(d.Logger, "handler", "getStep"))
debug.Log()

requestedStep := c.Param("step")

for _, step := range d.Release.Spec.Lifecycle.V1 {
Expand Down Expand Up @@ -48,7 +45,23 @@ func (d *NavcycleRoutes) getStep(c *gin.Context) {
func (d *NavcycleRoutes) hydrateStep(step daemontypes.Step) (*daemontypes.StepResponse, error) {
debug := level.Debug(log.With(d.Logger, "method", "hydrateStep"))

currentState, err := d.StateManager.TryLoad()
if err != nil {
level.Error(d.Logger).Log("event", "tryLoad.fail", "err", err)
return nil, errors.Wrap(err, "load state")
}

if step.Kustomize != nil {
builder, err := d.BuilderBuilder.FullBuilder(d.Release.Metadata, d.Release.Spec.Config.V1, currentState.CurrentConfig())
if err != nil {
return nil, errors.Wrap(err, "create kustomize template builder")
}
renderedBasePath, err := builder.String(step.Kustomize.BasePath)
if err != nil {
return nil, errors.Wrap(err, "render kusstomize basepath contents")
}
step.Kustomize.BasePath = renderedBasePath

tree, err := d.TreeLoader.LoadTree(step.Kustomize.BasePath)
if err != nil {
level.Error(d.Logger).Log("event", "loadTree.fail", "err", err)
Expand All @@ -57,12 +70,6 @@ func (d *NavcycleRoutes) hydrateStep(step daemontypes.Step) (*daemontypes.StepRe
step.Kustomize.Tree = *tree
}

currentState, err := d.StateManager.TryLoad()
if err != nil {
level.Error(d.Logger).Log("event", "tryLoad,fail", "err", err)
return nil, errors.Wrap(err, "load state")
}

if step.HelmValues != nil {
userValues := currentState.CurrentHelmValues()
defaultValues := currentState.CurrentHelmValuesDefaults()
Expand All @@ -85,6 +92,7 @@ func (d *NavcycleRoutes) hydrateStep(step daemontypes.Step) (*daemontypes.StepRe
step.HelmValues.ReleaseName = releaseName
step.HelmValues.Namespace = namespace
}

if step.Message != nil {
builder, err := d.BuilderBuilder.FullBuilder(d.Release.Metadata, d.Release.Spec.Config.V1, currentState.CurrentConfig())
if err != nil {
Expand Down
132 changes: 132 additions & 0 deletions pkg/lifecycle/daemon/routes_navcycle_getstep_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/golang/mock/gomock"
"github.com/replicatedhq/libyaml"
"github.com/replicatedhq/ship/pkg/api"
"github.com/replicatedhq/ship/pkg/filetree"
"github.com/replicatedhq/ship/pkg/lifecycle/daemon/daemontypes"
state2 "github.com/replicatedhq/ship/pkg/state"
"github.com/replicatedhq/ship/pkg/templates"
Expand Down Expand Up @@ -507,3 +508,134 @@ func TestHydrateStep(t *testing.T) {
})
}
}

func TestHydrateTemplatedKustomizeStep(t *testing.T) {
tests := []struct {
name string
step daemontypes.Step
state state2.State
fs map[string]string
release *api.Release
want *daemontypes.StepResponse
}{
{
name: "kustomize",
step: daemontypes.NewStep(api.Step{
Kustomize: &api.Kustomize{
Base: "{{repl ToUpper \"abcdef\"}}",
Dest: "more.yaml",
Overlay: "overlay/mine",
StepShared: api.StepShared{
ID: "bar",
},
},
}),
release: &api.Release{
Metadata: api.ReleaseMetadata{
CustomerID: "12345",
},
Spec: api.Spec{
Config: api.Config{
V1: []libyaml.ConfigGroup{
{
Items: []*libyaml.ConfigItem{
{
Name: "spam",
Value: "eggs",
},
},
},
},
},
},
},
state: state2.V0{},
want: &daemontypes.StepResponse{
CurrentStep: daemontypes.Step{
Source: api.Step{
Kustomize: &api.Kustomize{
Base: "{{repl ToUpper \"abcdef\"}}",
Dest: "more.yaml",
Overlay: "overlay/mine",
StepShared: api.StepShared{
ID: "bar",
},
},
},
Kustomize: &daemontypes.Kustomize{
BasePath: "ABCDEF",
Tree: filetree.Node{
Children: []filetree.Node{
filetree.Node{
Children: []filetree.Node{},
Name: "/",
Path: "/",
HasOverlay: false,
IsSupported: false,
IsExcluded: false,
},
},
Name: "/",
Path: "/",
HasOverlay: false,
IsSupported: false,
IsExcluded: false,
},
},
},
Phase: "kustomize",
Actions: []daemontypes.Action{
{
ButtonType: "primary",
Text: "Finalize Overlays",
LoadingText: "Finalizing Overlays",
OnClick: daemontypes.ActionRequest{
URI: "/navcycle/step/bar",
Method: "POST",
Body: "",
},
},
},
},
},
}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
req := require.New(t)

mc := gomock.NewController(t)
testLogger := &logger.TestLogger{T: t}
progressmap := &daemontypes.ProgressMap{}
mockFs := afero.Afero{Fs: afero.NewMemMapFs()}
mockState := state.NewMockManager(mc)
if test.state != nil {
mockState.EXPECT().TryLoad().Return(test.state, nil)
mockState.EXPECT().TryLoad().Return(test.state, nil)
}

treeLoader := filetree.NewLoader(mockFs, testLogger, mockState)

v2 := &NavcycleRoutes{
BuilderBuilder: templates.NewBuilderBuilder(
testLogger,
viper.New(),
mockState,
),
Logger: testLogger,
StepProgress: progressmap,
Fs: mockFs,
StateManager: mockState,
Release: test.release,
TreeLoader: treeLoader,
}

err := mockFs.MkdirAll("ABCDEF", 0755)
req.NoError(err, "mkdir")

response, err := v2.hydrateStep(test.step)
req.NoError(err, "hydrate templated kustomize step")
req.Equal(test.want, response)
})
}
}

0 comments on commit f4dad1d

Please sign in to comment.