Skip to content
This repository was archived by the owner on Jun 11, 2024. It is now read-only.

Commit

Permalink
Move secrets into a manifest instead of invoking kubectl
Browse files Browse the repository at this point in the history
  • Loading branch information
Blake Watters committed May 30, 2020
1 parent 998ba63 commit cbd7a95
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 33 deletions.
6 changes: 6 additions & 0 deletions command/profile_registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package command

import (
"fmt"
"path/filepath"

"github.com/spf13/viper"
)
Expand All @@ -28,6 +29,11 @@ type Profile struct {
BaseURL string `yaml:"base_url" mapstructure:"base_url" json:"base_url"`
}

// Organization returns the domain of the organization that owns the app
func (p Profile) Organization() string {
return filepath.Dir(p.App)
}

// ProfileRegistry provides an interface for managing configuration of app profiles
type ProfileRegistry struct {
viper *viper.Viper
Expand Down
43 changes: 12 additions & 31 deletions command/vital.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package command
import (
"bytes"
"context"
"encoding/base64"
"fmt"
"io"
"io/ioutil"
Expand Down Expand Up @@ -307,6 +308,7 @@ func (vitalCommand *vitalCommand) RunVitalDiscovery(cobraCmd *cobra.Command, arg
return nil
}

// TODO: This just duplicates exec.CombinedOutput
func (vitalCommand *vitalCommand) run(name string, args ...string) (*bytes.Buffer, error) {
outputBuffer := new(bytes.Buffer)
cmd := exec.Command(name, args...)
Expand All @@ -321,40 +323,14 @@ func init() {
}

func (vitalCommand *vitalCommand) InstallKubernetesManifests(cobraCmd *cobra.Command, args []string) error {
// TODO: This needs to be a manifest
err := vitalCommand.RunTaskWithSpinner(Task{
Description: "creating secrets...",
Success: "your secrets are safe and sound.",
Failure: "secret creation failed.\n",
Run: func() error {
output, err := vitalCommand.run("kubectl", "--kubeconfig", pathToDefaultKubeconfig(), "create", "secret", "generic", "opsani-servo-auth",
"--from-literal", fmt.Sprintf("token=%s", vitalCommand.AccessToken()))
if err != nil {
return fmt.Errorf("%s: %w", output, err)
}
return nil
},
})
if err != nil {
return err
}

if _, err := os.Stat("manifests"); os.IsNotExist(err) {
e := os.Mkdir("manifests", 0755)
if e != nil {
return e
}
}

org, app := vitalCommand.AppComponents()
vars := struct {
Account string
App string
}{
Account: org,
App: app,
}
err = pkger.Walk("/demo/manifests", func(path string, info os.FileInfo, err error) error {
err := pkger.Walk("/demo/manifests", func(path string, info os.FileInfo, err error) error {
if info.IsDir() || strings.HasPrefix(info.Name(), ".") {
return nil
}
Expand Down Expand Up @@ -391,12 +367,17 @@ func (vitalCommand *vitalCommand) InstallKubernetesManifests(cobraCmd *cobra.Com
return err
}

manifestName := filepath.Base(path)
manifestTemplate, err := ioutil.ReadAll(f)
if err != nil {
return err
}

tmpl, err := template.New("").Parse(string(manifestTemplate))
tmpl, err := template.New("").Funcs(template.FuncMap{
"base64encode": func(v string) string {
return base64.StdEncoding.EncodeToString([]byte(v))
},
}).Parse(string(manifestTemplate))
if err != nil {
return err
}
Expand All @@ -410,17 +391,17 @@ func (vitalCommand *vitalCommand) InstallKubernetesManifests(cobraCmd *cobra.Com
cmd.Stdout = outputBuffer
cmd.Stderr = outputBuffer
if err := cmd.Start(); err != nil {
return fmt.Errorf("%s: %w", outputBuffer, err)
return fmt.Errorf("failed applying manifest %q: %w\n%s", manifestName, err, outputBuffer)
}
renderedManifest := new(bytes.Buffer)
err = tmpl.Execute(renderedManifest, vars)
err = tmpl.Execute(renderedManifest, *vitalCommand.profile)
if err != nil {
panic(err)
}
fmt.Fprintln(kubeCtlPipe, renderedManifest)
kubeCtlPipe.Close()
if err := cmd.Wait(); err != nil {
return fmt.Errorf("%s: %w", outputBuffer, err)
return fmt.Errorf("failed applying manifest %q: %w\n%s", manifestName, err, outputBuffer)
}

// Write the manifest
Expand Down
6 changes: 5 additions & 1 deletion demo/manifests/servo/servo-configmap.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@ data:
cpu:
min: 0.1
max: 1.8
step: 0.1
step: 0.125
mem:
min: 128
max: 256
step: 0.125
replicas:
min: 1
max: 2
Expand Down
2 changes: 1 addition & 1 deletion demo/manifests/servo/servo-deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ spec:
- '--auth-token=/etc/opsani-auth/token'
env:
- name: OPTUNE_ACCOUNT
value: {{ .Account }}
value: {{ .Organization }}
- name: OPTUNE_NAMESPACE
value: default
- name: OPTUNE_USE_DEFAULT_NAMESPACE
Expand Down

0 comments on commit cbd7a95

Please sign in to comment.