Skip to content

Commit

Permalink
fix that the InterpretDependency operation is absent in the validatin…
Browse files Browse the repository at this point in the history
…g webhook

Signed-off-by: hejunhua <jayfantasyhjh@gmail.com>
  • Loading branch information
whitewindmills committed Jan 14, 2023
1 parent aa5868a commit 768874b
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 3 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
10 changes: 10 additions & 0 deletions examples/customresourceinterpreter/webhook/app/workloadwebhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"encoding/json"
"fmt"
corev1 "k8s.io/api/core/v1"
"net/http"

"k8s.io/apimachinery/pkg/runtime"
Expand Down Expand Up @@ -47,6 +48,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 +66,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: corev1.SchemeGroupVersion.String(), 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
26 changes: 25 additions & 1 deletion test/e2e/resourceinterpreter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,20 @@ import (
var _ = ginkgo.Describe("Resource interpreter webhook testing", func() {
var policyNamespace, policyName string
var workloadNamespace, workloadName string
var configMapNamespace, configMapName string
var workload *workloadv1alpha1.Workload
var policy *policyv1alpha1.PropagationPolicy
var cm *corev1.ConfigMap

ginkgo.BeforeEach(func() {
policyNamespace = testNamespace
policyName = workloadNamePrefix + rand.String(RandomStrLength)
workloadNamespace = testNamespace
workloadName = policyName
configMapNamespace = testNamespace
configMapName = configMapNamePrefix + rand.String(RandomStrLength)

workload = testhelper.NewWorkload(workloadNamespace, workloadName)
workload = testhelper.NewWorkload(workloadNamespace, workloadName, configMapName)
policy = testhelper.NewPropagationPolicy(policyNamespace, policyName, []policyv1alpha1.ResourceSelector{
{
APIVersion: workload.APIVersion,
Expand All @@ -51,14 +55,17 @@ var _ = ginkgo.Describe("Resource interpreter webhook testing", func() {
ClusterNames: framework.ClusterNames(),
},
})
cm = testhelper.NewConfigMap(configMapNamespace, configMapName, map[string]string{"RUN_ENV": "test"})
})

ginkgo.JustBeforeEach(func() {
framework.CreatePropagationPolicy(karmadaClient, policy)
framework.CreateWorkload(dynamicClient, workload)
framework.CreateConfigMap(kubeClient, cm)
ginkgo.DeferCleanup(func() {
framework.RemoveWorkload(dynamicClient, workload.Namespace, workload.Name)
framework.RemovePropagationPolicy(karmadaClient, policy.Namespace, policy.Name)
framework.RemoveConfigMap(kubeClient, configMapNamespace, configMapName)
})
})

Expand Down Expand Up @@ -160,6 +167,23 @@ var _ = ginkgo.Describe("Resource interpreter webhook testing", func() {
})
})

ginkgo.Context("InterpreterOperation InterpretDependency testing", func() {
ginkgo.It("InterpretDependency testing", func() {
ginkgo.By("check if workload's dependency is interpreted", func() {
resourceBindingName := names.GenerateBindingName(cm.Kind, configMapName)

gomega.Eventually(func(g gomega.Gomega) (int, error) {
resourceBinding, err := karmadaClient.WorkV1alpha2().ResourceBindings(configMapNamespace).Get(context.TODO(), resourceBindingName, metav1.GetOptions{})
g.Expect(err).NotTo(gomega.HaveOccurred())

klog.Infof("Attached ResourceBinding(%s/%s)'s schedule result is %v, existing schedule result is expected.",
resourceBinding.Namespace, resourceBinding.Name, resourceBinding.Spec.RequiredBy)
return len(resourceBinding.Spec.RequiredBy), nil
}, pollTimeout, pollInterval).ShouldNot(gomega.BeZero())
})
})
})

ginkgo.Context("InterpreterOperation AggregateStatus testing", func() {
ginkgo.It("AggregateStatus testing", func() {
ginkgo.By("check whether the workload status can be correctly collected", func() {
Expand Down
5 changes: 4 additions & 1 deletion test/helper/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -506,7 +506,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, configName string) *workloadv1alpha1.Workload {
podLabels := map[string]string{"app": "nginx"}

return &workloadv1alpha1.Workload{
Expand All @@ -528,6 +528,9 @@ func NewWorkload(namespace string, name string) *workloadv1alpha1.Workload {
Containers: []corev1.Container{{
Name: "nginx",
Image: "nginx:1.19.0",
EnvFrom: []corev1.EnvFromSource{{
ConfigMapRef: &corev1.ConfigMapEnvSource{LocalObjectReference: corev1.LocalObjectReference{Name: configName}, Optional: pointer.Bool(true)},
}},
}},
},
},
Expand Down

0 comments on commit 768874b

Please sign in to comment.