Skip to content

Commit

Permalink
Add flag to create cluster from a spec
Browse files Browse the repository at this point in the history
  • Loading branch information
ojmhetar committed Jan 28, 2019
1 parent cf5daff commit 64de9a7
Showing 1 changed file with 31 additions and 2 deletions.
33 changes: 31 additions & 2 deletions cmd/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,24 @@ var clusterCmdCreate = &cobra.Command{
Short: "Creates clusterspec in the current directory",
Run: func(cmd *cobra.Command, args []string) {

vip := cmd.Flag("vip").Value.String()
if cmd.Flag("f").Changed {
clusterObjFile := cmd.Flag("f").Value.String()
clusterObj, err := parseClusterObjFromFile(clusterObjFile)
if err != nil {
log.Fatalf("Unable to parse cluster object %v", err)
}

if _, err := state.ClusterClient.ClusterV1alpha1().Clusters(common.DefaultNamespace).Create(clusterObj); err != nil {
log.Fatalf("Unable to create cluster %q: %v", common.DefaultClusterName, err)
}
if err := state.PullFromAPIs(); err != nil {
log.Fatalf("Unable to sync on-disk state: %v", err)
}
log.Println("Cluster created successfully.")
return
}

vip := cmd.Flag("vip").Value.String()
// Verify that both routerID and vip are not defaults if one is specified
if (routerID == common.RouterID) != (len(vip) == 0) {
log.Fatalf("Must specify both routerID and vip, or leave both empty for non-HA cluster.")
Expand Down Expand Up @@ -170,7 +186,7 @@ func setKubeletConfigDefaults(clusterConfig *spv1.ClusterConfig) {
func parseClusterConfigFromFile(file string) (*spv1.ClusterConfig, error) {
data, err := ioutil.ReadFile(file)
if err != nil {
return nil, fmt.Errorf("unable to read cluster config file %s", file)
return nil, fmt.Errorf("unable to read cluster config file: %s", file)
}
clusterConfig := spv1.ClusterConfig{}
if err = yaml.Unmarshal(data, &clusterConfig); err != nil {
Expand All @@ -179,6 +195,18 @@ func parseClusterConfigFromFile(file string) (*spv1.ClusterConfig, error) {
return &clusterConfig, nil
}

func parseClusterObjFromFile(file string) (*clusterv1.Cluster, error) {
data, err := ioutil.ReadFile(file)
if err != nil {
return nil, fmt.Errorf("unable to read cluster object: %s", file)
}
clusterObj := clusterv1.Cluster{}
if err = yaml.Unmarshal(data, &clusterObj); err != nil {
return nil, fmt.Errorf("unable to decode cluster object: %v", err)
}
return &clusterObj, nil
}

func createCluster(clusterName, podsCIDR, servicesCIDR, vip string, routerID int, clusterConfig *spv1.ClusterConfig) (*clusterv1.Cluster, error) {
apiServerPortStr, ok := clusterConfig.KubeAPIServer[spconstants.KubeAPIServerSecurePortKey]
var apiServerPort int64
Expand Down Expand Up @@ -666,6 +694,7 @@ func init() {
clusterCmdCreate.Flags().String("saPrivateKey", "", "Location of file containing private key used for signing service account tokens")
clusterCmdCreate.Flags().String("saPublicKey", "", "Location of file containing public key used for signing service account tokens")
clusterCmdCreate.Flags().String("cluster-config", "", "Location of file containing configurable parameters for the cluster")
clusterCmdCreate.Flags().StringP("file", "f", "", "Location of file containing a cluster object")
//clusterCmdCreate.Flags().String("version", "1.10.2", "Kubernetes version")

deleteCmd.AddCommand(clusterCmdDelete)
Expand Down

0 comments on commit 64de9a7

Please sign in to comment.