68 lines
2.5 KiB
Go
68 lines
2.5 KiB
Go
/*
|
|
Copyright 2021 Alibaba Group Holding Limited.
|
|
|
|
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 extension
|
|
|
|
import (
|
|
"strings"
|
|
|
|
admissionv1 "k8s.io/api/admission/v1"
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
|
"k8s.io/apimachinery/pkg/runtime/schema"
|
|
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
|
|
)
|
|
|
|
// The codes following is copied from
|
|
// https://github.com/kubernetes-sigs/controller-runtime/blob/e13341355d/pkg/webhook/admission/defaulter_custom.go
|
|
// https://github.com/kubernetes-sigs/controller-runtime/blob/e13341355d/pkg/webhook/admission/validator_custom.go
|
|
// to support custom defaulter and validator on controller-runtime 0.9+.
|
|
|
|
// Here's the original license.
|
|
|
|
/*
|
|
Copyright 2021 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.
|
|
*/
|
|
|
|
// validationResponseFromStatus returns a response for admitting a request with provided Status object.
|
|
func validationResponseFromStatus(allowed bool, status metav1.Status) admission.Response {
|
|
resp := admission.Response{
|
|
AdmissionResponse: admissionv1.AdmissionResponse{
|
|
Allowed: allowed,
|
|
Result: &status,
|
|
},
|
|
}
|
|
return resp
|
|
}
|
|
|
|
func GenerateMutatePath(apiPath string, gvk schema.GroupVersionKind) string {
|
|
return apiPath + "/mutate-" + strings.ReplaceAll(gvk.Group, ".", "-") + "-" +
|
|
gvk.Version + "-" + strings.ToLower(gvk.Kind)
|
|
}
|
|
|
|
func GenerateValidatePath(apiPath string, gvk schema.GroupVersionKind) string {
|
|
return apiPath + "/validate-" + strings.ReplaceAll(gvk.Group, ".", "-") + "-" +
|
|
gvk.Version + "-" + strings.ToLower(gvk.Kind)
|
|
}
|