Skip to content

Commit

Permalink
feature: 过滤掉不合规范的labels TencentBlueKing#351
Browse files Browse the repository at this point in the history
  • Loading branch information
zmberg committed Feb 10, 2020
1 parent cf1eece commit 26e6d4c
Show file tree
Hide file tree
Showing 8 changed files with 28 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ func (store *managerStore) SaveApplication(application *types.Application) error
ObjectMeta: metav1.ObjectMeta{
Name: application.ID,
Namespace: application.RunAs,
Labels: application.ObjectMeta.Labels,
Labels: store.filterSpecialLabels(application.ObjectMeta.Labels),
Annotations: application.ObjectMeta.Annotations,
},
Spec: v2.ApplicationSpec{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func (store *managerStore) SaveConfigMap(configmap *commtypes.BcsConfigMap) erro
ObjectMeta: metav1.ObjectMeta{
Name: configmap.Name,
Namespace: configmap.NameSpace,
Labels: configmap.Labels,
Labels: store.filterSpecialLabels(configmap.Labels),
Annotations: configmap.Annotations,
},
Spec: v2.BcsConfigMapSpec{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ func (store *managerStore) SaveDeployment(deployment *types.Deployment) error {
ObjectMeta: metav1.ObjectMeta{
Name: deployment.ObjectMeta.Name,
Namespace: deployment.ObjectMeta.NameSpace,
Labels: deployment.ObjectMeta.Labels,
Labels: store.filterSpecialLabels(deployment.ObjectMeta.Labels),
Annotations: deployment.ObjectMeta.Annotations,
},
Spec: v2.DeploymentSpec{
Expand Down
2 changes: 1 addition & 1 deletion bcs-mesos/bcs-scheduler/src/manager/store/etcd/endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func (store *managerStore) SaveEndpoint(endpoint *commtypes.BcsEndpoint) error {
ObjectMeta: metav1.ObjectMeta{
Name: endpoint.Name,
Namespace: endpoint.NameSpace,
Labels: endpoint.Labels,
Labels: store.filterSpecialLabels(endpoint.Labels),
Annotations: endpoint.Annotations,
},
Spec: v2.BcsEndpointSpec{
Expand Down
2 changes: 1 addition & 1 deletion bcs-mesos/bcs-scheduler/src/manager/store/etcd/secret.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func (store *managerStore) SaveSecret(secret *commtypes.BcsSecret) error {
ObjectMeta: metav1.ObjectMeta{
Name: secret.Name,
Namespace: secret.NameSpace,
Labels: secret.Labels,
Labels: store.filterSpecialLabels(secret.Labels),
Annotations: secret.Annotations,
},
Spec: v2.BcsSecretSpec{
Expand Down
2 changes: 1 addition & 1 deletion bcs-mesos/bcs-scheduler/src/manager/store/etcd/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func (store *managerStore) SaveService(service *commtypes.BcsService) error {
ObjectMeta: metav1.ObjectMeta{
Name: service.Name,
Namespace: service.NameSpace,
Labels: service.Labels,
Labels: store.filterSpecialLabels(service.Labels),
Annotations: service.Annotations,
},
Spec: v2.BcsServiceSpec{
Expand Down
21 changes: 21 additions & 0 deletions bcs-mesos/bcs-scheduler/src/manager/store/etcd/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package etcd
import (
"context"
"fmt"
"regexp"
"strings"
"sync"
"time"
Expand Down Expand Up @@ -336,3 +337,23 @@ func (store *managerStore) ListDeploymentRunAs() ([]string, error) {

return store.ListRunAs()
}

func (store *managerStore) filterSpecialLabels(oriLabels map[string]string) map[string]string {
if oriLabels == nil {
return nil
}
regkey, _ := regexp.Compile("^([A-Za-z0-9][-A-Za-z0-9_.]*)?[A-Za-z0-9]$")
regvalue, _ := regexp.Compile("^(([A-Za-z0-9][-A-Za-z0-9_.]*)?[A-Za-z0-9])?$")
labels := make(map[string]string)
for k, v := range oriLabels {
if !regkey.MatchString(k) {
continue
}
if !regvalue.MatchString(v) {
continue
}

labels[k] = v
}
return labels
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func (store *managerStore) SaveTaskGroup(taskGroup *types.TaskGroup) error {
ObjectMeta: metav1.ObjectMeta{
Name: taskGroup.ID,
Namespace: taskGroup.RunAs,
Labels: taskGroup.ObjectMeta.Labels,
Labels: store.filterSpecialLabels(taskGroup.ObjectMeta.Labels),
Annotations: taskGroup.ObjectMeta.Annotations,
},
Spec: v2.TaskGroupSpec{
Expand Down

0 comments on commit 26e6d4c

Please sign in to comment.