Skip to content

Commit

Permalink
fix(namespace): sanitize owner label [EE-7122] (#11935)
Browse files Browse the repository at this point in the history
Co-authored-by: testa113 <testa113>
  • Loading branch information
testA113 committed Jun 12, 2024
1 parent ac3f1cd commit 2786598
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 15 deletions.
5 changes: 3 additions & 2 deletions api/kubernetes/cli/namespace.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down
16 changes: 5 additions & 11 deletions api/kubernetes/yaml.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -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,
}
}
Expand All @@ -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),
}
}

Expand Down
6 changes: 4 additions & 2 deletions api/stacks/stackutils/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package stackutils
import (
"fmt"
"regexp"
"strings"

portainer "github.com/portainer/portainer/api"
"github.com/portainer/portainer/api/filesystem"
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 2786598

Please sign in to comment.