Skip to content

Commit 6c959f9

Browse files
authored
Add anti-affinity rules to controller's replicas (#1397)
* Add anti-affinity rules to controller's replicas * fix e2e test: Error unknown shorthand flag 'n' in -n
1 parent 99d578f commit 6c959f9

File tree

2 files changed

+64
-3
lines changed

2 files changed

+64
-3
lines changed

config/100-deployment.yaml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,26 @@ spec:
8585
pipeline.tekton.dev/release: "devel"
8686
version: "devel"
8787
spec:
88+
affinity:
89+
nodeAffinity:
90+
requiredDuringSchedulingIgnoredDuringExecution:
91+
nodeSelectorTerms:
92+
- matchExpressions:
93+
- key: kubernetes.io/os
94+
operator: NotIn
95+
values:
96+
- windows
97+
podAntiAffinity:
98+
preferredDuringSchedulingIgnoredDuringExecution:
99+
- podAffinityTerm:
100+
labelSelector:
101+
matchLabels:
102+
app.kubernetes.io/name: controller
103+
app.kubernetes.io/component: controller
104+
app.kubernetes.io/instance: default
105+
app.kubernetes.io/part-of: tekton-chains
106+
topologyKey: kubernetes.io/hostname
107+
weight: 100
88108
serviceAccountName: tekton-chains-controller
89109
containers:
90110
- name: tekton-chains-controller

test/test_utils.go

Lines changed: 44 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626
"math/rand"
2727
"os"
2828
"os/exec"
29+
"regexp"
2930
"strings"
3031
"testing"
3132
"time"
@@ -241,16 +242,33 @@ func printDebugging(t *testing.T, obj objects.TektonObject) {
241242
t.Helper()
242243
kind := obj.GetObjectKind().GroupVersionKind().Kind
243244

245+
// Validate and sanitize inputs to prevent potential command injection
246+
if !isValidKubernetesKind(kind) {
247+
t.Logf("Invalid kind: %s, skipping logs", kind)
248+
return
249+
}
250+
251+
objNamespace := obj.GetNamespace()
252+
objName := obj.GetName()
253+
254+
if !isValidKubernetesName(objNamespace) || !isValidKubernetesName(objName) {
255+
t.Logf("Invalid namespace or name: %s/%s, skipping logs", objNamespace, objName)
256+
return
257+
}
258+
244259
t.Logf("============================== %s logs ==============================", obj.GetGVK())
245-
output, _ := exec.Command("tkn", strings.ToLower(kind), "logs", "-n", obj.GetNamespace(), obj.GetName()).CombinedOutput()
260+
// #nosec G204 -- objNamespace and objName are validated by isValidKubernetesName to prevent command injection
261+
output, _ := exec.Command("tkn", strings.ToLower(kind), "logs", "--namespace", objNamespace, objName).CombinedOutput()
246262
t.Log(string(output))
247263

248264
t.Logf("============================== %s describe ==============================", obj.GetGVK())
249-
output, _ = exec.Command("tkn", strings.ToLower(kind), "describe", "-n", obj.GetNamespace(), obj.GetName()).CombinedOutput()
265+
// #nosec G204 -- objNamespace and objName are validated by isValidKubernetesName to prevent command injection
266+
output, _ = exec.Command("tkn", strings.ToLower(kind), "describe", "--namespace", objNamespace, objName).CombinedOutput()
250267
t.Log(string(output))
251268

252269
t.Log("============================== chains controller logs ==============================")
253-
output, _ = exec.Command("kubectl", "logs", "deploy/tekton-chains-controller", "-n", namespace).CombinedOutput()
270+
// #nosec G204 -- namespace is a package-level constant, not user input
271+
output, _ = exec.Command("kubectl", "logs", "deploy/tekton-chains-controller", "--namespace", namespace).CombinedOutput()
254272
t.Log(string(output))
255273
}
256274

@@ -348,3 +366,26 @@ func restartControllerPod(ctx context.Context, c kubernetes.Interface, timeout t
348366
return false, nil
349367
})
350368
}
369+
370+
// isValidKubernetesKind validates that a string is a valid Kubernetes kind
371+
func isValidKubernetesKind(kind string) bool {
372+
// Allow common Tekton kinds
373+
validKinds := []string{"TaskRun", "PipelineRun", "Task", "Pipeline"}
374+
for _, validKind := range validKinds {
375+
if strings.EqualFold(kind, validKind) {
376+
return true
377+
}
378+
}
379+
return false
380+
}
381+
382+
// isValidKubernetesName validates that a string follows Kubernetes naming rules
383+
// DNS-1123 subdomain rules: lowercase alphanumeric characters, '-' or '.'
384+
func isValidKubernetesName(name string) bool {
385+
if len(name) == 0 || len(name) > 253 {
386+
return false
387+
}
388+
// Kubernetes names should match DNS-1123 subdomain rules
389+
matched, _ := regexp.MatchString(`^[a-z0-9]([-a-z0-9]*[a-z0-9])?(\.[a-z0-9]([-a-z0-9]*[a-z0-9])?)*$`, name)
390+
return matched
391+
}

0 commit comments

Comments
 (0)