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

Commit

Permalink
Merge pull request #1049 from laverya/persist-custom-json6902-patches
Browse files Browse the repository at this point in the history
persist custom json6902 patches in overlays dir
  • Loading branch information
laverya authored Aug 23, 2019
2 parents 44a7223 + 284b0a2 commit a0af920
Show file tree
Hide file tree
Showing 12 changed files with 86 additions and 21 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[{"op":"replace","path":"/spec/template/spec/containers/0/name","value":"replacedSlave"}]
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,12 @@ apiversion: ""
patchesStrategicMerge:
- redis-master-service.yaml
- redis-slave-service.yaml
patchesJson6902:
- target:
group: apps
version: v1
kind: Deployment
name: redis-slave
path: container-name-patch.json
bases:
- ../defaults
2 changes: 1 addition & 1 deletion integration/update/k8s-new-files/expected/rendered.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ spec:
- name: GET_HOSTS_FROM
value: dns
image: gcr.io/google_samples/gb-redisslave:v1
name: slave
name: replacedSlave
ports:
- containerPort: 6379
resources:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[{"op":"replace","path":"/spec/template/spec/containers/0/name","value":"replacedSlave"}]
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,10 @@ patchesStrategicMerge:
- redis-slave-service.yaml
bases:
- ../defaults
patchesJson6902:
- target:
group: apps
version: v1
kind: Deployment
name: redis-slave
path: container-name-patch.json
4 changes: 2 additions & 2 deletions pkg/lifecycle/daemon/routes_navcycle_kustomize_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ func TestV2KustomizeSaveFile(t *testing.T) {
}
return true
},
Describe: fmt.Sprintf("expect state equal to %s", test.ExpectState),
Describe: fmt.Sprintf("expect state equal to %+v", test.ExpectState),
}).Return(nil).AnyTimes()

err := v2.kustomizeDoSaveOverlay(test.Body)
Expand Down Expand Up @@ -286,7 +286,7 @@ func TestV2KustomizeDeleteFile(t *testing.T) {
}
return true
},
Describe: fmt.Sprintf("expect state equal to %s", test.ExpectState),
Describe: fmt.Sprintf("expect state equal to %+v", test.ExpectState),
}).Return(nil).AnyTimes()

err := v2.deleteFile(test.DeleteFileParams.pathQueryParam, test.DeleteFileParams.getFiles)
Expand Down
2 changes: 1 addition & 1 deletion pkg/lifecycle/kustomize/daemonless.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ func (l *Kustomizer) Execute(ctx context.Context, release *api.Release, step api
return err
}

err = l.writeOverlay(step, relativePatchPaths, relativeResourcePaths)
err = l.writeOverlay(step, relativePatchPaths, relativeResourcePaths, shipOverlay.RawKustomize)
if err != nil {
return errors.Wrap(err, "write overlay")
}
Expand Down
11 changes: 4 additions & 7 deletions pkg/lifecycle/kustomize/kustomizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,19 +174,16 @@ func (l *Kustomizer) writeOverlay(
step api.Kustomize,
relativePatchPaths []patch.StrategicMerge,
relativeResourcePaths []string,
kustomization ktypes.Kustomization,
) error {
// just always make a new kustomization.yaml for now
basePath, err := filepath.Rel(step.OverlayPath(), constants.DefaultOverlaysPath)
if err != nil {
return err
}
kustomization := ktypes.Kustomization{
Bases: []string{
basePath,
},
PatchesStrategicMerge: relativePatchPaths,
Resources: relativeResourcePaths,
}
kustomization.Bases = []string{basePath}
kustomization.PatchesStrategicMerge = relativePatchPaths
kustomization.Resources = relativeResourcePaths

marshalled, err := util.MarshalIndent(2, kustomization)
if err != nil {
Expand Down
46 changes: 41 additions & 5 deletions pkg/lifecycle/kustomize/kustomizer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ import (
"github.com/golang/mock/gomock"
"github.com/spf13/afero"
"github.com/stretchr/testify/require"
"sigs.k8s.io/kustomize/pkg/gvk"
"sigs.k8s.io/kustomize/pkg/patch"
"sigs.k8s.io/kustomize/pkg/types"

"github.com/replicatedhq/ship/pkg/api"
"github.com/replicatedhq/ship/pkg/constants"
Expand Down Expand Up @@ -123,10 +125,11 @@ func Test_kustomizer_writeOverlay(t *testing.T) {
}

tests := []struct {
name string
relativePatchPaths []patch.StrategicMerge
expectFile string
wantErr bool
name string
relativePatchPaths []patch.StrategicMerge
existingKustomization types.Kustomization
expectFile string
wantErr bool
}{
{
name: "No patches",
Expand All @@ -148,6 +151,39 @@ patchesStrategicMerge:
- c.yaml
bases:
- ../defaults
`,
},
{
name: "No patches but existing kustomization",
relativePatchPaths: []patch.StrategicMerge{},
existingKustomization: types.Kustomization{
PatchesJson6902: []patch.Json6902{
{
Path: "abc.json",
Target: &patch.Target{
Gvk: gvk.Gvk{
Group: "groupa",
Version: "versionb",
Kind: "kindc",
},
Namespace: "nsd",
Name: "namee",
},
},
},
},
expectFile: `kind: ""
apiversion: ""
patchesJson6902:
- target:
group: groupa
version: versionb
kind: kindc
namespace: nsd
name: namee
path: abc.json
bases:
- ../defaults
`,
},
}
Expand All @@ -168,7 +204,7 @@ bases:
},
Daemon: mockDaemon,
}
if err := l.writeOverlay(mockStep, tt.relativePatchPaths, nil); (err != nil) != tt.wantErr {
if err := l.writeOverlay(mockStep, tt.relativePatchPaths, nil, tt.existingKustomization); (err != nil) != tt.wantErr {
t.Errorf("kustomizer.writeOverlay() error = %v, wantErr %v", err, tt.wantErr)
}

Expand Down
4 changes: 2 additions & 2 deletions pkg/lifecycle/kustomize/pre_kustomize.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,8 +172,9 @@ func (l *Kustomizer) resolveExistingKustomize(ctx context.Context, overlayDir st
currentKustomize.Overlays = map[string]state.Overlay{}
}
currentOverlay := currentKustomize.Ship()
fsResources := make(map[string]string)
currentOverlay.RawKustomize = kustomization

fsResources := make(map[string]string)
for _, kustomizeResource := range kustomization.Resources {
// read resource referred to by kustomize yaml
kustomizeResourceContents, err := l.FS.ReadFile(filepath.Join(overlayDir, kustomizeResource))
Expand All @@ -186,7 +187,6 @@ func (l *Kustomizer) resolveExistingKustomize(ctx context.Context, overlayDir st
currentOverlay.Resources = fsResources

fsPatches := make(map[string]string)

for _, kustomizePatch := range kustomization.PatchesStrategicMerge {
// read resource referred to by kustomize yaml
kustomizePatchContents, err := l.FS.ReadFile(filepath.Join(overlayDir, string(kustomizePatch)))
Expand Down
14 changes: 14 additions & 0 deletions pkg/lifecycle/kustomize/pre_kustomize_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import (
"github.com/spf13/afero"
"github.com/spf13/viper"
"github.com/stretchr/testify/require"
"sigs.k8s.io/kustomize/pkg/patch"
"sigs.k8s.io/kustomize/pkg/types"
)

type testFile struct {
Expand Down Expand Up @@ -430,6 +432,11 @@ patchesStrategicMerge:
Patches: map[string]string{"/mypatch.yaml": "this is my patch"},
Resources: map[string]string{"/myresource.yaml": "this is my resource"},
ExcludedBases: []string{"excludedBase"},
RawKustomize: types.Kustomization{
PatchesStrategicMerge: []patch.StrategicMerge{"mypatch.yaml"},
Resources: []string{"myresource.yaml"},
Bases: []string{"../abc"},
},
},
},
},
Expand Down Expand Up @@ -468,6 +475,13 @@ resources:
"/myotherresource.yaml": "this is my other resource",
},
ExcludedBases: []string{},
RawKustomize: types.Kustomization{
Resources: []string{
"myresource.yaml",
"myotherresource.yaml",
},
Bases: []string{"../abc"},
},
},
},
},
Expand Down
8 changes: 5 additions & 3 deletions pkg/state/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"time"

"github.com/hashicorp/terraform/terraform"
"sigs.k8s.io/kustomize/pkg/types"

"github.com/replicatedhq/ship/pkg/api"
"github.com/replicatedhq/ship/pkg/util"
Expand Down Expand Up @@ -107,9 +108,10 @@ func (l *Lifeycle) WithCompletedStep(step api.Step) *Lifeycle {
}

type Overlay struct {
ExcludedBases []string `json:"excludedBases,omitempty" yaml:"excludedBases,omitempty" hcl:"excludedBases,omitempty"`
Patches map[string]string `json:"patches,omitempty" yaml:"patches,omitempty" hcl:"patches,omitempty"`
Resources map[string]string `json:"resources,omitempty" yaml:"resources,omitempty" hcl:"resources,omitempty"`
ExcludedBases []string `json:"excludedBases,omitempty" yaml:"excludedBases,omitempty" hcl:"excludedBases,omitempty"`
Patches map[string]string `json:"patches,omitempty" yaml:"patches,omitempty" hcl:"patches,omitempty"`
Resources map[string]string `json:"resources,omitempty" yaml:"resources,omitempty" hcl:"resources,omitempty"`
RawKustomize types.Kustomization `json:"-"`
}

func NewOverlay() Overlay {
Expand Down

0 comments on commit a0af920

Please sign in to comment.