From 87b142d728d328612bd61d9e1cd13c7a5e8cfa29 Mon Sep 17 00:00:00 2001 From: jianli-97 Date: Thu, 16 Nov 2023 09:33:21 +0800 Subject: [PATCH] Improve the Initial configuration method of the management module. --- .../asset/cluster/clusterasset.go | 77 ++++++++++++++++++- pkg/configmanager/asset/global/globalasset.go | 40 +++++++--- pkg/configmanager/manager/manager.go | 56 ++++++-------- 3 files changed, 130 insertions(+), 43 deletions(-) diff --git a/pkg/configmanager/asset/cluster/clusterasset.go b/pkg/configmanager/asset/cluster/clusterasset.go index 21efdc8..4dcd953 100644 --- a/pkg/configmanager/asset/cluster/clusterasset.go +++ b/pkg/configmanager/asset/cluster/clusterasset.go @@ -1,10 +1,73 @@ +/* +Copyright 2023 KylinSoft Co., Ltd. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + package cluster +import ( + "os" + + "github.com/spf13/cobra" + "gopkg.in/yaml.v2" +) + +var ClusterConfig *ClusterAsset + +// ========== Package method ========== + +func GetClusterConfig() (*ClusterAsset, error) { + return ClusterConfig, nil +} + +// ========== Structure method ========== + type ClusterAsset struct { + Node + KubernetesVersion string +} + +type Node struct { + Count int } -// TODO: Init inits the cluster asset. -func (ca *ClusterAsset) Initial() error { +// TODO: Initial inits the cluster asset. +func (ca *ClusterAsset) Initial(cmd *cobra.Command) error { + configFile, _ := cmd.Flags().GetString("cluster-config-file") + + if configFile != "" { + // Parse configuration file. + configData, err := os.ReadFile(configFile) + if err != nil { + return err + } + + err = yaml.Unmarshal(configData, ca) + if err != nil { + return err + } + } + + kubernetes_version, _ := cmd.Flags().GetString("kubernetes-version") + if kubernetes_version != "" { + ca.KubernetesVersion = kubernetes_version + } else { + ca.KubernetesVersion = "default k8s version" + } + + ClusterConfig = ca + return nil } @@ -16,5 +79,15 @@ 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) + if err != nil { + return err + } + + err = os.WriteFile("cluster_config.yaml", clusterConfig, 0644) + if err != nil { + return err + } + return nil } diff --git a/pkg/configmanager/asset/global/globalasset.go b/pkg/configmanager/asset/global/globalasset.go index 328e5e5..db95c73 100644 --- a/pkg/configmanager/asset/global/globalasset.go +++ b/pkg/configmanager/asset/global/globalasset.go @@ -16,29 +16,51 @@ limitations under the License. package global -var ( - GlobalConfig *GlobalAsset - IsInitial = false +import ( + "github.com/spf13/cobra" ) -// ========== 包方法 ========== +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 } -// TODO: Init inits the global asset. -func (ga *GlobalAsset) Initial() error { - ga.NKD_Version = "0.2.0" +// TODO: Initial inits the global asset. +func (ga *GlobalAsset) Initial(cmd *cobra.Command) error { + if ga.IsInitial { + return nil + } + + nkd_version, _ := cmd.Flags().GetString("version") + if nkd_version != "" { + ga.NKD_Version = nkd_version + } else { + ga.NKD_Version = "default nkd version" + } + + log_level, _ := cmd.Flags().GetString("log-level") + if log_level != "" { + ga.Log_Level = log_level + } else { + ga.Log_Level = "default log level" + } GlobalConfig = ga - IsInitial = true + + ga.IsInitial = true return nil } diff --git a/pkg/configmanager/manager/manager.go b/pkg/configmanager/manager/manager.go index e8902c2..7921f2e 100644 --- a/pkg/configmanager/manager/manager.go +++ b/pkg/configmanager/manager/manager.go @@ -1,48 +1,40 @@ +/* +Copyright 2023 KylinSoft Co., Ltd. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + package manager import ( "nestos-kubernetes-deployer/pkg/configmanager/asset/cluster" "nestos-kubernetes-deployer/pkg/configmanager/asset/global" - "os" "github.com/spf13/cobra" - "gopkg.in/yaml.v2" ) -func Init(cmd *cobra.Command) error { - clusterAsset := &cluster.ClusterAsset{} - - // Parse command line arguments. - file, _ := cmd.Flags().GetString("file") +func Initial(cmd *cobra.Command) error { + var globalAsset *global.GlobalAsset + var clusterAsset *cluster.ClusterAsset - if file != "" { - // Parse configuration file. - yamlFile, err := os.ReadFile(file) - if err != nil { - return err - } - - err = yaml.Unmarshal(yamlFile, clusterAsset) - if err != nil { - return err - } - } - - // TODO: 设置优先级 & 参数解析 - - // Initialize global assets. - if !global.IsInitial { - globalAsset := &global.GlobalAsset{} - err := globalAsset.Initial() - if err != nil { - return nil - } + err := globalAsset.Initial(cmd) + if err != nil { + return err } - // Initialize cluster assets. - err := clusterAsset.Initial() + err = clusterAsset.Initial(cmd) if err != nil { - return nil + return err } return nil -- Gitee