Skip to content

Commit

Permalink
Add support for configuring global pod-name prefix #82
Browse files Browse the repository at this point in the history
  • Loading branch information
joyrex2001 committed Mar 29, 2024
1 parent cb37b43 commit 9b4d37b
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ Container API calls are translated towards kubernetes pods. When a container is

Starting a container is a blocking call that will wait until it results in a running pod. By default it will wait for maximum 1 minute, but this is configurable with the `--timeout` argument. The logs API calls will always return the complete history of logs, and doesn't differentiate between stdout/stderr. All log output is send as stdout. Executions in the containers are supported.

By default, all containers will be orchestrated using kubernetes pods. If a container has been given a specific name, this will be visible in the name of the pod. If the label `com.joyrex2001.kubedock.name-prefix` has been set, this will be added as a prefix to the name.
By default, all containers will be orchestrated using kubernetes pods. If a container has been given a specific name, this will be visible in the name of the pod. If the label `com.joyrex2001.kubedock.name-prefix` has been set, this will be added as a prefix to the name. This can also be set with the environment variable `POD_NAME_PREFIX` or with the `--pod-name-prefix` argument.

The containers will be started with the `default` service account. This can be changed with the `--service-account`. If required, the uid of the user that runs inside the container can also be enforced with the `--runas-user` argument and the `com.joyrex2001.kubedock.runas-user` label.

Expand Down
3 changes: 3 additions & 0 deletions cmd/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ func init() {
serverCmd.PersistentFlags().String("service-account", "default", "Service account that should be used for deployed pods")
serverCmd.PersistentFlags().String("image-pull-secrets", "", "Comma separated list of image pull secrets that should be used")
serverCmd.PersistentFlags().String("pod-template", "", "Pod file that should be used as the base for creating pods")
serverCmd.PersistentFlags().String("pod-name-prefix", "kubedock", "The prefix of the name to be used in the created pods")
serverCmd.PersistentFlags().BoolP("inspector", "i", false, "Enable image inspect to fetch container port config from a registry")
serverCmd.PersistentFlags().DurationP("timeout", "t", 1*time.Minute, "Container creating/deletion timeout")
serverCmd.PersistentFlags().DurationP("reapmax", "r", 60*time.Minute, "Reap all resources older than this time")
Expand Down Expand Up @@ -75,6 +76,7 @@ func init() {
viper.BindPFlag("kubernetes.service-account", serverCmd.PersistentFlags().Lookup("service-account"))
viper.BindPFlag("kubernetes.image-pull-secrets", serverCmd.PersistentFlags().Lookup("image-pull-secrets"))
viper.BindPFlag("kubernetes.pod-template", serverCmd.PersistentFlags().Lookup("pod-template"))
viper.BindPFlag("kubernetes.pod-name-prefix", serverCmd.PersistentFlags().Lookup("pod-name-prefix"))
viper.BindPFlag("kubernetes.timeout", serverCmd.PersistentFlags().Lookup("timeout"))
viper.BindPFlag("kubernetes.request-cpu", serverCmd.PersistentFlags().Lookup("request-cpu"))
viper.BindPFlag("kubernetes.request-memory", serverCmd.PersistentFlags().Lookup("request-memory"))
Expand Down Expand Up @@ -102,6 +104,7 @@ func init() {
viper.BindEnv("kubernetes.service-account", "SERVICE_ACCOUNT")
viper.BindEnv("kubernetes.image-pull-secrets", "IMAGE_PULL_SECRETS")
viper.BindEnv("kubernetes.pod-template", "POD_TEMPLATE")
viper.BindEnv("kubernetes.pod-name-prefix", "POD_NAME_PREFIX")
viper.BindEnv("kubernetes.timeout", "TIME_OUT")
viper.BindEnv("kubernetes.request-cpu", "K8S_REQUEST_CPU")
viper.BindEnv("kubernetes.request-memory", "K8S_REQUEST_MEMORY")
Expand Down
1 change: 1 addition & 0 deletions config.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ The `server` command is the actual kubedock server, and is the command to start
|server|--service-account|default|SERVICE_ACCOUNT|Service account that should be used for deployed pods|
|server|--image-pull-secrets||IMAGE_PULL_SECRETS|Comma separated list of image pull secrets that should be used|
|server|--pod-template||POD_TEMPLATE|Pod file that should be used as the base for creating pods|
|server|--pod-name-prefix||POD_NAME_PREFIX|The prefix of the name to be used in the created pods|
|server|--inspector / -i|false||Enable image inspect to fetch container port config from a registry|
|server|--timeout / -t|1m|TIME_OUT|Container creating/deletion timeout|
|server|--reapmax / -r|60m|REAPER_REAPMAX|Reap all resources older than this time|
Expand Down
4 changes: 4 additions & 0 deletions internal/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,9 @@ func (s *Server) getGinEngine() *gin.Engine {
sa := viper.GetString("kubernetes.service-account")
klog.Infof("service account used in deployments: %s", sa)

podprfx := viper.GetString("kubernetes.pod-name-prefix")
klog.Infof("pod name prefix: %s", podprfx)

ads := viper.GetInt64("kubernetes.active-deadline-seconds")

klog.Infof("using namespace: %s", viper.GetString("kubernetes.namespace"))
Expand All @@ -144,6 +147,7 @@ func (s *Server) getGinEngine() *gin.Engine {
PortForward: pfwrd,
ReverseProxy: revprox,
PreArchive: prea,
NamePrefix: podprfx,
ActiveDeadlineSeconds: ads,
})
if err != nil {
Expand Down
2 changes: 2 additions & 0 deletions internal/server/routes/common/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ type Config struct {
ServiceAccount string
// ActiveDeadlineSeconds contains the active deadline seconds to be used for running containers
ActiveDeadlineSeconds int64
// NamePrefix contains a prefix for the names used for the container deployments (optional).
NamePrefix string
}

// ContextRouter is the object that contains shared context for the kubedock API endpoints.
Expand Down
3 changes: 3 additions & 0 deletions internal/server/routes/docker/containers.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ func ContainerCreate(cr *common.ContextRouter, c *gin.Context) {
// The User defined in HTTP request takes precedence over the cli and label.
in.Labels[types.LabelRunasUser] = in.User
}
if _, ok := in.Labels[types.LabelNamePrefix]; !ok && cr.Config.NamePrefix != "" {
in.Labels[types.LabelNamePrefix] = cr.Config.NamePrefix
}
if _, ok := in.Labels[types.LabelRequestCPU]; !ok && cr.Config.RequestCPU != "" {
in.Labels[types.LabelRequestCPU] = cr.Config.RequestCPU
}
Expand Down
6 changes: 6 additions & 0 deletions internal/server/routes/libpod/containers.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ func ContainerCreate(cr *common.ContextRouter, c *gin.Context) {
// The User defined in HTTP request takes precedence over the cli and label.
in.Labels[types.LabelRunasUser] = in.User
}
if _, ok := in.Labels[types.LabelNamePrefix]; !ok && cr.Config.NamePrefix != "" {
in.Labels[types.LabelNamePrefix] = cr.Config.NamePrefix
}
if _, ok := in.Labels[types.LabelRequestCPU]; !ok && cr.Config.RequestCPU != "" {
in.Labels[types.LabelRequestCPU] = cr.Config.RequestCPU
}
Expand All @@ -51,6 +54,9 @@ func ContainerCreate(cr *common.ContextRouter, c *gin.Context) {
if _, ok := in.Labels[types.LabelPullPolicy]; !ok && cr.Config.PullPolicy != "" {
in.Labels[types.LabelPullPolicy] = cr.Config.PullPolicy
}
if _, ok := in.Labels[types.LabelActiveDeadlineSeconds]; !ok && cr.Config.ActiveDeadlineSeconds >= 0 {
in.Labels[types.LabelActiveDeadlineSeconds] = fmt.Sprintf("%d", cr.Config.ActiveDeadlineSeconds)
}
in.Labels[types.LabelServiceAccount] = cr.Config.ServiceAccount

env := []string{}
Expand Down

0 comments on commit 9b4d37b

Please sign in to comment.