mirror of
https://github.com/ceph/ceph-csi.git
synced 2024-11-14 02:10:21 +00:00
61 lines
1.6 KiB
Go
61 lines
1.6 KiB
Go
/*
|
|
Copyright 2018 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 main
|
|
|
|
import (
|
|
"encoding/json"
|
|
|
|
"k8s.io/api/admission/v1beta1"
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
|
"k8s.io/klog"
|
|
)
|
|
|
|
const (
|
|
addFirstLabelPatch string = `[
|
|
{ "op": "add", "path": "/metadata/labels", "value": {"added-label": "yes"}}
|
|
]`
|
|
addAdditionalLabelPatch string = `[
|
|
{ "op": "add", "path": "/metadata/labels/added-label", "value": "yes" }
|
|
]`
|
|
)
|
|
|
|
// Add a label {"added-label": "yes"} to the object
|
|
func addLabel(ar v1beta1.AdmissionReview) *v1beta1.AdmissionResponse {
|
|
klog.V(2).Info("calling add-label")
|
|
obj := struct {
|
|
metav1.ObjectMeta
|
|
Data map[string]string
|
|
}{}
|
|
raw := ar.Request.Object.Raw
|
|
err := json.Unmarshal(raw, &obj)
|
|
if err != nil {
|
|
klog.Error(err)
|
|
return toAdmissionResponse(err)
|
|
}
|
|
|
|
reviewResponse := v1beta1.AdmissionResponse{}
|
|
reviewResponse.Allowed = true
|
|
if len(obj.ObjectMeta.Labels) == 0 {
|
|
reviewResponse.Patch = []byte(addFirstLabelPatch)
|
|
} else {
|
|
reviewResponse.Patch = []byte(addAdditionalLabelPatch)
|
|
}
|
|
pt := v1beta1.PatchTypeJSONPatch
|
|
reviewResponse.PatchType = &pt
|
|
return &reviewResponse
|
|
}
|