-
Notifications
You must be signed in to change notification settings - Fork 154
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add val webhook controller for blueprint resources
This commit adds go code to add a webhook server that does basics check on the bluerint resources when they are created. We are doing the only checks that we introduced as part of #1187
- Loading branch information
1 parent
54f7705
commit c57cb96
Showing
5 changed files
with
95 additions
and
21 deletions.
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
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
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
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
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,40 @@ | ||
package validatingwebhook | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"net/http" | ||
|
||
"sigs.k8s.io/controller-runtime/pkg/webhook/admission" | ||
|
||
kanister "github.com/kanisterio/kanister/pkg" | ||
crv1alpha1 "github.com/kanisterio/kanister/pkg/apis/cr/v1alpha1" | ||
"github.com/kanisterio/kanister/pkg/blueprint/validate" | ||
) | ||
|
||
type BlueprintValidator struct { | ||
decoder *admission.Decoder | ||
} | ||
|
||
func (b *BlueprintValidator) Handle(ctx context.Context, r admission.Request) admission.Response { | ||
bp := &crv1alpha1.Blueprint{} | ||
err := b.decoder.Decode(r, bp) | ||
if err != nil { | ||
return admission.Errored(http.StatusBadRequest, err) | ||
} | ||
|
||
if err := validate.Do(bp, kanister.DefaultVersion); err != nil { | ||
return admission.Denied(fmt.Sprintf("Failed to validate blueprint, error %s\n", err.Error())) | ||
} | ||
|
||
return admission.Allowed("") | ||
} | ||
|
||
// BlueprintValidator implements admission.DecoderInjector. | ||
// A decoder will be automatically injected. | ||
|
||
// InjectDecoder injects the decoder. | ||
func (b *BlueprintValidator) InjectDecoder(d *admission.Decoder) error { | ||
b.decoder = d | ||
return nil | ||
} |