Merge pull request #107559 from liggitt/invalid-selectors

Handle invalid selectors properly

Kubernetes-commit: 184daed0dbb6b9adac82b7b8b58f5cee0935d152
This commit is contained in:
Kubernetes Publisher
2022-01-19 14:49:31 -08:00
14 changed files with 39 additions and 29 deletions

8
go.mod
View File

@@ -30,8 +30,8 @@ require (
golang.org/x/term v0.0.0-20210615171337-6886f2dfbf5b
golang.org/x/time v0.0.0-20210723032227-1f47c861a9ac
google.golang.org/protobuf v1.27.1
k8s.io/api v0.0.0-20220114212057-37c9308dad1e
k8s.io/apimachinery v0.0.0-20220114211744-3c16f3dcfb0b
k8s.io/api v0.0.0-20220118151121-94676c7a1e94
k8s.io/apimachinery v0.0.0-20220118200222-162a22fc9219
k8s.io/klog/v2 v2.40.1
k8s.io/kube-openapi v0.0.0-20211115234752-e816edb12b65
k8s.io/utils v0.0.0-20211208161948-7d6a63dca704
@@ -40,6 +40,6 @@ require (
)
replace (
k8s.io/api => k8s.io/api v0.0.0-20220114212057-37c9308dad1e
k8s.io/apimachinery => k8s.io/apimachinery v0.0.0-20220114211744-3c16f3dcfb0b
k8s.io/api => k8s.io/api v0.0.0-20220118151121-94676c7a1e94
k8s.io/apimachinery => k8s.io/apimachinery v0.0.0-20220118200222-162a22fc9219
)

8
go.sum
View File

@@ -610,10 +610,10 @@ honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc/go.mod h1:rf3lG4BRIbNafJWh
honnef.co/go/tools v0.0.1-2019.2.3/go.mod h1:a3bituU0lyd329TUQxRnasdCoJDkEUEAqEt0JzvZhAg=
honnef.co/go/tools v0.0.1-2020.1.3/go.mod h1:X/FiERA/W4tHapMX5mGpAtMSVEeEUOyHaw9vFzvIQ3k=
honnef.co/go/tools v0.0.1-2020.1.4/go.mod h1:X/FiERA/W4tHapMX5mGpAtMSVEeEUOyHaw9vFzvIQ3k=
k8s.io/api v0.0.0-20220114212057-37c9308dad1e h1:yq1koxef1IPugvWJ0datOgWkA3bq6EQ/laIdBCRNV2M=
k8s.io/api v0.0.0-20220114212057-37c9308dad1e/go.mod h1:ydI/EZB8sQSGGgqIugVCax6qT4v8+NZ6Hbduo30cjbo=
k8s.io/apimachinery v0.0.0-20220114211744-3c16f3dcfb0b h1:7lh+0FnYetRD5shepu42q+Su/LHlUy2VgoeoQN1sd3Y=
k8s.io/apimachinery v0.0.0-20220114211744-3c16f3dcfb0b/go.mod h1:v8xabFQhCZrrX2Lm1DcO0GldEoXSxCi8KODudZ/jv5s=
k8s.io/api v0.0.0-20220118151121-94676c7a1e94 h1:K7V5wkq10suJgLBaL+f2EueXASB7EHppMWM8YbbE3ac=
k8s.io/api v0.0.0-20220118151121-94676c7a1e94/go.mod h1:ydI/EZB8sQSGGgqIugVCax6qT4v8+NZ6Hbduo30cjbo=
k8s.io/apimachinery v0.0.0-20220118200222-162a22fc9219 h1:loxHul3sbedOz2qdU+RyfXKJ9ykk3hjHYPbbKwYBg8I=
k8s.io/apimachinery v0.0.0-20220118200222-162a22fc9219/go.mod h1:v8xabFQhCZrrX2Lm1DcO0GldEoXSxCi8KODudZ/jv5s=
k8s.io/gengo v0.0.0-20210813121822-485abfe95c7c/go.mod h1:FiNAH4ZV3gBg2Kwh89tzAEV2be7d5xI0vBa/VySYy3E=
k8s.io/klog/v2 v2.0.0/go.mod h1:PBfzABfn139FHAV07az/IF9Wp1bkk3vpT2XSJ76fSDE=
k8s.io/klog/v2 v2.2.0/go.mod h1:Od+F08eJP+W3HUb4pSrPpgp9DGU4GzlpG/TmITuYh/Y=

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
}