diff --git a/pkg/controller/sidecarset/sidecarset_processor.go b/pkg/controller/sidecarset/sidecarset_processor.go index da3e06c9ec..4e4cec1479 100644 --- a/pkg/controller/sidecarset/sidecarset_processor.go +++ b/pkg/controller/sidecarset/sidecarset_processor.go @@ -310,6 +310,9 @@ func (p *Processor) getMatchingPods(s *appsv1alpha1.SidecarSet) ([]*corev1.Pod, func (p *Processor) getSelectedPods(namespaces sets.String, selector labels.Selector) (relatedPods []*corev1.Pod, err error) { // DisableDeepCopy:true, indicates must be deep copy before update pod objection listOpts := &client.ListOptions{LabelSelector: selector} + if selector.String() == "" { + listOpts = &client.ListOptions{} + } for _, ns := range namespaces.List() { allPods := &corev1.PodList{} listOpts.Namespace = ns diff --git a/pkg/controller/sidecarset/sidecarset_processor_test.go b/pkg/controller/sidecarset/sidecarset_processor_test.go index 8752b928d3..fc03994164 100644 --- a/pkg/controller/sidecarset/sidecarset_processor_test.go +++ b/pkg/controller/sidecarset/sidecarset_processor_test.go @@ -261,6 +261,16 @@ func TestScopeNamespacePods(t *testing.T) { if len(pods) != 50 { t.Fatalf("except matching pods count(%d), but get count(%d)", 50, len(pods)) } + sidecarSet.Spec.Selector = nil + pods, err = processor.getMatchingPods(sidecarSet) + if err != nil { + t.Fatalf("getMatchingPods failed: %s", err.Error()) + return + } + if len(pods) != 50 { + t.Fatalf("except matching pods count(%d), but get count(%d)", 50, len(pods)) + } + } func TestCanUpgradePods(t *testing.T) { diff --git a/pkg/webhook/sidecarset/validating/sidecarset_create_update_handler.go b/pkg/webhook/sidecarset/validating/sidecarset_create_update_handler.go index 10611abce3..ae082d810c 100644 --- a/pkg/webhook/sidecarset/validating/sidecarset_create_update_handler.go +++ b/pkg/webhook/sidecarset/validating/sidecarset_create_update_handler.go @@ -115,11 +115,12 @@ func (h *SidecarSetCreateUpdateHandler) validateSidecarSetSpec(obj *appsv1alpha1 allErrs := field.ErrorList{} //validate spec selector - if spec.Selector == nil { - allErrs = append(allErrs, field.Required(fldPath.Child("selector"), "no selector defined for SidecarSet")) - } else { + if spec.Selector != nil { allErrs = append(allErrs, validateSelector(spec.Selector, fldPath.Child("selector"))...) } + if spec.Selector == nil && spec.Namespace == "" && spec.NamespaceSelector == nil { + allErrs = append(allErrs, field.Required(fldPath.Child("selector"), "no selector defined for SidecarSet")) + } if spec.Namespace != "" && spec.NamespaceSelector != nil { allErrs = append(allErrs, field.Required(fldPath.Child("namespace, namespaceSelector"), "namespace and namespaceSelector are mutually exclusive")) } else if spec.NamespaceSelector != nil { diff --git a/test/e2e/apps/sidecarset.go b/test/e2e/apps/sidecarset.go index 8c636d129b..aa65580ed2 100644 --- a/test/e2e/apps/sidecarset.go +++ b/test/e2e/apps/sidecarset.go @@ -151,6 +151,32 @@ var _ = SIGDescribe("SidecarSet", func() { ginkgo.By(fmt.Sprintf("sidecarSet inject pod sidecar container done")) }) + framework.ConformanceIt("sidecarSet no Selector inject pod sidecar container", func() { + // create sidecarSet + sidecarSet := tester.NewBaseSidecarSet(ns) + sidecarSet.Spec.Selector = nil + ginkgo.By(fmt.Sprintf("Creating SidecarSet %s", sidecarSet.Name)) + tester.CreateSidecarSet(sidecarSet) + time.Sleep(time.Second) + + // create deployment + deployment := tester.NewBaseDeployment(ns) + ginkgo.By(fmt.Sprintf("Creating Deployment(%s/%s)", deployment.Namespace, deployment.Name)) + tester.CreateDeployment(deployment) + + // get pods + pods, err := tester.GetSelectorPods(deployment.Namespace, deployment.Spec.Selector) + gomega.Expect(err).NotTo(gomega.HaveOccurred()) + pod := pods[0] + gomega.Expect(pod.Spec.Containers).To(gomega.HaveLen(len(deployment.Spec.Template.Spec.Containers) + len(sidecarSet.Spec.Containers))) + gomega.Expect(pod.Spec.InitContainers).To(gomega.HaveLen(len(deployment.Spec.Template.Spec.InitContainers) + len(sidecarSet.Spec.InitContainers))) + exceptContainers := []string{"nginx-sidecar", "main", "busybox-sidecar"} + for i, except := range exceptContainers { + gomega.Expect(except).To(gomega.Equal(pod.Spec.Containers[i].Name)) + } + ginkgo.By(fmt.Sprintf("sidecarSet inject pod sidecar container done")) + }) + framework.ConformanceIt("sidecarSet inject pod sidecar container volumeMounts", func() { // create sidecarSet sidecarSet := tester.NewBaseSidecarSet(ns)