-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
WIP - fix: trim so name when is longer than 63 chars #2995
Closed
Closed
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/* | ||
Copyright 2021 The KEDA Authors | ||
|
||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package util | ||
|
||
import ( | ||
"strings" | ||
"unicode" | ||
) | ||
|
||
// Trim checks if the given string is longer than given value and trim it in that case. | ||
func Trim(s string, length int) string { | ||
result := s | ||
if len(result) > length { | ||
result = result[:length] | ||
result = strings.TrimRightFunc(result, func(r rune) bool { | ||
return !unicode.IsLetter(r) && !unicode.IsNumber(r) | ||
}) | ||
} | ||
return result | ||
} |
134 changes: 134 additions & 0 deletions
134
tests/scalers/kubernetes-workload-long-so-names.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
import * as fs from 'fs' | ||
import * as sh from 'shelljs' | ||
import * as tmp from 'tmp' | ||
import test from 'ava' | ||
import {createNamespace, waitForDeploymentReplicaCount} from "./helpers"; | ||
|
||
const testNamespace = 'scaled-object-name-trimming' | ||
const monitoredDeploymentFile = tmp.fileSync() | ||
const sutDeploymentFile = tmp.fileSync() | ||
|
||
test.before(t => { | ||
sh.config.silent = true | ||
createNamespace(testNamespace) | ||
|
||
fs.writeFileSync(monitoredDeploymentFile.name, monitoredDeploymentYaml) | ||
t.is( | ||
0, | ||
sh.exec(`kubectl apply -f ${monitoredDeploymentFile.name} --namespace ${testNamespace}`).code, | ||
'Deploying monitored deployment should work.' | ||
) | ||
|
||
fs.writeFileSync(sutDeploymentFile.name, sutDeploymentYaml) | ||
t.is( | ||
0, | ||
sh.exec(`kubectl apply -f ${sutDeploymentFile.name} --namespace ${testNamespace}`).code, | ||
'Deploying monitored deployment should work.' | ||
) | ||
}) | ||
|
||
test.serial('Deployment should have 0 replicas on start', t => { | ||
const replicaCount = sh.exec( | ||
`kubectl get deployment.apps/sut-deployment --namespace ${testNamespace} -o jsonpath="{.spec.replicas}"` | ||
).stdout | ||
t.is(replicaCount, '0', 'replica count should start out as 0') | ||
}) | ||
|
||
test.serial(`Deployment should scale to fit the amount of pods which match the selector`, async t => { | ||
|
||
sh.exec( | ||
`kubectl scale deployment.apps/monitored-deployment --namespace ${testNamespace} --replicas=5` | ||
) | ||
t.true(await waitForDeploymentReplicaCount(5, 'sut-deployment', testNamespace, 6, 10000), 'Replica count should be 5 after 60 seconds') | ||
|
||
sh.exec( | ||
`kubectl scale deployment.apps/monitored-deployment --namespace ${testNamespace} --replicas=10` | ||
) | ||
t.true(await waitForDeploymentReplicaCount(10, 'sut-deployment', testNamespace, 6, 10000), 'Replica count should be 10 after 60 seconds') | ||
|
||
sh.exec( | ||
`kubectl scale deployment.apps/monitored-deployment --namespace ${testNamespace} --replicas=5` | ||
) | ||
t.true(await waitForDeploymentReplicaCount(5, 'sut-deployment', testNamespace, 6, 10000), 'Replica count should be 5 after 60 seconds') | ||
|
||
sh.exec( | ||
`kubectl scale deployment.apps/monitored-deployment --namespace ${testNamespace} --replicas=0` | ||
) | ||
t.true(await waitForDeploymentReplicaCount(0, 'sut-deployment', testNamespace, 6, 10000), 'Replica count should be 0 after 60 seconds') | ||
}) | ||
|
||
test.after.always.cb('clean up workload test related deployments', t => { | ||
const resources = [ | ||
'scaledobject.keda.sh/sut-scaledobject', | ||
'deployment.apps/sut-deployment', | ||
'deployment.apps/monitored-deployment', | ||
] | ||
|
||
for (const resource of resources) { | ||
sh.exec(`kubectl delete ${resource} --namespace ${testNamespace}`) | ||
} | ||
sh.exec(`kubectl delete namespace ${testNamespace}`) | ||
t.end() | ||
}) | ||
|
||
const monitoredDeploymentYaml = `apiVersion: apps/v1 | ||
kind: Deployment | ||
metadata: | ||
name: monitored-deployment | ||
labels: | ||
deploy: workload-test | ||
spec: | ||
replicas: 0 | ||
selector: | ||
matchLabels: | ||
pod: workload-test | ||
template: | ||
metadata: | ||
labels: | ||
pod: workload-test | ||
spec: | ||
containers: | ||
- name: nginx | ||
image: 'nginx'` | ||
|
||
const sutDeploymentYaml = `apiVersion: apps/v1 | ||
kind: Deployment | ||
metadata: | ||
name: sut-deployment | ||
labels: | ||
deploy: workload-sut | ||
spec: | ||
replicas: 0 | ||
selector: | ||
matchLabels: | ||
pod: workload-sut | ||
template: | ||
metadata: | ||
labels: | ||
pod: workload-sut | ||
spec: | ||
containers: | ||
- name: nginx | ||
image: 'nginx' | ||
--- | ||
apiVersion: keda.sh/v1alpha1 | ||
kind: ScaledObject | ||
metadata: | ||
name: this-scaledobject-name-is-longer-than-63-characters-to-check-that-it-is-trimmed | ||
spec: | ||
scaleTargetRef: | ||
name: sut-deployment | ||
pollingInterval: 5 | ||
cooldownPeriod: 5 | ||
minReplicaCount: 0 | ||
maxReplicaCount: 10 | ||
advanced: | ||
horizontalPodAutoscalerConfig: | ||
behavior: | ||
scaleDown: | ||
stabilizationWindowSeconds: 15 | ||
triggers: | ||
- type: kubernetes-workload | ||
metadata: | ||
podSelector: 'pod=workload-test' | ||
value: '1'` |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As discussed, we need a more robust solution