Skip to content

Commit

Permalink
support --dry-run -o yaml
Browse files Browse the repository at this point in the history
  • Loading branch information
ngtuna committed Dec 19, 2017
1 parent 9ffd835 commit e5911f8
Showing 1 changed file with 59 additions and 2 deletions.
61 changes: 59 additions & 2 deletions cmd/up.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,14 @@ limitations under the License.
package cmd

import (
"encoding/json"
"fmt"
"io"
"strconv"
"strings"

"bytes"
"github.com/ghodss/yaml"
"github.com/gosuri/uitable"
"github.com/ksonnet/kubecfg/pkg/kubecfg"
"github.com/ksonnet/kubecfg/utils"
Expand Down Expand Up @@ -71,6 +74,11 @@ List of components that kubeapps up installs:
return fmt.Errorf("can't get --dry-run flag: %v", err)
}

out, err := cmd.Flags().GetString("out")
if err != nil {
return fmt.Errorf("can't get --out flag: %v", err)
}

c.GcTag = GcTag

c.ClientPool, c.Discovery, err = restClientPool()
Expand Down Expand Up @@ -100,7 +108,7 @@ List of components that kubeapps up installs:
// k8s on GKE
if ok, err := isGKE(c.Discovery); err != nil {
return err
} else if ok {
} else if ok && !c.DryRun {
gcloudPath, err := gke.SdkConfigPath()
if err != nil {
return fmt.Errorf("can't get sdk config path: %v", err)
Expand Down Expand Up @@ -148,6 +156,10 @@ List of components that kubeapps up installs:
objs = append(objs, prevsecret)
}

if c.DryRun {
return dump(out, objs)
}

err = c.Run(objs)
if err != nil {
return fmt.Errorf("can't install kubeapps components: %v", err)
Expand All @@ -170,7 +182,52 @@ List of components that kubeapps up installs:

func init() {
RootCmd.AddCommand(upCmd)
upCmd.Flags().Bool("dry-run", false, "Provides output to be submitted to the server.")
upCmd.Flags().Bool("dry-run", false, "Show manifest to be submitted to the k8s cluster without deploying.")
upCmd.Flags().StringP("out", "o", "yaml", "Provides manifest format: yaml | json")
}

func dump(out string, objs []*unstructured.Unstructured) error {
bObjs := [][]byte{}

switch out {
case "json":
for _, obj := range objs {
j, err := json.MarshalIndent(obj, "", " ")
if err != nil {
return fmt.Errorf("can't dump kubeapps manifest: %v", err)
}
bObjs = append(bObjs, j)
}

b := bytes.Join(bObjs, []byte(fmt.Sprintf(",\n")))
// insert '[' to the front
b = append(b, 0)
copy(b[1:], b[0:])
b[0] = byte('[')

// append ']'
b = append(b, ']')

fmt.Println(string(b[:]))

case "yaml":
for _, obj := range objs {
j, err := obj.MarshalJSON()
if err != nil {
return fmt.Errorf("can't dump kubeapps manifest: %v", err)
}
y, err := yaml.JSONToYAML(j)
if err != nil {
return err
}
bObjs = append(bObjs, y)
}

b := bytes.Join(bObjs, []byte(fmt.Sprintf("---\n")))
fmt.Println(string(b[:]))
}

return nil
}

func isGKE(disco discovery.DiscoveryInterface) (bool, error) {
Expand Down

0 comments on commit e5911f8

Please sign in to comment.