mirror of https://github.com/portainer/portainer
Sanitze kube labels (#7658)
parent
5232427a5b
commit
1950c4ca2b
|
@ -4,6 +4,7 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
|
"regexp"
|
||||||
"strconv"
|
"strconv"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
@ -93,6 +94,12 @@ type createKubernetesStackResponse struct {
|
||||||
Output string `json:"Output"`
|
Output string `json:"Output"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 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, ".")
|
||||||
|
}
|
||||||
|
|
||||||
func (handler *Handler) createKubernetesStackFromFileContent(w http.ResponseWriter, r *http.Request, endpoint *portainer.Endpoint, userID portainer.UserID) *httperror.HandlerError {
|
func (handler *Handler) createKubernetesStackFromFileContent(w http.ResponseWriter, r *http.Request, endpoint *portainer.Endpoint, userID portainer.UserID) *httperror.HandlerError {
|
||||||
var payload kubernetesStringDeploymentPayload
|
var payload kubernetesStringDeploymentPayload
|
||||||
if err := request.DecodeAndValidateJSONPayload(r, &payload); err != nil {
|
if err := request.DecodeAndValidateJSONPayload(r, &payload); err != nil {
|
||||||
|
@ -121,7 +128,7 @@ func (handler *Handler) createKubernetesStackFromFileContent(w http.ResponseWrit
|
||||||
Namespace: payload.Namespace,
|
Namespace: payload.Namespace,
|
||||||
Status: portainer.StackStatusActive,
|
Status: portainer.StackStatusActive,
|
||||||
CreationDate: time.Now().Unix(),
|
CreationDate: time.Now().Unix(),
|
||||||
CreatedBy: user.Username,
|
CreatedBy: sanitizeLabel(user.Username),
|
||||||
IsComposeFormat: payload.ComposeFormat,
|
IsComposeFormat: payload.ComposeFormat,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -143,7 +150,7 @@ func (handler *Handler) createKubernetesStackFromFileContent(w http.ResponseWrit
|
||||||
output, err := handler.deployKubernetesStack(user.ID, endpoint, stack, k.KubeAppLabels{
|
output, err := handler.deployKubernetesStack(user.ID, endpoint, stack, k.KubeAppLabels{
|
||||||
StackID: stackID,
|
StackID: stackID,
|
||||||
StackName: stack.Name,
|
StackName: stack.Name,
|
||||||
Owner: stack.CreatedBy,
|
Owner: sanitizeLabel(stack.CreatedBy),
|
||||||
Kind: "content",
|
Kind: "content",
|
||||||
})
|
})
|
||||||
|
|
||||||
|
@ -248,7 +255,7 @@ func (handler *Handler) createKubernetesStackFromGitRepository(w http.ResponseWr
|
||||||
output, err := handler.deployKubernetesStack(user.ID, endpoint, stack, k.KubeAppLabels{
|
output, err := handler.deployKubernetesStack(user.ID, endpoint, stack, k.KubeAppLabels{
|
||||||
StackID: stackID,
|
StackID: stackID,
|
||||||
StackName: stack.Name,
|
StackName: stack.Name,
|
||||||
Owner: stack.CreatedBy,
|
Owner: sanitizeLabel(stack.CreatedBy),
|
||||||
Kind: "git",
|
Kind: "git",
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
|
@ -4,6 +4,7 @@ import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
|
"regexp"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
@ -27,13 +28,19 @@ 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: kal.StackName,
|
||||||
labelPortainerAppName: kal.StackName,
|
labelPortainerAppName: kal.StackName,
|
||||||
labelPortainerAppOwner: kal.Owner,
|
labelPortainerAppOwner: sanitizeLabel(kal.Owner),
|
||||||
labelPortainerAppKind: kal.Kind,
|
labelPortainerAppKind: kal.Kind,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -42,7 +49,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: owner,
|
labelPortainerAppOwner: sanitizeLabel(owner),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue