Skip to content

Commit

Permalink
feat: add kit init
Browse files Browse the repository at this point in the history
  • Loading branch information
alexec committed Jan 6, 2023
1 parent 8960b40 commit e80979d
Show file tree
Hide file tree
Showing 10 changed files with 86 additions and 39 deletions.
11 changes: 9 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 |
|---------------------|---------------------|----------------|---------------|----------------|--------------|-------------|--------|
Expand All @@ -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
Expand Down
42 changes: 42 additions & 0 deletions init.go
Original file line number Diff line number Diff line change
@@ -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
},
}
}
2 changes: 1 addition & 1 deletion internal/proc/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
23 changes: 14 additions & 9 deletions internal/types/pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down
32 changes: 12 additions & 20 deletions kit.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
apiVersion: kit/v1
kind: Pod
metadata:
annotations:
help: https://github.com/alexec/kit
name: kit
spec:
containers:
Expand All @@ -14,6 +16,9 @@ spec:
name: foo
ports:
- hostPort: 8080
readinessProbe:
tcpSocket:
port: 8080
- build:
command:
- go
Expand Down Expand Up @@ -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: .
Expand Down
2 changes: 1 addition & 1 deletion lint.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
7 changes: 4 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion probe.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
2 changes: 1 addition & 1 deletion up.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion version.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit e80979d

Please sign in to comment.