Skip to content

Commit f415031

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 f415031

File tree

3 files changed

+15
-6
lines changed

3 files changed

+15
-6
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: 4 additions & 3 deletions
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

@@ -296,7 +297,7 @@ func (spm *Mutator) HandleCreate(req *admission.Request) admission.Response {
296297
if sp.Labels == nil {
297298
sp.Labels = map[string]string{}
298299
}
299-
sp.Labels[consts.CreatorLabelKey] = creatorName
300+
sp.Labels[consts.CreatorLabelKey] = resource.EscapeLabel(creatorName)
300301

301302
marshaledShadowPod, err := json.Marshal(sp)
302303
if err != nil {
@@ -333,14 +334,14 @@ func (spm *Mutator) HandleUpdate(req *admission.Request) admission.Response {
333334
return admission.Denied(err.Error())
334335
}
335336

336-
if oldCreatorName != creatorName {
337+
if oldCreatorName != resource.EscapeLabel(creatorName) {
337338
return admission.Denied("creator name cannot be modified")
338339
}
339340

340341
if sp.Labels == nil {
341342
sp.Labels = map[string]string{}
342343
}
343-
sp.Labels[consts.CreatorLabelKey] = creatorName
344+
sp.Labels[consts.CreatorLabelKey] = resource.EscapeLabel(creatorName)
344345

345346
marshaledShadowPod, err := json.Marshal(sp)
346347
if err != nil {

0 commit comments

Comments
 (0)