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

Fix Spec.Drain.PodSelector #239

Merged
merged 1 commit into from
May 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion e2e/suite/job_generate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ var _ = Describe("Job Generation", func() {
Expect(err).ToNot(HaveOccurred())
Expect(jobs).To(HaveLen(1))
Expect(jobs[0].Status.Succeeded).To(BeNumerically("==", 0))
Expect(jobs[0].Status.Active).To(BeNumerically("==", 0))
Expect(jobs[0].Status.Failed).To(BeNumerically(">=", 1))

plan, err = e2e.GetPlan(plan.Name, metav1.GetOptions{})
Expand All @@ -57,6 +58,7 @@ var _ = Describe("Job Generation", func() {
It("should apply successfully after edit", func() {
Expect(jobs).To(HaveLen(1))
Expect(jobs[0].Status.Succeeded).To(BeNumerically("==", 1))
Expect(jobs[0].Status.Active).To(BeNumerically("==", 0))
Expect(jobs[0].Status.Failed).To(BeNumerically("==", 0))
})
})
Expand Down Expand Up @@ -112,9 +114,11 @@ var _ = Describe("Job Generation", func() {
It("should apply successfully after edit", func() {
Expect(jobs).To(HaveLen(1))
Expect(jobs[0].Status.Succeeded).To(BeNumerically("==", 1))
Expect(jobs[0].Status.Active).To(BeNumerically("==", 0))
Expect(jobs[0].Status.Failed).To(BeNumerically("==", 0))
Expect(jobs[0].Spec.Template.Spec.InitContainers).To(HaveLen(1))
Expect(jobs[0].Spec.Template.Spec.InitContainers[0].Args).To(ContainSubstring("csi-attacher"))
Expect(jobs[0].Spec.Template.Spec.InitContainers[0].Args).To(ContainSubstring("!upgrade.cattle.io/controller"))
Expect(jobs[0].Spec.Template.Spec.InitContainers[0].Args).To(ContainSubstring("app notin (csi-attacher,csi-provisioner)"))
})
})
})
17 changes: 14 additions & 3 deletions pkg/upgrade/job/job.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/selection"
)

const (
Expand Down Expand Up @@ -240,12 +241,22 @@ func New(plan *upgradeapiv1.Plan, node *corev1.Node, controllerName string) *bat
// then we cordon/drain
cordon, drain := plan.Spec.Cordon, plan.Spec.Drain
if drain != nil {
podSelector := `!` + upgradeapi.LabelController
controllerRequirement, _ := labels.NewRequirement(upgradeapi.LabelController, selection.DoesNotExist, nil)
podSelector := labels.NewSelector().Add(*controllerRequirement)

if drain.PodSelector != nil {
podSelector = podSelector + `,` + drain.PodSelector.String()
if selector, err := metav1.LabelSelectorAsSelector(drain.PodSelector); err != nil {
logrus.Warnf("failed to convert Spec.Drain.PodSelector to selector: %v", err)
} else {
if requirements, ok := selector.Requirements(); !ok {
logrus.Warnf("Spec.Drain.PodSelector requirements are not selectable")
} else {
podSelector = podSelector.Add(requirements...)
}
}
}

args := []string{"drain", node.Name, "--pod-selector", podSelector}
args := []string{"drain", node.Name, "--pod-selector", podSelector.String()}
if drain.IgnoreDaemonSets == nil || *plan.Spec.Drain.IgnoreDaemonSets {
args = append(args, "--ignore-daemonsets")
}
Expand Down
12 changes: 11 additions & 1 deletion pkg/upgrade/plan/plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ const (
)

var (
ErrDrainDeleteConflict = fmt.Errorf("spec.drain cannot specify both deleteEmptydirData and deleteLocalData")
ErrDrainDeleteConflict = fmt.Errorf("spec.drain cannot specify both deleteEmptydirData and deleteLocalData")
ErrDrainPodSelectorNotSelectable = fmt.Errorf("spec.drain.podSelector is not selectable")

PollingInterval = func(defaultValue time.Duration) time.Duration {
if str, ok := os.LookupEnv("SYSTEM_UPGRADE_PLAN_POLLING_INTERVAL"); ok {
Expand Down Expand Up @@ -237,6 +238,15 @@ func Validate(plan *upgradeapiv1.Plan) error {
if drainSpec.DeleteEmptydirData != nil && drainSpec.DeleteLocalData != nil {
return ErrDrainDeleteConflict
}
if drainSpec.PodSelector != nil {
selector, err := metav1.LabelSelectorAsSelector(drainSpec.PodSelector)
if err != nil {
return err
}
if _, ok := selector.Requirements(); !ok {
return ErrDrainPodSelectorNotSelectable
}
}
}
return nil
}