Fix: escape resource name for label selector (#4348)

Signed-off-by: Somefive <yd219913@alibaba-inc.com>
This commit is contained in:
Somefive
2022-07-11 15:26:14 +08:00
committed by GitHub
parent b9b6c5f8bf
commit 2cfedf7439
4 changed files with 15 additions and 6 deletions
@@ -54,6 +54,7 @@ import (
"github.com/oam-dev/kubevela/pkg/oam"
"github.com/oam-dev/kubevela/pkg/oam/util"
"github.com/oam-dev/kubevela/pkg/policy/envbinding"
pkgutils "github.com/oam-dev/kubevela/pkg/utils"
)
type contextKey string
@@ -625,7 +626,7 @@ func (h *AppHandler) handleComponentRevisionNameUnspecified(ctx context.Context,
crList := &appsv1.ControllerRevisionList{}
listOpts := []client.ListOption{client.MatchingLabels{
oam.LabelControllerRevisionComponent: comp.Name,
oam.LabelControllerRevisionComponent: pkgutils.EscapeResourceNameToLabelValue(comp.Name),
}, client.InNamespace(h.getComponentRevisionNamespace(ctx))}
if err := h.r.List(auth.ContextWithUserInfo(ctx, h.app), crList, listOpts...); err != nil {
return err
@@ -732,10 +733,10 @@ func (h *AppHandler) createControllerRevision(ctx context.Context, cm *types.Com
Name: cm.RevisionName,
Namespace: h.getComponentRevisionNamespace(ctx),
Labels: map[string]string{
oam.LabelAppComponent: cm.Name,
oam.LabelAppComponent: pkgutils.EscapeResourceNameToLabelValue(cm.Name),
oam.LabelAppCluster: multicluster.ClusterNameInContext(ctx),
oam.LabelAppEnv: envbinding.EnvNameInContext(ctx),
oam.LabelControllerRevisionComponent: cm.Name,
oam.LabelControllerRevisionComponent: pkgutils.EscapeResourceNameToLabelValue(cm.Name),
oam.LabelComponentRevisionHash: cm.RevisionHash,
},
},
@@ -962,7 +963,7 @@ func cleanUpWorkflowComponentRevision(ctx context.Context, h *AppHandler) error
for _, curComp := range h.app.Status.AppliedResources {
crList := &appsv1.ControllerRevisionList{}
listOpts := []client.ListOption{client.MatchingLabels{
oam.LabelControllerRevisionComponent: curComp.Name,
oam.LabelControllerRevisionComponent: pkgutils.EscapeResourceNameToLabelValue(curComp.Name),
}, client.InNamespace(h.getComponentRevisionNamespace(ctx))}
_ctx := multicluster.ContextWithClusterName(ctx, curComp.Cluster)
if err := h.r.List(_ctx, crList, listOpts...); err != nil {
@@ -38,6 +38,7 @@ import (
"github.com/oam-dev/kubevela/apis/core.oam.dev/v1alpha2"
"github.com/oam-dev/kubevela/pkg/controller/utils"
"github.com/oam-dev/kubevela/pkg/oam"
pkgutil "github.com/oam-dev/kubevela/pkg/utils"
)
// ControllerRevisionComponentLabel indicate which component the revision belong to
@@ -189,7 +190,7 @@ func (c *ComponentHandler) createControllerRevision(mt metav1.Object, obj client
},
},
Labels: map[string]string{
ControllerRevisionComponentLabel: comp.Name,
ControllerRevisionComponentLabel: pkgutil.EscapeResourceNameToLabelValue(comp.Name),
},
},
Revision: nextRevision,
@@ -250,7 +251,7 @@ func sortedControllerRevision(appConfigs []v1alpha2.ApplicationConfiguration, re
func (c *ComponentHandler) cleanupControllerRevision(curComp *v1alpha2.Component) error {
labels := &metav1.LabelSelector{
MatchLabels: map[string]string{
ControllerRevisionComponentLabel: curComp.Name,
ControllerRevisionComponentLabel: pkgutil.EscapeResourceNameToLabelValue(curComp.Name),
},
}
selector, err := metav1.LabelSelectorAsSelector(labels)
+1
View File
@@ -20,6 +20,7 @@ import "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
// ContainsResources check if resources all exist
func (h *resourceKeeper) ContainsResources(resources []*unstructured.Unstructured) bool {
h.ClearNamespaceForClusterScopedResources(resources)
for _, rsc := range resources {
if rsc == nil {
continue
+6
View File
@@ -21,6 +21,7 @@ import (
"encoding/json"
"fmt"
"io/ioutil"
"strings"
authv1 "k8s.io/api/authentication/v1"
corev1 "k8s.io/api/core/v1"
@@ -183,3 +184,8 @@ func CreateOrUpdate(ctx context.Context, cli client.Client, obj client.Object) (
return nil
})
}
// EscapeResourceNameToLabelValue parse characters in resource name to label valid name
func EscapeResourceNameToLabelValue(resourceName string) string {
return strings.ReplaceAll(resourceName, ":", "_")
}