Skip to content

Commit

Permalink
Merge pull request #1206 from jbrette/namespace
Browse files Browse the repository at this point in the history
Addresses issue discovered in attempting to patch K8s objects across different namespaces.
  • Loading branch information
monopole authored Jun 19, 2019
2 parents 293c8be + 934e036 commit b08f338
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 0 deletions.
3 changes: 3 additions & 0 deletions k8sdeps/transformer/patch/transformer.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ func (tf *transformer) Transform(m resmap.ResMap) error {
}
for _, patch := range patches.Resources() {
target, err := tf.findPatchTarget(m, patch.OrgId())
if err != nil {
return err
}
merged := map[string]interface{}{}
versionedObj, err := scheme.Scheme.New(
toSchemaGvk(patch.OrgId().Gvk))
Expand Down
59 changes: 59 additions & 0 deletions k8sdeps/transformer/patch/transformer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,65 @@ func TestMultiplePatchesWithConflict(t *testing.T) {
}
}

func TestPatchesWithWrongNamespace(t *testing.T) {
base := resmaptest_test.NewRmBuilder(t, rf).
Add(map[string]interface{}{
"apiVersion": "apps/v1",
"kind": "Deployment",
"metadata": map[string]interface{}{
"name": "deploy1",
"namespace": "namespace1",
},
"spec": map[string]interface{}{
"template": map[string]interface{}{
"spec": map[string]interface{}{
"containers": []interface{}{
map[string]interface{}{
"name": "nginx",
"image": "nginx",
},
},
},
},
},
}).ResMap()

patch := []*resource.Resource{
rf.FromMap(map[string]interface{}{
"apiVersion": "apps/v1",
"kind": "Deployment",
"metadata": map[string]interface{}{
"name": "deploy1",
"namespace": "namespace2",
},
"spec": map[string]interface{}{
"template": map[string]interface{}{
"spec": map[string]interface{}{
"containers": []interface{}{
map[string]interface{}{
"name": "nginx",
"image": "nginx:1.7.9",
},
},
},
},
},
}),
}

lt, err := NewTransformer(patch, rf)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
err = lt.Transform(base)
if err == nil {
t.Fatalf("did not get expected error")
}
if !strings.Contains(err.Error(), "failed to find target for patch") {
t.Fatalf("expected error to contain %q but get %v", "failed to find target for patch", err)
}
}

func TestNoSchemaOverlayRun(t *testing.T) {
base := resmaptest_test.NewRmBuilder(t, rf).
Add(map[string]interface{}{
Expand Down

0 comments on commit b08f338

Please sign in to comment.