Skip to content

Commit

Permalink
Experimental Implementation for Hostname AntiAffinity
Browse files Browse the repository at this point in the history
  • Loading branch information
ellistarn committed Jan 25, 2022
1 parent 8b8e61f commit 21f3221
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 9 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ require (
github.com/census-instrumentation/opencensus-proto v0.3.0 // indirect
github.com/cespare/xxhash/v2 v2.1.2 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/ellistarn/slang v0.0.0-20211216011006-2f61b4cedc41 // indirect
github.com/evanphx/json-patch v4.11.0+incompatible // indirect
github.com/evanphx/json-patch/v5 v5.6.0 // indirect
github.com/fsnotify/fsnotify v1.4.9 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,8 @@ github.com/eapache/go-resiliency v1.2.0/go.mod h1:kFI+JgMyC7bLPUVY133qvEBtVayf5m
github.com/eapache/go-xerial-snappy v0.0.0-20180814174437-776d5712da21/go.mod h1:+020luEh2TKB4/GOp8oxxtq0Daoen/Cii55CzbTV6DU=
github.com/eapache/queue v1.1.0/go.mod h1:6eCeP0CKFpHLu8blIFXhExK/dRa7WDZfr6jVFPTqq+I=
github.com/elazarl/goproxy v0.0.0-20180725130230-947c36da3153/go.mod h1:/Zj4wYkgs4iZTTu3o/KG3Itv/qCCa8VVMlb3i9OVuzc=
github.com/ellistarn/slang v0.0.0-20211216011006-2f61b4cedc41 h1:hFoV78ttlNIHlSsRVCD5mZvAf/CFCEs5B/fzVpYl4rM=
github.com/ellistarn/slang v0.0.0-20211216011006-2f61b4cedc41/go.mod h1:uUcAC7W7D/Idxwn8xy1F8CiLtx0vblj0Mcx+qcz2Mu4=
github.com/emicklei/go-restful v0.0.0-20170410110728-ff4f55a20633/go.mod h1:otzb+WCGbkyDHkqmQmT5YD2WR4BBwUdeQoFo8l/7tVs=
github.com/emicklei/go-restful v2.9.5+incompatible/go.mod h1:otzb+WCGbkyDHkqmQmT5YD2WR4BBwUdeQoFo8l/7tVs=
github.com/envoyproxy/go-control-plane v0.6.9/go.mod h1:SBwIajubJHhxtWwsL9s8ss4safvEdbitLhGGK48rN6g=
Expand Down
20 changes: 11 additions & 9 deletions pkg/controllers/selection/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ type Controller struct {
provisioners *provisioning.Controller
preferences *Preferences
volumeTopology *VolumeTopology
antiAffinity *AntiAffinity
}

// NewController constructs a controller instance
Expand All @@ -52,6 +53,7 @@ func NewController(kubeClient client.Client, provisioners *provisioning.Controll
provisioners: provisioners,
preferences: NewPreferences(),
volumeTopology: NewVolumeTopology(kubeClient),
antiAffinity: NewAntiAffinity(),
}
}

Expand All @@ -69,7 +71,7 @@ func (c *Controller) Reconcile(ctx context.Context, req reconcile.Request) (reco
if !isProvisionable(pod) {
return reconcile.Result{}, nil
}
if err := validate(pod); err != nil {
if err := c.validate(pod); err != nil {
logging.FromContext(ctx).Debugf("Ignoring pod, %s", err.Error())
return reconcile.Result{}, nil
}
Expand Down Expand Up @@ -120,14 +122,14 @@ func isProvisionable(p *v1.Pod) bool {
!pod.IsOwnedByNode(p)
}

func validate(p *v1.Pod) error {
func (c *Controller) validate(p *v1.Pod) error {
return multierr.Combine(
validateAffinity(p),
validateTopology(p),
c.validateAffinity(p),
c.validateTopology(p),
)
}

func validateTopology(pod *v1.Pod) (errs error) {
func (c *Controller) validateTopology(pod *v1.Pod) (errs error) {
for _, constraint := range pod.Spec.TopologySpreadConstraints {
if supported := sets.NewString(v1.LabelHostname, v1.LabelTopologyZone); !supported.Has(constraint.TopologyKey) {
errs = multierr.Append(errs, fmt.Errorf("unsupported topology key, %s not in %s", constraint.TopologyKey, supported))
Expand All @@ -136,16 +138,16 @@ func validateTopology(pod *v1.Pod) (errs error) {
return errs
}

func validateAffinity(pod *v1.Pod) (errs error) {
func (c *Controller) validateAffinity(pod *v1.Pod) (errs error) {
if pod.Spec.Affinity == nil {
return nil
}
if err := c.antiAffinity.Validate(pod).ViaField("affinity.podAntiAffinity"); err != nil {
errs = multierr.Append(errs, fmt.Errorf(err.Error()))
}
if pod.Spec.Affinity.PodAffinity != nil {
errs = multierr.Append(errs, fmt.Errorf("pod affinity is not supported"))
}
if pod.Spec.Affinity.PodAntiAffinity != nil {
errs = multierr.Append(errs, fmt.Errorf("pod anti-affinity is not supported"))
}
if pod.Spec.Affinity.NodeAffinity != nil {
for _, term := range pod.Spec.Affinity.NodeAffinity.PreferredDuringSchedulingIgnoredDuringExecution {
errs = multierr.Append(errs, validateNodeSelectorTerm(term.Preference))
Expand Down

0 comments on commit 21f3221

Please sign in to comment.