Skip to content

Commit

Permalink
modify sidecarSet webhook and controller, add e2e test
Browse files Browse the repository at this point in the history
Signed-off-by: pingjiang <xiangpingjiang1998@gmail.com>
  • Loading branch information
xiangpingjiang committed Aug 15, 2023
1 parent 93326ca commit c7959cc
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 3 deletions.
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() == "" {
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

0 comments on commit c7959cc

Please sign in to comment.