Skip to content
This repository has been archived by the owner on Nov 2, 2023. It is now read-only.

readd yurtingress webhook #98

Merged
merged 1 commit into from
Aug 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions pkg/yurtappmanager/webhook/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"github.com/openyurtio/yurt-app-manager/pkg/yurtappmanager/webhook/nodepool"
"github.com/openyurtio/yurt-app-manager/pkg/yurtappmanager/webhook/yurtappdaemon"
"github.com/openyurtio/yurt-app-manager/pkg/yurtappmanager/webhook/yurtappset"
"github.com/openyurtio/yurt-app-manager/pkg/yurtappmanager/webhook/yurtingress"
)

func SetupWebhooks(mgr ctrl.Manager) error {
Expand All @@ -37,6 +38,11 @@ func SetupWebhooks(mgr ctrl.Manager) error {
if err := (&yurtappset.YurtAppSetHandler{Client: mgr.GetClient()}).SetupWebhookWithManager(mgr); err != nil {
return errors.Wrapf(err, "unable to create webhook for YurtAppSet")
}

if err := (&yurtingress.YurtIngressHandler{Client: mgr.GetClient()}).SetupWebhookWithManager(mgr); err != nil {
return errors.Wrapf(err, "unable to create webhook for YurtIngress")
}

return nil
}

Expand Down
84 changes: 84 additions & 0 deletions pkg/yurtappmanager/webhook/yurtingress/yurtingress_webhook_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/*
Copyright 2022 The OpenYurt authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
// +kubebuilder:docs-gen:collapse=Apache License

package yurtingress

import (
"context"
"testing"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/client/fake"

"github.com/openyurtio/yurt-app-manager/pkg/yurtappmanager/apis/apps/v1alpha1"
)

var defaultYurtIngress = &v1alpha1.YurtIngress{
ObjectMeta: metav1.ObjectMeta{
Name: "fooboo",
Namespace: "default",
},
Spec: v1alpha1.YurtIngressSpec{
Replicas: 1,
IngressControllerImage: "k8s.gcr.io/ingress-nginx/controller:v0.49.0",
IngressWebhookCertGenImage: "k8s.gcr.io/ingress-nginx/kube-webhook-certgen:v0.49.0",
Pools: []v1alpha1.IngressPool{{Name: "beijing"}},
},
}

func TestYurtAppSetDefaulter(t *testing.T) {
webhook := &YurtIngressHandler{}
if err := webhook.Default(context.TODO(), defaultYurtIngress); err != nil {
t.Fatal(err)
}
}

func TestYurtAppSetValidator(t *testing.T) {

scheme := runtime.NewScheme()
_ = clientgoscheme.AddToScheme(scheme)
_ = v1alpha1.AddToScheme(scheme)

bjNp := &v1alpha1.NodePool{
ObjectMeta: metav1.ObjectMeta{
Name: "beijing",
Namespace: "default",
},
Spec: v1alpha1.NodePoolSpec{},
}
objs := []client.Object{bjNp}

client := fake.NewClientBuilder().WithScheme(scheme).WithObjects(objs...).Build()

webhook := &YurtIngressHandler{Client: client}

// set default value
if err := webhook.Default(context.TODO(), defaultYurtIngress); err != nil {
t.Fatal(err)
}

if err := webhook.ValidateCreate(context.TODO(), defaultYurtIngress); err != nil {
t.Fatal("should create success", err)
}

npNotExist := defaultYurtIngress.DeepCopy()
npNotExist.Spec.Pools = []v1alpha1.IngressPool{{Name: "noneexist"}}
if err := webhook.ValidateCreate(context.TODO(), npNotExist); err == nil {
t.Fatal("should create fail", err)
}

}