Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(validation): migrate validation to ValidatingAdmissionPolicy #131

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions config/default/kustomization.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ bases:
- ../crd
- ../rbac
- ../manager
- ../validation
# [WEBHOOK] To enable webhook, uncomment all the sections with [WEBHOOK] prefix including the one in
# crd/kustomization.yaml
#- ../webhook
Expand Down
4 changes: 4 additions & 0 deletions config/validation/kustomization.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
resources:
- validation.yaml
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
43 changes: 43 additions & 0 deletions config/validation/validation.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
apiVersion: admissionregistration.k8s.io/v1beta1
kind: ValidatingAdmissionPolicy
metadata:
name: "druid.apache.org"
spec:
failurePolicy: Fail
matchConstraints:
resourceRules:
- apiGroups: ["druid.apache.org"]
apiVersions: ["v1alpha1"]
operations: ["CREATE", "UPDATE"]
resources: ["druids"]
validations:
- expression: |
object.spec.nodes.all(node, node.image != '' || object.spec.image != '')
messageExpression: "Object must have image specified either on object level or node level."
reason: "Invalid"
message: "Image is missing from spec. should be provided on either object level or node level."
- expression: |
object.spec.nodes.all(node, node.key.matches('[a-z0-9]([-a-z0-9]*[a-z0-9])?(\\.[a-z0-9]([-a-z0-9]*[a-z0-9])?)*'))
messageExpression: "All node groups must stand with kubernetes standard naming constraints."
reason: "Invalid"
message: "Node key does not matching kubernetes standard naming constraints."
- expression: |
object.spec.nodes.all(node, node.kind == 'StatefulSet' && node.volumeClaimTemplates.all(vct, vct.storageClassName == nil))
messageExpression: "All node groups' volume claim templates must have storage class specified."
reason: "Forbidden"
message: "Node group has volume claim template without storage class which is not allowed."
- expression: |
object.spec.nodes.all(node, node.additionalContainers.all(nac, object.spec.additionalContainers.all(oac, nac.containerName != oac.containerName))) &&
object.spec.nodes.all(node, node.additionalContainers.all(nac, node.additionalContainers.all(nac2, nac2.containerName != nac.containerName))) &&
object.spec.additionalContainers.all(oac, object.spec.additionalContainers.all(oac2, oac2.containerName != oac.containerName))
messageExpression: "All additional containers on all levels must have a unique container name."
reason: "Invalid"
message: "Node group has at least two additional containers with the same name."
---
apiVersion: admissionregistration.k8s.io/v1beta1
kind: ValidatingAdmissionPolicyBinding
metadata:
name: "druid.apache.org"
spec:
policyName: "druid.apache.org"
validationActions: [Deny, Audit]
39 changes: 0 additions & 39 deletions controllers/druid/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"encoding/json"
"errors"
"fmt"
"regexp"
"sort"
"strconv"
"time"
Expand Down Expand Up @@ -40,12 +39,6 @@ var logger = logf.Log.WithName("druid_operator_handler")

func deployDruidCluster(ctx context.Context, sdk client.Client, m *v1alpha1.Druid, emitEvents EventEmitter) error {

if err := verifyDruidSpec(m); err != nil {
e := fmt.Errorf("invalid DruidSpec[%s:%s] due to [%s]", m.Kind, m.Name, err.Error())
emitEvents.EmitEventGeneric(m, "DruidOperatorInvalidSpec", "", e)
return nil
}

allNodeSpecs, err := getAllNodeSpecsInDruidPrescribedOrder(m)
if err != nil {
e := fmt.Errorf("invalid DruidSpec[%s:%s] due to [%s]", m.Kind, m.Name, err.Error())
Expand Down Expand Up @@ -1359,38 +1352,6 @@ func sendEvent(ctx context.Context, sdk client.Client, drd *v1alpha1.Druid, even
}
}

func verifyDruidSpec(drd *v1alpha1.Druid) error {
keyValidationRegex, err := regexp.Compile("[a-z0-9]([-a-z0-9]*[a-z0-9])?(\\.[a-z0-9]([-a-z0-9]*[a-z0-9])?)*")
if err != nil {
return err
}

if err = validateAdditionalContainersSpec(drd); err != nil {
return err
}

if err = validateVolumeClaimTemplateSpec(drd); err != nil {
return err
}

errorMsg := ""
for key, node := range drd.Spec.Nodes {
if drd.Spec.Image == "" && node.Image == "" {
errorMsg = fmt.Sprintf("%sImage missing from Druid Cluster Spec\n", errorMsg)
}

if !keyValidationRegex.MatchString(key) {
errorMsg = fmt.Sprintf("%sNode[%s] Key must match k8s resource name regex '[a-z0-9]([-a-z0-9]*[a-z0-9])?(\\.[a-z0-9]([-a-z0-9]*[a-z0-9])?)*'", errorMsg, key)
}
}

if errorMsg == "" {
return nil
} else {
return fmt.Errorf(errorMsg)
}
}

type keyAndNodeSpec struct {
key string
spec v1alpha1.DruidNodeSpec
Expand Down
Loading