-
Notifications
You must be signed in to change notification settings - Fork 1k
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
WIP: add a namespace selector to the provisioner spec #1496
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -19,6 +19,9 @@ import ( | |||||
"fmt" | ||||||
"time" | ||||||
|
||||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||||||
"k8s.io/apimachinery/pkg/labels" | ||||||
|
||||||
"github.com/go-logr/zapr" | ||||||
"go.uber.org/multierr" | ||||||
"go.uber.org/zap" | ||||||
|
@@ -75,6 +78,7 @@ func (c *Controller) Reconcile(ctx context.Context, req reconcile.Request) (reco | |||||
logging.FromContext(ctx).Errorf("Ignoring pod, %s", err) | ||||||
return reconcile.Result{}, nil | ||||||
} | ||||||
|
||||||
// Select a provisioner, wait for it to bind the pod, and verify scheduling succeeded in the next loop | ||||||
if err := c.selectProvisioner(ctx, pod); err != nil { | ||||||
logging.FromContext(ctx).Debugf("Could not schedule pod, %s", err) | ||||||
|
@@ -96,13 +100,29 @@ func (c *Controller) selectProvisioner(ctx context.Context, pod *v1.Pod) (errs e | |||||
if len(provisioners) == 0 { | ||||||
return nil | ||||||
} | ||||||
|
||||||
// lookup the pod namespace for matching against the provisioner | ||||||
var podNamespace v1.Namespace | ||||||
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. I'd leave things unqualified unless it's unclear
Suggested change
|
||||||
if err := c.kubeClient.Get(ctx, client.ObjectKey{Name: pod.Namespace}, &podNamespace); err != nil { | ||||||
return err | ||||||
} | ||||||
|
||||||
for _, candidate := range c.provisioners.List(ctx) { | ||||||
// check if the provisioner is allowed to provision pods in this namespace | ||||||
if err := validateNamespace(candidate, podNamespace); err != nil { | ||||||
errs = multierr.Append(errs, fmt.Errorf("tried provisioner/%s: %w", candidate.Name, err)) | ||||||
continue | ||||||
} | ||||||
|
||||||
// ValidatePod is on Constraints, which is embedded in ProvisionerSpec. If that gets reworked, consider moving | ||||||
// validateNamespace to there as well | ||||||
if err := candidate.Spec.DeepCopy().ValidatePod(pod); err != nil { | ||||||
errs = multierr.Append(errs, fmt.Errorf("tried provisioner/%s: %w", candidate.Name, err)) | ||||||
} else { | ||||||
provisioner = candidate | ||||||
break | ||||||
continue | ||||||
} | ||||||
|
||||||
provisioner = candidate | ||||||
break | ||||||
} | ||||||
if provisioner == nil { | ||||||
return fmt.Errorf("matched 0/%d provisioners, %w", len(multierr.Errors(errs)), errs) | ||||||
|
@@ -114,6 +134,35 @@ func (c *Controller) selectProvisioner(ctx context.Context, pod *v1.Pod) (errs e | |||||
return nil | ||||||
} | ||||||
|
||||||
// validateNamespace returns nil if the candidate provisioner is configured to provision pods in the provided | ||||||
// namespace | ||||||
func validateNamespace(candidate *provisioning.Provisioner, namespace v1.Namespace) error { | ||||||
// no namespace list or label selector provided, so everything passes | ||||||
if len(candidate.Spec.Namespaces) == 0 && candidate.Spec.NamespaceSelector == nil { | ||||||
return nil | ||||||
} | ||||||
|
||||||
// the namespace of the pod must match one of the list of namespaces or the selector | ||||||
for _, ns := range candidate.Spec.Namespaces { | ||||||
if ns == namespace.Name { | ||||||
return nil | ||||||
} | ||||||
} | ||||||
|
||||||
// For an undefined namespace selector, the selector itself matches nothing. This | ||||||
// provides the desired semantics here as we know there is either a namespace list | ||||||
// or a namespace label selector and the namespace has already failed to match the | ||||||
// possibly empty list | ||||||
selector, err := metav1.LabelSelectorAsSelector(candidate.Spec.NamespaceSelector) | ||||||
if err != nil { | ||||||
return err | ||||||
} | ||||||
if !selector.Matches(labels.Set(namespace.Labels)) { | ||||||
return fmt.Errorf("doesn't match namespaces being provisioned") | ||||||
} | ||||||
return nil | ||||||
} | ||||||
|
||||||
func isProvisionable(p *v1.Pod) bool { | ||||||
return !pod.IsScheduled(p) && | ||||||
!pod.IsPreempting(p) && | ||||||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Totally minor nit, but I just realized that our CRD should be installed first, since our webhooks reference them. I believe it only matters on first installation.