Skip to content

Commit

Permalink
add mutatingwebhook
Browse files Browse the repository at this point in the history
Signed-off-by: zhuxiaoyang <[email protected]>
  • Loading branch information
soulseen committed Sep 23, 2019
1 parent f847c4c commit c962f2a
Show file tree
Hide file tree
Showing 6 changed files with 131 additions and 1 deletion.
25 changes: 25 additions & 0 deletions pkg/webhook/default_server/add_validating.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package defaultserver
import (
"fmt"

s2iBuilderMutating "github.com/kubesphere/s2ioperator/pkg/webhook/default_server/s2ibuilder/mutating"
s2iBuilderValidating "github.com/kubesphere/s2ioperator/pkg/webhook/default_server/s2ibuilder/validating"
s2iBuilderTemplateValidating "github.com/kubesphere/s2ioperator/pkg/webhook/default_server/s2ibuildertemplate/validating"
s2iRunValidating "github.com/kubesphere/s2ioperator/pkg/webhook/default_server/s2irun/validating"
Expand Down Expand Up @@ -94,4 +95,28 @@ func init() {
}
HandlerMap[k] = v
}

for k, v := range s2iBuilderMutating.Builders {
_, found := builderMap[k]
if found {
log.V(1).Info(fmt.Sprintf(
"conflicting webhook builder names in builder map: %v", k))
}
builderMap[k] = v
}

for k, v := range s2iBuilderMutating.HandlerMap {
_, found := HandlerMap[k]
if found {
log.V(1).Info(fmt.Sprintf(
"conflicting webhook builder names in handler map: %v", k))
}
_, found = builderMap[k]
if !found {
log.V(1).Info(fmt.Sprintf(
"can't find webhook builder name %q in builder map", k))
continue
}
HandlerMap[k] = v
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package mutating

import (
devopsv1alpha1 "github.com/kubesphere/s2ioperator/pkg/apis/devops/v1alpha1"
admissionregistrationv1beta1 "k8s.io/api/admissionregistration/v1beta1"
"sigs.k8s.io/controller-runtime/pkg/runtime/log"
"sigs.k8s.io/controller-runtime/pkg/webhook/admission/builder"
)

var logger = log.Log.WithName("s2ibuilder-mutate")

func init() {
builderName := "mutating-create-s2ibuilder"
Builders[builderName] = builder.
NewWebhookBuilder().
Name(builderName+".kubesphere.io").
Path("/"+builderName).
Mutating().
Operations(admissionregistrationv1beta1.Create, admissionregistrationv1beta1.Update).
FailurePolicy(admissionregistrationv1beta1.Fail).
ForType(&devopsv1alpha1.S2iBuilder{})
}
1 change: 1 addition & 0 deletions pkg/webhook/default_server/s2ibuilder/mutating/mutate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package mutating
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package mutating

import (
"context"
devopsv1alpha1 "github.com/kubesphere/s2ioperator/pkg/apis/devops/v1alpha1"
"net/http"
"sigs.k8s.io/controller-runtime/pkg/runtime/inject"
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
"sigs.k8s.io/controller-runtime/pkg/webhook/admission/types"
)

const (
DefaultRevisionId = "master"
DefaultTag = "latest"
)

func init() {
webhookName := "mutating-create-s2ibuilder"
if HandlerMap[webhookName] == nil {
HandlerMap[webhookName] = []admission.Handler{}
}
HandlerMap[webhookName] = append(HandlerMap[webhookName], &S2iBuilderCreateHandler{})
}

// S2iBuilderCreateHandler handles S2iBuilder
type S2iBuilderCreateHandler struct {
Decoder types.Decoder
}

// Implement admission.Handler so the controller can handle admission request.
var _ admission.Handler = &S2iBuilderCreateHandler{}

// S2iBuilderCreateHandler adds an default status info to S2iBuilder
func (h *S2iBuilderCreateHandler) Handle(ctx context.Context, req types.Request) types.Response {
s2ibuilder := &devopsv1alpha1.S2iBuilder{}
err := h.Decoder.Decode(req, s2ibuilder)

if err != nil {
return admission.ErrorResponse(http.StatusBadRequest, err)
}
s2ib := s2ibuilder.DeepCopy()

err = h.mutatingS2iBuilderFn(ctx, s2ib)
if err != nil {
return admission.ErrorResponse(http.StatusInternalServerError, err)
}
return admission.PatchResponse(s2ibuilder, s2ib)
}

func (h *S2iBuilderCreateHandler) mutatingS2iBuilderFn(ctx context.Context, obj *devopsv1alpha1.S2iBuilder) error {

if obj.Spec.Config.RevisionId == "" {
obj.Spec.Config.RevisionId = DefaultRevisionId
}

if obj.Spec.Config.Tag == "" {
obj.Spec.Config.Tag = DefaultTag
}

return nil
}

var _ inject.Decoder = &S2iBuilderCreateHandler{}

// InjectDecoder injects the decoder.
func (h *S2iBuilderCreateHandler) InjectDecoder(d types.Decoder) error {
h.Decoder = d
return nil
}
13 changes: 13 additions & 0 deletions pkg/webhook/default_server/s2ibuilder/mutating/webhooks.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package mutating

import (
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
"sigs.k8s.io/controller-runtime/pkg/webhook/admission/builder"
)

var (
// Builders contain admission webhook builders
Builders = map[string]*builder.WebhookBuilder{}
// HandlerMap contains admission webhook handlers
HandlerMap = map[string][]admission.Handler{}
)
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func (h *S2iRunCreateUpdateHandler) validatingS2iRunFn(ctx context.Context, obj

builder := &devopsv1alpha1.S2iBuilder{}

err := h.Client.Get(context.TODO(), types2.NamespacedName{Namespace: origin.Namespace, Name: obj.Spec.BuilderName}, builder)
err := h.Client.Get(context.TODO(), types2.NamespacedName{Namespace: obj.Namespace, Name: obj.Spec.BuilderName}, builder)
if err != nil && !k8serror.IsNotFound(err) {
return false, "validate failed", errors.NewFieldInvalidValueWithReason("no", "could not call k8s api")
}
Expand Down

0 comments on commit c962f2a

Please sign in to comment.