Skip to content

Commit

Permalink
new targets for creating json or yaml
Browse files Browse the repository at this point in the history
  • Loading branch information
chrislovecnm committed Jul 15, 2017
1 parent f456de4 commit 504d9a4
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 2 deletions.
46 changes: 44 additions & 2 deletions cmd/kops/create_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,12 @@ import (
"io/ioutil"
"strconv"
"strings"
"time"

"github.com/golang/glog"
"github.com/spf13/cobra"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/sets"
"k8s.io/kops"
"k8s.io/kops/cmd/kops/util"
Expand Down Expand Up @@ -116,6 +118,9 @@ type CreateClusterOptions struct {
VSphereDatastore string
}

var targetOptions = sets.NewString(cloudup.TargetDryRun, cloudup.TargetCloudformation, cloudup.TargetDirect, cloudup.TargetJSON, cloudup.TargetYAML, cloudup.TargetTerraform)
var allTargetOptions = strings.Join(targetOptions.List(), ",")

func (o *CreateClusterOptions) InitDefaults() {
o.Yes = false
o.Target = cloudup.TargetDirect
Expand Down Expand Up @@ -217,7 +222,7 @@ func NewCmdCreateCluster(f *util.Factory, out io.Writer) *cobra.Command {
}

cmd.Flags().BoolVar(&options.Yes, "yes", options.Yes, "Specify --yes to immediately create the cluster")
cmd.Flags().StringVar(&options.Target, "target", options.Target, "Target - direct, terraform, cloudformation")
cmd.Flags().StringVar(&options.Target, "target", options.Target, "Valid targets: "+allTargetOptions)
cmd.Flags().StringVar(&options.Models, "model", options.Models, "Models to apply (separate multiple models with commas)")

cmd.Flags().StringVar(&options.Cloud, "cloud", options.Cloud, "Cloud provider to use - gce, aws, vsphere")
Expand Down Expand Up @@ -294,8 +299,10 @@ func NewCmdCreateCluster(f *util.Factory, out io.Writer) *cobra.Command {

func RunCreateCluster(f *util.Factory, out io.Writer, c *CreateClusterOptions) error {
isDryrun := false
// direct requires --yes (others do not, because they don't make changes)

targetName := c.Target

// direct requires --yes (others do not, because they don't make changes)
if c.Target == cloudup.TargetDirect {
if !c.Yes {
isDryrun = true
Expand All @@ -306,6 +313,7 @@ func RunCreateCluster(f *util.Factory, out io.Writer, c *CreateClusterOptions) e
isDryrun = true
targetName = cloudup.TargetDryRun
}

clusterName := c.ClusterName
if clusterName == "" {
return fmt.Errorf("--name is required")
Expand Down Expand Up @@ -881,6 +889,39 @@ func RunCreateCluster(f *util.Factory, out io.Writer, c *CreateClusterOptions) e
return err
}

switch targetName {
case cloudup.TargetYAML:

var clusters []*api.Cluster
cluster.ObjectMeta.CreationTimestamp = v1.NewTime(time.Now())
clusters = append(clusters, cluster)

for _, group := range instanceGroups {
group.ObjectMeta.CreationTimestamp = v1.NewTime(time.Now())
}

if err := fullOutputYAML(clusters, fullInstanceGroups, out); err != nil {
return fmt.Errorf("error writing yaml to stdout: %v", err)
}

return igOutputYAML(instanceGroups, out)

case cloudup.TargetJSON:
var clusters []*api.Cluster
cluster.ObjectMeta.CreationTimestamp = v1.NewTime(time.Now())
clusters = append(clusters, cluster)

for _, group := range instanceGroups {
group.ObjectMeta.CreationTimestamp = v1.NewTime(time.Now())
}

if err := fullOutputJson(clusters, instanceGroups, out); err != nil {
return fmt.Errorf("error writing json to stdout: %v", err)
}

return nil
}

// Note we perform as much validation as we can, before writing a bad config
err = registry.CreateClusterConfig(clientset, cluster, fullInstanceGroups)
if err != nil {
Expand All @@ -904,6 +945,7 @@ func RunCreateCluster(f *util.Factory, out io.Writer, c *CreateClusterOptions) e
}
}

// Can we acutally get to this if??
if targetName != "" {
if isDryrun {
fmt.Fprintf(out, "Previewing changes that will be made:\n\n")
Expand Down
2 changes: 2 additions & 0 deletions upup/pkg/fi/cloudup/target.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,5 @@ const TargetDirect = "direct"
const TargetDryRun = "dryrun"
const TargetTerraform = "terraform"
const TargetCloudformation = "cloudformation"
const TargetYAML = "yaml"
const TargetJSON = "json"

0 comments on commit 504d9a4

Please sign in to comment.