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

Commit

Permalink
ingress: move YurtIngress CR default values to mutating webhook
Browse files Browse the repository at this point in the history
Signed-off-by: zzguang <zhengguang.zhang@intel.com>
  • Loading branch information
zzguang committed May 10, 2022
1 parent 95d9251 commit baf021f
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 25 deletions.
18 changes: 18 additions & 0 deletions config/yurt-app-manager/webhook/manifests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,24 @@ webhooks:
- UPDATE
resources:
- yurtappdaemons
- clientConfig:
caBundle: Cg==
service:
name: webhook-service
namespace: system
path: /mutate-apps-openyurt-io-v1alpha1-yurtingress
failurePolicy: Fail
name: myurtingress.kb.io
rules:
- apiGroups:
- apps.openyurt.io
apiVersions:
- v1alpha1
operations:
- CREATE
- UPDATE
resources:
- yurtingresses

---
apiVersion: admissionregistration.k8s.io/v1beta1
Expand Down
20 changes: 20 additions & 0 deletions pkg/yurtappmanager/apis/apps/v1alpha1/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,26 @@ import (
utilpointer "k8s.io/utils/pointer"
)

const (
defaultIngressControllerImage string = "k8s.gcr.io/ingress-nginx/controller:v0.48.1"
defaultIngressWebhookCertGenImage string = "docker.io/jettech/kube-webhook-certgen:v1.5.1"
)

// SetDefaultsYurtIngress set default values for YurtIngress.
func SetDefaultsYurtIngress(obj *YurtIngress) {

if obj.Spec.IngressControllerImage == "" {
obj.Spec.IngressControllerImage = defaultIngressControllerImage
}
if obj.Spec.IngressWebhookCertGenImage == "" {
obj.Spec.IngressWebhookCertGenImage = defaultIngressWebhookCertGenImage
}
if obj.Spec.Replicas == 0 {
obj.Spec.Replicas = 1
}

}

// SetDefaultsYurtAppDaemon set default values for YurtAppDaemon.
func SetDefaultsYurtAppDaemon(obj *YurtAppDaemon) {

Expand Down
11 changes: 2 additions & 9 deletions pkg/yurtappmanager/apis/apps/v1alpha1/yurtingress_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,8 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

// Define the default nodepool ingress related values
const (
// DefaultNginxIngressControllerImage defines the default nginx ingress controller image
DefaultNginxIngressControllerImage string = "k8s.gcr.io/ingress-nginx/controller:v0.48.1"
// DefaultNginxIngressWebhookCertGenImage defines the default nginx ingress controller webhook certgen image
DefaultNginxIngressWebhookCertGenImage string = "docker.io/jettech/kube-webhook-certgen:v1.5.1"
// YurtIngressFinalizer is used to cleanup ingress resources when YurtIngress CR is deleted
YurtIngressFinalizer string = "ingress.operator.openyurt.io"
)
// YurtIngressFinalizer is used to cleanup ingress resources when YurtIngress CR is deleted
const YurtIngressFinalizer string = "ingress.operator.openyurt.io"

type IngressNotReadyType string

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,16 +145,7 @@ func (r *YurtIngressReconciler) Reconcile(ctx context.Context, req ctrl.Request)
desiredPools = getDesiredPools(instance)
currentPools = getCurrentPools(instance)
isYurtIngressCRChanged := false
if instance.Spec.IngressControllerImage == "" {
instance.Spec.IngressControllerImage = appsv1alpha1.DefaultNginxIngressControllerImage
}
if instance.Spec.IngressWebhookCertGenImage == "" {
instance.Spec.IngressWebhookCertGenImage = appsv1alpha1.DefaultNginxIngressWebhookCertGenImage
}
if instance.Spec.Replicas == 0 {
klog.V(4).Infof("set default per-pool replicas to 1")
instance.Spec.Replicas = 1
}

addedPools, removedPools, unchangedPools := getPools(desiredPools, currentPools)
if addedPools != nil {
klog.V(4).Infof("added pool list is %s", addedPools)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (
webhookutil "github.com/openyurtio/yurt-app-manager/pkg/yurtappmanager/webhook/util"
)

// kubebuilder:webhook:path=/mutate-apps-openyurt-io-v1alpha1-yurtingress,mutating=true,failurePolicy=fail,groups=apps.openyurt.io,resources=yurtingresses,verbs=create;update,versions=v1alpha1,name=myurtingress.kb.io
// +kubebuilder:webhook:path=/mutate-apps-openyurt-io-v1alpha1-yurtingress,mutating=true,failurePolicy=fail,groups=apps.openyurt.io,resources=yurtingresses,verbs=create;update,versions=v1alpha1,name=myurtingress.kb.io

var (
// HandlerMap contains admission webhook handlers
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"net/http"

"k8s.io/klog"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"

appsv1alpha1 "github.com/openyurtio/yurt-app-manager/pkg/yurtappmanager/apis/apps/v1alpha1"
Expand All @@ -31,32 +32,40 @@ import (

// YurtIngressCreateUpdateHandler handles YurtIngress
type YurtIngressCreateUpdateHandler struct {
// To use the client, you need to do the following:
// - uncomment it
// - import sigs.k8s.io/controller-runtime/pkg/client
// - uncomment the InjectClient method at the bottom of this file.
Client client.Client

// Decoder decodes objects
Decoder *admission.Decoder
}

var _ webhookutil.Handler = &YurtIngressCreateUpdateHandler{}

func (h *YurtIngressCreateUpdateHandler) SetOptions(options webhookutil.Options) {
//return
h.Client = options.Client
}

// Handle handles admission requests.
func (h *YurtIngressCreateUpdateHandler) Handle(ctx context.Context, req admission.Request) admission.Response {
np_ing := appsv1alpha1.YurtIngress{}
err := h.Decoder.Decode(req, &np_ing)
obj := &appsv1alpha1.YurtIngress{}
err := h.Decoder.Decode(req, obj)
if err != nil {
return admission.Errored(http.StatusBadRequest, err)
}

marshalled, err := json.Marshal(&np_ing)
appsv1alpha1.SetDefaultsYurtIngress(obj)

marshalled, err := json.Marshal(&obj)
if err != nil {
return admission.Errored(http.StatusInternalServerError, err)
}
resp := admission.PatchResponseFromRaw(req.AdmissionRequest.Object.Raw,
marshalled)
if len(resp.Patches) > 0 {
klog.V(5).Infof("Admit YurtIngress %s patches: %v", np_ing.Name, util.DumpJSON(resp.Patches))
klog.V(5).Infof("Admit YurtIngress %s patches: %v", obj.Name, util.DumpJSON(resp.Patches))
}
return resp
}
Expand Down

0 comments on commit baf021f

Please sign in to comment.