Skip to content
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

Support Naming Policy #17

Merged
merged 20 commits into from
Nov 11, 2021
Merged

Support Naming Policy #17

merged 20 commits into from
Nov 11, 2021

Conversation

bells17
Copy link
Contributor

@bells17 bells17 commented Sep 21, 2021

#6

@bells17 bells17 marked this pull request as ready for review September 21, 2021 18:43
@zoetrope zoetrope self-requested a review September 22, 2021 04:11
Copy link
Member

@ymmt2005 ymmt2005 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please validate the regular expressions beforehand, as commented.
Also, please add a user manual for this feature.

return admission.Allowed("")
}

func (v *subNamespaceValidator) notMatchingNamingPolicy(ctx context.Context, sn *accuratev1.SubNamespace) (bool, error) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The regular expression should be pre-compiled when loaded from the configuration file,
so that no error would be returned during execution.

pkg/config/types.go Show resolved Hide resolved
Comment on lines 14 to 15
Root string `json:"root,omitempty"`
Match string `json:"match,omitempty"`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These two are not expected to be empty, so omitempty should be removed.

@bells17
Copy link
Contributor Author

bells17 commented Oct 6, 2021

Thank you @ymmt2005 .
I've corrected the points you commented on.
Would you review this PR again?

Also, please add a user manual for this feature.

I added the description and example for the naming policy setting to values.yaml.
Should we write the docs aside from the above description and example?

Copy link
Member

@zoetrope zoetrope left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would be useful to be able to refer to the group that matched root pattern in match pattern.

for example:

namingPolicies:
  - root: app-(.*)
    match: app-\1-.*

This way, we don't have to change our namingPolicies as the number of teams increases.
What do you think?

return admission.Denied(fmt.Sprintf("namespace %s is neither a root nor a sub namespace", ns.Name))
func (v *subNamespaceValidator) notMatchingNamingPolicy(ctx context.Context, sn *accuratev1.SubNamespace) (bool, error) {
for _, policy := range v.namingPolicies {
if policy.Root.MatchString(sn.Namespace) {
Copy link
Member

@zoetrope zoetrope Oct 7, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think sn.Namespace is the parent, not the root.
Is this correct?

@zoetrope zoetrope requested a review from ymmt2005 October 7, 2021 07:07
serv := mgr.GetWebhookServer()

m := &subNamespaceMutator{
dec: dec,
}
serv.Register("/mutate-accurate-cybozu-com-v1-subnamespace", &webhook.Admission{Handler: m})

namingPolicyRegexps, err := compileNamingPolicies(namingPolicies)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

compile and validate policies in pkg/config.Config.Validate method.

docs/config.md Outdated
Comment on lines 56 to 60
# namingPolicies:
# - root: foo
# match: foo_.*
# - root: bar
# match: bar_.*
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why don't we uncomment these lines?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

uncommented.


func (v *subNamespaceValidator) notMatchingNamingPolicy(ctx context.Context, ns, root string) (bool, string, error) {
for _, policy := range v.namingPolicies {
matches := policy.Root.FindAllSubmatch([]byte(root), -1)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It'd be simpler if we use Regexp.Expand(String).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed it.

Comment on lines 59 to 62
# - root: foo
# match: foo_.*
# - root: bar
# match: bar_.*
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we adopt the proposed spec from @zoetrope, this example should be updated too.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added an example.

@bells17 bells17 requested a review from ymmt2005 October 19, 2021 16:57

func (v *subNamespaceValidator) notMatchingNamingPolicy(ctx context.Context, ns, root string) (bool, string, error) {
for _, policy := range v.namingPolicies {
matches := policy.Root.FindAllSubmatchIndex([]byte(root), -1)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if len(matches) > 0 {
m := []byte{}
for _, match := range matches {
m = policy.Root.Expand(m, []byte(policy.Match), []byte(root), match)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

config.yaml Outdated
@@ -25,3 +25,5 @@ watches:
kind: Secret
- version: v1
kind: ResourceQuota

namingPolicies: []
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add comments just like in the other fields.

@@ -15,7 +15,7 @@ type: application
# This is the chart version. This version number should be incremented each time you make changes
# to the chart and its templates, including the app version.
# Versions are expected to follow Semantic Versioning (https://semver.org/)
version: 0.1.2
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I updated the chart version.
#32

Please fix it.

zoetrope
zoetrope previously approved these changes Nov 2, 2021
Copy link
Member

@zoetrope zoetrope left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

pkg/config/types.go Outdated Show resolved Hide resolved
zoetrope
zoetrope previously approved these changes Nov 5, 2021
Copy link
Member

@zoetrope zoetrope left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@ymmt2005
Copy link
Member

ymmt2005 commented Nov 5, 2021

@bells17
I'm a little bit confused that there are almost two identical files, namely,
charts/accurate/values.yaml and config.yaml.

Can we remove config.yaml?

@bells17
Copy link
Contributor Author

bells17 commented Nov 8, 2021

@ymmt2005 Thank you for your reviewing and I removed config.yaml.
Would you check it?

@bells17 bells17 requested a review from zoetrope November 8, 2021 11:40
Copy link
Member

@ymmt2005 ymmt2005 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@ymmt2005 ymmt2005 merged commit 606318c into main Nov 11, 2021
@ymmt2005 ymmt2005 deleted the support-naming-policy branch November 11, 2021 09:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants