Skip to content

Commit

Permalink
Merge pull request #353 from zmberg/1.15.x
Browse files Browse the repository at this point in the history
feature: 过滤掉不合规范的labels #351
  • Loading branch information
DeveloperJim authored Feb 13, 2020
2 parents 2426708 + b929791 commit 9a94387
Show file tree
Hide file tree
Showing 8 changed files with 34 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
27 changes: 27 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 @@ -70,6 +71,9 @@ type managerStore struct {
k8sClient *kubernetes.Clientset
extensionClient *extensionClientset.Clientset

regkey *regexp.Regexp
regvalue *regexp.Regexp

wg sync.WaitGroup
ctx context.Context
cancel context.CancelFunc
Expand Down Expand Up @@ -268,6 +272,10 @@ func NewEtcdStore(kubeconfig string) (store.Store, error) {
extensionClient: extensionClient,
}

//init object labels regexp
m.regkey, _ = regexp.Compile("^([A-Za-z0-9][-A-Za-z0-9_.]*)?[A-Za-z0-9]$")
m.regvalue, _ = regexp.Compile("^(([A-Za-z0-9][-A-Za-z0-9_.]*)?[A-Za-z0-9])?$")

//init clientset crds
err = m.initKubeCrd()
if err != nil {
Expand Down Expand Up @@ -336,3 +344,22 @@ 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
}

labels := make(map[string]string)
for k, v := range oriLabels {
if !store.regkey.MatchString(k) {
continue
}
if !store.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 9a94387

Please sign in to comment.