From e80979dba5dd79b1617131ab2c06e758183aeeb5 Mon Sep 17 00:00:00 2001 From: Alex Collins Date: Wed, 4 Jan 2023 20:10:15 -0800 Subject: [PATCH] feat: add `kit init` --- README.md | 11 ++++++++-- init.go | 42 ++++++++++++++++++++++++++++++++++++++ internal/proc/container.go | 2 +- internal/types/pod.go | 23 +++++++++++++-------- kit.yaml | 32 +++++++++++------------------ lint.go | 2 +- main.go | 7 ++++--- probe.go | 2 +- up.go | 2 +- version.go | 2 +- 10 files changed, 86 insertions(+), 39 deletions(-) create mode 100644 init.go diff --git a/README.md b/README.md index f98a178..483e313 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,8 @@ concurrently and their status is **muxed into a single terminal** window (so you container and host process **auto-rebuild** and restart. When you're done, **ctrl+c** to and they're all cleanly stopped. **Logs are captured** so you can look at them anytime. -You could think of it as `docker compose up` or `podman kube play` that supports host processes, or `foreman` that supports containers. +You could think of it as `docker compose up` or `podman kube play` that supports host processes, or `foreman` that +supports containers. | tool | container processes | host processes | auto re-build | ctrl+c to stop | terminal mux | log capture | probes | |---------------------|---------------------|----------------|---------------|----------------|--------------|-------------|--------| @@ -28,7 +29,13 @@ brew install kit ## Usage -Describe you application in a [`kit.yaml`](kit.yaml) file, then start: +Initialize your [`kit.yaml`](kit.yaml) file, + +```bash +kit init +``` + +Update kit.yaml, then start: ```bash kit up diff --git a/init.go b/init.go new file mode 100644 index 0000000..ce50e47 --- /dev/null +++ b/init.go @@ -0,0 +1,42 @@ +package main + +import ( + _ "embed" + "os" + "path/filepath" + + "github.com/alexec/kit/internal/types" + "github.com/spf13/cobra" + "sigs.k8s.io/yaml" +) + +//go:embed kit.yaml +var kitYaml string + +func initCmd() *cobra.Command { + return &cobra.Command{ + Use: "init", + Long: "Initialize config file", + RunE: func(cmd *cobra.Command, args []string) error { + configFile := defaultConfigFile + pod := &types.Pod{} + err := yaml.Unmarshal([]byte(kitYaml), pod) + if err != nil { + return err + } + + wd, _ := os.Getwd() + pod.Metadata.Name = filepath.Base(wd) + + data, err := yaml.Marshal(pod) + if err != nil { + return err + } + err = os.WriteFile(configFile, data, 0o0644) + if err != nil { + return err + } + return nil + }, + } +} diff --git a/internal/proc/container.go b/internal/proc/container.go index d8ffbbb..e939613 100644 --- a/internal/proc/container.go +++ b/internal/proc/container.go @@ -65,7 +65,7 @@ func (h *container) Build(ctx context.Context, stdout, stderr io.Writer) error { if _, err = io.Copy(stdout, resp.Body); err != nil { return err } - } else if h.ImagePullPolicy != "PullNever" { + } else if h.ImagePullPolicy != "Never" { r, err := h.cli.ImagePull(ctx, h.Image, dockertypes.ImagePullOptions{}) if err != nil { return err diff --git a/internal/types/pod.go b/internal/types/pod.go index 26a59a4..671bd82 100644 --- a/internal/types/pod.go +++ b/internal/types/pod.go @@ -87,14 +87,15 @@ func (c Container) GetHostPorts() []uint16 { } type Pod struct { - Spec Spec `json:"spec"` - ApiVersion string `json:"apiVersion,omitempty"` - Kind string `json:"kind,omitempty"` - Metadata *Metadata `json:"metadata,omitempty"` + Spec Spec `json:"spec"` + ApiVersion string `json:"apiVersion,omitempty"` + Kind string `json:"kind,omitempty"` + Metadata Metadata `json:"metadata"` } type Metadata struct { - Name string `json:"name"` + Name string `json:"name"` + Annotations map[string]string `json:"annotations,omitempty"` } type Probe struct { @@ -170,14 +171,18 @@ func (a HTTPGetAction) GetProto() string { } func (a HTTPGetAction) GetURL() string { - return fmt.Sprintf("%s://localhost:%v%s", a.GetProto(), a.GetPort(), a.Path) + return fmt.Sprintf("%s://localhost:%s%s", a.GetProto(), a.GetPort(), a.Path) } -func (a HTTPGetAction) GetPort() int32 { +func (a HTTPGetAction) GetPort() string { if a.Port == nil { - return 80 + if a.GetProto() == "http" { + return "80" + } else { + return "443" + } } - return a.Port.IntVal + return a.Port.String() } func (a *HTTPGetAction) DeepCopy() *HTTPGetAction { diff --git a/kit.yaml b/kit.yaml index 4e82621..f37379d 100644 --- a/kit.yaml +++ b/kit.yaml @@ -1,6 +1,8 @@ apiVersion: kit/v1 kind: Pod metadata: + annotations: + help: https://github.com/alexec/kit name: kit spec: containers: @@ -14,6 +16,9 @@ spec: name: foo ports: - hostPort: 8080 + readinessProbe: + tcpSocket: + port: 8080 - build: command: - go @@ -41,29 +46,16 @@ spec: readinessProbe: httpGet: port: 7070 - - image: demo/qux - name: qux - ports: - - containerPort: 80 - hostPort: 6060 - readinessProbe: - httpGet: - port: 6060 - volumeMounts: - - mountPath: /work - name: work - - command: - - sh - - -c - - | - set -eux - sleep 10 - name: kef initContainers: - command: - - sleep - - "1" + - ls + - /work + image: ubuntu name: init + volumeMounts: + - mountPath: /work + name: work + terminationGracePeriodSeconds: 3 volumes: - hostPath: path: . diff --git a/lint.go b/lint.go index 30ed139..c5932db 100644 --- a/lint.go +++ b/lint.go @@ -8,7 +8,7 @@ import ( "sigs.k8s.io/yaml" ) -func lint() *cobra.Command { +func lintCmd() *cobra.Command { return &cobra.Command{ Use: "lint", Short: "Lint file", diff --git a/main.go b/main.go index 91251eb..36c0a31 100644 --- a/main.go +++ b/main.go @@ -18,9 +18,10 @@ const defaultConfigFile = "kit.yaml" func main() { cmd := &cobra.Command{Use: "kit"} - cmd.AddCommand(lint()) - cmd.AddCommand(up()) - cmd.AddCommand(version()) + cmd.AddCommand(initCmd()) + cmd.AddCommand(lintCmd()) + cmd.AddCommand(upCmd()) + cmd.AddCommand(versionCmd()) err := cmd.Execute() if err != nil { diff --git a/probe.go b/probe.go index d3088e1..5e9187a 100644 --- a/probe.go +++ b/probe.go @@ -23,7 +23,7 @@ func probeLoop(ctx context.Context, stopEverything func(), name string, probe ty default: var err error if tcp := probe.TCPSocket; tcp != nil { - _, err = net.Dial("tcp", fmt.Sprintf("127.0.0.1:%d", tcp.Port.IntVal)) + _, err = net.Dial("tcp", fmt.Sprintf("127.0.0.1:%s", tcp.Port.String())) } else if httpGet := probe.HTTPGet; httpGet != nil { err = func() error { resp, err := http.Get(httpGet.GetURL()) diff --git a/up.go b/up.go index 10a5cf8..bae6c47 100644 --- a/up.go +++ b/up.go @@ -23,7 +23,7 @@ import ( "sigs.k8s.io/yaml" ) -func up() *cobra.Command { +func upCmd() *cobra.Command { return &cobra.Command{ Use: "up [config_file]", Short: "Start-up processes", diff --git a/version.go b/version.go index 45da4fd..90d4ee3 100644 --- a/version.go +++ b/version.go @@ -11,7 +11,7 @@ import ( //go:embed tag var tag string -func version() *cobra.Command { +func versionCmd() *cobra.Command { return &cobra.Command{ Use: "version", Short: "Print version",