fix(namespace): sanitize owner label [EE-7122] (#11936)

Co-authored-by: testa113 <testa113>
pull/11943/head
Ali 5 months ago committed by GitHub
parent 93ce33fac8
commit 69f9a509c8
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -8,6 +8,7 @@ import (
"github.com/pkg/errors" "github.com/pkg/errors"
portainer "github.com/portainer/portainer/api" portainer "github.com/portainer/portainer/api"
models "github.com/portainer/portainer/api/http/models/kubernetes" models "github.com/portainer/portainer/api/http/models/kubernetes"
"github.com/portainer/portainer/api/stacks/stackutils"
"github.com/rs/zerolog/log" "github.com/rs/zerolog/log"
v1 "k8s.io/api/core/v1" v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource" "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. // CreateNamespace creates a new ingress in a given namespace in a k8s endpoint.
func (kcl *KubeClient) CreateNamespace(info models.K8sNamespaceDetails) error { func (kcl *KubeClient) CreateNamespace(info models.K8sNamespaceDetails) error {
portainerLabels := map[string]string{ portainerLabels := map[string]string{
"io.portainer.kubernetes.resourcepool.name": info.Name, "io.portainer.kubernetes.resourcepool.name": stackutils.SanitizeLabel(info.Name),
"io.portainer.kubernetes.resourcepool.owner": info.Owner, "io.portainer.kubernetes.resourcepool.owner": stackutils.SanitizeLabel(info.Owner),
} }
var ns v1.Namespace var ns v1.Namespace

@ -4,11 +4,11 @@ import (
"bytes" "bytes"
"fmt" "fmt"
"io" "io"
"regexp"
"strconv" "strconv"
"strings" "strings"
"github.com/pkg/errors" "github.com/pkg/errors"
"github.com/portainer/portainer/api/stacks/stackutils"
"gopkg.in/yaml.v3" "gopkg.in/yaml.v3"
) )
@ -28,19 +28,13 @@ type KubeAppLabels struct {
Kind string 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 // ToMap converts KubeAppLabels to a map[string]string
func (kal *KubeAppLabels) ToMap() map[string]string { func (kal *KubeAppLabels) ToMap() map[string]string {
return map[string]string{ return map[string]string{
labelPortainerAppStackID: strconv.Itoa(kal.StackID), labelPortainerAppStackID: strconv.Itoa(kal.StackID),
labelPortainerAppStack: kal.StackName, labelPortainerAppStack: stackutils.SanitizeLabel(kal.StackName),
labelPortainerAppName: kal.StackName, labelPortainerAppName: stackutils.SanitizeLabel(kal.StackName),
labelPortainerAppOwner: sanitizeLabel(kal.Owner), labelPortainerAppOwner: stackutils.SanitizeLabel(kal.Owner),
labelPortainerAppKind: kal.Kind, labelPortainerAppKind: kal.Kind,
} }
} }
@ -49,7 +43,7 @@ func (kal *KubeAppLabels) ToMap() map[string]string {
func GetHelmAppLabels(name, owner string) map[string]string { func GetHelmAppLabels(name, owner string) map[string]string {
return map[string]string{ return map[string]string{
labelPortainerAppName: name, labelPortainerAppName: name,
labelPortainerAppOwner: sanitizeLabel(owner), labelPortainerAppOwner: stackutils.SanitizeLabel(owner),
} }
} }

@ -3,6 +3,7 @@ package stackutils
import ( import (
"fmt" "fmt"
"regexp" "regexp"
"strings"
portainer "github.com/portainer/portainer/api" portainer "github.com/portainer/portainer/api"
"github.com/portainer/portainer/api/filesystem" "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) 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 { func SanitizeLabel(value string) string {
re := regexp.MustCompile(`[^A-Za-z0-9\.\-\_]+`) 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 // IsGitStack checks if the stack is a git stack or not

Loading…
Cancel
Save