Repository: zegl/kube-score
Pull request #469 (2022-05-13 20:28:44)

Implement Feature request: fail if port name is longer than 15 charac…
…ters #465

```
RELNOTE:  Check to see if a container port name exceeds 15 characters
```



Patch:
diff --git a/score/container/container.go b/score/container/container.go
index 374b459..b612598 100644
--- a/score/container/container.go
+++ b/score/container/container.go
@@ -256,6 +256,7 @@ func containerStorageEphemeralRequestEqualsLimit(podTemplate corev1.PodTemplateS
 // does not prevent it from being exposed. Specifying it does not expose the port outside the cluster; that
 // requires a Service object. However misspecifying elements of this optional Container
 func containerPortsCheck(podTemplate corev1.PodTemplateSpec, typeMeta metav1.TypeMeta) (score scorecard.TestScore) {
+	const maxPortNameLength = 15
 
 	allContainers := podTemplate.Spec.InitContainers
 	allContainers = append(allContainers, podTemplate.Spec.Containers...)
@@ -273,6 +274,10 @@ func containerPortsCheck(podTemplate corev1.PodTemplateSpec, typeMeta metav1.Typ
 					score.Grade = scorecard.GradeCritical
 				}
 			}
+			if len(port.Name) > maxPortNameLength {
+				score.AddComment(container.Name, "Container Port Check", "Container port.Name length exceeds maximum permitted characters")
+				score.Grade = scorecard.GradeCritical
+			}
 			if port.ContainerPort == 0 {
 				score.AddComment(container.Name, "Container Port Check", "Container ports.containerPort cannot be empty")
 				score.Grade = scorecard.GradeCritical


Test patch:
diff --git a/score/score_test.go b/score/score_test.go
index e8fa0c8..b2e9154 100644
--- a/score/score_test.go
+++ b/score/score_test.go
@@ -6,7 +6,6 @@ import (
 
 	"github.com/google/go-cmp/cmp"
 	"github.com/stretchr/testify/assert"
-
 	"github.com/zegl/kube-score/config"
 	ks "github.com/zegl/kube-score/domain"
 	"github.com/zegl/kube-score/parser"
@@ -394,6 +393,18 @@ func TestPodContainerPortsDuplicateNames(t *testing.T) {
 	}, "Container Ports Check", scorecard.GradeCritical)
 }
 
+func TestPodContainerPortsNameLength(t *testing.T) {
+	t.Parallel()
+
+	structMap := make(map[string]struct{})
+	structMap["container-ports-check"] = struct{}{}
+
+	testExpectedScoreWithConfig(t, config.Configuration{
+		AllFiles:             []ks.NamedReader{testFile("pod-container-ports-name-too-long.yaml")},
+		EnabledOptionalTests: structMap,
+	}, "Container Ports Check", scorecard.GradeCritical)
+}
+
 func TestPodContainerPortsOK(t *testing.T) {
 	t.Parallel()
 
diff --git a/score/testdata/pod-container-ports-name-too-long.yaml b/score/testdata/pod-container-ports-name-too-long.yaml
new file mode 100644
index 0000000..0dcd401
--- /dev/null
+++ b/score/testdata/pod-container-ports-name-too-long.yaml
@@ -0,0 +1,28 @@
+apiVersion: apps/v1
+kind: Deployment
+metadata:
+  name: app
+  labels:
+    app: app
+spec:
+  selector:
+    matchLabels:
+      app: app
+  template:
+    metadata:
+      labels:
+        app: app
+    spec:
+      nodeSelector:
+        beta.kubernetes.io/os: linux
+      containers:
+      - name: app
+        image: app:dev
+        imagePullPolicy: "Always"
+        ports:
+        - name: my-very-long-port-name-exceeds-fifteen-characters
+          containerPort: 80
+          protocol: TCP
+        - name: app 
+          containerPort: 8080
+          protocol: TCP