Skip to content

Commit

Permalink
feat: add self modifier support (#856)
Browse files Browse the repository at this point in the history
* feat: add self modifier support

Signed-off-by: Charles-Edouard Brétéché <[email protected]>

* bump kyverno-json

Signed-off-by: Charles-Edouard Brétéché <[email protected]>

* fix tests

Signed-off-by: Charles-Edouard Brétéché <[email protected]>

---------

Signed-off-by: Charles-Edouard Brétéché <[email protected]>
  • Loading branch information
eddycharly authored Feb 12, 2024
1 parent ed3ee28 commit 55c2f65
Show file tree
Hide file tree
Showing 9 changed files with 59 additions and 40 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ require (
github.com/jmespath-community/go-jmespath v1.1.2-0.20240117150817-e430401a2172
github.com/kudobuilder/kuttl v0.15.0
github.com/kyverno/kyverno v1.5.0-rc1.0.20240202083228-5f0d53fe3482
github.com/kyverno/kyverno-json v0.0.3-0.20240211090543-b139511f7c17
github.com/kyverno/kyverno-json v0.0.3-0.20240212172113-9e87961c227c
github.com/spf13/cobra v1.8.0
github.com/spf13/pflag v1.0.5
github.com/stretchr/testify v1.8.4
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -479,8 +479,8 @@ github.com/kudobuilder/kuttl v0.15.0 h1:OmCbpY3ebn+myJaatD+IT1AL1OTz34+Cv6ettiXd
github.com/kudobuilder/kuttl v0.15.0/go.mod h1:UtAOt+GE8HQZFveEN8p1fk3j+H1Cp5Fk1eMdQkXPqH0=
github.com/kyverno/kyverno v1.5.0-rc1.0.20240202083228-5f0d53fe3482 h1:mGQbOhxoHBhPCGkxYjy+u3qtWH43BLjIxUs4zWtvKSg=
github.com/kyverno/kyverno v1.5.0-rc1.0.20240202083228-5f0d53fe3482/go.mod h1:uEm7WtaqOsPP3Jx6EOkO2PjHu6vf0MFaMD6w1ol7hAQ=
github.com/kyverno/kyverno-json v0.0.3-0.20240211090543-b139511f7c17 h1:6eMgQzTnM6Q5ANFJP/hfd3a4MvhyDlBdf39mgZE+dcY=
github.com/kyverno/kyverno-json v0.0.3-0.20240211090543-b139511f7c17/go.mod h1:oz27arF3YFnUuUTNngae/6OTaEWQ/8gxmCp7CavXYt8=
github.com/kyverno/kyverno-json v0.0.3-0.20240212172113-9e87961c227c h1:xL3mAOnyNQbHOBsLNPRj44PMGxI2E6ZTI0oZ7LGtSCs=
github.com/kyverno/kyverno-json v0.0.3-0.20240212172113-9e87961c227c/go.mod h1:oz27arF3YFnUuUTNngae/6OTaEWQ/8gxmCp7CavXYt8=
github.com/mailru/easyjson v0.7.7 h1:UGYAvKxe3sBsEDzO8ZeWOSlIQfWFlxbzLZe7hwFURr0=
github.com/mailru/easyjson v0.7.7/go.mod h1:xzfreul335JAWq5oZzymOObrkdz5UnU4kGfJJLY9Nlc=
github.com/mattn/go-colorable v0.0.9/go.mod h1:9vuHe8Xs5qXnSaW/c/ABM9alt+Vo+STaOChaDxuIBZU=
Expand Down
30 changes: 24 additions & 6 deletions pkg/runner/mutate/convert.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,35 @@
package mutate

func convert(in any) map[string]any {
func convertSlice(in any) []any {
data, ok := in.([]any)
if !ok {
return nil
}
out := []any{}
for _, v := range data {
out = append(out, convert(v))
}
return out
}

func convertMap(in any) map[string]any {
data, ok := in.(map[any]any)
if !ok {
return nil
}
out := map[string]any{}
for k, v := range data {
if c := convert(v); c != nil {
out[k.(string)] = c
} else {
out[k.(string)] = v
}
out[k.(string)] = convert(v)
}
return out
}

func convert(in any) any {
if result := convertSlice(in); result != nil {
return result
}
if result := convertMap(in); result != nil {
return result
}
return in
}
2 changes: 1 addition & 1 deletion pkg/runner/mutate/convert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func Test_convert(t *testing.T) {
}}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := convert(tt.in)
got := convertMap(tt.in)
assert.Equal(t, tt.out, got)
})
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/runner/mutate/merge.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func Merge(ctx context.Context, obj unstructured.Unstructured, bindings binding.
if err != nil {
return obj, err
}
obj.SetUnstructuredContent(mapsutils.Merge(obj.UnstructuredContent(), convert(patch)))
obj.SetUnstructuredContent(mapsutils.Merge(obj.UnstructuredContent(), convertMap(patch)))
}
}
return obj, nil
Expand Down
20 changes: 10 additions & 10 deletions pkg/runner/operations/apply/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,16 +60,16 @@ func (o *operation) Exec(ctx context.Context) (err error) {
defer func() {
internal.LogEnd(logger, logging.Apply, err)
}()
// selfModifier := v1alpha1.Modifier{
// Merge: &v1alpha1.Any{
// Value: obj.UnstructuredContent(),
// },
// }
// if merged, err := mutate.Merge(ctx, obj, o.bindings, selfModifier); err != nil {
// return err
// } else {
// obj = merged
// }
selfModifier := v1alpha1.Modifier{
Merge: &v1alpha1.Any{
Value: obj.UnstructuredContent(),
},
}
if merged, err := mutate.Merge(ctx, obj, o.bindings, selfModifier); err != nil {
return err
} else {
obj = merged
}
if merged, err := mutate.Merge(ctx, obj, o.bindings, o.modifiers...); err != nil {
return err
} else {
Expand Down
20 changes: 10 additions & 10 deletions pkg/runner/operations/create/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,16 +58,16 @@ func (o *operation) Exec(ctx context.Context) (err error) {
defer func() {
internal.LogEnd(logger, logging.Create, err)
}()
// selfModifier := v1alpha1.Modifier{
// Merge: &v1alpha1.Any{
// Value: obj.UnstructuredContent(),
// },
// }
// if merged, err := mutate.Merge(ctx, obj, o.bindings, selfModifier); err != nil {
// return err
// } else {
// obj = merged
// }
selfModifier := v1alpha1.Modifier{
Merge: &v1alpha1.Any{
Value: obj.UnstructuredContent(),
},
}
if merged, err := mutate.Merge(ctx, obj, o.bindings, selfModifier); err != nil {
return err
} else {
obj = merged
}
if merged, err := mutate.Merge(ctx, obj, o.bindings, o.modifiers...); err != nil {
return err
} else {
Expand Down
3 changes: 2 additions & 1 deletion testdata/e2e/examples/modifiers/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

| # | Name | Try | Catch | Finally |
|:-:|---|:-:|:-:|:-:|
| 1 | [step-1](#step-step-1) | 1 | 0 | 0 |
| 1 | [step-1](#step-step-1) | 2 | 0 | 0 |

## Step: `step-1`

Expand All @@ -17,3 +17,4 @@
| # | Operation | Description |
|:-:|---|---|
| 1 | `apply` | *No description* |
| 2 | `apply` | *No description* |
16 changes: 8 additions & 8 deletions testdata/e2e/examples/modifiers/chainsaw-test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ metadata:
spec:
steps:
- try:
# - apply:
# resource:
# apiVersion: v1
# kind: Namespace
# metadata:
# name: ($namespace)
# annotations:
# keptn.sh/lifecycle-toolkit: enabled
- apply:
resource:
apiVersion: v1
kind: Namespace
metadata:
name: ($namespace)
annotations:
keptn.sh/lifecycle-toolkit: enabled
- apply:
resource:
apiVersion: v1
Expand Down

0 comments on commit 55c2f65

Please sign in to comment.