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 dd96bed
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 2 deletions.
42 changes: 41 additions & 1 deletion 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 @@ -306,6 +311,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 +887,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 +943,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: 1 addition & 1 deletion docs/cli/kops_create_cluster.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ kops create cluster
--out string Path to write any local output
--project string Project to use (must be set on GCE)
--ssh-public-key string SSH public key to use (default "~/.ssh/id_rsa.pub")
--target string Target - direct, terraform, cloudformation (default "direct")
--target string Valid targets: cloudformation,direct,dryrun,json,terraform,yaml (default "direct")
-t, --topology string Controls network topology for the cluster. public|private. Default is 'public'. (default "public")
--vpc string Set to use a shared VPC
--yes Specify --yes to immediately create the cluster
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 dd96bed

Please sign in to comment.