Skip to content

Commit

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

Automated cherry pick of #3052: fix that the InterpretDependency operation is absent in the
  • Loading branch information
karmada-bot committed Jan 28, 2023
2 parents 6192db6 + 7f51a87 commit 5bd15cd
Show file tree
Hide file tree
Showing 5 changed files with 57 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", "InterpretHealth", "InterpretStatus" ]
- operations: [ "InterpretReplica","ReviseReplica","Retain","AggregateStatus", "InterpretHealth", "InterpretStatus", "InterpretDependency" ]
apiGroups: [ "workload.example.io" ]
apiVersions: [ "v1alpha1" ]
kinds: [ "Workload" ]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ func (e *workloadInterpreter) Handle(ctx context.Context, req interpreter.Reques
return e.responseWithExploreInterpretHealth(workload)
case configv1alpha1.InterpreterOperationInterpretStatus:
return e.responseWithExploreInterpretStatus(workload)
case configv1alpha1.InterpreterOperationInterpretDependency:
return e.responseWithExploreDependency(workload)
default:
return interpreter.Errored(http.StatusBadRequest, fmt.Errorf("wrong request operation type: %s", req.Operation))
}
Expand All @@ -63,6 +65,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
45 changes: 45 additions & 0 deletions test/e2e/resourceinterpreter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/onsi/gomega"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/rand"
Expand Down Expand Up @@ -273,6 +274,50 @@ 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)))
})
})
})
})

var _ = framework.SerialDescribe("Resource interpreter customization testing", func() {
Expand Down
2 changes: 1 addition & 1 deletion test/helper/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -505,7 +505,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 5bd15cd

Please sign in to comment.