diff --git a/pkg/configmanager/asset/cluster/clusterasset.go b/pkg/configmanager/asset/cluster/clusterasset.go index 4dcd9530d8a58533925c61ff85aa8c39b370c054..225c6854beb81d934528ad9686ff1c9de3dc6ad8 100644 --- a/pkg/configmanager/asset/cluster/clusterasset.go +++ b/pkg/configmanager/asset/cluster/clusterasset.go @@ -23,18 +23,14 @@ import ( "gopkg.in/yaml.v2" ) -var ClusterConfig *ClusterAsset - -// ========== Package method ========== - -func GetClusterConfig() (*ClusterAsset, error) { - return ClusterConfig, nil -} +// Set global data +var ClusterConfig map[string]*ClusterAsset // ========== Structure method ========== type ClusterAsset struct { Node + ClusterID string KubernetesVersion string } @@ -44,6 +40,15 @@ type Node struct { // TODO: Initial inits the cluster asset. func (ca *ClusterAsset) Initial(cmd *cobra.Command) error { + if err := ca.setClusterAsset(cmd); err != nil { + return err + } + ClusterConfig[ca.ClusterID] = ca + + return nil +} + +func (ca *ClusterAsset) setClusterAsset(cmd *cobra.Command) error { configFile, _ := cmd.Flags().GetString("cluster-config-file") if configFile != "" { @@ -53,8 +58,7 @@ func (ca *ClusterAsset) Initial(cmd *cobra.Command) error { return err } - err = yaml.Unmarshal(configData, ca) - if err != nil { + if err := yaml.Unmarshal(configData, ca); err != nil { return err } } @@ -66,8 +70,6 @@ func (ca *ClusterAsset) Initial(cmd *cobra.Command) error { ca.KubernetesVersion = "default k8s version" } - ClusterConfig = ca - return nil } @@ -78,13 +80,13 @@ func (ca *ClusterAsset) Delete() error { // TODO: Persist persists the cluster asset. func (ca *ClusterAsset) Persist() error { - // TODO: Serialize the cluster asset to json or yaml. - clusterConfig, err := yaml.Marshal(ca) + // Serialize the cluster asset to yaml. + clusterData, err := yaml.Marshal(ca) if err != nil { return err } - err = os.WriteFile("cluster_config.yaml", clusterConfig, 0644) + err = os.WriteFile("cluster_config.yaml", clusterData, 0644) if err != nil { return err } diff --git a/pkg/configmanager/asset/global/globalasset.go b/pkg/configmanager/asset/global/globalasset.go index db95c73192f1739c0c5841407b3af4bcc3555ee2..8a0e9190c016d289ca0f745852b3c2454afe08c7 100644 --- a/pkg/configmanager/asset/global/globalasset.go +++ b/pkg/configmanager/asset/global/globalasset.go @@ -20,37 +20,26 @@ import ( "github.com/spf13/cobra" ) +// Set global data var GlobalConfig *GlobalAsset -// ========== Package method ========== - -func GetGlobalConfig() (*GlobalAsset, error) { - return GlobalConfig, nil -} - // ========== Structure method ========== type GlobalAsset struct { - // Having previously set IsInitial as a global variable, it cannot be fixed to true after - // GlobalAsset executes Initial for the first time, so it is placed in the struct first. - IsInitial bool - NKD_Version string - Log_Level string + Log_Level string } // TODO: Initial inits the global asset. func (ga *GlobalAsset) Initial(cmd *cobra.Command) error { - if ga.IsInitial { - return nil + if err := ga.setGlobalAsset(cmd); err != nil { + return err } + GlobalConfig = ga - nkd_version, _ := cmd.Flags().GetString("version") - if nkd_version != "" { - ga.NKD_Version = nkd_version - } else { - ga.NKD_Version = "default nkd version" - } + return nil +} +func (ga *GlobalAsset) setGlobalAsset(cmd *cobra.Command) error { log_level, _ := cmd.Flags().GetString("log-level") if log_level != "" { ga.Log_Level = log_level @@ -58,9 +47,6 @@ func (ga *GlobalAsset) Initial(cmd *cobra.Command) error { ga.Log_Level = "default log level" } - GlobalConfig = ga - - ga.IsInitial = true return nil } @@ -71,6 +57,6 @@ func (ga *GlobalAsset) Delete() error { // TODO: Persist persists the global asset. func (ga *GlobalAsset) Persist() error { - // TODO: Serialize the global asset to json or yaml. + // TODO return nil } diff --git a/pkg/configmanager/manager/manager.go b/pkg/configmanager/manager/manager.go index 7921f2e73f04410876275cf598bc7bbd7cdae66b..fcf3227c8b4c19c983a253b9fc853731bac4bc10 100644 --- a/pkg/configmanager/manager/manager.go +++ b/pkg/configmanager/manager/manager.go @@ -17,6 +17,7 @@ limitations under the License. package manager import ( + "errors" "nestos-kubernetes-deployer/pkg/configmanager/asset/cluster" "nestos-kubernetes-deployer/pkg/configmanager/asset/global" @@ -24,16 +25,15 @@ import ( ) func Initial(cmd *cobra.Command) error { - var globalAsset *global.GlobalAsset - var clusterAsset *cluster.ClusterAsset - - err := globalAsset.Initial(cmd) - if err != nil { + // Init global asset. + globalAsset := &global.GlobalAsset{} + if err := globalAsset.Initial(cmd); err != nil { return err } - err = clusterAsset.Initial(cmd) - if err != nil { + // Init cluster asset. + clusterAsset := &cluster.ClusterAsset{} + if err := clusterAsset.Initial(cmd); err != nil { return err } @@ -44,7 +44,36 @@ func Search() error { return nil } +func GetGlobalConfig() (*global.GlobalAsset, error) { + return global.GlobalConfig, nil +} + +func GetClusterConfig(clusterID string) (*cluster.ClusterAsset, error) { + clusterConfig, ok := cluster.ClusterConfig[clusterID] + if !ok { + return nil, errors.New("ClusterID not found") + } + + return clusterConfig, nil +} + func Persist() error { + // Persist global asset. + globalConfig, err := GetGlobalConfig() + if err != nil { + return err + } + if err := globalConfig.Persist(); err != nil { + return err + } + + // Persist cluster asset. + for _, clusterConfig := range cluster.ClusterConfig { + if err := clusterConfig.Persist(); err != nil { + return err + } + } + return nil }