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

SidecarSet inject sidecar container to all Pods in some namespaces #1369

Closed
Closed
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
4 changes: 4 additions & 0 deletions pkg/control/sidecarcontrol/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,10 @@ func PodMatchedSidecarSet(c client.Client, pod *corev1.Pod, sidecarSet *appsv1al
if err != nil {
return false, err
}
// if no selector set, return true
if selector.String() == "" {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if selector.Empty() {

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

selector.Empty() return false when no selector set, so I turned to selector.String() == ""

return true, nil
}

if !selector.Empty() && selector.Matches(labels.Set(pod.Labels)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove the !selector.Empty()

return true, nil
Expand Down
26 changes: 26 additions & 0 deletions pkg/control/sidecarcontrol/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1244,6 +1244,32 @@ func TestPodMatchedSidecarSet(t *testing.T) {
},
expect: false,
},
{
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You need modidy the sidecarSet webhook and controller.

And you must add e2e for this scenario.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done, please review my latest pr, thanks

name: "test6",
getSidecarSet: func() *appsv1alpha1.SidecarSet {
demo := &appsv1alpha1.SidecarSet{
ObjectMeta: metav1.ObjectMeta{Name: "sidecarset-test"},
Spec: appsv1alpha1.SidecarSetSpec{
Namespace: "app1",
},
}
return demo
},
getPod: func() *corev1.Pod {
demo := &corev1.Pod{
ObjectMeta: metav1.ObjectMeta{
Name: "test-pod",
Labels: map[string]string{"app": "nginx"},
Namespace: "app1",
},
}
return demo
},
getNs: func() []*corev1.Namespace {
return nil
},
expect: true,
},
}

for _, cs := range cases {
Expand Down
3 changes: 3 additions & 0 deletions pkg/controller/sidecarset/sidecarset_processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() == "" {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove the If, and i think it will be ok.

Copy link
Contributor Author

@xiangpingjiang xiangpingjiang Sep 6, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It won't work,
Because if we remove

	if selector.String() == "" {
		listOpts = &client.ListOptions{}
	}

when sidecarSet.Spec.Selector = nil , p.Client.List(context.TODO(), allPods, listOpts, utilclient.DisableDeepCopy) get always allPods.Items = 0.

listOpts = &client.ListOptions{}
}
for _, ns := range namespaces.List() {
allPods := &corev1.PodList{}
listOpts.Namespace = ns
Expand Down
10 changes: 10 additions & 0 deletions pkg/controller/sidecarset/sidecarset_processor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
26 changes: 26 additions & 0 deletions test/e2e/apps/sidecarset.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Loading