Handle invalid selectors properly

Kubernetes-commit: c0af728f43025bb47b209ebbb45604f9c0424354
This commit is contained in:
Jordan Liggitt 2022-01-14 11:41:07 -05:00 committed by Kubernetes Publisher
parent 664b1a6c8c
commit aab0bb899e
12 changed files with 31 additions and 21 deletions

View File

@ -60,8 +60,8 @@ func (s *daemonSetLister) GetPodDaemonSets(pod *v1.Pod) ([]*apps.DaemonSet, erro
}
selector, err = metav1.LabelSelectorAsSelector(daemonSet.Spec.Selector)
if err != nil {
// this should not happen if the DaemonSet passed validation
return nil, err
// This object has an invalid selector, it does not match the pod
continue
}
// If a daemonSet with a nil or empty selector creeps in, it should match nothing, not everything.
@ -96,7 +96,8 @@ func (s *daemonSetLister) GetHistoryDaemonSets(history *apps.ControllerRevision)
for _, ds := range list {
selector, err := metav1.LabelSelectorAsSelector(ds.Spec.Selector)
if err != nil {
return nil, fmt.Errorf("invalid label selector: %v", err)
// This object has an invalid selector, it does not match the history
continue
}
// If a DaemonSet with a nil or empty selector creeps in, it should match nothing, not everything.
if selector.Empty() || !selector.Matches(labels.Set(history.Labels)) {

View File

@ -55,7 +55,8 @@ func (s *replicaSetLister) GetPodReplicaSets(pod *v1.Pod) ([]*apps.ReplicaSet, e
}
selector, err := metav1.LabelSelectorAsSelector(rs.Spec.Selector)
if err != nil {
return nil, fmt.Errorf("invalid selector: %v", err)
// This object has an invalid selector, it does not match the pod
continue
}
// If a ReplicaSet with a nil or empty selector creeps in, it should match nothing, not everything.

View File

@ -59,7 +59,8 @@ func (s *statefulSetLister) GetPodStatefulSets(pod *v1.Pod) ([]*apps.StatefulSet
}
selector, err = metav1.LabelSelectorAsSelector(ps.Spec.Selector)
if err != nil {
return nil, fmt.Errorf("invalid selector: %v", err)
// This object has an invalid selector, it does not match the pod
continue
}
// If a StatefulSet with a nil or empty selector creeps in, it should match nothing, not everything.

View File

@ -59,7 +59,8 @@ func (s *statefulSetLister) GetPodStatefulSets(pod *v1.Pod) ([]*apps.StatefulSet
}
selector, err = metav1.LabelSelectorAsSelector(ps.Spec.Selector)
if err != nil {
return nil, fmt.Errorf("invalid selector: %v", err)
// This object has an invalid selector, it does not match the pod
continue
}
// If a StatefulSet with a nil or empty selector creeps in, it should match nothing, not everything.

View File

@ -60,8 +60,8 @@ func (s *daemonSetLister) GetPodDaemonSets(pod *v1.Pod) ([]*apps.DaemonSet, erro
}
selector, err = metav1.LabelSelectorAsSelector(daemonSet.Spec.Selector)
if err != nil {
// this should not happen if the DaemonSet passed validation
return nil, err
// This object has an invalid selector, it does not match the pod
continue
}
// If a daemonSet with a nil or empty selector creeps in, it should match nothing, not everything.
@ -96,7 +96,8 @@ func (s *daemonSetLister) GetHistoryDaemonSets(history *apps.ControllerRevision)
for _, ds := range list {
selector, err := metav1.LabelSelectorAsSelector(ds.Spec.Selector)
if err != nil {
return nil, fmt.Errorf("invalid label selector: %v", err)
// This object has an invalid selector, it does not match the history object
continue
}
// If a DaemonSet with a nil or empty selector creeps in, it should match nothing, not everything.
if selector.Empty() || !selector.Matches(labels.Set(history.Labels)) {

View File

@ -55,7 +55,8 @@ func (s *replicaSetLister) GetPodReplicaSets(pod *v1.Pod) ([]*apps.ReplicaSet, e
}
selector, err := metav1.LabelSelectorAsSelector(rs.Spec.Selector)
if err != nil {
return nil, fmt.Errorf("invalid selector: %v", err)
// This object has an invalid selector, it does not match the pod
continue
}
// If a ReplicaSet with a nil or empty selector creeps in, it should match nothing, not everything.

View File

@ -59,7 +59,8 @@ func (s *statefulSetLister) GetPodStatefulSets(pod *v1.Pod) ([]*apps.StatefulSet
}
selector, err = metav1.LabelSelectorAsSelector(ps.Spec.Selector)
if err != nil {
return nil, fmt.Errorf("invalid selector: %v", err)
// This object has an invalid selector, it does not match the pod
continue
}
// If a StatefulSet with a nil or empty selector creeps in, it should match nothing, not everything.

View File

@ -51,7 +51,11 @@ func (l *jobLister) GetPodJobs(pod *v1.Pod) (jobs []batch.Job, err error) {
return
}
for _, job := range list {
selector, _ := metav1.LabelSelectorAsSelector(job.Spec.Selector)
selector, err := metav1.LabelSelectorAsSelector(job.Spec.Selector)
if err != nil {
// This object has an invalid selector, it does not match the pod
continue
}
if !selector.Matches(labels.Set(pod.Labels)) {
continue
}

View File

@ -61,8 +61,8 @@ func (s *daemonSetLister) GetPodDaemonSets(pod *v1.Pod) ([]*v1beta1.DaemonSet, e
}
selector, err = metav1.LabelSelectorAsSelector(daemonSet.Spec.Selector)
if err != nil {
// this should not happen if the DaemonSet passed validation
return nil, err
// This object has an invalid selector, it does not match the pod
continue
}
// If a daemonSet with a nil or empty selector creeps in, it should match nothing, not everything.
@ -97,7 +97,8 @@ func (s *daemonSetLister) GetHistoryDaemonSets(history *apps.ControllerRevision)
for _, ds := range list {
selector, err := metav1.LabelSelectorAsSelector(ds.Spec.Selector)
if err != nil {
return nil, fmt.Errorf("invalid label selector: %v", err)
// This object has an invalid selector, it does not match the history object
continue
}
// If a DaemonSet with a nil or empty selector creeps in, it should match nothing, not everything.
if selector.Empty() || !selector.Matches(labels.Set(history.Labels)) {

View File

@ -55,7 +55,8 @@ func (s *replicaSetLister) GetPodReplicaSets(pod *v1.Pod) ([]*extensions.Replica
}
selector, err := metav1.LabelSelectorAsSelector(rs.Spec.Selector)
if err != nil {
return nil, fmt.Errorf("invalid selector: %v", err)
// This object has an invalid selector, it does not match the pod
continue
}
// If a ReplicaSet with a nil or empty selector creeps in, it should match nothing, not everything.

View File

@ -23,7 +23,6 @@ import (
policy "k8s.io/api/policy/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/klog/v2"
)
// PodDisruptionBudgetListerExpansion allows custom methods to be added to
@ -50,7 +49,7 @@ func (s *podDisruptionBudgetLister) GetPodPodDisruptionBudgets(pod *v1.Pod) ([]*
pdb := list[i]
selector, err = metav1.LabelSelectorAsSelector(pdb.Spec.Selector)
if err != nil {
klog.Warningf("invalid selector: %v", err)
// This object has an invalid selector, it does not match the pod
continue
}

View File

@ -23,7 +23,6 @@ import (
policy "k8s.io/api/policy/v1beta1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/klog/v2"
)
// PodDisruptionBudgetListerExpansion allows custom methods to be added to
@ -50,8 +49,7 @@ func (s *podDisruptionBudgetLister) GetPodPodDisruptionBudgets(pod *v1.Pod) ([]*
pdb := list[i]
selector, err = metav1.LabelSelectorAsSelector(pdb.Spec.Selector)
if err != nil {
klog.Warningf("invalid selector: %v", err)
// TODO(mml): add an event to the PDB
// This object has an invalid selector, it does not match the pod
continue
}