This repository has been archived by the owner on Nov 28, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for admission.k8s.io/v1 AdmissionReview and admissionregi…
…stration.k8s.io/v1 MutatingWebhookConfiguration (in addition to v1beta1) (#49)
- Loading branch information
1 parent
f9ed1d3
commit 5845650
Showing
15 changed files
with
681 additions
and
321 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
4.2.2 | ||
4.3.0 |
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 |
---|---|---|
@@ -1 +1 @@ | ||
7.1.2 | ||
7.2.0 |
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 |
---|---|---|
@@ -1 +1 @@ | ||
7.1.1 | ||
7.2.0 |
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,89 @@ | ||
// Copyright © 2019-2021 Talend - www.talend.com | ||
// | ||
// 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 webhook | ||
|
||
import ( | ||
"unsafe" | ||
|
||
admv1 "k8s.io/api/admission/v1" | ||
admv1beta1 "k8s.io/api/admission/v1beta1" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/apimachinery/pkg/types" | ||
) | ||
|
||
// Note: | ||
// ===== | ||
// These conversions come from https://github.com/jetstack/cert-manager/blob/ab0cd57dc58fd73a76fd96bd9d1402bd5ae96582/pkg/webhook/server/util/convert.go | ||
// (which are adapted from https://github.com/kubernetes/kubernetes/blob/03d322035d2f199f2163658d94a153ed2b9de667/pkg/apis/admission/v1beta1/zz_generated.conversion.go) | ||
|
||
func Convert_v1beta1_AdmissionReview_To_admission_AdmissionReview(in *admv1beta1.AdmissionReview, out *admv1.AdmissionReview) { | ||
if in.Request != nil { | ||
if out.Request == nil { | ||
out.Request = &admv1.AdmissionRequest{} | ||
} | ||
in, out := &in.Request, &out.Request | ||
*out = new(admv1.AdmissionRequest) | ||
Convert_v1beta1_AdmissionRequest_To_admission_AdmissionRequest(*in, *out) | ||
} else { | ||
out.Request = nil | ||
} | ||
out.Response = (*admv1.AdmissionResponse)(unsafe.Pointer(in.Response)) | ||
} | ||
|
||
func Convert_v1beta1_AdmissionRequest_To_admission_AdmissionRequest(in *admv1beta1.AdmissionRequest, out *admv1.AdmissionRequest) { | ||
out.UID = types.UID(in.UID) | ||
out.Kind = in.Kind | ||
out.Resource = in.Resource | ||
out.SubResource = in.SubResource | ||
out.RequestKind = (*metav1.GroupVersionKind)(unsafe.Pointer(in.RequestKind)) | ||
out.RequestResource = (*metav1.GroupVersionResource)(unsafe.Pointer(in.RequestResource)) | ||
out.RequestSubResource = in.RequestSubResource | ||
out.Name = in.Name | ||
out.Namespace = in.Namespace | ||
out.Operation = admv1.Operation(in.Operation) | ||
out.Object = in.Object | ||
out.OldObject = in.OldObject | ||
out.Options = in.Options | ||
} | ||
|
||
func Convert_admission_AdmissionReview_To_v1beta1_AdmissionReview(in *admv1.AdmissionReview, out *admv1beta1.AdmissionReview) { | ||
if in.Request != nil { | ||
if out.Request == nil { | ||
out.Request = &admv1beta1.AdmissionRequest{} | ||
} | ||
in, out := &in.Request, &out.Request | ||
*out = new(admv1beta1.AdmissionRequest) | ||
Convert_admission_AdmissionRequest_To_v1beta1_AdmissionRequest(*in, *out) | ||
} else { | ||
out.Request = nil | ||
} | ||
out.Response = (*admv1beta1.AdmissionResponse)(unsafe.Pointer(in.Response)) | ||
} | ||
|
||
func Convert_admission_AdmissionRequest_To_v1beta1_AdmissionRequest(in *admv1.AdmissionRequest, out *admv1beta1.AdmissionRequest) { | ||
out.UID = types.UID(in.UID) | ||
out.Kind = in.Kind | ||
out.Resource = in.Resource | ||
out.SubResource = in.SubResource | ||
out.RequestKind = (*metav1.GroupVersionKind)(unsafe.Pointer(in.RequestKind)) | ||
out.RequestResource = (*metav1.GroupVersionResource)(unsafe.Pointer(in.RequestResource)) | ||
out.RequestSubResource = in.RequestSubResource | ||
out.Name = in.Name | ||
out.Namespace = in.Namespace | ||
out.Operation = admv1beta1.Operation(in.Operation) | ||
out.Object = in.Object | ||
out.OldObject = in.OldObject | ||
out.Options = in.Options | ||
} |
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,109 @@ | ||
// Copyright © 2019-2021 Talend - www.talend.com | ||
// | ||
// 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 webhook | ||
|
||
import ( | ||
"encoding/json" | ||
ctx "talend/vault-sidecar-injector/pkg/context" | ||
|
||
admv1 "k8s.io/api/admission/v1" | ||
corev1 "k8s.io/api/core/v1" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/klog" | ||
) | ||
|
||
func (vaultInjector *VaultInjector) mutate(ar *admv1.AdmissionReview) *admv1.AdmissionResponse { | ||
var pod corev1.Pod | ||
var podName, podNamespace string | ||
|
||
req := ar.Request | ||
|
||
if err := json.Unmarshal(req.Object.Raw, &pod); err != nil { | ||
klog.Errorf("Could not unmarshal raw object: %v", err) | ||
return &admv1.AdmissionResponse{ | ||
UID: req.UID, | ||
Result: &metav1.Status{ | ||
Message: err.Error(), | ||
}, | ||
} | ||
} | ||
|
||
if klog.V(5) { // enabled by providing '-v=5' at least | ||
klog.Infof("Pod=%+v", pod) | ||
} | ||
|
||
if pod.Name == "" { | ||
podName = pod.GenerateName | ||
} else { | ||
podName = pod.Name | ||
} | ||
|
||
if pod.Namespace == "" { | ||
podNamespace = metav1.NamespaceDefault | ||
} else { | ||
podNamespace = pod.Namespace | ||
} | ||
|
||
klog.Infof("AdmissionReview '%v' for '%+v', Namespace=%v Name='%v (%s/%s)' UID=%v patchOperation=%v", | ||
ar.GroupVersionKind(), req.Kind, req.Namespace, req.Name, podNamespace, podName, req.UID, req.Operation) | ||
|
||
// Determine whether to perform mutation | ||
if !mutationRequired(ignoredNamespaces, vaultInjector.VaultInjectorAnnotationsFQ, &pod.ObjectMeta) { | ||
klog.Infof("Skipping mutation for %s/%s due to policy check", podNamespace, podName) | ||
return &admv1.AdmissionResponse{ | ||
UID: req.UID, | ||
Allowed: true, | ||
} | ||
} | ||
|
||
annotations := map[string]string{vaultInjector.VaultInjectorAnnotationsFQ[ctx.VaultInjectorAnnotationStatusKey]: ctx.VaultInjectorStatusInjected} | ||
patchBytes, err := vaultInjector.createPatch(&pod, annotations) | ||
if err != nil { | ||
return &admv1.AdmissionResponse{ | ||
UID: req.UID, | ||
Allowed: false, | ||
Result: &metav1.Status{ | ||
Message: err.Error(), | ||
}, | ||
} | ||
} | ||
|
||
klog.Infof("AdmissionResponse: patch=%v\n", string(patchBytes)) | ||
return &admv1.AdmissionResponse{ | ||
UID: req.UID, | ||
Allowed: true, | ||
Patch: patchBytes, | ||
PatchType: func() *admv1.PatchType { | ||
pt := admv1.PatchTypeJSONPatch | ||
return &pt | ||
}(), | ||
} | ||
} | ||
|
||
// Create mutation patch for resources | ||
func (vaultInjector *VaultInjector) createPatch(pod *corev1.Pod, annotations map[string]string) ([]byte, error) { | ||
|
||
patchPodSpec, err := vaultInjector.updatePodSpec(pod) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
var patch []ctx.PatchOperation | ||
|
||
patch = append(patch, patchPodSpec...) | ||
patch = append(patch, updateAnnotation(pod.Annotations, annotations)...) | ||
|
||
return json.Marshal(patch) | ||
} |
Oops, something went wrong.