mirror of https://github.com/portainer/portainer
fix(namespace): sanitize owner label [EE-7122] (#11935)
Co-authored-by: testa113 <testa113>pull/11942/head
parent
ac3f1cd5c3
commit
27865981df
|
@ -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
|
||||
|
|
|
@ -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),
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue