forked from koordinator-sh/koordinator
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
webhook: forbid use internal protocols (koordinator-sh#1911)
Signed-off-by: 刘硕 <liushuo@zetyun.com> Co-authored-by: 刘硕 <liushuo@zetyun.com>
- Loading branch information
Showing
3 changed files
with
187 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
/* | ||
Copyright 2022 The Koordinator 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 validating | ||
|
||
import ( | ||
"context" | ||
|
||
admissionv1 "k8s.io/api/admission/v1" | ||
corev1 "k8s.io/api/core/v1" | ||
"k8s.io/apimachinery/pkg/util/validation/field" | ||
"sigs.k8s.io/controller-runtime/pkg/webhook/admission" | ||
|
||
"github.com/koordinator-sh/koordinator/apis/extension" | ||
"github.com/koordinator-sh/koordinator/pkg/util/reservation" | ||
) | ||
|
||
func (h *PodValidatingHandler) clusterReservationValidatingPod(ctx context.Context, req admission.Request) (bool, string, error) { | ||
newPod := &corev1.Pod{} | ||
var allErrs field.ErrorList | ||
switch req.Operation { | ||
case admissionv1.Create: | ||
if err := h.Decoder.DecodeRaw(req.Object, newPod); err != nil { | ||
return false, "", err | ||
} | ||
case admissionv1.Update: | ||
oldPod := &corev1.Pod{} | ||
if err := h.Decoder.DecodeRaw(req.OldObject, oldPod); err != nil { | ||
return false, "", err | ||
} | ||
if err := h.Decoder.DecodeRaw(req.Object, newPod); err != nil { | ||
return false, "", err | ||
} | ||
|
||
} | ||
|
||
allErrs = append(allErrs, forbidSpecialAnnotations(newPod)...) | ||
err := allErrs.ToAggregate() | ||
allowed := true | ||
reason := "" | ||
if err != nil { | ||
allowed = false | ||
reason = err.Error() | ||
} | ||
return allowed, reason, err | ||
} | ||
|
||
var forbidAnnotations = []string{ | ||
extension.AnnotationReservationAllocated, | ||
reservation.AnnotationReservePod, | ||
} | ||
|
||
func forbidSpecialAnnotations(pod *corev1.Pod) field.ErrorList { | ||
if pod.Annotations == nil { | ||
return nil | ||
} | ||
errorList := field.ErrorList{} | ||
|
||
for _, annotation := range forbidAnnotations { | ||
if _, ok := pod.Annotations[annotation]; ok { | ||
errorList = append(errorList, field.Forbidden(field.NewPath("annotations", extension.AnnotationReservationAllocated), "cannot set in annotations")) | ||
} | ||
} | ||
return errorList | ||
} |
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,105 @@ | ||
/* | ||
Copyright 2022 The Koordinator 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 validating | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
admissionv1 "k8s.io/api/admission/v1" | ||
corev1 "k8s.io/api/core/v1" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/apimachinery/pkg/runtime" | ||
"k8s.io/client-go/kubernetes/scheme" | ||
"sigs.k8s.io/controller-runtime/pkg/client/fake" | ||
"sigs.k8s.io/controller-runtime/pkg/webhook/admission" | ||
|
||
configv1alpha1 "github.com/koordinator-sh/koordinator/apis/config/v1alpha1" | ||
"github.com/koordinator-sh/koordinator/apis/extension" | ||
"github.com/koordinator-sh/koordinator/pkg/util" | ||
) | ||
|
||
func init() { | ||
_ = configv1alpha1.AddToScheme(scheme.Scheme) | ||
} | ||
|
||
func TestClusterReservationValidatingPod(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
operation admissionv1.Operation | ||
oldPod *corev1.Pod | ||
newPod *corev1.Pod | ||
wantAllowed bool | ||
wantReason string | ||
wantErr bool | ||
}{ | ||
|
||
{ | ||
name: "forbidden resources annotations", | ||
operation: admissionv1.Create, | ||
newPod: &corev1.Pod{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Annotations: map[string]string{ | ||
extension.AnnotationReservationAllocated: "", | ||
}, | ||
}, | ||
}, | ||
wantErr: true, | ||
wantAllowed: false, | ||
wantReason: `annotations.scheduling.koordinator.sh/reservation-allocated: Forbidden: cannot set in annotations`, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
client := fake.NewClientBuilder().Build() | ||
decoder, _ := admission.NewDecoder(scheme.Scheme) | ||
h := &PodValidatingHandler{ | ||
Client: client, | ||
Decoder: decoder, | ||
} | ||
|
||
var objRawExt, oldObjRawExt runtime.RawExtension | ||
if tt.newPod != nil { | ||
objRawExt = runtime.RawExtension{ | ||
Raw: []byte(util.DumpJSON(tt.newPod)), | ||
} | ||
} | ||
if tt.oldPod != nil { | ||
oldObjRawExt = runtime.RawExtension{ | ||
Raw: []byte(util.DumpJSON(tt.oldPod)), | ||
} | ||
} | ||
|
||
req := newAdmissionRequest(tt.operation, objRawExt, oldObjRawExt, "pods") | ||
gotAllowed, gotReason, err := h.clusterReservationValidatingPod(context.TODO(), admission.Request{AdmissionRequest: req}) | ||
if (err != nil) != tt.wantErr { | ||
t.Errorf("clusterReservationValidatingPod() error = %v, wantErr %v", err, tt.wantErr) | ||
return | ||
} | ||
if gotAllowed != tt.wantAllowed { | ||
t.Errorf("clusterReservationValidatingPod() gotAllowed = %v, want %v", gotAllowed, tt.wantAllowed) | ||
} | ||
if gotReason != tt.wantReason { | ||
t.Errorf("clusterReservationValidatingPod():\n"+ | ||
"gotReason = %v,\n"+ | ||
"want = %v", gotReason, tt.wantReason) | ||
t.Errorf("got=%v, want=%v", len(gotReason), len(tt.wantReason)) | ||
} | ||
}) | ||
} | ||
} |