Skip to content

Commit 4245145

Browse files
author
Romuald Atchadé
committed
Merge branch 'k8s-ignore-empty-kubernetes-user-options' into 'main'
Suppress unnecessary warnings when Kubernetes user values are empty See merge request https://gitlab.com/gitlab-org/gitlab-runner/-/merge_requests/5551 Merged-by: Romuald Atchadé <[email protected]> Approved-by: Joe Burnett <[email protected]> Reviewed-by: Joe Burnett <[email protected]>
2 parents 623413d + c4fab4b commit 4245145

File tree

2 files changed

+62
-0
lines changed

2 files changed

+62
-0
lines changed

common/network.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -467,6 +467,10 @@ func (iko *ImageKubernetesOptions) UnmarshalJSON(data []byte) error {
467467
}
468468

469469
func (iko *ImageKubernetesOptions) GetUIDGID() (int64, int64, error) {
470+
if iko.User == "" {
471+
return 0, 0, nil
472+
}
473+
470474
user, group, ok := strings.Cut(iko.User, ":")
471475

472476
uid, err := strconv.ParseInt(user, 10, 64)

common/network_test.go

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -556,6 +556,64 @@ func Test_Image_ExecutorOptions_UnmarshalJSON(t *testing.T) {
556556
}
557557
}
558558

559+
func Test_Image_ExecutorOptions_GetUIDGID(t *testing.T) {
560+
tests := map[string]struct {
561+
kubernetesOptions func() *ImageKubernetesOptions
562+
expectedError bool
563+
expectedUID int64
564+
expectedGID int64
565+
}{
566+
"empty user": {
567+
kubernetesOptions: func() *ImageKubernetesOptions {
568+
return &ImageKubernetesOptions{
569+
User: "",
570+
}
571+
},
572+
},
573+
"only user": {
574+
kubernetesOptions: func() *ImageKubernetesOptions {
575+
return &ImageKubernetesOptions{
576+
User: "1000",
577+
}
578+
},
579+
expectedUID: int64(1000),
580+
},
581+
"uid and gid": {
582+
kubernetesOptions: func() *ImageKubernetesOptions {
583+
return &ImageKubernetesOptions{
584+
User: "1000:1000",
585+
}
586+
},
587+
expectedUID: int64(1000),
588+
expectedGID: int64(1000),
589+
},
590+
"invalid user": {
591+
kubernetesOptions: func() *ImageKubernetesOptions {
592+
return &ImageKubernetesOptions{
593+
User: "gitlab-runner",
594+
}
595+
},
596+
expectedError: true,
597+
},
598+
}
599+
600+
for name, tt := range tests {
601+
t.Run(name, func(t *testing.T) {
602+
uid, gid, err := tt.kubernetesOptions().GetUIDGID()
603+
if tt.expectedError {
604+
require.Error(t, err)
605+
require.Equal(t, int64(0), uid)
606+
require.Equal(t, int64(0), gid)
607+
return
608+
}
609+
610+
require.NoError(t, err)
611+
require.Equal(t, tt.expectedUID, uid)
612+
require.Equal(t, tt.expectedGID, gid)
613+
})
614+
}
615+
}
616+
559617
func TestJobResponse_Run(t *testing.T) {
560618
tests := map[string]struct {
561619
json string

0 commit comments

Comments
 (0)