Skip to content

Commit

Permalink
Add validation of updates and deletions to ClusterQueue references fo…
Browse files Browse the repository at this point in the history
…r ResourceFlavor to prevent data inconsistencies during Quota in ClusterQueue.
  • Loading branch information
xiaoxubeii committed Jun 14, 2022
1 parent 3100df4 commit e18ccd8
Show file tree
Hide file tree
Showing 10 changed files with 311 additions and 59 deletions.
4 changes: 4 additions & 0 deletions PROJECT
Expand Up @@ -42,6 +42,10 @@ resources:
kind: ResourceFlavor
path: sigs.k8s.io/kueue/apis/kueue/v1alpha1
version: v1alpha1
webhooks:
defaulting: true
validation: true
webhookVersion: v1
- api:
crdVersion: v1
domain: kueue.x-k8s.io
Expand Down
2 changes: 2 additions & 0 deletions apis/kueue/v1alpha1/resourceflavor_types.go
Expand Up @@ -29,6 +29,8 @@ type ResourceFlavor struct {
metav1.TypeMeta `json:",inline"`
metav1.ObjectMeta `json:"metadata,omitempty"`

ClusterQueues map[ClusterQueueReference]string `json:"clusterQueues,omitempty"`

// labels associated with this flavor. They are matched against or
// converted to node affinity constraints on the workload’s pods.
// For example, cloud.provider.com/accelerator: nvidia-tesla-k80.
Expand Down
78 changes: 78 additions & 0 deletions apis/kueue/v1alpha1/resourceflavor_webhook.go
@@ -0,0 +1,78 @@
/*
Copyright 2022 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package v1alpha1

import (
"fmt"
"k8s.io/apimachinery/pkg/runtime"
"reflect"
ctrl "sigs.k8s.io/controller-runtime"
logf "sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/webhook"
)

// log is for logging in this package.
var resourceflavorlog = logf.Log.WithName("resourceflavor-resource")

func (r *ResourceFlavor) SetupWebhookWithManager(mgr ctrl.Manager) error {
return ctrl.NewWebhookManagedBy(mgr).
For(r).
Complete()
}

//+kubebuilder:webhook:path=/mutate-kueue-x-k8s-io-v1alpha1-resourceflavor,mutating=true,failurePolicy=fail,sideEffects=None,groups=kueue.x-k8s.io,resources=resourceflavors,verbs=create;update,versions=v1alpha1,name=mresourceflavor.kb.io,admissionReviewVersions=v1

var _ webhook.Defaulter = &ResourceFlavor{}

// Default implements webhook.Defaulter so a webhook will be registered for the type
func (r *ResourceFlavor) Default() {
resourceflavorlog.Info("default", "name", r.Name)
}

//+kubebuilder:webhook:path=/validate-kueue-x-k8s-io-v1alpha1-resourceflavor,mutating=false,failurePolicy=fail,sideEffects=None,groups=kueue.x-k8s.io,resources=resourceflavors,verbs=create;update;delete,versions=v1alpha1,name=vresourceflavor.kb.io,admissionReviewVersions=v1

var _ webhook.Validator = &ResourceFlavor{}

// ValidateCreate implements webhook.Validator so a webhook will be registered for the type
func (r *ResourceFlavor) ValidateCreate() error {
resourceflavorlog.Info("validate create", "name", r.Name)
return nil
}

// ValidateUpdate implements webhook.Validator so a webhook will be registered for the type
func (r *ResourceFlavor) ValidateUpdate(old runtime.Object) error {
resourceflavorlog.Info("validate update", "name", r.Name)
oldRf, match := old.(*ResourceFlavor)
if !match {
return fmt.Errorf("the object is not resource flavor")
}
if len(oldRf.ClusterQueues) > 0 && !reflect.DeepEqual(r.Labels, oldRf.Labels) {
return fmt.Errorf("labels updates is prohibited with references")
}

return nil
}

// ValidateDelete implements webhook.Validator so a webhook will be registered for the type
func (r *ResourceFlavor) ValidateDelete() error {
resourceflavorlog.Info("validate delete", "name", r.Name)
if len(r.ClusterQueues) > 0 {
return fmt.Errorf("deletion is prohibited with references")
}

return nil
}
7 changes: 7 additions & 0 deletions apis/kueue/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions config/crd/bases/kueue.x-k8s.io_resourceflavors.yaml
Expand Up @@ -25,6 +25,10 @@ spec:
of an object. Servers should convert recognized schemas to the latest
internal value, and may reject unrecognized values. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources'
type: string
clusterQueues:
additionalProperties:
type: string
type: object
kind:
description: 'Kind is a string value representing the REST resource this
object represents. Servers may infer this from the endpoint the client
Expand Down
41 changes: 41 additions & 0 deletions config/webhook/manifests.yaml
Expand Up @@ -5,6 +5,26 @@ metadata:
creationTimestamp: null
name: mutating-webhook-configuration
webhooks:
- admissionReviewVersions:
- v1
clientConfig:
service:
name: webhook-service
namespace: system
path: /mutate-kueue-x-k8s-io-v1alpha1-resourceflavor
failurePolicy: Fail
name: mresourceflavor.kb.io
rules:
- apiGroups:
- kueue.x-k8s.io
apiVersions:
- v1alpha1
operations:
- CREATE
- UPDATE
resources:
- resourceflavors
sideEffects: None
- admissionReviewVersions:
- v1
clientConfig:
Expand Down Expand Up @@ -32,6 +52,27 @@ metadata:
creationTimestamp: null
name: validating-webhook-configuration
webhooks:
- admissionReviewVersions:
- v1
clientConfig:
service:
name: webhook-service
namespace: system
path: /validate-kueue-x-k8s-io-v1alpha1-resourceflavor
failurePolicy: Fail
name: vresourceflavor.kb.io
rules:
- apiGroups:
- kueue.x-k8s.io
apiVersions:
- v1alpha1
operations:
- CREATE
- UPDATE
- DELETE
resources:
- resourceflavors
sideEffects: None
- admissionReviewVersions:
- v1
clientConfig:
Expand Down
33 changes: 21 additions & 12 deletions go.mod
Expand Up @@ -10,14 +10,14 @@ require (
github.com/open-policy-agent/cert-controller v0.3.0
github.com/prometheus/client_golang v1.12.1
go.uber.org/zap v1.21.0
k8s.io/api v0.23.4
k8s.io/apimachinery v0.23.4
k8s.io/client-go v0.23.4
k8s.io/component-base v0.23.3
k8s.io/api v0.24.0
k8s.io/apimachinery v0.24.0
k8s.io/client-go v0.24.0
k8s.io/component-base v0.24.0
k8s.io/component-helpers v0.23.4
k8s.io/klog/v2 v2.40.1
k8s.io/klog/v2 v2.60.1
k8s.io/utils v0.0.0-20220210201930-3a6ce19ff2f9
sigs.k8s.io/controller-runtime v0.11.1
sigs.k8s.io/controller-runtime v0.12.1-0.20220613074012-11e533d55213
)

require (
Expand All @@ -28,48 +28,57 @@ require (
github.com/Azure/go-autorest/autorest/date v0.3.0 // indirect
github.com/Azure/go-autorest/logger v0.2.1 // indirect
github.com/Azure/go-autorest/tracing v0.6.0 // indirect
github.com/PuerkitoBio/purell v1.1.1 // indirect
github.com/PuerkitoBio/urlesc v0.0.0-20170810143723-de5bf2ad4578 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/blang/semver v3.5.1+incompatible // indirect
github.com/blang/semver/v4 v4.0.0 // indirect
github.com/cespare/xxhash/v2 v2.1.2 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/emicklei/go-restful v2.9.5+incompatible // indirect
github.com/evanphx/json-patch v5.6.0+incompatible // indirect
github.com/fsnotify/fsnotify v1.5.1 // indirect
github.com/go-logr/zapr v1.2.2 // indirect
github.com/go-openapi/jsonpointer v0.19.5 // indirect
github.com/go-openapi/jsonreference v0.19.5 // indirect
github.com/go-openapi/swag v0.19.14 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang-jwt/jwt/v4 v4.3.0 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/google/gnostic v0.5.7-v3refs // indirect
github.com/google/gofuzz v1.2.0 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/googleapis/gnostic v0.5.5 // indirect
github.com/imdario/mergo v0.3.12 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/mailru/easyjson v0.7.6 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.2-0.20181231171920-c182affec369 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/prometheus/client_model v0.2.0 // indirect
github.com/prometheus/common v0.32.1 // indirect
github.com/prometheus/procfs v0.7.3 // indirect
github.com/spf13/pflag v1.0.5 // indirect
go.uber.org/atomic v1.9.0 // indirect
go.uber.org/multierr v1.7.0 // indirect
golang.org/x/crypto v0.0.0-20220210151621-f4118a5b28e2 // indirect
golang.org/x/crypto v0.0.0-20220214200702-86341886e292 // indirect
golang.org/x/net v0.0.0-20220225172249-27dd8689420f // indirect
golang.org/x/oauth2 v0.0.0-20211104180415-d3ed0bb246c8 // indirect
golang.org/x/sys v0.0.0-20220319134239-a9b59b0215f8 // indirect
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211 // indirect
golang.org/x/text v0.3.7 // indirect
golang.org/x/time v0.0.0-20211116232009-f0f3c7e86c11 // indirect
golang.org/x/time v0.0.0-20220210224613-90d013bbcef8 // indirect
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect
gomodules.xyz/jsonpatch/v2 v2.2.0 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/protobuf v1.27.1 // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
k8s.io/apiextensions-apiserver v0.23.3 // indirect
k8s.io/kube-openapi v0.0.0-20220124234850-424119656bbf // indirect
k8s.io/apiextensions-apiserver v0.24.0 // indirect
k8s.io/kube-openapi v0.0.0-20220328201542-3ee0da9b0b42 // indirect
sigs.k8s.io/json v0.0.0-20211208200746-9f7c6b3444d2 // indirect
sigs.k8s.io/structured-merge-diff/v4 v4.2.1 // indirect
sigs.k8s.io/yaml v1.3.0 // indirect
Expand Down

0 comments on commit e18ccd8

Please sign in to comment.