@@ -12,13 +12,21 @@ import (
1212
1313 admissionv1 "k8s.io/api/admission/v1"
1414 appsv1 "k8s.io/api/apps/v1"
15+ "k8s.io/apimachinery/pkg/types"
16+ "k8s.io/klog/v2"
1517 "sigs.k8s.io/controller-runtime/pkg/manager"
1618 "sigs.k8s.io/controller-runtime/pkg/webhook"
1719 "sigs.k8s.io/controller-runtime/pkg/webhook/admission"
1820
1921 "go.goms.io/fleet/pkg/utils"
2022)
2123
24+ const (
25+ deniedReplicaSetResource = "ReplicaSet creation is disallowed in the fleet hub cluster"
26+ allowedReplicaSetResource = "ReplicaSet creation is allowed in the fleet hub cluster"
27+ replicaSetDeniedFormat = "ReplicaSet %s/%s creation is disallowed in the fleet hub cluster."
28+ )
29+
2230var (
2331 // ValidationPath is the webhook service path which admission requests are routed to for validating ReplicaSet resources.
2432 ValidationPath = fmt .Sprintf (utils .ValidationPathFmt , appsv1 .SchemeGroupVersion .Group , appsv1 .SchemeGroupVersion .Version , "replicaset" )
@@ -37,14 +45,18 @@ func Add(mgr manager.Manager) error {
3745
3846// Handle replicaSetValidator denies all creation requests.
3947func (v * replicaSetValidator ) Handle (_ context.Context , req admission.Request ) admission.Response {
48+ namespacedName := types.NamespacedName {Name : req .Name , Namespace : req .Namespace }
4049 if req .Operation == admissionv1 .Create {
50+ klog .V (2 ).InfoS ("handling replicaSet resource" , "operation" , req .Operation , "subResource" , req .SubResource , "namespacedName" , namespacedName )
4151 rs := & appsv1.ReplicaSet {}
4252 if err := v .decoder .Decode (req , rs ); err != nil {
4353 return admission .Errored (http .StatusBadRequest , err )
4454 }
4555 if ! utils .IsReservedNamespace (rs .Namespace ) {
46- return admission .Denied (fmt .Sprintf ("ReplicaSet %s/%s creation is disallowed in the fleet hub cluster." , rs .Namespace , rs .Name ))
56+ klog .V (2 ).InfoS (deniedReplicaSetResource , "user" , req .UserInfo .Username , "groups" , req .UserInfo .Groups , "operation" , req .Operation , "GVK" , req .RequestKind , "subResource" , req .SubResource , "namespacedName" , namespacedName )
57+ return admission .Denied (fmt .Sprintf (replicaSetDeniedFormat , rs .Namespace , rs .Name ))
4758 }
4859 }
60+ klog .V (3 ).InfoS (allowedReplicaSetResource , "user" , req .UserInfo .Username , "groups" , req .UserInfo .Groups , "operation" , req .Operation , "GVK" , req .RequestKind , "subResource" , req .SubResource , "namespacedName" , namespacedName )
4961 return admission .Allowed ("" )
5062}
0 commit comments