Skip to content

Commit d6f4451

Browse files
fix: extra labels propagation based on k8s label regex matching (#5216)
* extra labels propagation based on k8s label regex matching * sanitizing extra labels for spaces * logger added * label regex link added * comment fix
1 parent d63de2a commit d6f4451

File tree

2 files changed

+33
-5
lines changed

2 files changed

+33
-5
lines changed

pkg/app/AppCrudOperationService.go

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -613,11 +613,25 @@ func (impl AppCrudOperationServiceImpl) getExtraAppLabelsToPropagate(appId int,
613613
impl.logger.Errorw("error in finding app and project by appId", "appId", appId, "err", err)
614614
return nil, err
615615
}
616-
return map[string]string{
617-
bean3.AppNameDevtronLabel: appName,
618-
bean3.EnvNameDevtronLabel: envName,
619-
bean3.ProjectNameDevtronLabel: appMetaInfo.Team.Name,
620-
}, nil
616+
regexp := regexp.MustCompile(LabelMatchingRegex)
617+
extraAppLabels := make(map[string]string)
618+
619+
extraAppLabels[bean3.AppNameDevtronLabel] = appName
620+
extraAppLabels[bean3.EnvNameDevtronLabel] = envName
621+
extraAppLabels[bean3.ProjectNameDevtronLabel] = appMetaInfo.Team.Name
622+
623+
extraAppLabels = sanitizeLabels(extraAppLabels)
624+
for labelKey, labelValue := range extraAppLabels {
625+
if regexp.MatchString(labelValue) {
626+
extraAppLabels[labelKey] = labelValue
627+
} else {
628+
// in case extra labels are failing k8s official label matching regex even after sanitization then
629+
//delete the label as this can break deployments.
630+
impl.logger.Warnw("extra label failed LabelMatchingRegex validation, regex:- ^(([A-Za-z0-9][-A-Za-z0-9_.]*)?[A-Za-z0-9])?$", "labelKey", labelKey, "labelValue", labelValue)
631+
delete(extraAppLabels, labelKey)
632+
}
633+
}
634+
return extraAppLabels, nil
621635
}
622636

623637
func (impl AppCrudOperationServiceImpl) GetAppLabelsForDeployment(appId int, appName, envName string) ([]byte, error) {

pkg/app/helper.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@
1616

1717
package app
1818

19+
import "strings"
20+
21+
// LabelMatchingRegex is the official k8s label matching regex, pls refer https://github.com/kubernetes/apimachinery/blob/bfd2aff97e594f6aad77acbe2cbbe190acc93cbc/pkg/util/validation/validation.go#L167
22+
const LabelMatchingRegex = "^(([A-Za-z0-9][-A-Za-z0-9_.]*)?[A-Za-z0-9])?$"
23+
1924
// MergeChildMapToParentMap merges child map of generic type map into parent map of generic type
2025
// and returns merged mapping, if parentMap is nil then nil is returned.
2126
func MergeChildMapToParentMap[T comparable, R any](parentMap map[T]R, toMergeMap map[T]R) map[T]R {
@@ -29,3 +34,12 @@ func MergeChildMapToParentMap[T comparable, R any](parentMap map[T]R, toMergeMap
2934
}
3035
return parentMap
3136
}
37+
38+
func sanitizeLabels(extraAppLabels map[string]string) map[string]string {
39+
for lkey, lvalue := range extraAppLabels {
40+
if strings.Contains(lvalue, " ") {
41+
extraAppLabels[lkey] = strings.ReplaceAll(lvalue, " ", "_")
42+
}
43+
}
44+
return extraAppLabels
45+
}

0 commit comments

Comments
 (0)