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

Update directory structure for ship init command #238

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions pkg/constants/filepaths.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,14 @@ const OverlaysPrefixPath = "overlays/ship"
// StatePath is the default state file path
const StatePath = ".ship/state.json"

// KustomizeHelmPath is the path used to store helm chart contents
const KustomizeHelmPath = ".ship/kustomize/chart"
// KustomizeHelmPath is the path used to store Helm chart contents
const KustomizeHelmPath = "chart"

// RenderedHelmTempPath is the path where the `helm template` command writes to
const RenderedHelmTempPath = ".ship/tmp-rendered"

// RenderedHelmPath is the path where rendered Helm charts are written to
const RenderedHelmPath = "base"

// TempHelmValuesPath is the folder path used to store the updated values.yaml
const TempHelmValuesPath = ".ship/kustomize/tmp"
const TempHelmValuesPath = "chart/tmp"
11 changes: 11 additions & 0 deletions pkg/lifecycle/daemon/routes_kustomize.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,17 @@ func (d *ShipDaemon) kustomizeSaveOverlay(c *gin.Context) {
return
}

if request.Path == "" {
c.JSON(
400,
map[string]string{
"error": "bad_request",
"detail": "path cannot be empty",
},
)
return
}

debug.Log("event", "request.bind")
currentState, err := d.StateManager.TryLoad()
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion pkg/lifecycle/kustomize/kustomizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ func (l *kustomizer) writeOutOverlays(
}
debug.Log("event", "mkdir", "dir", destDir)

// write the overlay patches, updating kustomizeation.yaml's patch list
// write the overlay patches, updating kustomization.yaml's patch list
for file, contents := range shipOverlay.Patches {
name := path.Join(destDir, file)
err = l.writePatch(name, destDir, contents)
Expand Down
58 changes: 49 additions & 9 deletions pkg/lifecycle/render/helm/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@ package helm
import (
"fmt"
"os/exec"
"path"
"strings"

"github.com/replicatedhq/ship/pkg/constants"

"io/ioutil"

"regexp"
Expand Down Expand Up @@ -48,13 +51,23 @@ func (f *ForkTemplater) Template(
configGroups []libyaml.ConfigGroup,
templateContext map[string]interface{},
) error {
debug := level.Debug(log.With(f.Logger, "step.type", "render", "render.phase", "execute", "asset.type", "helm", "dest", asset.Dest, "description", asset.Description))

debug.Log("event", "mkdirall.attempt", "dest", asset.Dest, "basePath", asset.Dest)
if err := f.FS.MkdirAll(asset.Dest, 0755); err != nil {
debug.Log("event", "mkdirall.fail", "err", err, "basePath", asset.Dest)
return errors.Wrapf(err, "write directory to %s", asset.Dest)
debug := level.Debug(
log.With(
f.Logger,
"step.type", "render",
"render.phase", "execute",
"asset.type", "helm",
"chartRoot", chartRoot,
"dest", asset.Dest,
"description", asset.Description,
),
)
debug.Log("event", "mkdirall.attempt", "helmtempdir", constants.RenderedHelmTempPath, "dest", asset.Dest)
if err := f.FS.MkdirAll(constants.RenderedHelmTempPath, 0755); err != nil {
debug.Log("event", "mkdirall.fail", "err", err, "helmtempdir", constants.RenderedHelmTempPath)
return errors.Wrapf(err, "write tmp directory to %s", constants.RenderedHelmTempPath)
}
defer f.FS.RemoveAll(constants.RenderedHelmTempPath)

releaseName := strings.ToLower(fmt.Sprintf("%s", meta.ChannelName))
releaseName = releaseNameRegex.ReplaceAllLiteralString(releaseName, "-")
Expand All @@ -65,7 +78,7 @@ func (f *ForkTemplater) Template(
cmd.Args = append(
cmd.Args,
"template", chartRoot,
"--output-dir", asset.Dest,
"--output-dir", constants.RenderedHelmTempPath,
"--name", releaseName,
)

Expand Down Expand Up @@ -98,6 +111,33 @@ func (f *ForkTemplater) Template(
return errors.Wrap(err, "execute helm")
}

subChartsDirName := "charts"
tempRenderedChartDir := path.Join(constants.RenderedHelmTempPath, meta.HelmChartMetadata.Name)
tempRenderedChartTemplatesDir := path.Join(tempRenderedChartDir, "templates")
tempRenderedSubChartsDir := path.Join(tempRenderedChartDir, subChartsDirName)

debug.Log("event", "rename")
if templatesDirExists, err := f.FS.IsDir(tempRenderedChartTemplatesDir); err == nil && templatesDirExists {
if err := f.FS.Rename(tempRenderedChartTemplatesDir, asset.Dest); err != nil {
return errors.Wrap(err, "failed to rename templates dir")
}
} else {
debug.Log("event", "rename", "folder", tempRenderedChartTemplatesDir, "message", "Folder does not exist")
}

if subChartsExist, err := f.FS.IsDir(tempRenderedSubChartsDir); err == nil && subChartsExist {
if err := f.FS.Rename(tempRenderedSubChartsDir, path.Join(asset.Dest, subChartsDirName)); err != nil {
return errors.Wrap(err, "failed to rename subcharts dir")
}
} else {
debug.Log("event", "rename", "folder", tempRenderedSubChartsDir, "message", "Folder does not exist")
}

debug.Log("event", "temphelmvalues.remove", "path", constants.TempHelmValuesPath)
if err := f.FS.RemoveAll(constants.TempHelmValuesPath); err != nil {
return errors.Wrap(err, "failed to remove Helm values tmp dir")
}

// todo link up stdout/stderr debug logs
return nil
}
Expand Down Expand Up @@ -202,7 +242,7 @@ func (f *ForkTemplater) helmDependencyUpdate(chartRoot string) error {
chartRoot,
)

debug.Log("event", "helm.update", "args", cmd.Args)
debug.Log("event", "helm.update", "args", fmt.Sprintf("%v", cmd.Args))

stdout, stderr, err := f.fork(cmd)

Expand All @@ -225,7 +265,7 @@ func (f *ForkTemplater) helmInitClient(chartRoot string) error {
"--client-only",
)

debug.Log("event", "helm.initClient", "args", cmd.Args)
debug.Log("event", "helm.initClient", "args", fmt.Sprintf("%v", cmd.Args))

stdout, stderr, err := f.fork(cmd)

Expand Down
16 changes: 10 additions & 6 deletions pkg/lifecycle/render/helm/template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (

"github.com/replicatedhq/libyaml"
"github.com/replicatedhq/ship/pkg/api"
"github.com/replicatedhq/ship/pkg/constants"
"github.com/replicatedhq/ship/pkg/templates"
"github.com/replicatedhq/ship/pkg/testing/logger"
"github.com/spf13/afero"
Expand Down Expand Up @@ -46,7 +47,10 @@ func TestForkTemplater(t *testing.T) {
// this is janky, but works for our purposes, use pipe | for separator, since its unlikely to be in argv
"EXPECT_HELM_ARGV=--foo|bar|--output-dir|fake",
},
expectError: "execute helm: exit status 2: stdout: \"\"; stderr: \"expected args [--foo bar --output-dir fake], got args [template /tmp/chartroot --output-dir k8s/ --name frobnitz]; FAIL\";",
expectError: fmt.Sprintf(
"execute helm: exit status 2: stdout: \"\"; stderr: \"expected args [--foo bar --output-dir fake], got args [template /tmp/chartroot --output-dir %s --name frobnitz]; FAIL\";",
constants.RenderedHelmTempPath,
),
},
{
name: "helm test proper args",
Expand All @@ -56,7 +60,7 @@ func TestForkTemplater(t *testing.T) {
"EXPECT_HELM_ARGV=" +
"template|" +
"/tmp/chartroot|" +
"--output-dir|k8s/|" +
"--output-dir|" + constants.RenderedHelmTempPath + "|" +
"--name|frobnitz",
},
expectError: "",
Expand All @@ -69,7 +73,7 @@ func TestForkTemplater(t *testing.T) {
"EXPECT_HELM_ARGV=" +
"template|" +
"/tmp/chartroot|" +
"--output-dir|k8s/|" +
"--output-dir|" + constants.RenderedHelmTempPath + "|" +
"--name|frobnitz|" +
"--set|service.clusterIP=10.3.9.2",
},
Expand All @@ -84,7 +88,7 @@ func TestForkTemplater(t *testing.T) {
"EXPECT_HELM_ARGV=" +
"template|" +
"/tmp/chartroot|" +
"--output-dir|k8s/|" +
"--output-dir|" + constants.RenderedHelmTempPath + "|" +
"--name|frobnitz|" +
"--set|service.clusterIP=10.3.9.2",
},
Expand All @@ -100,7 +104,7 @@ func TestForkTemplater(t *testing.T) {
"EXPECT_HELM_ARGV=" +
"template|" +
"/tmp/chartroot|" +
"--output-dir|k8s/|" +
"--output-dir|" + constants.RenderedHelmTempPath + "|" +
"--name|1-2-3---------frobnitz|" +
"--set|service.clusterIP=10.3.9.2",
},
Expand All @@ -117,7 +121,7 @@ func TestForkTemplater(t *testing.T) {
"EXPECT_HELM_ARGV=" +
"template|" +
"/tmp/chartroot|" +
"--output-dir|k8s/|" +
"--output-dir|" + constants.RenderedHelmTempPath + "|" +
"--name|1-2-3---------frobnitz|" +
"--set|service.clusterIP=10.3.9.2",
},
Expand Down
6 changes: 5 additions & 1 deletion pkg/lifecycle/render/planner/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,11 @@ func (p *CLIPlanner) Build(assets []api.Asset, configGroups []libyaml.ConfigGrou
plan = append(plan, p.dockerStep(*asset.Docker, meta, templateContext, configGroups))
}
} else if asset.Helm != nil {
asset.Helm.Dest = filepath.Join(constants.InstallerPrefixPath, asset.Helm.Dest)
// For now, ignore destination reassign if Helm has LocalOpts
// This is the codepath for `ship init`
if asset.Helm.Local == nil {
asset.Helm.Dest = filepath.Join(constants.InstallerPrefixPath, asset.Helm.Dest)
}
evaluatedWhen, err := p.evalAssetWhen(debug, builder, asset, asset.Helm.AssetShared.When)
if err != nil {
return nil, err
Expand Down
4 changes: 2 additions & 2 deletions pkg/ship/kustomize.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ func (s *Ship) buildRelease(helmChartMetadata api.HelmChartMetadata) *api.Releas
{
Helm: &api.HelmAsset{
AssetShared: api.AssetShared{
Dest: ".",
Dest: constants.RenderedHelmPath,
},
Local: &api.LocalHelmOpts{
ChartRoot: constants.KustomizeHelmPath,
Expand All @@ -183,7 +183,7 @@ func (s *Ship) buildRelease(helmChartMetadata api.HelmChartMetadata) *api.Releas
},
{
Kustomize: &api.Kustomize{
BasePath: path.Join(constants.InstallerPrefixPath, helmChartMetadata.Name),
BasePath: constants.RenderedHelmPath,
Dest: path.Join("overlays", "ship"),
},
},
Expand Down
3 changes: 2 additions & 1 deletion pkg/state/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package state

import (
"encoding/json"
"fmt"
"os"
"path/filepath"

Expand Down Expand Up @@ -100,7 +101,7 @@ func (s *MManager) TryLoad() (State, error) {
return nil, errors.Wrap(err, "unmarshal state")
}

level.Debug(s.Logger).Log("event", "state.unmarshal", "type", "versioned", "value", state)
level.Debug(s.Logger).Log("event", "state.unmarshal", "type", "versioned", "value", fmt.Sprintf("%+v", state))

if state.V1 != nil {
level.Debug(s.Logger).Log("event", "state.resolve", "type", "versioned")
Expand Down