Skip to content

Commit

Permalink
Merge pull request #3077 from XiShanYongYe-Chang/automated-cherry-pic…
Browse files Browse the repository at this point in the history
…k-of-#3052-upstream-release-1.2

Automated cherry pick of #3052: fix that the InterpretDependency operation is absent in the
  • Loading branch information
karmada-bot authored Jan 29, 2023
2 parents d7d9b51 + acddf06 commit ae5e32c
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ metadata:
webhooks:
- name: workloads.example.com
rules:
- operations: [ "InterpretReplica","ReviseReplica","Retain","AggregateStatus" ]
- operations: [ "InterpretReplica","ReviseReplica","Retain","AggregateStatus", "InterpretDependency" ]
apiGroups: [ "workload.example.io" ]
apiVersions: [ "v1alpha1" ]
kinds: [ "Workload" ]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ func (e *workloadInterpreter) Handle(ctx context.Context, req interpreter.Reques
return e.responseWithExploreRetaining(workload, req)
case configv1alpha1.InterpreterOperationAggregateStatus:
return e.responseWithExploreAggregateStatus(workload, req)
case configv1alpha1.InterpreterOperationInterpretDependency:
return e.responseWithExploreDependency(workload)
default:
return interpreter.Errored(http.StatusBadRequest, fmt.Errorf("wrong request operation type: %s", req.Operation))
}
Expand All @@ -57,6 +59,13 @@ func (e *workloadInterpreter) responseWithExploreReplica(workload *workloadv1alp
return res
}

func (e *workloadInterpreter) responseWithExploreDependency(workload *workloadv1alpha1.Workload) interpreter.Response {
res := interpreter.Succeeded("")
res.Dependencies = []configv1alpha1.DependentObjectReference{{APIVersion: "v1", Kind: "ConfigMap",
Namespace: workload.Namespace, Name: workload.Spec.Template.Spec.Containers[0].EnvFrom[0].ConfigMapRef.Name}}
return res
}

func (e *workloadInterpreter) responseWithExploreReviseReplica(workload *workloadv1alpha1.Workload, req interpreter.Request) interpreter.Response {
wantedWorkload := workload.DeepCopy()
wantedWorkload.Spec.Replicas = req.DesiredReplicas
Expand Down
1 change: 1 addition & 0 deletions pkg/webhook/configuration/validating.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ func (v *ValidatingAdmission) InjectDecoder(d *admission.Decoder) error {
var supportedInterpreterOperation = sets.NewString(
string(configv1alpha1.InterpreterOperationAll),
string(configv1alpha1.InterpreterOperationInterpretReplica),
string(configv1alpha1.InterpreterOperationInterpretDependency),
string(configv1alpha1.InterpreterOperationReviseReplica),
string(configv1alpha1.InterpreterOperationRetain),
string(configv1alpha1.InterpreterOperationAggregateStatus),
Expand Down
46 changes: 46 additions & 0 deletions test/e2e/resourceinterpreter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (

"github.com/onsi/ginkgo/v2"
"github.com/onsi/gomega"
corev1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/rand"
"k8s.io/apimachinery/pkg/util/wait"
Expand Down Expand Up @@ -190,4 +192,48 @@ var _ = ginkgo.Describe("Resource interpreter webhook testing", func() {
})
})
})

ginkgo.Context("InterpreterOperation InterpretDependency testing", func() {
var configMapNamespace, configMapName string

ginkgo.BeforeEach(func() {
configMapNamespace = testNamespace
configMapName = configMapNamePrefix + rand.String(RandomStrLength)

workload.Spec.Template.Spec.Containers[0].EnvFrom = []corev1.EnvFromSource{{
ConfigMapRef: &corev1.ConfigMapEnvSource{
LocalObjectReference: corev1.LocalObjectReference{Name: configMapName},
}}}
// configmaps should be propagated automatically.
policy.Spec.PropagateDeps = true

cm := testhelper.NewConfigMap(configMapNamespace, configMapName, map[string]string{"RUN_ENV": "test"})

framework.CreateConfigMap(kubeClient, cm)
ginkgo.DeferCleanup(func() {
framework.RemoveConfigMap(kubeClient, configMapNamespace, configMapName)
})
})

ginkgo.It("InterpretDependency testing", func() {
ginkgo.By("check if workload's dependency is interpreted", func() {
clusterNames := framework.ClusterNames()
gomega.Eventually(func(g gomega.Gomega) (int, error) {
var configmapNum int
for _, clusterName := range clusterNames {
clusterClient := framework.GetClusterClient(clusterName)
gomega.Expect(clusterClient).ShouldNot(gomega.BeNil())
if _, err := clusterClient.CoreV1().ConfigMaps(configMapNamespace).Get(context.TODO(), configMapName, metav1.GetOptions{}); err != nil {
if apierrors.IsNotFound(err) {
continue
}
g.Expect(err).NotTo(gomega.HaveOccurred())
}
configmapNum++
}
return configmapNum, nil
}, pollTimeout, pollInterval).Should(gomega.Equal(len(clusterNames)))
})
})
})
})
2 changes: 1 addition & 1 deletion test/helper/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -496,7 +496,7 @@ func NewClusterWithResource(name string, allocatable, allocating, allocated core
}

// NewWorkload will build a workload object.
func NewWorkload(namespace string, name string) *workloadv1alpha1.Workload {
func NewWorkload(namespace, name string) *workloadv1alpha1.Workload {
podLabels := map[string]string{"app": "nginx"}

return &workloadv1alpha1.Workload{
Expand Down

0 comments on commit ae5e32c

Please sign in to comment.