-
Notifications
You must be signed in to change notification settings - Fork 743
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Mutation: conversion functions from assign / assignmetadata to Mutator #938
Conversation
pkg/mutation/assign_mutator.go
Outdated
return res | ||
} | ||
|
||
func mutatorForAssign(assign *mutationsv1.Assign) (MutatorWithSchema, error) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should return type assignMutator
rather than the MutatorWithSchema
interface, per golang conventions.
pkg/mutation/assignmeta_mutator.go
Outdated
return res | ||
} | ||
|
||
func mutatorForAssignMetadata(assignMeta *mutationsv1.AssignMetadata) (Mutator, error) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same comment here WRT return type
pkg/mutation/mutator.go
Outdated
@@ -61,3 +66,19 @@ func MakeID(obj runtime.Object) (ID, error) { | |||
Namespace: meta.GetNamespace(), | |||
}, nil | |||
} | |||
|
|||
// MutatorFor returns a mutator generated |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we removed the MutatorFor function after refactoring the MutationSystem interface
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, was a bit torn about having a single entry point that dispatches to the right function or two type safe ones.
Will remove it
f7fc400
to
4b72d64
Compare
4b72d64
to
af23697
Compare
Codecov Report
@@ Coverage Diff @@
## master #938 +/- ##
==========================================
+ Coverage 46.78% 46.93% +0.14%
==========================================
Files 56 58 +2
Lines 3454 3541 +87
==========================================
+ Hits 1616 1662 +46
- Misses 1636 1669 +33
- Partials 202 210 +8
Flags with carried forward coverage won't be shown. Click here to find out more.
Continue to review full report at Codecov.
|
630eb87
to
9789800
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
pkg/mutation/conversion_test.go
Outdated
t.Fatal(tc.tname, "Failed to convert second to mutator", err) | ||
} | ||
hasDiff := firstMutator.HasDiff(secondMutator) | ||
hasDiff1 := firstMutator.HasDiff(secondMutator) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should we swap caller/callee here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Indeed :-)
pkg/mutation/conversion_test.go
Outdated
} | ||
first.APIVersion, first.Kind = gvk.ToAPIVersionAndKind() | ||
|
||
second := &mutationsv1.Assign{ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You might be able to write: second := first.DeepCopy()
pkg/mutation/conversion_test.go
Outdated
} | ||
first.APIVersion, first.Kind = gvk.ToAPIVersionAndKind() | ||
|
||
second := &mutationsv1.AssignMetadata{ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Deepcopy?
pkg/mutation/conversion_test.go
Outdated
t.Fatal(tc.tname, "Failed to convert second to mutator", err) | ||
} | ||
hasDiff := firstMutator.HasDiff(secondMutator) | ||
hasDiff1 := firstMutator.HasDiff(secondMutator) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Swap call order?
9789800
to
ee582b2
Compare
@maxsmythe applied new changes |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
pkg/mutation/assign_mutator.go
Outdated
|
||
import ( | ||
"github.com/google/go-cmp/cmp" | ||
mutationsv1 "github.com/open-policy-agent/gatekeeper/apis/mutations/v1alpha1" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To be consistent, let's use mutationsv1alpha1
pkg/mutation/assign_mutator.go
Outdated
id: m.id, | ||
assign: m.assign.DeepCopy(), | ||
path: make([]path.Entry, len(m.path)), | ||
bindings: make([]SchemaBinding, 0), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should this also be initialized with len(m.bindings)
pkg/mutation/assignmeta_mutator.go
Outdated
|
||
import ( | ||
"github.com/google/go-cmp/cmp" | ||
mutationsv1 "github.com/open-policy-agent/gatekeeper/apis/mutations/v1alpha1" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
mutationsv1alpha1
pkg/mutation/assignmeta_mutator.go
Outdated
func MutatorForAssignMetadata(assignMeta *mutationsv1.AssignMetadata) (*AssignMetadataMutator, error) { | ||
id, err := MakeID(assignMeta) | ||
if err != nil { | ||
return nil, errors.Wrapf(err, "Failed to retrieve id for assign type") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
return nil, errors.Wrapf(err, "Failed to retrieve id for assign type") | |
return nil, errors.Wrapf(err, "Failed to retrieve id for assignMetadata type") |
pkg/mutation/conversion_test.go
Outdated
"testing" | ||
|
||
"github.com/google/go-cmp/cmp" | ||
mutationsv1 "github.com/open-policy-agent/gatekeeper/apis/mutations/v1alpha1" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
mutationsv1alpha1
pkg/mutation/conversion_test.go
Outdated
|
||
for _, tc := range table { | ||
t.Run(tc.tname, func(t *testing.T) { | ||
secondAssign := second.DeepCopy() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: secondAssignMeta
@fedepaol just few nits. mostly looks good! |
ee582b2
to
d98f7af
Compare
I think I applied all the suggestions, thanks for the review! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lgtm
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changes from recently merged PRs will need to be reflected in this PR now
We expose a generic MutatorFor(o Object) to build a mutator instance that wraps the cr. Signed-off-by: Federico Paolinelli <fpaoline@redhat.com>
b3af09f
to
52f6017
Compare
Done |
It looks like linting is failing? |
Fixing it |
Location parser is merged. Here we fill the path field for both the mutators. We also add a new test validating that the conversion fails if an invalid location is specified. Signed-off-by: Federico Paolinelli <fpaoline@redhat.com>
Done, sorry. I took the change too lightly. |
@maxsmythe I also added a minimal verification for assignmeta. This is under the idea that we want to limit the mutation to labels and annotations, but we can make the constraint lighter and have it validate that the path is a valid metadata path. |
Thanks for also checking the metadata prefix! The changes LGTM. @ritazh, it looks like all your comments were addressed. Does this LGTY? |
Also, @fedepaol, if this looks good to Rita tomorrow, are you ready for this to merge? |
pkg/mutation/assignmeta_mutator.go
Outdated
}, | ||
} | ||
|
||
annotationValidiSubPath = []parser.Node{ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: is annotationValidiSubPath
intentional or a typo?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Was a typo, fixed it
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lgtm
just one nit
This checks that the only valid location for assignmetadata is related to labels or annotations. Signed-off-by: Federico Paolinelli <fpaoline@redhat.com>
34e6bb5
to
c09743a
Compare
It is, I fixed the nit. |
Awesome! Merging in |
What this PR does / why we need it:
This implements MutatorFor plus the type specific objects implementing the interfaces.
Depends on other prs to get merged.
Fixes #936
Special notes for your reviewer: