Skip to content

Commit

Permalink
Service CRD name check (#1209)
Browse files Browse the repository at this point in the history
* Service CRD name check

Fixes: #1208
  • Loading branch information
devdattakulkarni authored Feb 7, 2024
1 parent 92d6b41 commit 2e71470
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
1 change: 1 addition & 0 deletions mutating-webhook/versions.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,4 @@
3.0.11
3.0.12
3.0.13
3.0.14
32 changes: 32 additions & 0 deletions mutating-webhook/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,15 @@ func (whsvr *WebhookServer) mutate(ar *v1.AdmissionReview, httpMethod string) *v
if req.Kind.Kind == "ResourceComposition" {
if strings.Contains(user, "kubeplus-saas-provider") {

crdNameCheck := checkCRDNameValidity(ar)
if crdNameCheck != "" {
return &v1.AdmissionResponse{
Result: &metav1.Status{
Message: crdNameCheck,
},
}
}

message := checkChartExists(ar)
if message != "" {
return &v1.AdmissionResponse{
Expand Down Expand Up @@ -1279,6 +1288,29 @@ func getPaCAnnotation(ar *v1.AdmissionReview) map[string]string {
return annotations1
}


func checkCRDNameValidity(ar *v1.AdmissionReview) string {
fmt.Printf("Inside checkCRDNameValidity...\n")

req := ar.Request
body := req.Object.Raw
kind, err := jsonparser.GetUnsafeString(body, "spec","newResource","resource","kind")
if err != nil {
fmt.Errorf("Error:%s\n", err)
}

crname, err := jsonparser.GetUnsafeString(req.Object.Raw, "metadata", "name")
fmt.Printf("CR Name:%s\n", crname)
fmt.Printf("Kind:%s\n", kind)

message1 := "";
if strings.Contains(kind, ".") {
message1 = "Kind name " + kind + " invalid. Cannot contain period (.)\n"
}
return message1
}


func checkChartExists(ar *v1.AdmissionReview) string {
fmt.Printf("Inside checkChartExists...\n")

Expand Down

0 comments on commit 2e71470

Please sign in to comment.