Skip to content

Commit

Permalink
fix: decoder, cannot be injected anymore
Browse files Browse the repository at this point in the history
Signed-off-by: realanna <anna.reale@dynatrace.com>
  • Loading branch information
RealAnna committed May 22, 2023
1 parent a509d09 commit bbd0a25
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 15 deletions.
13 changes: 10 additions & 3 deletions operator/test/e2e/operator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,17 @@ var _ = Describe("[E2E] KeptnOperator", Ordered, func() {
ObjectMeta: metav1.ObjectMeta{
Name: name,
Annotations: annotations,
Namespace: "default",
Namespace: "mydefault",
OwnerReferences: []metav1.OwnerReference{
{
Kind: "ReplicaSet",
UID: "123",
Name: "test-123",
APIVersion: "1.0.0",
},
},
},
Spec: apiv1.PodSpec{
SchedulerName: "",
Containers: []apiv1.Container{
{
Name: "mybusy",
Expand Down Expand Up @@ -74,7 +81,7 @@ var _ = Describe("[E2E] KeptnOperator", Ordered, func() {

})

It(" should be gated", func() {
It(" should be gated if namespace annotated", func() {
Expect(newPod.Spec.SchedulingGates).To(ContainElement(apiv1.PodSchedulingGate{Name: "klt-gated"}))
})
})
Expand Down
18 changes: 17 additions & 1 deletion operator/test/e2e/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,13 @@ import (
"time"

klcv1alpha3 "github.com/keptn/lifecycle-toolkit/operator/apis/lifecycle/v1alpha3"
apicommon "github.com/keptn/lifecycle-toolkit/operator/apis/lifecycle/v1alpha3/common"
. "github.com/onsi/ginkgo/v2"
"github.com/onsi/ginkgo/v2/types"
. "github.com/onsi/gomega"
apiv1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes/scheme"
"k8s.io/client-go/rest"
"sigs.k8s.io/controller-runtime/pkg/client"
Expand Down Expand Up @@ -78,14 +81,27 @@ var _ = BeforeSuite(func() {
Expect(err).NotTo(HaveOccurred())
Expect(k8sClient).NotTo(BeNil())

annotatedNamespace := &apiv1.Namespace{
ObjectMeta: metav1.ObjectMeta{
Name: "mydefault",
Annotations: map[string]string{
apicommon.NamespaceEnabledAnnotation: "enabled",
},
},
}
err = k8sClient.Create(ctx, annotatedNamespace)
Expect(ignoreAlreadyExists(err)).NotTo(HaveOccurred(), "could not create namespace")

wg = &sync.WaitGroup{}

go func() {
defer GinkgoRecover()
time.Sleep(3 * time.Second) // wait for test to start
wg.Wait()
fmt.Println("SUITE FINISHED")
err := testEnv.Stop()
err := k8sClient.Delete(ctx, annotatedNamespace)
Expect(err).NotTo(HaveOccurred(), "could not remove namespace")
err = testEnv.Stop()
Expect(err).ToNot(HaveOccurred())
}()

Expand Down
6 changes: 1 addition & 5 deletions operator/webhooks/gating/gating_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,7 @@ func (a *PodGatingWebhook) Handle(ctx context.Context, req admission.Request) ad
ctx, span := a.Tracer.Start(ctx, "gate_pod", trace.WithNewRoot(), trace.WithSpanKind(trace.SpanKindServer))
defer span.End()
pod := &corev1.Pod{}
err := a.Decoder.Decode(req, pod) //no clue why this fails so I directly use the runtime decoder
//var deserializer runtime.Decoder
//factory := serializer.NewCodecFactory(a.Client.Scheme())
//deserializer = factory.UniversalDeserializer()
//err := runtime.DecodeInto(deserializer, req.Object.Raw, pod)
err := a.Decoder.Decode(req, pod)
if err != nil {
return admission.Errored(http.StatusBadRequest, err)
}
Expand Down
9 changes: 3 additions & 6 deletions operator/webhooks/pod_mutator/pod_mutating_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ import (
type PodMutatingWebhook struct {
Client client.Client
Tracer trace.Tracer
Decoder *admission.Decoder
Decoder *admission.Decoder //decoder cannot be injected from controller runtime 0.15.0 onward
Recorder record.EventRecorder
Log logr.Logger
}
Expand All @@ -59,11 +59,8 @@ func (a *PodMutatingWebhook) Handle(ctx context.Context, req admission.Request)
pod := &corev1.Pod{}

logger.Info("checking the request", "req obj: ", req.Object, "req raw", req.Object.Raw)
err := a.Decoder.Decode(req, pod) //no clue why this fails so I directly use the runtime decoder
//var deserializer runtime.Decoder
//factory := serializer.NewCodecFactory(a.Client.Scheme())
//deserializer = factory.UniversalDeserializer()
//err := runtime.DecodeInto(deserializer, req.Object.Raw, pod)
err := a.Decoder.Decode(req, pod)

if err != nil {
return admission.Errored(http.StatusBadRequest, err)
}
Expand Down

0 comments on commit bbd0a25

Please sign in to comment.