Skip to content

Commit

Permalink
add e2e test for scheduling base on cluster provider/region
Browse files Browse the repository at this point in the history
Signed-off-by: lihanbo <lihanbo2@huawei.com>
  • Loading branch information
mrlihanbo committed May 24, 2021
1 parent 8cf024e commit 3a98a56
Show file tree
Hide file tree
Showing 7 changed files with 198 additions and 34 deletions.
25 changes: 0 additions & 25 deletions pkg/util/helper/policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,10 @@ package helper
import (
"fmt"

v1 "k8s.io/api/core/v1"
"k8s.io/klog/v2"
"sigs.k8s.io/controller-runtime/pkg/client"

policyv1alpha1 "github.com/karmada-io/karmada/pkg/apis/policy/v1alpha1"
"github.com/karmada-io/karmada/pkg/util"
)

// DenyReasonResourceSelectorsModify constructs a reason indicating that modify ResourceSelectors is not allowed.
Expand Down Expand Up @@ -75,26 +73,3 @@ func IsDependentClusterOverridesPresent(c client.Client, policy *policyv1alpha1.

return true, nil
}

// ValidateFieldSelector tests if the fieldSelector is valid.
func ValidateFieldSelector(fieldSelector *policyv1alpha1.FieldSelector) error {
if fieldSelector == nil {
return nil
}

for _, matchExpression := range fieldSelector.MatchExpressions {
switch matchExpression.Key {
case util.ProviderField, util.RegionField, util.ZoneField:
default:
return fmt.Errorf("unsupported key %q, must be provider, region, or zone", matchExpression.Key)
}

switch matchExpression.Operator {
case v1.NodeSelectorOpIn, v1.NodeSelectorOpNotIn:
default:
return fmt.Errorf("unsupported operator %q, must be In or NotIn", matchExpression.Operator)
}
}

return nil
}
3 changes: 0 additions & 3 deletions pkg/util/selector.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,9 +157,6 @@ func nodeSelectorRequirementsAsSelector(nsm []v1.NodeSelectorRequirement) (label

func extractClusterFields(cluster *clusterv1alpha1.Cluster) labels.Set {
clusterFieldsMap := make(labels.Set)
if cluster.Name != "" {
clusterFieldsMap["metadata.name"] = cluster.Name
}

if cluster.Spec.Provider != "" {
clusterFieldsMap[ProviderField] = cluster.Spec.Provider
Expand Down
27 changes: 27 additions & 0 deletions pkg/util/validation/validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@ import (
"fmt"
"net/url"

v1 "k8s.io/api/core/v1"
kubevalidation "k8s.io/apimachinery/pkg/util/validation"

policyv1alpha1 "github.com/karmada-io/karmada/pkg/apis/policy/v1alpha1"
"github.com/karmada-io/karmada/pkg/util"
)

const clusterNameMaxLength int = 48
Expand Down Expand Up @@ -44,3 +48,26 @@ func ValidateClusterProxyURL(proxyURL string) []string {

return nil
}

// ValidatePolicyFieldSelector tests if the fieldSelector of propagation policy is valid.
func ValidatePolicyFieldSelector(fieldSelector *policyv1alpha1.FieldSelector) error {
if fieldSelector == nil {
return nil
}

for _, matchExpression := range fieldSelector.MatchExpressions {
switch matchExpression.Key {
case util.ProviderField, util.RegionField, util.ZoneField:
default:
return fmt.Errorf("unsupported key %q, must be provider, region, or zone", matchExpression.Key)
}

switch matchExpression.Operator {
case v1.NodeSelectorOpIn, v1.NodeSelectorOpNotIn:
default:
return fmt.Errorf("unsupported operator %q, must be In or NotIn", matchExpression.Operator)
}
}

return nil
}
10 changes: 7 additions & 3 deletions pkg/webhook/clusterpropagationpolicy/validating.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (

policyv1alpha1 "github.com/karmada-io/karmada/pkg/apis/policy/v1alpha1"
"github.com/karmada-io/karmada/pkg/util/helper"
"github.com/karmada-io/karmada/pkg/util/validation"
)

// ValidatingAdmission validates ClusterPropagationPolicy object when creating/updating/deleting.
Expand Down Expand Up @@ -50,9 +51,12 @@ func (v *ValidatingAdmission) Handle(ctx context.Context, req admission.Request)
return admission.Denied(err.Error())
}

if err := helper.ValidateFieldSelector(policy.Spec.Placement.ClusterAffinity.FieldSelector); err != nil {
klog.Error(err)
return admission.Denied(err.Error())
if policy.Spec.Placement.ClusterAffinity != nil && policy.Spec.Placement.ClusterAffinity.FieldSelector != nil {
err := validation.ValidatePolicyFieldSelector(policy.Spec.Placement.ClusterAffinity.FieldSelector)
if err != nil {
klog.Error(err)
return admission.Denied(err.Error())
}
}

return admission.Allowed("")
Expand Down
10 changes: 7 additions & 3 deletions pkg/webhook/propagationpolicy/validating.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (

policyv1alpha1 "github.com/karmada-io/karmada/pkg/apis/policy/v1alpha1"
"github.com/karmada-io/karmada/pkg/util/helper"
"github.com/karmada-io/karmada/pkg/util/validation"
)

// ValidatingAdmission validates PropagationPolicy object when creating/updating/deleting.
Expand Down Expand Up @@ -50,9 +51,12 @@ func (v *ValidatingAdmission) Handle(ctx context.Context, req admission.Request)
return admission.Denied(err.Error())
}

if err := helper.ValidateFieldSelector(policy.Spec.Placement.ClusterAffinity.FieldSelector); err != nil {
klog.Error(err)
return admission.Denied(err.Error())
if policy.Spec.Placement.ClusterAffinity != nil && policy.Spec.Placement.ClusterAffinity.FieldSelector != nil {
err := validation.ValidatePolicyFieldSelector(policy.Spec.Placement.ClusterAffinity.FieldSelector)
if err != nil {
klog.Error(err)
return admission.Denied(err.Error())
}
}

return admission.Allowed("")
Expand Down
127 changes: 127 additions & 0 deletions test/e2e/fieldselector_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
package e2e

import (
"context"
"fmt"

"github.com/onsi/ginkgo"
"github.com/onsi/gomega"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/rand"
"sigs.k8s.io/controller-runtime/pkg/client"

clusterv1alpha1 "github.com/karmada-io/karmada/pkg/apis/cluster/v1alpha1"
policyv1alpha1 "github.com/karmada-io/karmada/pkg/apis/policy/v1alpha1"
"github.com/karmada-io/karmada/pkg/util"
"github.com/karmada-io/karmada/test/helper"
)

var _ = ginkgo.Describe("propagation with fieldSelector testing", func() {
ginkgo.Context("Deployment propagation testing", func() {
policyNamespace := testNamespace
policyName := deploymentNamePrefix + rand.String(RandomStrLength)
deploymentNamespace := testNamespace
deploymentName := policyName
deployment := helper.NewDeployment(deploymentNamespace, deploymentName)

originalClusterProviderInfo := make(map[string]string)
originalClusterRegionInfo := make(map[string]string)
desiredProvider := []string{"huaweicloud"}
undesiredRegion := []string{"cn-north-1"}
desiredScheduleResult := "member1"

// desire to schedule to clusters of huaweicloud but not in cn-north-1 region
filedSelector := &policyv1alpha1.FieldSelector{
MatchExpressions: []corev1.NodeSelectorRequirement{
{
Key: util.ProviderField,
Operator: corev1.NodeSelectorOpIn,
Values: desiredProvider,
},
{
Key: util.RegionField,
Operator: corev1.NodeSelectorOpNotIn,
Values: undesiredRegion,
},
},
}

policy := helper.NewPolicyWithFieldSelector(policyNamespace, policyName, deployment, clusterNames, filedSelector)

ginkgo.BeforeEach(func() {
ginkgo.By(fmt.Sprintf("creating policy(%s/%s)", policyNamespace, policyName), func() {
_, err := karmadaClient.PolicyV1alpha1().PropagationPolicies(policyNamespace).Create(context.TODO(), policy, metav1.CreateOptions{})
gomega.Expect(err).ShouldNot(gomega.HaveOccurred())
})
})

ginkgo.BeforeEach(func() {
ginkgo.By("setting provider and region for clusters", func() {
providerMap := []string{"huaweicloud", "huaweicloud", "kind"}
regionMap := []string{"cn-south-1", "cn-north-1", "cn-east-1"}
for index, cluster := range clusterNames {
if index > 2 {
break
}
fmt.Printf("setting provider and region for cluster %v", cluster)
clusterObj := &clusterv1alpha1.Cluster{}
err := controlPlaneClient.Get(context.TODO(), client.ObjectKey{Name: cluster}, clusterObj)
gomega.Expect(err).ShouldNot(gomega.HaveOccurred())

originalClusterProviderInfo[cluster] = clusterObj.Spec.Provider
originalClusterRegionInfo[cluster] = clusterObj.Spec.Region
clusterObj.Spec.Provider = providerMap[index]
clusterObj.Spec.Region = regionMap[index]

err = controlPlaneClient.Update(context.TODO(), clusterObj)
gomega.Expect(err).ShouldNot(gomega.HaveOccurred())
}
})
})

ginkgo.AfterEach(func() {
ginkgo.By(fmt.Sprintf("removing policy(%s/%s)", policyNamespace, policyName), func() {
err := karmadaClient.PolicyV1alpha1().PropagationPolicies(policyNamespace).Delete(context.TODO(), policyName, metav1.DeleteOptions{})
gomega.Expect(err).ShouldNot(gomega.HaveOccurred())
})
})

ginkgo.AfterEach(func() {
ginkgo.By("recovering provider and region for clusters", func() {
for index, cluster := range clusterNames {
if index > 2 {
break
}
clusterObj := &clusterv1alpha1.Cluster{}
err := controlPlaneClient.Get(context.TODO(), client.ObjectKey{Name: cluster}, clusterObj)
gomega.Expect(err).ShouldNot(gomega.HaveOccurred())

clusterObj.Spec.Provider = originalClusterProviderInfo[cluster]
clusterObj.Spec.Region = originalClusterRegionInfo[cluster]
err = controlPlaneClient.Update(context.TODO(), clusterObj)
gomega.Expect(err).ShouldNot(gomega.HaveOccurred())
}
})
})

ginkgo.It("propagation with fieldSelector testing", func() {
ginkgo.By(fmt.Sprintf("creating deployment(%s/%s)", deploymentNamespace, deploymentName), func() {
_, err := kubeClient.AppsV1().Deployments(testNamespace).Create(context.TODO(), deployment, metav1.CreateOptions{})
gomega.Expect(err).ShouldNot(gomega.HaveOccurred())
})

ginkgo.By("check whether deployment is scheduled to clusters which meeting the fieldSelector requirements", func() {
targetClusterNames, err := getTargetClusterNames(deployment)
gomega.Expect(err).ShouldNot(gomega.HaveOccurred())
gomega.Expect(len(targetClusterNames) == 1).Should(gomega.BeTrue())
gomega.Expect(targetClusterNames[0] == desiredScheduleResult).Should(gomega.BeTrue())
})

ginkgo.By(fmt.Sprintf("removing deployment(%s/%s)", deploymentNamespace, deploymentName), func() {
err := kubeClient.AppsV1().Deployments(testNamespace).Delete(context.TODO(), deploymentName, metav1.DeleteOptions{})
gomega.Expect(err).ShouldNot(gomega.HaveOccurred())
})
})
})
})
30 changes: 30 additions & 0 deletions test/helper/propagationpolicy.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ func NewPolicyWithClusterToleration(namespace, name string, deployment *appsv1.D
return newPolicyWithClusterToleration(namespace, name, deployment.APIVersion, deployment.Kind, deployment.Name, clusters, clusterTolerations)
}

// NewPolicyWithFieldSelector will build a PropagationPolicy object.
func NewPolicyWithFieldSelector(namespace, name string, deployment *appsv1.Deployment, clusters []string, filedSelector *policyv1alpha1.FieldSelector) *policyv1alpha1.PropagationPolicy {
return newPolicyWithFieldSelector(namespace, name, deployment.APIVersion, deployment.Kind, deployment.Name, clusters, filedSelector)
}

// newPolicy will build a PropagationPolicy object.
func newPolicy(namespace, policyName, apiVersion, kind, resourceName string, clusters []string) *policyv1alpha1.PropagationPolicy {
return &policyv1alpha1.PropagationPolicy{
Expand Down Expand Up @@ -119,3 +124,28 @@ func newPolicyWithClusterToleration(namespace, policyName, apiVersion, kind, res
},
}
}

// newPolicyWithFieldSelector will build a PropagationPolicy object with fieldSelector.
func newPolicyWithFieldSelector(namespace, policyName, apiVersion, kind, resourceName string, clusters []string, filedSelector *policyv1alpha1.FieldSelector) *policyv1alpha1.PropagationPolicy {
return &policyv1alpha1.PropagationPolicy{
ObjectMeta: metav1.ObjectMeta{
Namespace: namespace,
Name: policyName,
},
Spec: policyv1alpha1.PropagationSpec{
ResourceSelectors: []policyv1alpha1.ResourceSelector{
{
APIVersion: apiVersion,
Kind: kind,
Name: resourceName,
},
},
Placement: policyv1alpha1.Placement{
ClusterAffinity: &policyv1alpha1.ClusterAffinity{
ClusterNames: clusters,
FieldSelector: filedSelector,
},
},
},
}
}

0 comments on commit 3a98a56

Please sign in to comment.