Skip to content

Commit

Permalink
fix(operator): ensure that generated resource names contain no unallo…
Browse files Browse the repository at this point in the history
…wed character (#1661)

Signed-off-by: Florian Bacher <florian.bacher@dynatrace.com>
  • Loading branch information
bacherfl committed Jul 3, 2023
1 parent d0d5bcb commit 59db60f
Show file tree
Hide file tree
Showing 8 changed files with 132 additions and 4 deletions.
12 changes: 10 additions & 2 deletions operator/common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,17 @@ func CreateResourceName(maxLen int, minSubstrLen int, str ...string) string {
str[i] = str[i][:len(str[i])-cut]
}
} else {
return strings.ToLower(newStr)
return sanitizeResourceNameString(newStr)
}
}

return strings.ToLower(strings.Join(str, "-"))
return sanitizeResourceNameString(strings.Join(str, "-"))
}

func sanitizeResourceNameString(name string) string {
// ensure lower case and replace '_' with '-'
// the replacement is done because names for resources generated by KLT
// can be derived from label values, which can include '_' characters (https://kubernetes.io/docs/concepts/overview/working-with-objects/labels/#syntax-and-character-set).
// However, '_' is not an allowed character for resource names (https://kubernetes.io/docs/concepts/overview/working-with-objects/names/#dns-subdomain-names).
return strings.ReplaceAll(strings.ToLower(name), "_", "-")
}
11 changes: 11 additions & 0 deletions operator/common/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,17 @@ func Test_CreateResourceName(t *testing.T) {
Min: 100,
Want: "str111-str22222-str3",
},
{
Name: "part contains underscore",
Input: []string{
"str_1",
"str2",
"str3",
},
Max: 20,
Min: 5,
Want: "str-1-str2-str3",
},
}
for _, tt := range tests {
t.Run(tt.Name, func(t *testing.T) {
Expand Down
12 changes: 10 additions & 2 deletions scheduler/pkg/klcpermit/workflow_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,11 +163,19 @@ func createResourceName(maxLen int, minSubstrLen int, str ...string) string {
str[i] = str[i][:len(str[i])-cut]
}
} else {
return strings.ToLower(newStr)
return sanitizeResourceNameString(newStr)
}
}

return strings.ToLower(strings.Join(str, "-"))
return sanitizeResourceNameString(strings.Join(str, "-"))
}

func sanitizeResourceNameString(name string) string {
// ensure lower case and replace '_' with '-'
// the replacement is done because names for resources generated by KLT
// can be derived from label values, which can include '_' characters (https://kubernetes.io/docs/concepts/overview/working-with-objects/labels/#syntax-and-character-set).
// However, '_' is not an allowed character for resource names (https://kubernetes.io/docs/concepts/overview/working-with-objects/names/#dns-subdomain-names).
return strings.ReplaceAll(strings.ToLower(name), "_", "-")
}

func getCRDName(pod *corev1.Pod) string {
Expand Down
11 changes: 11 additions & 0 deletions scheduler/pkg/klcpermit/workflow_manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,17 @@ func Test_CreateResourceName(t *testing.T) {
Min: 100,
Want: "str111-str22222-str3",
},
{
Name: "part containing an underscore",
Input: []string{
"str_1",
"str2",
"str3",
},
Max: 20,
Min: 100,
Want: "str-1-str2-str3",
},
}
for _, tt := range tests {
t.Run(tt.Name, func(t *testing.T) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
app: test
name: test
status:
readyReplicas: 1
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
apiVersion: lifecycle.keptn.sh/v1alpha3
kind: KeptnTaskDefinition
metadata:
name: pre-deployment-hello
spec:
function:
inline:
code: |
console.log("Pre-Deployment Task has been executed");
---
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
app: test
name: test
spec:
replicas: 1
selector:
matchLabels:
app: test
strategy: {}
template:
metadata:
labels:
app: test
annotations:
keptn.sh/workload: waiter
keptn.sh/version: "0.4.0_1"
keptn.sh/pre-deployment-tasks: pre-deployment-hello
keptn.sh/post-deployment-tasks: pre-deployment-hello
spec:
containers:
- image: busybox
name: busybox
command: ['sh', '-c', 'echo The app is running! && sleep infinity']
initContainers:
- name: init-myservice
image: busybox:1.36.1
command: ['sh', '-c', 'sleep 30']
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
apiVersion: kuttl.dev/v1
kind: TestStep
commands:
- script: kubectl annotate ns $NAMESPACE keptn.sh/lifecycle-toolkit='enabled'
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
apiVersion: lifecycle.keptn.sh/v1alpha3
kind: KeptnWorkload
metadata:
name: waiter-waiter

---

apiVersion: lifecycle.keptn.sh/v1alpha3
kind: KeptnWorkloadInstance
metadata:
name: waiter-waiter-0.4.0-1
status:
currentPhase: Completed
deploymentStatus: Succeeded
postDeploymentEvaluationStatus: Succeeded
postDeploymentStatus: Succeeded
postDeploymentTaskStatus:
- status: Succeeded
definitionName: pre-deployment-hello
preDeploymentEvaluationStatus: Succeeded
preDeploymentStatus: Succeeded
preDeploymentTaskStatus:
- status: Succeeded
definitionName: pre-deployment-hello

---

apiVersion: lifecycle.keptn.sh/v1alpha3
kind: KeptnApp
metadata:
name: waiter

---

apiVersion: lifecycle.keptn.sh/v1alpha3
kind: KeptnAppVersion
metadata:
name: waiter-62c130d876-6b86b273

0 comments on commit 59db60f

Please sign in to comment.