-
Notifications
You must be signed in to change notification settings - Fork 4.7k
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
SCC can't be patched via JSONPatch because users is nil #17185
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -518,8 +518,16 @@ func autoConvert_security_SecurityContextConstraints_To_v1_SecurityContextConstr | |
} | ||
out.ReadOnlyRootFilesystem = in.ReadOnlyRootFilesystem | ||
out.SeccompProfiles = *(*[]string)(unsafe.Pointer(&in.SeccompProfiles)) | ||
out.Users = *(*[]string)(unsafe.Pointer(&in.Users)) | ||
out.Groups = *(*[]string)(unsafe.Pointer(&in.Groups)) | ||
if in.Users == nil { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Generated files in their own commit? |
||
out.Users = make([]string, 0) | ||
} else { | ||
out.Users = *(*[]string)(unsafe.Pointer(&in.Users)) | ||
} | ||
if in.Groups == nil { | ||
out.Groups = make([]string, 0) | ||
} else { | ||
out.Groups = *(*[]string)(unsafe.Pointer(&in.Groups)) | ||
} | ||
return nil | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -50,7 +50,27 @@ func (strategy) PrepareForCreate(_ genericapirequest.Context, obj runtime.Object | |
func (strategy) PrepareForUpdate(_ genericapirequest.Context, obj, old runtime.Object) { | ||
} | ||
|
||
// Canonicalize removes duplicate user and group values, preserving order. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do you need to preserve order, or do you just need consistent order? If you only need the latter, you could just use StringKeySet.List which would sort and dedupe. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. order is better in case you have a patch that removes a specific index |
||
func (strategy) Canonicalize(obj runtime.Object) { | ||
scc := obj.(*securityapi.SecurityContextConstraints) | ||
scc.Users = uniqueStrings(scc.Users) | ||
scc.Groups = uniqueStrings(scc.Groups) | ||
} | ||
|
||
func uniqueStrings(values []string) []string { | ||
if len(values) < 2 { | ||
return values | ||
} | ||
updated := make([]string, 0, len(values)) | ||
existing := make(map[string]struct{}) | ||
for _, value := range values { | ||
if _, ok := existing[value]; ok { | ||
continue | ||
} | ||
existing[value] = struct{}{} | ||
updated = append(updated, value) | ||
} | ||
return updated | ||
} | ||
|
||
func (strategy) Validate(ctx genericapirequest.Context, obj runtime.Object) field.ErrorList { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package securitycontextconstraints | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
|
||
securityapi "github.com/openshift/origin/pkg/security/apis/security" | ||
) | ||
|
||
func TestCanonicalize(t *testing.T) { | ||
testCases := []struct { | ||
obj *securityapi.SecurityContextConstraints | ||
expect *securityapi.SecurityContextConstraints | ||
}{ | ||
{ | ||
obj: &securityapi.SecurityContextConstraints{}, | ||
expect: &securityapi.SecurityContextConstraints{}, | ||
}, | ||
{ | ||
obj: &securityapi.SecurityContextConstraints{ | ||
Users: []string{"a"}, | ||
}, | ||
expect: &securityapi.SecurityContextConstraints{ | ||
Users: []string{"a"}, | ||
}, | ||
}, | ||
{ | ||
obj: &securityapi.SecurityContextConstraints{ | ||
Users: []string{"a", "a"}, | ||
Groups: []string{"b", "b"}, | ||
}, | ||
expect: &securityapi.SecurityContextConstraints{ | ||
Users: []string{"a"}, | ||
Groups: []string{"b"}, | ||
}, | ||
}, | ||
{ | ||
obj: &securityapi.SecurityContextConstraints{ | ||
Users: []string{"a", "b", "a"}, | ||
Groups: []string{"c", "d", "c"}, | ||
}, | ||
expect: &securityapi.SecurityContextConstraints{ | ||
Users: []string{"a", "b"}, | ||
Groups: []string{"c", "d"}, | ||
}, | ||
}, | ||
} | ||
for i, testCase := range testCases { | ||
Strategy.Canonicalize(testCase.obj) | ||
if !reflect.DeepEqual(testCase.expect, testCase.obj) { | ||
t.Errorf("%d: unexpected object: %#v", i, testCase.obj) | ||
} | ||
} | ||
} |
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.
surprised this didn't regenerate the proto files
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.
oh, it did. surprised that didn't regenerate the fileDescriptorGenerated blob
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.
Generated strips comments (last I checked)