Skip to content
This repository was archived by the owner on Dec 31, 2025. It is now read-only.

Commit 64de9a7

Browse files
committed
Add flag to create cluster from a spec
1 parent cf5daff commit 64de9a7

File tree

1 file changed

+31
-2
lines changed

1 file changed

+31
-2
lines changed

cmd/cluster.go

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,24 @@ var clusterCmdCreate = &cobra.Command{
4242
Short: "Creates clusterspec in the current directory",
4343
Run: func(cmd *cobra.Command, args []string) {
4444

45-
vip := cmd.Flag("vip").Value.String()
45+
if cmd.Flag("f").Changed {
46+
clusterObjFile := cmd.Flag("f").Value.String()
47+
clusterObj, err := parseClusterObjFromFile(clusterObjFile)
48+
if err != nil {
49+
log.Fatalf("Unable to parse cluster object %v", err)
50+
}
51+
52+
if _, err := state.ClusterClient.ClusterV1alpha1().Clusters(common.DefaultNamespace).Create(clusterObj); err != nil {
53+
log.Fatalf("Unable to create cluster %q: %v", common.DefaultClusterName, err)
54+
}
55+
if err := state.PullFromAPIs(); err != nil {
56+
log.Fatalf("Unable to sync on-disk state: %v", err)
57+
}
58+
log.Println("Cluster created successfully.")
59+
return
60+
}
4661

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

198+
func parseClusterObjFromFile(file string) (*clusterv1.Cluster, error) {
199+
data, err := ioutil.ReadFile(file)
200+
if err != nil {
201+
return nil, fmt.Errorf("unable to read cluster object: %s", file)
202+
}
203+
clusterObj := clusterv1.Cluster{}
204+
if err = yaml.Unmarshal(data, &clusterObj); err != nil {
205+
return nil, fmt.Errorf("unable to decode cluster object: %v", err)
206+
}
207+
return &clusterObj, nil
208+
}
209+
182210
func createCluster(clusterName, podsCIDR, servicesCIDR, vip string, routerID int, clusterConfig *spv1.ClusterConfig) (*clusterv1.Cluster, error) {
183211
apiServerPortStr, ok := clusterConfig.KubeAPIServer[spconstants.KubeAPIServerSecurePortKey]
184212
var apiServerPort int64
@@ -666,6 +694,7 @@ func init() {
666694
clusterCmdCreate.Flags().String("saPrivateKey", "", "Location of file containing private key used for signing service account tokens")
667695
clusterCmdCreate.Flags().String("saPublicKey", "", "Location of file containing public key used for signing service account tokens")
668696
clusterCmdCreate.Flags().String("cluster-config", "", "Location of file containing configurable parameters for the cluster")
697+
clusterCmdCreate.Flags().StringP("file", "f", "", "Location of file containing a cluster object")
669698
//clusterCmdCreate.Flags().String("version", "1.10.2", "Kubernetes version")
670699

671700
deleteCmd.AddCommand(clusterCmdDelete)

0 commit comments

Comments
 (0)