Skip to content

Commit

Permalink
Add ability to supply pod/container spec (#61)
Browse files Browse the repository at this point in the history
  • Loading branch information
stepro authored Jul 18, 2021
1 parent 202a488 commit 8450db4
Show file tree
Hide file tree
Showing 10 changed files with 1,668 additions and 35 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,8 @@ Flag | Default | Description
`-A, --inherit-annotations` | `false` | inherit pod annotations
`--label` | `[]` | inherit, set or remove pod labels in the form `name[=[value]]`
`--annotate` | `[]` | inherit, set or remove pod annotations in the form `name[=[value]]`
`--pod-spec | `{...}` | customize overall pod specification
`--spec | `{...}` | customize overall container specification
`-e, --env` | `[]` | set container environment variables in the form `name=value`
`--no-lifecycle` | `false` | do not inherit container lifecycle
`--no-probes` | `false` | do not inherit container probes
Expand All @@ -154,7 +156,9 @@ By default, when inheriting an existing configuration, pod labels and annotation

Whether or not labels or annotations are inherited, the final set of label or annotation entries can be customized using the `--label` and `--annotate` flags. If a value is simply in the form `name`, then its entry is inherited. If a value is in the form `name=value`, it adds or overrides any existing entry. Lastly, if a value is in the form `name=`, it removes an entry that may otherwise be inherited.

The `-e, --env` flags set container environment variables, and in the case of an inherited configuration, override any inherited environment variables.
The `--pod-spec` and `--spec` flags can be used to customize overall configuration of the pod specification or container specification respectively, using a JSON merge patch, and is applied after any inherited configuration but before more specific configuration through the `-e, --env`, `--no-lifecycle` or `--no-probes` flags.

The `-e, --env` flags set container environment variables, and in the case of an inherited and/or customized configuration, override container environment variables.

When inheriting an existing configuration, there are cases when the existing container lifecycle and probe configuration are not implemented, would cause problems, or are entirely irrelevant for the scenario. The `--no-lifecyle` and `--no-probes` flags can be used to ensure these properties are not inherited.

Expand Down
33 changes: 28 additions & 5 deletions cli/kdo/kdo.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"crypto/sha1"
"encoding/json"
"errors"
"fmt"
"os"
Expand All @@ -27,7 +28,7 @@ import (
var cmd = &cobra.Command{
Short: "Kdo: deployless development on Kubernetes",
Use: usage,
Version: "0.6.1",
Version: "0.7.0",
Example: examples,
RunE: run,
}
Expand Down Expand Up @@ -95,6 +96,8 @@ var flags struct {
inheritAnnotations bool
labels []string
annotations []string
podSpec string
spec string
env []string
noLifecycle bool
noProbes bool
Expand Down Expand Up @@ -124,7 +127,7 @@ var flags struct {
var out *output.Interface

func fatal(err error) {
fmt.Fprintf(os.Stderr, fmt.Sprintf("Fatal error: %v", err))
fmt.Fprintf(os.Stderr, "Fatal error: %v", err)
os.Exit(1)
}

Expand Down Expand Up @@ -176,6 +179,10 @@ func init() {
"label", nil, "inherit, set or remove pod labels")
cmd.Flags().StringArrayVar(&flags.config.annotations,
"annotate", nil, "inherit, set or remove pod annotations")
cmd.Flags().StringVar(&flags.config.podSpec,
"pod-spec", "", "customize overall pod configuration")
cmd.Flags().StringVar(&flags.config.spec,
"spec", "", "customize overall container configuration")
cmd.Flags().StringArrayVarP(&flags.config.env,
"env", "e", nil, "set container environment variables")
cmd.Flags().BoolVar(&flags.config.noLifecycle,
Expand Down Expand Up @@ -462,6 +469,20 @@ func run(cmd *cobra.Command, args []string) error {
return pod.Exec(k, hash, container, flags.command.prekill, flags.session.forward, flags.command.stdin, flags.command.tty, command...)
}

var spec map[string]interface{}
if flags.config.podSpec != "" {
if err = json.Unmarshal([]byte(flags.config.podSpec), &spec); err != nil {
return fmt.Errorf(`cannot parse pod spec: %s`, err)
}
}

var containerSpec map[string]interface{}
if flags.config.spec != "" {
if err = json.Unmarshal([]byte(flags.config.spec), &containerSpec); err != nil {
return fmt.Errorf(`cannot parse spec: %s`, err)
}
}

var build func(pod string) error
if buildDir != "" {
build = func(pod string) error {
Expand Down Expand Up @@ -493,6 +514,8 @@ func run(cmd *cobra.Command, args []string) error {
InheritAnnotations: flags.config.inheritAnnotations,
Labels: parseKeyValues(flags.config.labels),
Annotations: parseKeyValues(flags.config.annotations),
Spec: spec,
ContainerSpec: containerSpec,
Container: container,
Image: image,
Env: parseKeyValues(flags.config.env),
Expand Down Expand Up @@ -531,9 +554,9 @@ func run(cmd *cobra.Command, args []string) error {
defer stop()
}

if len(flags.session.listen) > 0 {
// TODO
}
// TODO
// if len(flags.session.listen) > 0 {
// }

var cmdArgs []string
if flags.command.stdin && !p.Exited() {
Expand Down
7 changes: 4 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ module github.com/stepro/kdo
go 1.12

require (
github.com/docker/docker v17.12.0-ce-rc1.0.20190717161051-705d9623b7c1+incompatible
github.com/docker/docker v20.10.7+incompatible
github.com/ghodss/yaml v1.0.0
github.com/spf13/cobra v1.0.0
golang.org/x/sys v0.0.0-20200523222454-059865788121
github.com/moby/buildkit v0.9.0
github.com/spf13/cobra v1.2.1
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c
)

replace github.com/Sirupsen/logrus => github.com/sirupsen/logrus v1.6.0
Loading

0 comments on commit 8450db4

Please sign in to comment.