Skip to content

Commit ae5e32c

Browse files
authored
Merge pull request #3077 from XiShanYongYe-Chang/automated-cherry-pick-of-#3052-upstream-release-1.2
Automated cherry pick of #3052: fix that the InterpretDependency operation is absent in the
2 parents d7d9b51 + acddf06 commit ae5e32c

File tree

5 files changed

+58
-2
lines changed

5 files changed

+58
-2
lines changed

examples/customresourceinterpreter/webhook-configuration.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ metadata:
66
webhooks:
77
- name: workloads.example.com
88
rules:
9-
- operations: [ "InterpretReplica","ReviseReplica","Retain","AggregateStatus" ]
9+
- operations: [ "InterpretReplica","ReviseReplica","Retain","AggregateStatus", "InterpretDependency" ]
1010
apiGroups: [ "workload.example.io" ]
1111
apiVersions: [ "v1alpha1" ]
1212
kinds: [ "Workload" ]

examples/customresourceinterpreter/webhook/app/workloadwebhook.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ func (e *workloadInterpreter) Handle(ctx context.Context, req interpreter.Reques
4141
return e.responseWithExploreRetaining(workload, req)
4242
case configv1alpha1.InterpreterOperationAggregateStatus:
4343
return e.responseWithExploreAggregateStatus(workload, req)
44+
case configv1alpha1.InterpreterOperationInterpretDependency:
45+
return e.responseWithExploreDependency(workload)
4446
default:
4547
return interpreter.Errored(http.StatusBadRequest, fmt.Errorf("wrong request operation type: %s", req.Operation))
4648
}
@@ -57,6 +59,13 @@ func (e *workloadInterpreter) responseWithExploreReplica(workload *workloadv1alp
5759
return res
5860
}
5961

62+
func (e *workloadInterpreter) responseWithExploreDependency(workload *workloadv1alpha1.Workload) interpreter.Response {
63+
res := interpreter.Succeeded("")
64+
res.Dependencies = []configv1alpha1.DependentObjectReference{{APIVersion: "v1", Kind: "ConfigMap",
65+
Namespace: workload.Namespace, Name: workload.Spec.Template.Spec.Containers[0].EnvFrom[0].ConfigMapRef.Name}}
66+
return res
67+
}
68+
6069
func (e *workloadInterpreter) responseWithExploreReviseReplica(workload *workloadv1alpha1.Workload, req interpreter.Request) interpreter.Response {
6170
wantedWorkload := workload.DeepCopy()
6271
wantedWorkload.Spec.Replicas = req.DesiredReplicas

pkg/webhook/configuration/validating.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ func (v *ValidatingAdmission) InjectDecoder(d *admission.Decoder) error {
6565
var supportedInterpreterOperation = sets.NewString(
6666
string(configv1alpha1.InterpreterOperationAll),
6767
string(configv1alpha1.InterpreterOperationInterpretReplica),
68+
string(configv1alpha1.InterpreterOperationInterpretDependency),
6869
string(configv1alpha1.InterpreterOperationReviseReplica),
6970
string(configv1alpha1.InterpreterOperationRetain),
7071
string(configv1alpha1.InterpreterOperationAggregateStatus),

test/e2e/resourceinterpreter_test.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import (
77

88
"github.com/onsi/ginkgo/v2"
99
"github.com/onsi/gomega"
10+
corev1 "k8s.io/api/core/v1"
11+
apierrors "k8s.io/apimachinery/pkg/api/errors"
1012
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1113
"k8s.io/apimachinery/pkg/util/rand"
1214
"k8s.io/apimachinery/pkg/util/wait"
@@ -190,4 +192,48 @@ var _ = ginkgo.Describe("Resource interpreter webhook testing", func() {
190192
})
191193
})
192194
})
195+
196+
ginkgo.Context("InterpreterOperation InterpretDependency testing", func() {
197+
var configMapNamespace, configMapName string
198+
199+
ginkgo.BeforeEach(func() {
200+
configMapNamespace = testNamespace
201+
configMapName = configMapNamePrefix + rand.String(RandomStrLength)
202+
203+
workload.Spec.Template.Spec.Containers[0].EnvFrom = []corev1.EnvFromSource{{
204+
ConfigMapRef: &corev1.ConfigMapEnvSource{
205+
LocalObjectReference: corev1.LocalObjectReference{Name: configMapName},
206+
}}}
207+
// configmaps should be propagated automatically.
208+
policy.Spec.PropagateDeps = true
209+
210+
cm := testhelper.NewConfigMap(configMapNamespace, configMapName, map[string]string{"RUN_ENV": "test"})
211+
212+
framework.CreateConfigMap(kubeClient, cm)
213+
ginkgo.DeferCleanup(func() {
214+
framework.RemoveConfigMap(kubeClient, configMapNamespace, configMapName)
215+
})
216+
})
217+
218+
ginkgo.It("InterpretDependency testing", func() {
219+
ginkgo.By("check if workload's dependency is interpreted", func() {
220+
clusterNames := framework.ClusterNames()
221+
gomega.Eventually(func(g gomega.Gomega) (int, error) {
222+
var configmapNum int
223+
for _, clusterName := range clusterNames {
224+
clusterClient := framework.GetClusterClient(clusterName)
225+
gomega.Expect(clusterClient).ShouldNot(gomega.BeNil())
226+
if _, err := clusterClient.CoreV1().ConfigMaps(configMapNamespace).Get(context.TODO(), configMapName, metav1.GetOptions{}); err != nil {
227+
if apierrors.IsNotFound(err) {
228+
continue
229+
}
230+
g.Expect(err).NotTo(gomega.HaveOccurred())
231+
}
232+
configmapNum++
233+
}
234+
return configmapNum, nil
235+
}, pollTimeout, pollInterval).Should(gomega.Equal(len(clusterNames)))
236+
})
237+
})
238+
})
193239
})

test/helper/resource.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -496,7 +496,7 @@ func NewClusterWithResource(name string, allocatable, allocating, allocated core
496496
}
497497

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

502502
return &workloadv1alpha1.Workload{

0 commit comments

Comments
 (0)