Skip to content

Commit a9e74e1

Browse files
committed
Record user name and id as AppWrapper labels
1 parent b87fa22 commit a9e74e1

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

internal/webhook/appwrapper_webhook.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import (
3030
discovery "k8s.io/client-go/discovery"
3131
"k8s.io/client-go/kubernetes"
3232
authClientv1 "k8s.io/client-go/kubernetes/typed/authorization/v1"
33+
utilmaps "sigs.k8s.io/kueue/pkg/util/maps"
3334

3435
ctrl "sigs.k8s.io/controller-runtime"
3536
"sigs.k8s.io/controller-runtime/pkg/log"
@@ -44,6 +45,11 @@ import (
4445
"github.com/project-codeflare/appwrapper/pkg/utils"
4546
)
4647

48+
const (
49+
AppWrapperUsernameLabel = "workload.codeflare.dev/user"
50+
AppWrapperUserIDLabel = "workload.codeflare.dev/userid"
51+
)
52+
4753
type AppWrapperWebhook struct {
4854
Config *config.AppWrapperConfig
4955
SubjectAccessReviewer authClientv1.SubjectAccessReviewInterface
@@ -66,6 +72,14 @@ func (w *AppWrapperWebhook) Default(ctx context.Context, obj runtime.Object) err
6672
log.FromContext(ctx).Info("Error raised during podSet inference", "job", aw)
6773
return err
6874
}
75+
76+
// inject labels with user name and id
77+
request, err := admission.RequestFromContext(ctx)
78+
if err != nil {
79+
return err
80+
}
81+
userInfo := request.UserInfo
82+
aw.SetLabels(utilmaps.MergeKeepFirst(map[string]string{AppWrapperUsernameLabel: userInfo.Username, AppWrapperUserIDLabel: userInfo.UID}, aw.GetLabels()))
6983
return nil
7084
}
7185

@@ -258,6 +272,14 @@ func (w *AppWrapperWebhook) validateAppWrapperUpdate(old *workloadv1beta2.AppWra
258272
}
259273
}
260274

275+
// ensure user name and id are not mutated
276+
if old.GetLabels()[AppWrapperUsernameLabel] != new.GetLabels()[AppWrapperUsernameLabel] {
277+
allErrors = append(allErrors, field.Forbidden(field.NewPath("metadata").Child("labels").Key(AppWrapperUsernameLabel), msg))
278+
}
279+
if old.GetLabels()[AppWrapperUserIDLabel] != new.GetLabels()[AppWrapperUserIDLabel] {
280+
allErrors = append(allErrors, field.Forbidden(field.NewPath("metadata").Child("labels").Key(AppWrapperUserIDLabel), msg))
281+
}
282+
261283
return allErrors
262284
}
263285

0 commit comments

Comments
 (0)