Skip to content

Commit

Permalink
feat(kubernetes): Apply
Browse files Browse the repository at this point in the history
Adds apply support to the kubernetes provider.
The provider should be feature complete now
  • Loading branch information
sh0rez committed Jul 30, 2019
1 parent 166614c commit 8fcb4c1
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 2 deletions.
13 changes: 13 additions & 0 deletions cmd/tk/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,19 @@ func applyCmd() *cobra.Command {
Short: "[Requires Provider] apply the configuration to the target",
}
cmd.Run = func(cmd *cobra.Command, args []string) {
raw, err := evalDict()
if err != nil {
log.Fatalln("evaluating jsonnet:", err)
}

desired, err := prov.Reconcile(raw)
if err != nil {
log.Fatalln("reconciling:", err)
}

if err := prov.Apply(desired); err != nil {
log.Fatalln("applying:", err)
}
}
return cmd
}
Expand Down
26 changes: 26 additions & 0 deletions pkg/provider/kubernetes/kubectl.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,32 @@ func (k Kubectl) Get(namespace, kind, name string) (map[string]interface{}, erro
return obj, nil
}

// Apply applies the given yaml to the cluster
func (k Kubectl) Apply(yaml string) error {
if err := k.setupContext(); err != nil {
return err
}
argv := []string{"apply",
"--context", k.context,
"-f", "-",
}
cmd := exec.Command("kubectl", argv...)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr

stdin, err := cmd.StdinPipe()
if err != nil {
return err
}

go func() {
fmt.Fprintln(stdin, yaml)
stdin.Close()
}()

return cmd.Run()
}

// Diff takes a desired state as yaml and returns the differences
// to the system in common diff format
func (k Kubectl) Diff(yaml string) (string, error) {
Expand Down
8 changes: 6 additions & 2 deletions pkg/provider/kubernetes/kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,12 @@ func (k *Kubernetes) Fmt(state interface{}) (string, error) {
}

// Apply receives a state object generated using `Reconcile()` and may apply it to the target system
func (k *Kubernetes) Apply(desired interface{}) error {
panic("not implemented")
func (k *Kubernetes) Apply(state interface{}) error {
yaml, err := k.Fmt(state)
if err != nil {
return err
}
return client.Apply(yaml)
}

// Diff takes the desired state and returns the differences from the cluster
Expand Down

0 comments on commit 8fcb4c1

Please sign in to comment.