Skip to content

Commit d91633b

Browse files
committed
fix: escape the shadowpod creator label
When a token of a service account is used as identity for a virtual node, the creation of a shadowpod fails due to the `liqo.io/creator-user` which contains a string like the following, when the token of a SA is used: "system:serviceaccount:liqo-tenant-cl01:user01". This PR make sure that the string is escaped before being written in the label field.
1 parent 2b41212 commit d91633b

File tree

3 files changed

+13
-4
lines changed

3 files changed

+13
-4
lines changed

pkg/utils/getters/k8sGetters.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ import (
4141
offloadingv1beta1 "github.com/liqotech/liqo/apis/offloading/v1beta1"
4242
"github.com/liqotech/liqo/pkg/consts"
4343
liqolabels "github.com/liqotech/liqo/pkg/utils/labels"
44+
"github.com/liqotech/liqo/pkg/utils/resource"
4445
vkforge "github.com/liqotech/liqo/pkg/vkMachinery/forge"
4546
)
4647

@@ -463,7 +464,7 @@ func GetKubeconfigSecretFromIdentity(ctx context.Context, cl client.Client, iden
463464
// ListShadowPodsByCreator returns the list of ShadowPods created by the given user.
464465
func ListShadowPodsByCreator(ctx context.Context, cl client.Client, creator string) (*offloadingv1beta1.ShadowPodList, error) {
465466
list := new(offloadingv1beta1.ShadowPodList)
466-
if err := cl.List(ctx, list, client.MatchingLabels{consts.CreatorLabelKey: creator}); err != nil {
467+
if err := cl.List(ctx, list, client.MatchingLabels{consts.CreatorLabelKey: resource.EscapeLabel(creator)}); err != nil {
467468
return nil, err
468469
}
469470
return list, nil
@@ -479,7 +480,7 @@ func GetQuotaByUser(ctx context.Context, cl client.Client,
479480
}
480481

481482
for i := range quotas.Items {
482-
if quotas.Items[i].Spec.User == user {
483+
if resource.EscapeLabel(quotas.Items[i].Spec.User) == user {
483484
return &quotas.Items[i], nil
484485
}
485486
}

pkg/utils/resource/labels.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,15 @@ package resource
1616

1717
import (
1818
"maps"
19+
"regexp"
1920

2021
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2122
)
2223

2324
var (
2425
// globalLabels stores the global labels that should be added to all resources.
25-
globalLabels = make(map[string]string)
26+
globalLabels = make(map[string]string)
27+
regexLabelEscape = regexp.MustCompile(`[^\w\-.]`)
2628
)
2729

2830
// SetGlobalLabels sets the global labels that should be added to all resources.
@@ -47,3 +49,8 @@ func AddGlobalLabels(obj metav1.Object) {
4749
}
4850
maps.Copy(obj.GetLabels(), globalLabels)
4951
}
52+
53+
// EscapeLabel escapes a label value so that it is compliant.
54+
func EscapeLabel(val string) string {
55+
return regexLabelEscape.ReplaceAllString(val, "-")
56+
}

pkg/webhooks/shadowpod/webhook.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import (
3333
"github.com/liqotech/liqo/pkg/consts"
3434
"github.com/liqotech/liqo/pkg/utils/getters"
3535
pod "github.com/liqotech/liqo/pkg/utils/pod"
36+
"github.com/liqotech/liqo/pkg/utils/resource"
3637
"github.com/liqotech/liqo/pkg/virtualKubelet/forge"
3738
)
3839

@@ -357,7 +358,7 @@ func (spm *Mutator) HandleDelete() admission.Response {
357358
}
358359

359360
func extractCreatorInfo(userInfo *authenticationv1.UserInfo) (creatorName string, err error) {
360-
creatorName = userInfo.Username
361+
creatorName = resource.EscapeLabel(userInfo.Username)
361362
if creatorName == "" {
362363
return "", fmt.Errorf("missing creator name")
363364
}

0 commit comments

Comments
 (0)