1
0
Fork 0
mirror of https://github.com/portainer/portainer.git synced 2025-07-24 07:49:41 +02:00

Sanitze kube labels (#7658)

This commit is contained in:
Matt Hook 2022-09-20 16:19:54 +12:00 committed by GitHub
parent 5232427a5b
commit 1950c4ca2b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 19 additions and 5 deletions

View file

@ -4,6 +4,7 @@ import (
"bytes"
"fmt"
"io"
"regexp"
"strconv"
"strings"
@ -27,13 +28,19 @@ type KubeAppLabels struct {
Kind string
}
// convert string to valid kubernetes label by replacing invalid characters with periods
func sanitizeLabel(value string) string {
re := regexp.MustCompile(`[^A-Za-z0-9\.\-\_]+`)
return re.ReplaceAllString(value, ".")
}
// ToMap converts KubeAppLabels to a map[string]string
func (kal *KubeAppLabels) ToMap() map[string]string {
return map[string]string{
labelPortainerAppStackID: strconv.Itoa(kal.StackID),
labelPortainerAppStack: kal.StackName,
labelPortainerAppName: kal.StackName,
labelPortainerAppOwner: kal.Owner,
labelPortainerAppOwner: sanitizeLabel(kal.Owner),
labelPortainerAppKind: kal.Kind,
}
}
@ -42,7 +49,7 @@ func (kal *KubeAppLabels) ToMap() map[string]string {
func GetHelmAppLabels(name, owner string) map[string]string {
return map[string]string{
labelPortainerAppName: name,
labelPortainerAppOwner: owner,
labelPortainerAppOwner: sanitizeLabel(owner),
}
}