feature: support UID:GID in the user key

This commit is contained in:
favonia 2024-08-28 08:00:11 -05:00
parent 51d61400e0
commit 0ace11079e
No known key found for this signature in database
GPG Key ID: 06210CB7076F1EEC
3 changed files with 53 additions and 14 deletions

View File

@ -641,11 +641,30 @@ func (k *Kubernetes) UpdateKubernetesObjects(name string, service kobject.Servic
securityContext.Privileged = &service.Privileged securityContext.Privileged = &service.Privileged
} }
if service.User != "" { if service.User != "" {
uid, err := strconv.ParseInt(service.User, 10, 64) switch userparts := strings.Split(service.User, ":"); len(userparts) {
if err != nil { default:
log.Warn("Ignoring user directive. User to be specified as a UID (numeric).") log.Warn("Ignoring ill-formed user directive. Must be in format UID or UID:GID.")
} else { case 1:
securityContext.RunAsUser = &uid uid, err := strconv.ParseInt(userparts[0], 10, 64)
if err != nil {
log.Warn("Ignoring user directive. User to be specified as a UID (numeric).")
} else {
securityContext.RunAsUser = &uid
}
case 2:
uid, err := strconv.ParseInt(userparts[0], 10, 64)
if err != nil {
log.Warn("Ignoring user name in user directive. User to be specified as a UID (numeric).")
} else {
securityContext.RunAsUser = &uid
}
gid, err := strconv.ParseInt(userparts[1], 10, 64)
if err != nil {
log.Warn("Ignoring group name in user directive. Group to be specified as a GID (numeric).")
} else {
securityContext.RunAsGroup = &gid
}
} }
} }

View File

@ -22,7 +22,6 @@ import (
"path/filepath" "path/filepath"
"reflect" "reflect"
"sort" "sort"
"strconv"
"testing" "testing"
"github.com/kubernetes/kompose/pkg/kobject" "github.com/kubernetes/kompose/pkg/kobject"
@ -208,7 +207,7 @@ func TestCreateServiceWithServiceUser(t *testing.T) {
Expose: []string{"expose"}, // not supported Expose: []string{"expose"}, // not supported
Privileged: true, Privileged: true,
Restart: "always", Restart: "always",
User: "1234", User: "1234:5678",
} }
komposeObject := kobject.KomposeObject{ komposeObject := kobject.KomposeObject{
@ -224,8 +223,9 @@ func TestCreateServiceWithServiceUser(t *testing.T) {
for _, obj := range objects { for _, obj := range objects {
if deploy, ok := obj.(*appsv1.Deployment); ok { if deploy, ok := obj.(*appsv1.Deployment); ok {
uid := *deploy.Spec.Template.Spec.Containers[0].SecurityContext.RunAsUser uid := *deploy.Spec.Template.Spec.Containers[0].SecurityContext.RunAsUser
if strconv.FormatInt(uid, 10) != service.User { gid := *deploy.Spec.Template.Spec.Containers[0].SecurityContext.RunAsGroup
t.Errorf("User in ServiceConfig is not matching user in PodSpec") if fmt.Sprintf("%d:%d", uid, gid) != service.User {
t.Errorf("User and group in ServiceConfig is not matching user in PodSpec")
} }
} }
} }

View File

@ -3,6 +3,7 @@ package kubernetes
import ( import (
"reflect" "reflect"
"strconv" "strconv"
"strings"
mapset "github.com/deckarep/golang-set" mapset "github.com/deckarep/golang-set"
"github.com/kubernetes/kompose/pkg/kobject" "github.com/kubernetes/kompose/pkg/kobject"
@ -143,11 +144,30 @@ func SecurityContext(name string, service kobject.ServiceConfig) PodSpecOption {
securityContext.Privileged = &service.Privileged securityContext.Privileged = &service.Privileged
} }
if service.User != "" { if service.User != "" {
uid, err := strconv.ParseInt(service.User, 10, 64) switch userparts := strings.Split(service.User, ":"); len(userparts) {
if err != nil { default:
log.Warn("Ignoring user directive. User to be specified as a UID (numeric).") log.Warn("Ignoring ill-formed user directive. Must be in format UID or UID:GID.")
} else { case 1:
securityContext.RunAsUser = &uid uid, err := strconv.ParseInt(userparts[0], 10, 64)
if err != nil {
log.Warn("Ignoring user directive. User to be specified as a UID (numeric).")
} else {
securityContext.RunAsUser = &uid
}
case 2:
uid, err := strconv.ParseInt(userparts[0], 10, 64)
if err != nil {
log.Warn("Ignoring user name in user directive. User to be specified as a UID (numeric).")
} else {
securityContext.RunAsUser = &uid
}
gid, err := strconv.ParseInt(userparts[1], 10, 64)
if err != nil {
log.Warn("Ignoring group name in user directive. Group to be specified as a GID (numeric).")
} else {
securityContext.RunAsGroup = &gid
}
} }
} }