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

Adds labels validation to restrict namespaces #774

Merged
merged 5 commits into from
Oct 28, 2021

Conversation

suket22
Copy link
Contributor

@suket22 suket22 commented Oct 28, 2021

1. Issue, if available:
#728

2. Description of changes:

  • Prevents labels that are in the k8s.io or kubernetes.io namespace. See this for more detail on why this was changed upstream 1.16+.
  • In addition to that, also making sure we prevent labels in the karpenter.sh and k8s.aws namespaces.

The upstream checks are a little more nuanced. See this and this. The checks I'm enforcing here are stricter.

Testing -

  • Unit tests to demonstrate well known label logic still works.
  • Made sure labels with k8s.io and kubernetes.io were being rejected. Also made sure that well known labels as per karpenter like node.kubernetes.io/instance-type were still accepted and honored.

3. Does this change impact docs?

  • Yes, PR includes docs updates
  • Yes, issue opened: link to issue
  • No

This should be called out in our docs as well.

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

@netlify
Copy link

netlify bot commented Oct 28, 2021

✔️ Deploy Preview for karpenter-docs-prod canceled.

🔨 Explore the source changes: 7a2da21

🔍 Inspect the deploy log: https://app.netlify.com/sites/karpenter-docs-prod/deploys/617b04ab89a58800076f0d3b

@@ -98,10 +98,30 @@ func (c *Constraints) validateLabels() (errs *apis.FieldError) {
if known, ok := WellKnownLabels[key]; ok && !functional.ContainsString(known, value) {
errs = errs.Also(apis.ErrInvalidValue(fmt.Sprintf("%s not in %s", value, known), fmt.Sprintf("labels[%s]", key)))
}
if _, ok := WellKnownLabels[key]; !ok && isRestrictedLabelPrefix(key) {
Copy link
Contributor

Choose a reason for hiding this comment

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

I know we've gone back and forth, but you could call this "IsRestrictedLabelDomain", since these are domains at the end of the day.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Works for me!

return false
}

func getLabelPrefix(key string) string {
Copy link
Contributor

Choose a reason for hiding this comment

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

check out url.parse, url.Hostname()

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'm not sure I follow how that helps. I still need to split the string by / regardless in this method, right?

The suffix check I need to do too because url.Hostname() gives me the whole path and not just the first subdomain which is what I'm looking for. Also, looks like url.Hostname() doesn't really work great when there's no scheme preceding the prefix like in our case.

"kubernetes.io",
"k8s.io",
"karpenter.sh",
"k8s.aws",
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you register this in the aws/apis/v1alpha1/register.go instead of putting in vendor neutral code?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good point! I'll move k8s.aws into aws/apis/v1alpha1/register.go but leave the others here since the rest should apply to the other cloud providers too.

@@ -190,7 +190,7 @@ metadata:
name: default
spec:
requirements:
- key: node.k8s.io/capacity-type
- key: node.k8s.aws/capacity-type
Copy link
Contributor

Choose a reason for hiding this comment

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

Nice!

@suket22 suket22 merged commit 75c20d7 into aws:main Oct 28, 2021
gfcroft pushed a commit to gfcroft/karpenter-provider-aws that referenced this pull request Nov 25, 2023
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.

3 participants