-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: zhuxiaoyang <[email protected]>
- Loading branch information
soulseen
committed
Sep 23, 2019
1 parent
f847c4c
commit c962f2a
Showing
6 changed files
with
131 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
pkg/webhook/default_server/s2ibuilder/mutating/create_update_webhook.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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{}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
package mutating |
69 changes: 69 additions & 0 deletions
69
pkg/webhook/default_server/s2ibuilder/mutating/s2ibuilder_create_update_handler.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
13
pkg/webhook/default_server/s2ibuilder/mutating/webhooks.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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{} | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters