Skip to content

fix: extra labels propagation based on k8s label regex matching #5216

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
May 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 19 additions & 5 deletions pkg/app/AppCrudOperationService.go
Original file line number Diff line number Diff line change
Expand Up @@ -613,11 +613,25 @@ func (impl AppCrudOperationServiceImpl) getExtraAppLabelsToPropagate(appId int,
impl.logger.Errorw("error in finding app and project by appId", "appId", appId, "err", err)
return nil, err
}
return map[string]string{
bean3.AppNameDevtronLabel: appName,
bean3.EnvNameDevtronLabel: envName,
bean3.ProjectNameDevtronLabel: appMetaInfo.Team.Name,
}, nil
regexp := regexp.MustCompile(LabelMatchingRegex)
extraAppLabels := make(map[string]string)

extraAppLabels[bean3.AppNameDevtronLabel] = appName
extraAppLabels[bean3.EnvNameDevtronLabel] = envName
extraAppLabels[bean3.ProjectNameDevtronLabel] = appMetaInfo.Team.Name

extraAppLabels = sanitizeLabels(extraAppLabels)
for labelKey, labelValue := range extraAppLabels {
if regexp.MatchString(labelValue) {
extraAppLabels[labelKey] = labelValue
} else {
// in case extra labels are failing k8s official label matching regex even after sanitization then
//delete the label as this can break deployments.
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)
delete(extraAppLabels, labelKey)
}
}
return extraAppLabels, nil
}

func (impl AppCrudOperationServiceImpl) GetAppLabelsForDeployment(appId int, appName, envName string) ([]byte, error) {
Expand Down
14 changes: 14 additions & 0 deletions pkg/app/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@

package app

import "strings"

// LabelMatchingRegex is the official k8s label matching regex, pls refer https://github.com/kubernetes/apimachinery/blob/bfd2aff97e594f6aad77acbe2cbbe190acc93cbc/pkg/util/validation/validation.go#L167
const LabelMatchingRegex = "^(([A-Za-z0-9][-A-Za-z0-9_.]*)?[A-Za-z0-9])?$"

// MergeChildMapToParentMap merges child map of generic type map into parent map of generic type
// and returns merged mapping, if parentMap is nil then nil is returned.
func MergeChildMapToParentMap[T comparable, R any](parentMap map[T]R, toMergeMap map[T]R) map[T]R {
Expand All @@ -29,3 +34,12 @@ func MergeChildMapToParentMap[T comparable, R any](parentMap map[T]R, toMergeMap
}
return parentMap
}

func sanitizeLabels(extraAppLabels map[string]string) map[string]string {
for lkey, lvalue := range extraAppLabels {
if strings.Contains(lvalue, " ") {
extraAppLabels[lkey] = strings.ReplaceAll(lvalue, " ", "_")
}
}
return extraAppLabels
}
Loading