Skip to content

Commit

Permalink
upgrade webhook from v1beta1 to v1 (volcano-sh#43)
Browse files Browse the repository at this point in the history
- cherry picked from 7a5d8be
- change failurePolicy to Ignore

Co-authored-by: Yunus Olgun <yunuso@spotify.com>
  • Loading branch information
yolgun and yolgun committed Oct 24, 2022
1 parent 380f81e commit 0fb729b
Show file tree
Hide file tree
Showing 25 changed files with 298 additions and 344 deletions.
23 changes: 16 additions & 7 deletions cmd/webhook-manager/app/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (
"regexp"
"strings"

"k8s.io/api/admissionregistration/v1beta1"
"k8s.io/api/admissionregistration/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
Expand All @@ -35,7 +35,10 @@ import (
)

func registerWebhookConfig(kubeClient *kubernetes.Clientset, config *options.Config, service *router.AdmissionService, caBundle []byte) {
clientConfig := v1beta1.WebhookClientConfig{
failurePolicy := v1.Ignore
sideEffect := v1.SideEffectClassNoneOnDryRun
reviewVersions := []string{"v1"}
clientConfig := v1.WebhookClientConfig{
CABundle: caBundle,
}
if config.WebhookURL != "" {
Expand All @@ -44,7 +47,7 @@ func registerWebhookConfig(kubeClient *kubernetes.Clientset, config *options.Con
klog.Infof("The URL of webhook manager is <%s>.", url)
}
if config.WebhookName != "" && config.WebhookNamespace != "" {
clientConfig.Service = &v1beta1.ServiceReference{
clientConfig.Service = &v1.ServiceReference{
Name: config.WebhookName,
Namespace: config.WebhookNamespace,
Path: &service.Path,
Expand All @@ -54,7 +57,10 @@ func registerWebhookConfig(kubeClient *kubernetes.Clientset, config *options.Con
}
if service.MutatingConfig != nil {
for i := range service.MutatingConfig.Webhooks {
service.MutatingConfig.Webhooks[i].SideEffects = &sideEffect
service.MutatingConfig.Webhooks[i].AdmissionReviewVersions = reviewVersions
service.MutatingConfig.Webhooks[i].ClientConfig = clientConfig
service.MutatingConfig.Webhooks[i].FailurePolicy = &failurePolicy
}

service.MutatingConfig.ObjectMeta.Name = webhookConfigName(config.WebhookName, service.Path)
Expand All @@ -68,7 +74,10 @@ func registerWebhookConfig(kubeClient *kubernetes.Clientset, config *options.Con
}
if service.ValidatingConfig != nil {
for i := range service.ValidatingConfig.Webhooks {
service.ValidatingConfig.Webhooks[i].SideEffects = &sideEffect
service.ValidatingConfig.Webhooks[i].AdmissionReviewVersions = reviewVersions
service.ValidatingConfig.Webhooks[i].ClientConfig = clientConfig
service.ValidatingConfig.Webhooks[i].FailurePolicy = &failurePolicy
}

service.ValidatingConfig.ObjectMeta.Name = webhookConfigName(config.WebhookName, service.Path)
Expand Down Expand Up @@ -130,8 +139,8 @@ func configTLS(config *options.Config, restConfig *rest.Config) *tls.Config {
return &tls.Config{}
}

func registerMutateWebhook(clientset *kubernetes.Clientset, hook *v1beta1.MutatingWebhookConfiguration) error {
client := clientset.AdmissionregistrationV1beta1().MutatingWebhookConfigurations()
func registerMutateWebhook(clientset *kubernetes.Clientset, hook *v1.MutatingWebhookConfiguration) error {
client := clientset.AdmissionregistrationV1().MutatingWebhookConfigurations()
existing, err := client.Get(context.TODO(), hook.Name, metav1.GetOptions{})
if err != nil && !apierrors.IsNotFound(err) {
return err
Expand All @@ -152,8 +161,8 @@ func registerMutateWebhook(clientset *kubernetes.Clientset, hook *v1beta1.Mutati
return nil
}

func registerValidateWebhook(clientset *kubernetes.Clientset, hook *v1beta1.ValidatingWebhookConfiguration) error {
client := clientset.AdmissionregistrationV1beta1().ValidatingWebhookConfigurations()
func registerValidateWebhook(clientset *kubernetes.Clientset, hook *v1.ValidatingWebhookConfiguration) error {
client := clientset.AdmissionregistrationV1().ValidatingWebhookConfigurations()

existing, err := client.Get(context.TODO(), hook.Name, metav1.GetOptions{})
if err != nil && !apierrors.IsNotFound(err) {
Expand Down
4 changes: 2 additions & 2 deletions docs/design/queue/queue-state-management.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ under the status of queue, not the `state` under the `spec` of queue.
Add `validatingwebhookconfiguration` for queue validation during creating, updating or deleting of queue.

```yaml
apiVersion: admissionregistration.k8s.io/v1beta1
apiVersion: admissionregistration.k8s.io/v1
kind: ValidatingWebhookConfiguration
metadata:
name: {{ .Release.Name }}-validate-queue
Expand Down Expand Up @@ -165,7 +165,7 @@ We need another `webhook` to set default state value for queue during queue crea
and `MutateQueues` function

```yaml
apiVersion: admissionregistration.k8s.io/v1beta1
apiVersion: admissionregistration.k8s.io/v1
kind: MutatingWebhookConfiguration
metadata:
name: {{ .Release.Name }}-mutate-queue
Expand Down
24 changes: 12 additions & 12 deletions pkg/webhooks/admission/jobs/mutate/mutate_job.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ import (
"fmt"
"strconv"

"k8s.io/api/admission/v1beta1"
whv1beta1 "k8s.io/api/admissionregistration/v1beta1"
admissionv1 "k8s.io/api/admission/v1"
whv1 "k8s.io/api/admissionregistration/v1"
v1 "k8s.io/api/core/v1"
"k8s.io/klog"

Expand All @@ -49,13 +49,13 @@ var service = &router.AdmissionService{
Path: "/jobs/mutate",
Func: Jobs,

MutatingConfig: &whv1beta1.MutatingWebhookConfiguration{
Webhooks: []whv1beta1.MutatingWebhook{{
MutatingConfig: &whv1.MutatingWebhookConfiguration{
Webhooks: []whv1.MutatingWebhook{{
Name: "mutatejob.volcano.sh",
Rules: []whv1beta1.RuleWithOperations{
Rules: []whv1.RuleWithOperations{
{
Operations: []whv1beta1.OperationType{whv1beta1.Create},
Rule: whv1beta1.Rule{
Operations: []whv1.OperationType{whv1.Create},
Rule: whv1.Rule{
APIGroups: []string{"batch.volcano.sh"},
APIVersions: []string{"v1alpha1"},
Resources: []string{"jobs"},
Expand All @@ -72,8 +72,8 @@ type patchOperation struct {
Value interface{} `json:"value,omitempty"`
}

// MutateJobs mutate jobs.
func Jobs(ar v1beta1.AdmissionReview) *v1beta1.AdmissionResponse {
// Jobs mutate jobs.
func Jobs(ar admissionv1.AdmissionReview) *admissionv1.AdmissionResponse {
klog.V(3).Infof("mutating jobs")

job, err := schema.DecodeJob(ar.Request.Object, ar.Request.Resource)
Expand All @@ -83,7 +83,7 @@ func Jobs(ar v1beta1.AdmissionReview) *v1beta1.AdmissionResponse {

var patchBytes []byte
switch ar.Request.Operation {
case v1beta1.Create:
case admissionv1.Create:
patchBytes, _ = createPatch(job)
break
default:
Expand All @@ -92,11 +92,11 @@ func Jobs(ar v1beta1.AdmissionReview) *v1beta1.AdmissionResponse {
}

klog.V(3).Infof("AdmissionResponse: patch=%v", string(patchBytes))
reviewResponse := v1beta1.AdmissionResponse{
reviewResponse := admissionv1.AdmissionResponse{
Allowed: true,
Patch: patchBytes,
}
pt := v1beta1.PatchTypeJSONPatch
pt := admissionv1.PatchTypeJSONPatch
reviewResponse.PatchType = &pt

return &reviewResponse
Expand Down
24 changes: 12 additions & 12 deletions pkg/webhooks/admission/jobs/validate/admit_job.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ import (
"fmt"
"strings"

"k8s.io/api/admission/v1beta1"
whv1beta1 "k8s.io/api/admissionregistration/v1beta1"
admissionv1 "k8s.io/api/admission/v1"
whv1 "k8s.io/api/admissionregistration/v1"
v1 "k8s.io/api/core/v1"
apiequality "k8s.io/apimachinery/pkg/api/equality"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down Expand Up @@ -52,13 +52,13 @@ var service = &router.AdmissionService{

Config: config,

ValidatingConfig: &whv1beta1.ValidatingWebhookConfiguration{
Webhooks: []whv1beta1.ValidatingWebhook{{
ValidatingConfig: &whv1.ValidatingWebhookConfiguration{
Webhooks: []whv1.ValidatingWebhook{{
Name: "validatejob.volcano.sh",
Rules: []whv1beta1.RuleWithOperations{
Rules: []whv1.RuleWithOperations{
{
Operations: []whv1beta1.OperationType{whv1beta1.Create, whv1beta1.Update},
Rule: whv1beta1.Rule{
Operations: []whv1.OperationType{whv1.Create, whv1.Update},
Rule: whv1.Rule{
APIGroups: []string{"batch.volcano.sh"},
APIVersions: []string{"v1alpha1"},
Resources: []string{"jobs"},
Expand All @@ -72,21 +72,21 @@ var service = &router.AdmissionService{
var config = &router.AdmissionServiceConfig{}

// AdmitJobs is to admit jobs and return response.
func AdmitJobs(ar v1beta1.AdmissionReview) *v1beta1.AdmissionResponse {
func AdmitJobs(ar admissionv1.AdmissionReview) *admissionv1.AdmissionResponse {
klog.V(3).Infof("admitting jobs -- %s", ar.Request.Operation)

job, err := schema.DecodeJob(ar.Request.Object, ar.Request.Resource)
if err != nil {
return util.ToAdmissionResponse(err)
}
var msg string
reviewResponse := v1beta1.AdmissionResponse{}
reviewResponse := admissionv1.AdmissionResponse{}
reviewResponse.Allowed = true

switch ar.Request.Operation {
case v1beta1.Create:
case admissionv1.Create:
msg = validateJobCreate(job, &reviewResponse)
case v1beta1.Update:
case admissionv1.Update:
oldJob, err := schema.DecodeJob(ar.Request.OldObject, ar.Request.Resource)
if err != nil {
return util.ToAdmissionResponse(err)
Expand All @@ -106,7 +106,7 @@ func AdmitJobs(ar v1beta1.AdmissionReview) *v1beta1.AdmissionResponse {
return &reviewResponse
}

func validateJobCreate(job *v1alpha1.Job, reviewResponse *v1beta1.AdmissionResponse) string {
func validateJobCreate(job *v1alpha1.Job, reviewResponse *admissionv1.AdmissionResponse) string {
var msg string
taskNames := map[string]string{}
var totalReplicas int32
Expand Down

0 comments on commit 0fb729b

Please sign in to comment.