Skip to content

Commit

Permalink
support * as matcher
Browse files Browse the repository at this point in the history
Signed-off-by: stoneshi-yunify <stoneshi@kubesphere.io>
  • Loading branch information
stoneshi-yunify committed Sep 19, 2023
1 parent 3410581 commit 2f39c69
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 8 deletions.
4 changes: 2 additions & 2 deletions webhook/pvc.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,6 @@ func (a *Admitter) serverPVCRequest(w http.ResponseWriter, r *http.Request) {
}

func (a *Admitter) AdmitPVC(ar admissionv1.AdmissionReview) *admissionv1.AdmissionResponse {
klog.Infof("admission review: %v", ar)

if ar.Request.Operation != admissionv1.Create {
return reviewResponse
}
Expand Down Expand Up @@ -87,6 +85,8 @@ func (a *Admitter) AdmitPVC(ar admissionv1.AdmissionReview) *admissionv1.Admissi
operator: string(ar.Request.Operation),
storageClassName: *newPVC.Spec.StorageClassName,
}

klog.Infof("request pvc: %v", reqPVC)
return a.decidePVCV1(context.Background(), reqPVC)
}

Expand Down
28 changes: 22 additions & 6 deletions webhook/selector.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,24 @@ func matchLabel(info map[string]string, expressions []v1alpha1.MatchExpressions)
if len(item.Values) == 0 {
continue
}
var labelKeyExist bool
_, ok := info[item.Key]
if ok {
labelKeyExist = true
}
switch item.Operator {
case v1alpha1.In:
rulePass = rulePass && slices.Contains(item.Values, info[item.Key])
rulePass = rulePass && labelKeyExist && (slices.Contains(item.Values, "*") || slices.Contains(item.Values, info[item.Key]))
case v1alpha1.NotIn:
rulePass = rulePass && !slices.Contains(item.Values, info[item.Key])
if labelKeyExist {
rulePass = rulePass && !slices.Contains(item.Values, "*") && !slices.Contains(item.Values, info[item.Key])
}
default:
continue
}
if !rulePass {
break
}
}
if rulePass {
return rulePass
Expand Down Expand Up @@ -58,12 +68,15 @@ func nsMatchField(ns *corev1.Namespace, expressions []v1alpha1.FieldExpressions)
}
switch item.Operator {
case v1alpha1.In:
rulePass = rulePass && slices.Contains(item.Values, val)
rulePass = rulePass && (slices.Contains(item.Values, "*") || slices.Contains(item.Values, val))
case v1alpha1.NotIn:
rulePass = rulePass && !slices.Contains(item.Values, val)
rulePass = rulePass && !slices.Contains(item.Values, "*") && !slices.Contains(item.Values, val)
default:
continue
}
if !rulePass {
break
}
}
if rulePass {
return rulePass
Expand Down Expand Up @@ -95,12 +108,15 @@ func wsMatchField(ws *workspacev1alpha1.Workspace, expressions []v1alpha1.FieldE
}
switch item.Operator {
case v1alpha1.In:
pass = pass && slices.Contains(item.Values, val)
pass = pass && (slices.Contains(item.Values, "*") || slices.Contains(item.Values, val))
case v1alpha1.NotIn:
pass = pass && !slices.Contains(item.Values, val)
pass = pass && !slices.Contains(item.Values, "*") && !slices.Contains(item.Values, val)
default:
continue
}
if !pass {
break
}
}
if pass {
return pass
Expand Down

0 comments on commit 2f39c69

Please sign in to comment.