Skip to content

Commit

Permalink
Write helm midstream with HelmChart namespace
Browse files Browse the repository at this point in the history
  • Loading branch information
emosbaugh committed Dec 14, 2021
1 parent 9350953 commit f4dda2c
Show file tree
Hide file tree
Showing 18 changed files with 221 additions and 10 deletions.
58 changes: 58 additions & 0 deletions pkg/base/base.go
Expand Up @@ -26,12 +26,70 @@ type Base struct {
Bases []Base
}

func (in *Base) DeepCopyInto(out *Base) {
*out = *in
if in.Files != nil {
out.Files = make([]BaseFile, len(in.Files))
for i, file := range in.Files {
out.Files[i] = *file.DeepCopy()
}
}
if in.ErrorFiles != nil {
out.ErrorFiles = make([]BaseFile, len(in.ErrorFiles))
for i, file := range in.ErrorFiles {
out.ErrorFiles[i] = *file.DeepCopy()
}
}
if in.AdditionalFiles != nil {
out.AdditionalFiles = make([]BaseFile, len(in.AdditionalFiles))
for i, file := range in.AdditionalFiles {
out.AdditionalFiles[i] = *file.DeepCopy()
}
}
if in.Bases != nil {
out.Bases = make([]Base, len(in.Bases))
for i, base := range in.Bases {
out.Bases[i] = *base.DeepCopy()
}
}
return
}

func (in *Base) DeepCopy() *Base {
if in == nil {
return nil
}
out := new(Base)
in.DeepCopyInto(out)
return out
}

func (b *Base) SetNamespace(namespace string) {
b.Namespace = namespace
for i := range b.Bases {
b.Bases[i].SetNamespace(namespace)
}
}

type BaseFile struct {
Path string
Content []byte
Error error
}

func (in *BaseFile) DeepCopyInto(out *BaseFile) {
*out = *in
}

func (in *BaseFile) DeepCopy() *BaseFile {
if in == nil {
return nil
}
out := new(BaseFile)
in.DeepCopyInto(out)
return out
}

type OverlySimpleGVK struct {
APIVersion string `yaml:"apiVersion"`
Kind string `yaml:"kind"`
Expand Down
55 changes: 55 additions & 0 deletions pkg/base/base_test.go
Expand Up @@ -498,3 +498,58 @@ func TestBaseFile_IsKotsKind(t *testing.T) {
})
}
}

func TestBaseFile_DeepCopy(t *testing.T) {
var baseFile = &BaseFile{Path: "path", Content: []byte("content")}
baseFile2 := baseFile.DeepCopy()
baseFile2.Path = "change"
baseFile2.Content = []byte("change")

assert.Equal(t, "path", baseFile.Path)
assert.Equal(t, []byte("content"), baseFile.Content)
}

func TestBase_DeepCopy(t *testing.T) {
var subBase = &Base{
Path: "path",
Namespace: "namespace",
Files: []BaseFile{{Path: "path7", Content: []byte("content7")}, {Path: "path10", Content: []byte("content10")}},
ErrorFiles: []BaseFile{{Path: "path8", Content: []byte("content8")}, {Path: "path11", Content: []byte("content11")}},
AdditionalFiles: []BaseFile{{Path: "path9", Content: []byte("content9")}, {Path: "path12", Content: []byte("content12")}},
Bases: []Base{},
}
var base = &Base{
Path: "path",
Namespace: "namespace",
Files: []BaseFile{{Path: "path1", Content: []byte("content1")}, {Path: "path4", Content: []byte("content4")}},
ErrorFiles: []BaseFile{{Path: "path2", Content: []byte("content2")}, {Path: "path5", Content: []byte("content5")}},
AdditionalFiles: []BaseFile{{Path: "path3", Content: []byte("content3")}, {Path: "path6", Content: []byte("content6")}},
Bases: []Base{*subBase},
}

base2 := base.DeepCopy()
base2.Path = "change"
base2.Files = nil
base2.ErrorFiles[0].Path = "change"
base2.ErrorFiles[0].Content = []byte("change")
base2.AdditionalFiles[1] = BaseFile{Path: "change", Content: []byte("change")}
base2.Bases[0].Path = "change"
base2.Bases[0].Files = nil
base2.Bases[0].ErrorFiles[0].Path = "change"
base2.Bases[0].ErrorFiles[0].Content = []byte("change")
base2.Bases[0].AdditionalFiles[1] = BaseFile{Path: "change", Content: []byte("change")}

assert.Equal(t, "path", base.Path)
assert.Equal(t, "namespace", base.Namespace)
assert.Equal(t, []BaseFile{{Path: "path1", Content: []byte("content1")}, {Path: "path4", Content: []byte("content4")}}, base.Files)
assert.Equal(t, []BaseFile{{Path: "path2", Content: []byte("content2")}, {Path: "path5", Content: []byte("content5")}}, base.ErrorFiles)
assert.Equal(t, []BaseFile{{Path: "path3", Content: []byte("content3")}, {Path: "path6", Content: []byte("content6")}}, base.AdditionalFiles)
assert.Equal(t, []Base{{
Path: "path",
Namespace: "namespace",
Files: []BaseFile{{Path: "path7", Content: []byte("content7")}, {Path: "path10", Content: []byte("content10")}},
ErrorFiles: []BaseFile{{Path: "path8", Content: []byte("content8")}, {Path: "path11", Content: []byte("content11")}},
AdditionalFiles: []BaseFile{{Path: "path9", Content: []byte("content9")}, {Path: "path12", Content: []byte("content12")}},
Bases: []Base{},
}}, base.Bases)
}
3 changes: 3 additions & 0 deletions pkg/base/helm.go
Expand Up @@ -99,6 +99,9 @@ func writeHelmBase(chartName string, baseFiles []BaseFile, renderOptions *Render
base := &Base{
Path: path.Join("charts", chartName),
}
if renderOptions.UseHelmInstall {
base.Namespace = renderOptions.Namespace
}
for _, baseFile := range rest {
fileBaseFiles, err := writeHelmBaseFile(baseFile, renderOptions)
if err != nil {
Expand Down
64 changes: 64 additions & 0 deletions pkg/base/helm_test.go
Expand Up @@ -325,6 +325,70 @@ func Test_RenderHelm(t *testing.T) {
},
},
},
{
name: "test subcharts with namespace",
args: args{
upstream: &upstreamtypes.Upstream{
Name: "namespace-test",
Files: []upstreamtypes.UpstreamFile{
{
Path: "Chart.yaml",
Content: []byte("name: test-chart\nversion: 0.1.0"),
},
{
Path: "templates/deploy-1.yaml",
Content: []byte("apiVersion: apps/v1\nkind: Deployment\nmetadata:\n name: deploy-1"),
},
{
Path: "charts/test-subchart/Chart.yaml",
Content: []byte("name: test-subchart\nversion: 0.2.0"),
},
{
Path: "charts/test-subchart/templates/deploy-2.yaml",
Content: []byte("apiVersion: apps/v1\nkind: Deployment\nmetadata:\n name: deploy-2"),
},
},
},
renderOptions: &RenderOptions{
HelmVersion: "v3",
Namespace: "test-namespace",
UseHelmInstall: true,
},
},
want: &Base{
Namespace: "test-namespace",
Files: []BaseFile{
{
Path: "templates/deploy-1.yaml",
Content: []byte("apiVersion: apps/v1\nkind: Deployment\nmetadata:\n name: deploy-1\n namespace: test-namespace"),
},
},
AdditionalFiles: []BaseFile{
{
Path: "Chart.yaml",
Content: []byte("name: test-chart\nversion: 0.1.0"),
},
},
Bases: []Base{
{
Namespace: "test-namespace",
Path: "charts/test-subchart",
Files: []BaseFile{
{
Path: "templates/deploy-2.yaml",
Content: []byte("apiVersion: apps/v1\nkind: Deployment\nmetadata:\n name: deploy-2\n namespace: test-namespace"),
},
},
AdditionalFiles: []BaseFile{
{
Path: "Chart.yaml",
Content: []byte("name: test-subchart\nversion: 0.2.0"),
},
},
},
},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down
1 change: 1 addition & 0 deletions pkg/base/replicated.go
Expand Up @@ -284,6 +284,7 @@ func renderReplicatedHelmBase(u *upstreamtypes.Upstream, renderOptions *RenderOp

return &Base{
Path: helmBase.Path,
Namespace: helmBase.Namespace,
Files: helmBaseFiles,
AdditionalFiles: helmBase.AdditionalFiles,
Bases: helmBaseBases,
Expand Down
14 changes: 10 additions & 4 deletions pkg/pull/pull.go
Expand Up @@ -369,15 +369,18 @@ func Pull(upstreamURI string, pullOptions PullOptions) (string, error) {
}

for _, helmBase := range helmBases {
helmBaseCopy := helmBase.DeepCopy()
// strip namespace. helm render takes care of injecting the namespace
helmBaseCopy.SetNamespace("")
writeBaseOptions := base.WriteOptions{
BaseDir: u.GetBaseDir(writeUpstreamOptions),
SkippedDir: u.GetSkippedDir(writeUpstreamOptions),
Overwrite: true,
ExcludeKotsKinds: pullOptions.ExcludeKotsKinds,
IsHelmBase: true,
}
if err := helmBase.WriteBase(writeBaseOptions); err != nil {
return "", errors.Wrapf(err, "failed to write helm base %s", helmBase.Path)
if err := helmBaseCopy.WriteBase(writeBaseOptions); err != nil {
return "", errors.Wrapf(err, "failed to write helm base %s", helmBaseCopy.Path)
}
}

Expand Down Expand Up @@ -460,9 +463,12 @@ func Pull(upstreamURI string, pullOptions PullOptions) (string, error) {
writeMidstreamOptions.MidstreamDir = filepath.Join(helmBase.GetOverlaysDir(writeBaseOptions), "midstream", helmBase.Path)
writeMidstreamOptions.BaseDir = filepath.Join(u.GetBaseDir(writeUpstreamOptions), helmBase.Path)

helmBaseCopy := helmBase
helmBaseCopy := helmBase.DeepCopy()

helmMidstream, err := writeMidstream(writeMidstreamOptions, pullOptions, u, &helmBaseCopy, fetchOptions.License, identityConfig, u.GetUpstreamDir(writeUpstreamOptions), log)
pullOptionsCopy := pullOptions
pullOptionsCopy.Namespace = helmBaseCopy.Namespace

helmMidstream, err := writeMidstream(writeMidstreamOptions, pullOptionsCopy, u, helmBaseCopy, fetchOptions.License, identityConfig, u.GetUpstreamDir(writeUpstreamOptions), log)
if err != nil {
return "", errors.Wrapf(err, "failed to write helm midstream %s", helmBase.Path)
}
Expand Down
14 changes: 10 additions & 4 deletions pkg/rewrite/rewrite.go
Expand Up @@ -178,15 +178,18 @@ func Rewrite(rewriteOptions RewriteOptions) error {
}

for _, helmBase := range helmBases {
helmBaseCopy := helmBase.DeepCopy()
// strip namespace. helm render takes care of injecting the namespace
helmBaseCopy.SetNamespace("")
writeBaseOptions := base.WriteOptions{
BaseDir: u.GetBaseDir(writeUpstreamOptions),
SkippedDir: u.GetSkippedDir(writeUpstreamOptions),
Overwrite: true,
ExcludeKotsKinds: rewriteOptions.ExcludeKotsKinds,
IsHelmBase: true,
}
if err := helmBase.WriteBase(writeBaseOptions); err != nil {
return errors.Wrapf(err, "failed to write helm base %s", helmBase.Path)
if err := helmBaseCopy.WriteBase(writeBaseOptions); err != nil {
return errors.Wrapf(err, "failed to write helm base %s", helmBaseCopy.Path)
}
}

Expand Down Expand Up @@ -270,9 +273,12 @@ func Rewrite(rewriteOptions RewriteOptions) error {
writeMidstreamOptions.MidstreamDir = filepath.Join(helmBase.GetOverlaysDir(writeBaseOptions), "midstream", helmBase.Path)
writeMidstreamOptions.BaseDir = filepath.Join(u.GetBaseDir(writeUpstreamOptions), helmBase.Path)

helmBaseCopy := helmBase
helmBaseCopy := helmBase.DeepCopy()

helmMidstream, err := writeMidstream(writeMidstreamOptions, rewriteOptions, &helmBaseCopy, fetchOptions.License, u.GetUpstreamDir(writeUpstreamOptions), log)
rewriteOptionsCopy := rewriteOptions
rewriteOptionsCopy.K8sNamespace = helmBaseCopy.Namespace

helmMidstream, err := writeMidstream(writeMidstreamOptions, rewriteOptionsCopy, helmBaseCopy, fetchOptions.License, u.GetUpstreamDir(writeUpstreamOptions), log)
if err != nil {
return errors.Wrapf(err, "failed to write helm midstream %s", helmBase.Path)
}
Expand Down
1 change: 1 addition & 0 deletions pkg/tests/pull/cases/configcontext/testcase.yaml
@@ -1,5 +1,6 @@
Name: test helm pull
PullOptions:
Namespace: app-namespace
ExcludeAdminConsole: true
IsAirgap: true
Silent: true
Expand Down
Expand Up @@ -8,6 +8,7 @@ metadata:
helm.sh/hook-weight: "-9999"
creationTimestamp: null
name: my-app-test-chart-registry
namespace: helmns
type: kubernetes.io/dockerconfigjson

---
Expand All @@ -21,4 +22,5 @@ metadata:
helm.sh/hook-weight: "-9999"
creationTimestamp: null
name: kotsadm-replicated-registry
namespace: helmns
type: kubernetes.io/dockerconfigjson
Expand Up @@ -8,6 +8,7 @@ metadata:
helm.sh/hook-weight: "-9999"
creationTimestamp: null
name: my-app-registry
namespace: app-namespace
type: kubernetes.io/dockerconfigjson

---
Expand All @@ -21,4 +22,5 @@ metadata:
helm.sh/hook-weight: "-9999"
creationTimestamp: null
name: kotsadm-replicated-registry
namespace: app-namespace
type: kubernetes.io/dockerconfigjson
1 change: 1 addition & 0 deletions pkg/tests/pull/cases/simple/testcase.yaml
@@ -1,5 +1,6 @@
Name: test simple pull
PullOptions:
Namespace: app-namespace
ExcludeAdminConsole: true
IsAirgap: true
Silent: true
Expand Down
1 change: 1 addition & 0 deletions pkg/tests/pull/cases/subcharts/testcase.yaml
@@ -1,5 +1,6 @@
Name: test subcharts pull
PullOptions:
Namespace: app-namespace
ExcludeAdminConsole: true
IsAirgap: true
Silent: true
Expand Down
Expand Up @@ -8,6 +8,7 @@ metadata:
helm.sh/hook-weight: "-9999"
creationTimestamp: null
name: my-app-registry
namespace: helmns
type: kubernetes.io/dockerconfigjson

---
Expand All @@ -21,4 +22,5 @@ metadata:
helm.sh/hook-weight: "-9999"
creationTimestamp: null
name: kotsadm-replicated-registry
namespace: helmns
type: kubernetes.io/dockerconfigjson
Expand Up @@ -8,6 +8,7 @@ metadata:
helm.sh/hook-weight: "-9999"
creationTimestamp: null
name: my-app-registry
namespace: helmns
type: kubernetes.io/dockerconfigjson

---
Expand All @@ -21,4 +22,5 @@ metadata:
helm.sh/hook-weight: "-9999"
creationTimestamp: null
name: kotsadm-replicated-registry
namespace: helmns
type: kubernetes.io/dockerconfigjson
Expand Up @@ -8,6 +8,7 @@ metadata:
helm.sh/hook-weight: "-9999"
creationTimestamp: null
name: my-app-registry
namespace: helmns
type: kubernetes.io/dockerconfigjson

---
Expand All @@ -21,4 +22,5 @@ metadata:
helm.sh/hook-weight: "-9999"
creationTimestamp: null
name: kotsadm-replicated-registry
namespace: helmns
type: kubernetes.io/dockerconfigjson
Expand Up @@ -8,6 +8,7 @@ metadata:
helm.sh/hook-weight: "-9999"
creationTimestamp: null
name: my-app-chart-registry
namespace: helmns
type: kubernetes.io/dockerconfigjson

---
Expand All @@ -21,4 +22,5 @@ metadata:
helm.sh/hook-weight: "-9999"
creationTimestamp: null
name: kotsadm-replicated-registry
namespace: helmns
type: kubernetes.io/dockerconfigjson

0 comments on commit f4dda2c

Please sign in to comment.