diff --git a/api/kubernetes/cli/namespace.go b/api/kubernetes/cli/namespace.go index b78276ca5..a3fcfe211 100644 --- a/api/kubernetes/cli/namespace.go +++ b/api/kubernetes/cli/namespace.go @@ -8,6 +8,7 @@ import ( "github.com/pkg/errors" portainer "github.com/portainer/portainer/api" models "github.com/portainer/portainer/api/http/models/kubernetes" + "github.com/portainer/portainer/api/stacks/stackutils" "github.com/rs/zerolog/log" v1 "k8s.io/api/core/v1" "k8s.io/apimachinery/pkg/api/resource" @@ -64,8 +65,8 @@ func (kcl *KubeClient) GetNamespace(name string) (portainer.K8sNamespaceInfo, er // CreateNamespace creates a new ingress in a given namespace in a k8s endpoint. func (kcl *KubeClient) CreateNamespace(info models.K8sNamespaceDetails) error { portainerLabels := map[string]string{ - "io.portainer.kubernetes.resourcepool.name": info.Name, - "io.portainer.kubernetes.resourcepool.owner": info.Owner, + "io.portainer.kubernetes.resourcepool.name": stackutils.SanitizeLabel(info.Name), + "io.portainer.kubernetes.resourcepool.owner": stackutils.SanitizeLabel(info.Owner), } var ns v1.Namespace diff --git a/api/kubernetes/yaml.go b/api/kubernetes/yaml.go index 1bde934e9..4a90d1e5c 100644 --- a/api/kubernetes/yaml.go +++ b/api/kubernetes/yaml.go @@ -4,11 +4,11 @@ import ( "bytes" "fmt" "io" - "regexp" "strconv" "strings" "github.com/pkg/errors" + "github.com/portainer/portainer/api/stacks/stackutils" "gopkg.in/yaml.v3" ) @@ -28,19 +28,13 @@ 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: sanitizeLabel(kal.Owner), + labelPortainerAppStack: stackutils.SanitizeLabel(kal.StackName), + labelPortainerAppName: stackutils.SanitizeLabel(kal.StackName), + labelPortainerAppOwner: stackutils.SanitizeLabel(kal.Owner), labelPortainerAppKind: kal.Kind, } } @@ -49,7 +43,7 @@ func (kal *KubeAppLabels) ToMap() map[string]string { func GetHelmAppLabels(name, owner string) map[string]string { return map[string]string{ labelPortainerAppName: name, - labelPortainerAppOwner: sanitizeLabel(owner), + labelPortainerAppOwner: stackutils.SanitizeLabel(owner), } } diff --git a/api/stacks/stackutils/util.go b/api/stacks/stackutils/util.go index 91c9d464f..5dd5bf4c5 100644 --- a/api/stacks/stackutils/util.go +++ b/api/stacks/stackutils/util.go @@ -3,6 +3,7 @@ package stackutils import ( "fmt" "regexp" + "strings" portainer "github.com/portainer/portainer/api" "github.com/portainer/portainer/api/filesystem" @@ -37,10 +38,11 @@ func ResourceControlID(endpointID portainer.EndpointID, name string) string { return fmt.Sprintf("%d_%s", endpointID, name) } -// convert string to valid kubernetes label by replacing invalid characters with periods +// convert string to valid kubernetes label by replacing invalid characters with periods and removing any periods at the beginning or end of the string func SanitizeLabel(value string) string { re := regexp.MustCompile(`[^A-Za-z0-9\.\-\_]+`) - return re.ReplaceAllString(value, ".") + onlyAllowedCharacterString := re.ReplaceAllString(value, ".") + return strings.Trim(onlyAllowedCharacterString, ".-_") } // IsGitStack checks if the stack is a git stack or not