Repository: datreeio/datree
Pull request #612 (2022-05-11 14:31:29)

feat: add rule 54 and toggle off rule 14


Patch:
diff --git a/pkg/policy/defaultRules.yaml b/pkg/policy/defaultRules.yaml
index c0b2f5a..533f84f 100644
--- a/pkg/policy/defaultRules.yaml
+++ b/pkg/policy/defaultRules.yaml
@@ -388,7 +388,7 @@ rules:
   - id: 14
     name: Ensure HPA has maximum replicas configured
     uniqueName: HPA_MISSING_MAXREPLICAS_KEY
-    enabledByDefault: true
+    enabledByDefault: false
     documentationUrl: "https://hub.datree.io/ensure-hpa-maximum-replicas"
     messageOnFailure: "Missing property object `maxReplicas` - the value should be within the accepted boundaries recommended by the organization"
     category: Other
@@ -1516,3 +1516,37 @@ rules:
       allOf:
         - $ref: "#/definitions/podPattern"
         - $ref: "#/definitions/serviceAccountPattern"
+  - id: 54
+    name: Ensure resource has a configured name
+    uniqueName: RESOURCE_MISSING_NAME
+    enabledByDefault: true
+    documentationUrl: "https://hub.datree.io/built-in-rules/ensure-resource-name"
+    messageOnFailure: "Missing key `name` or `generateName` - one of them must be set to apply resource to a cluster"
+    category: Other
+    schema:
+      definitions:
+        metadataNamePattern:
+          properties:
+            metadata:
+              type: object
+              properties:
+                name:
+                  type: string
+              required:
+                - name
+          required:
+            - metadata
+        metadataGenerateNamePattern:
+          properties:
+            metadata:
+              type: object
+              properties:
+                generateName:
+                  type: string
+              required:
+                - generateName
+          required:
+            - metadata
+      anyOf:
+        - $ref: "#/definitions/metadataNamePattern"
+        - $ref: "#/definitions/metadataGenerateNamePattern"
diff --git a/pkg/policy/defaultRulesSchema.json b/pkg/policy/defaultRulesSchema.json
index e0698e4..8639626 100644
--- a/pkg/policy/defaultRulesSchema.json
+++ b/pkg/policy/defaultRulesSchema.json
@@ -10,15 +10,15 @@
     },
     "rules": {
       "type": "array",
-      "minItems": 53,
-      "maxItems": 53,
+      "minItems": 54,
+      "maxItems": 54,
       "items": {
         "type": "object",
         "properties": {
           "id": {
             "type": "number",
             "minimum": 1,
-            "maximum": 53
+            "maximum": 54
           },
           "name": {
             "type": "string",


Test patch:
diff --git a/bl/evaluation/printer_test_expected_outputs/JUnit_output.xml b/bl/evaluation/printer_test_expected_outputs/JUnit_output.xml
index 4c2345b..381e30d 100755
--- a/bl/evaluation/printer_test_expected_outputs/JUnit_output.xml
+++ b/bl/evaluation/printer_test_expected_outputs/JUnit_output.xml
@@ -22,7 +22,6 @@
 		</testcase>
 		<testcase name="Ensure each container has a configured readiness probe" classname="CONTAINERS_MISSING_READINESSPROBE_KEY"></testcase>
 		<testcase name="Ensure HPA has minimum replicas configured" classname="HPA_MISSING_MINREPLICAS_KEY"></testcase>
-		<testcase name="Ensure HPA has maximum replicas configured" classname="HPA_MISSING_MAXREPLICAS_KEY"></testcase>
 		<testcase name="Prevent workload from using the default namespace" classname="WORKLOAD_INCORRECT_NAMESPACE_VALUE_DEFAULT"></testcase>
 		<testcase name="Ensure Deployment has more than one replica configured" classname="DEPLOYMENT_INCORRECT_REPLICAS_VALUE"></testcase>
 		<testcase name="Ensure CronJob has a configured deadline" classname="CRONJOB_MISSING_STARTINGDEADLINESECOND_KEY"></testcase>
@@ -30,6 +29,7 @@
 		<testcase name="Prevent deprecated APIs in Kubernetes v1.17" classname="K8S_DEPRECATED_APIVERSION_1.17"></testcase>
 		<testcase name="Prevent containers from having root access capabilities" classname="CONTAINERS_INCORRECT_PRIVILEGED_VALUE_TRUE"></testcase>
 		<testcase name="Prevent CronJob from executing jobs concurrently" classname="CRONJOB_MISSING_CONCURRENCYPOLICY_KEY"></testcase>
+		<testcase name="Ensure resource has a configured name" classname="RESOURCE_MISSING_NAME"></testcase>
 	</testsuite>
 	<testsuite name="policySummary">
 		<properties>
diff --git a/pkg/policy/tests/54-fail.yaml b/pkg/policy/tests/54-fail.yaml
new file mode 100644
index 0000000..c3dd238
--- /dev/null
+++ b/pkg/policy/tests/54-fail.yaml
@@ -0,0 +1,14 @@
+apiVersion: v1
+kind: Pod
+spec:
+  containers:
+    - image: k8s.gcr.io/test-webserver
+      name: test-container
+      volumeMounts:
+        - mountPath: /test-pd
+          name: test-volume
+  volumes:
+    - name: test-volume
+      hostPath:
+        path: /data
+        type: Directory
diff --git a/pkg/policy/tests/54-pass.yaml b/pkg/policy/tests/54-pass.yaml
new file mode 100644
index 0000000..ecd989a
--- /dev/null
+++ b/pkg/policy/tests/54-pass.yaml
@@ -0,0 +1,16 @@
+apiVersion: v1
+kind: Pod
+metadata:
+ generateName: fsd
+spec:
+  containers:
+    - image: k8s.gcr.io/test-webserver
+      name: test-container
+      volumeMounts:
+        - mountPath: /test-pd
+          name: test-volume
+  volumes:
+    - name: test-volume
+      hostPath:
+        path: /data
+        type: Directory