Merge pull request #631 from XiShanYongYe-Chang/fix-flake-614

fix flake: propagation with taint and toleration testing
This commit is contained in:
karmada-bot 2021-08-21 18:06:36 +08:00 committed by GitHub
commit ec71095d53
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 59 additions and 25 deletions

View File

@ -59,26 +59,24 @@ var _ = ginkgo.Describe("propagation with taint and toleration testing", func()
ginkgo.BeforeEach(func() { ginkgo.BeforeEach(func() {
ginkgo.By("adding taints to clusters", func() { ginkgo.By("adding taints to clusters", func() {
for _, cluster := range clusterNames { for _, clusterName := range clusterNames {
clusterObj := &clusterv1alpha1.Cluster{} taints := constructAddedTaints(tolerationKey, clusterName)
err := controlPlaneClient.Get(context.TODO(), client.ObjectKey{Name: cluster}, clusterObj)
gomega.Expect(err).ShouldNot(gomega.HaveOccurred())
// TODO(RainbowMango): This will overrides the taints already exist in the cluster. gomega.Eventually(func() bool {
// Should merge new taint to it and cleanup after testing. clusterObj := &clusterv1alpha1.Cluster{}
clusterObj.Spec.Taints = []corev1.Taint{ err := controlPlaneClient.Get(context.TODO(), client.ObjectKey{Name: clusterName}, clusterObj)
{ gomega.Expect(err).ShouldNot(gomega.HaveOccurred())
Key: tolerationKey,
Value: clusterObj.Name,
Effect: corev1.TaintEffectNoSchedule,
},
}
for _, taint := range clusterObj.Spec.Taints { clusterObj.Spec.Taints = append(clusterObj.Spec.Taints, taints...)
klog.Infof("Adding taints(%s) to cluster(%s)", taint.ToString(), clusterObj.Name) klog.Infof("update taints(%s) of cluster(%s)", clusterObj.Spec.Taints, clusterName)
}
err = controlPlaneClient.Update(context.TODO(), clusterObj) err = controlPlaneClient.Update(context.TODO(), clusterObj)
gomega.Expect(err).ShouldNot(gomega.HaveOccurred()) if err != nil {
klog.Errorf("Failed to update cluster(%s), err: %v", clusterName, err)
return false
}
return true
}, pollTimeout, pollInterval).Should(gomega.Equal(true))
} }
}) })
}) })
@ -92,14 +90,22 @@ var _ = ginkgo.Describe("propagation with taint and toleration testing", func()
ginkgo.AfterEach(func() { ginkgo.AfterEach(func() {
ginkgo.By("removing taints in cluster", func() { ginkgo.By("removing taints in cluster", func() {
for _, cluster := range clusterNames { for _, clusterName := range clusterNames {
clusterObj := &clusterv1alpha1.Cluster{} gomega.Eventually(func() bool {
err := controlPlaneClient.Get(context.TODO(), client.ObjectKey{Name: cluster}, clusterObj) clusterObj := &clusterv1alpha1.Cluster{}
gomega.Expect(err).ShouldNot(gomega.HaveOccurred()) err := controlPlaneClient.Get(context.TODO(), client.ObjectKey{Name: clusterName}, clusterObj)
gomega.Expect(err).ShouldNot(gomega.HaveOccurred())
clusterObj.Spec.Taints = nil clusterObj.Spec.Taints = removeTargetFromSource(clusterObj.Spec.Taints, constructAddedTaints(tolerationKey, clusterName))
err = controlPlaneClient.Update(context.TODO(), clusterObj) klog.Infof("update taints(%s) of cluster(%s)", clusterObj.Spec.Taints, clusterName)
gomega.Expect(err).ShouldNot(gomega.HaveOccurred())
err = controlPlaneClient.Update(context.TODO(), clusterObj)
if err != nil {
klog.Errorf("Failed to update cluster(%s), err: %v", clusterName, err)
return false
}
return true
}, pollTimeout, pollInterval).Should(gomega.Equal(true))
} }
}) })
}) })
@ -124,3 +130,31 @@ var _ = ginkgo.Describe("propagation with taint and toleration testing", func()
}) })
}) })
}) })
func constructAddedTaints(tolerationKey, clusterName string) []corev1.Taint {
return []corev1.Taint{
{
Key: tolerationKey,
Value: clusterName,
Effect: corev1.TaintEffectNoSchedule,
},
}
}
func removeTargetFromSource(source, target []corev1.Taint) []corev1.Taint {
var result []corev1.Taint
for si := range source {
deleted := false
for tj := range target {
if source[si].MatchTaint(&target[tj]) {
deleted = true
break
}
}
if !deleted {
result = append(result, source[si])
}
}
return result
}