diff --git a/cmd/command/setup_opts.go b/cmd/command/setup_opts.go index bc0205060188816d29633284593717d7f9fe08f0..8ce4fdef543814676da9a031f08076074200747e 100644 --- a/cmd/command/setup_opts.go +++ b/cmd/command/setup_opts.go @@ -25,7 +25,7 @@ import ( func SetupDeployCmdOpts(deployCmd *cobra.Command) { flags := deployCmd.Flags() flags.StringVarP(&opts.Opts.ClusterConfigFile, "file", "f", "", "Location of the cluster deploy config file") - flags.StringVarP(&opts.Opts.ClusterID, "clusterID", "", "", "Unique identifier for the cluster") + flags.StringVarP(&opts.Opts.ClusterID, "cluster-id", "", "", "Unique identifier for the cluster") flags.StringVar(&opts.Opts.Arch, "arch", "", "Architecture for Kubernetes cluster deployment (e.g., amd64 or arm64)") flags.StringVarP(&opts.Opts.Platform, "platform", "", "", "Infrastructure platform for deploying the cluster (supports 'libvirt' 'openstack' 'pxe' 'ipxe')") diff --git a/cmd/deploy.go b/cmd/deploy.go index 618941c1a8c3b2e4a39528dc167c74d8763b359d..2d8d65f5f5e1cd86ed2b60aaa55084a320030515 100755 --- a/cmd/deploy.go +++ b/cmd/deploy.go @@ -42,6 +42,7 @@ import ( "github.com/sirupsen/logrus" "github.com/spf13/cobra" + "gopkg.in/yaml.v2" corev1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" wait "k8s.io/apimachinery/pkg/util/wait" @@ -60,7 +61,6 @@ func NewDeployCommand() *cobra.Command { } const ( - clusterID = "cluster" clusterConfigFile = "cluster_config.yaml" namespace = "housekeeper-system" ) @@ -69,12 +69,30 @@ func runDeployCmd(cmd *cobra.Command, args []string) error { cleanup := command.SetuploggerHook(opts.Opts.RootOptDir) defer cleanup() - if err := validateDeployConfig(); err != nil { + var clusterID string + if opts.Opts.ClusterID != "" { + clusterID = opts.Opts.ClusterID + } else if opts.Opts.ClusterConfigFile != "" { + configData, err := os.ReadFile(opts.Opts.ClusterConfigFile) + if err != nil { + return err + } + + fileData := &asset.ClusterAsset{} + if err := yaml.Unmarshal(configData, fileData); err != nil { + return err + } + clusterID = fileData.ClusterID + } else { + clusterID = "cluster" + } + + if err := validateDeployConfig(clusterID); err != nil { return err } // Initialize configuration parameters - config, err := getClusterConfig(&opts.Opts) + config, err := getClusterConfig(&opts.Opts, clusterID) if err != nil { return err } @@ -89,13 +107,12 @@ func runDeployCmd(cmd *cobra.Command, args []string) error { return nil } -func validateDeployConfig() error { - opts.Opts.ClusterID = clusterID - clusterConfigFile := filepath.Join(opts.Opts.RootOptDir, opts.Opts.ClusterID, clusterConfigFile) +func validateDeployConfig(clusterID string) error { + clusterConfigFile := filepath.Join(opts.Opts.RootOptDir, clusterID, clusterConfigFile) // Check if clusterConfigFile already exists if _, err := os.Stat(clusterConfigFile); err == nil { - logrus.Debugf("cluster ID: %s already exists", opts.Opts.ClusterID) - return fmt.Errorf("cluster ID: %s already exists", opts.Opts.ClusterID) + logrus.Debugf("cluster ID: %s already exists", clusterID) + return fmt.Errorf("cluster ID: %s already exists", clusterID) } // Check if kubectl is installed @@ -106,7 +123,7 @@ func validateDeployConfig() error { return nil } -func getClusterConfig(options *opts.OptionsList) (*asset.ClusterAsset, error) { +func getClusterConfig(options *opts.OptionsList, clusterID string) (*asset.ClusterAsset, error) { if err := configmanager.Initial(options); err != nil { logrus.Errorf("Failed to initialize configuration parameters: %v", err) return nil, err diff --git a/cmd/extend.go b/cmd/extend.go index 8410ae71f7e08ae63d2a88fe7231fb7b8640c340..538728c76392223ad3b061dcac0de40fb1489713 100755 --- a/cmd/extend.go +++ b/cmd/extend.go @@ -57,14 +57,12 @@ func runExtendCmd(cmd *cobra.Command, args []string) error { cleanup := command.SetuploggerHook(opts.Opts.RootOptDir) defer cleanup() - clusterID, err := cmd.Flags().GetString("cluster-id") - if err != nil { - logrus.Errorf("Failed to get cluster-id: %v", err) - return err - } + clusterID := opts.Opts.ClusterID if clusterID == "" { - logrus.Errorf("cluster-id is not provided: %v", err) + logrus.Errorf("cluster-id is not provided") + return nil } + opts.Opts.ClusterID = "" if err := configmanager.Initial(&opts.Opts); err != nil { logrus.Errorf("Failed to initialize configuration parameters: %v", err) @@ -221,7 +219,7 @@ func extendArray(c *asset.ClusterAsset, count int) error { num := len(c.Worker) for i := 0; i < count; i++ { - hostname := fmt.Sprintf("k8s-worker%02d", num+i+1) + hostname := fmt.Sprintf("%s-worker%02d", c.ClusterID, num+i+1) c.Worker = append(c.Worker, asset.NodeAsset{ Hostname: hostname, IP: "", diff --git a/pkg/configmanager/asset/clusterasset.go b/pkg/configmanager/asset/clusterasset.go index 8cc65e419758a9a20a7375de3f248ea741dc7d1e..bcdd4fe78b8de4d3ff280fdf5a5258b0e976a2b5 100644 --- a/pkg/configmanager/asset/clusterasset.go +++ b/pkg/configmanager/asset/clusterasset.go @@ -393,7 +393,7 @@ func GetDefaultClusterConfig(arch string, platform string) (*ClusterAsset, error case "libvirt", "openstack": clusterAsset.Master = []NodeAsset{ { - Hostname: "k8s-master01", + Hostname: "cluster-master01", IP: "", HardwareInfo: HardwareInfo{ CPU: 4, @@ -404,7 +404,7 @@ func GetDefaultClusterConfig(arch string, platform string) (*ClusterAsset, error } clusterAsset.Worker = []NodeAsset{ { - Hostname: "k8s-worker01", + Hostname: "cluster-worker01", IP: "", HardwareInfo: HardwareInfo{ CPU: 4,