Skip to content

Commit

Permalink
optimize: parse the custom resource definitions method refactoring (#…
Browse files Browse the repository at this point in the history
…1409)

* Parse the Custom Resource Definitions method refactoring

Signed-off-by: wb-hjh933779 <[email protected]>

* move decode.go file to util pkg

Signed-off-by: wb-hjh933779 <[email protected]>

* add decode v1 cluster function test .

Signed-off-by: wb-hjh933779 <[email protected]>

* fix originally phase plugin execution

Signed-off-by: wb-hjh933779 <[email protected]>

* optimize the writing of the label plugin: ignore the spaces before and after the paragraph

Signed-off-by: wb-hjh933779 <[email protected]>
  • Loading branch information
bxy4543 authored May 20, 2022
1 parent a58ccc5 commit ea12fef
Show file tree
Hide file tree
Showing 17 changed files with 753 additions and 314 deletions.
4 changes: 3 additions & 1 deletion apply/applydriver/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ package applydriver
import (
"fmt"

"github.com/sealerio/sealer/utils"

osi "github.com/sealerio/sealer/utils/os"

"github.com/sealerio/sealer/apply/processor"
Expand Down Expand Up @@ -169,7 +171,7 @@ func (c *Applier) scaleCluster(mj, md, nj, nd []string) error {
}
var cluster *v2.Cluster
if !scaleProcessor.(*processor.ScaleProcessor).IsScaleUp {
c, err := runtime.DecodeCRDFromFile(common.GetClusterWorkClusterfile(c.ClusterDesired.Name), common.Cluster)
c, err := utils.DecodeCRDFromFile(common.GetClusterWorkClusterfile(c.ClusterDesired.Name), common.Cluster)
if err != nil {
return err
} else if c != nil {
Expand Down
7 changes: 5 additions & 2 deletions apply/processor/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,15 @@ func (c *CreateProcessor) GetPipeLine() ([]func(cluster *v2.Cluster) error, erro

func (c *CreateProcessor) PreProcess(cluster *v2.Cluster) error {
c.Config = config.NewConfiguration(cluster)
c.initPlugin(cluster)
if err := c.initPlugin(cluster); err != nil {
return err
}
return clusterfile.SaveToDisk(cluster, cluster.Name)
}

func (c *CreateProcessor) initPlugin(cluster *v2.Cluster) {
func (c *CreateProcessor) initPlugin(cluster *v2.Cluster) error {
c.Plugins = plugin.NewPlugins(cluster, c.ClusterFile.GetPlugins())
return c.Plugins.Load()
}

func (c *CreateProcessor) MountImage(cluster *v2.Cluster) error {
Expand Down
11 changes: 8 additions & 3 deletions common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,14 @@ const (

//CRD kind
const (
Config = "Config"
Plugin = "Plugin"
Cluster = "Cluster"
Config = "Config"
Plugin = "Plugin"
Cluster = "Cluster"
InitConfiguration = "InitConfiguration"
JoinConfiguration = "JoinConfiguration"
ClusterConfiguration = "ClusterConfiguration"
KubeletConfiguration = "KubeletConfiguration"
KubeProxyConfiguration = "KubeProxyConfiguration"
)

const (
Expand Down
44 changes: 20 additions & 24 deletions pkg/clusterfile/pre_process.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ import (
"io"
"os"

yaml2 "github.com/sealerio/sealer/utils"

v2 "github.com/sealerio/sealer/types/api/v2"
runtime2 "k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/util/yaml"

Expand All @@ -29,7 +32,6 @@ import (
"github.com/sealerio/sealer/pkg/env"
"github.com/sealerio/sealer/pkg/runtime"
v1 "github.com/sealerio/sealer/types/api/v1"
"github.com/sealerio/sealer/utils"
)

type PreProcessor interface {
Expand Down Expand Up @@ -68,19 +70,16 @@ func (c *ClusterFile) PrePareEnv() error {
if err != nil {
return err
}
err = c.DecodePlugins(clusterFileData)
if err != nil && err != ErrTypeNotFound {
return err
}
err = c.DecodeConfigs(clusterFileData)
if err != nil && err != ErrTypeNotFound {

if err = c.DecodePlugins(clusterFileData); err != nil {
return err
}
err = c.DecodeKubeadmConfig(clusterFileData)
if err != nil && err != ErrTypeNotFound {

if err = c.DecodeConfigs(clusterFileData); err != nil {
return err
}
return nil

return c.DecodeKubeadmConfig(clusterFileData)
}

func (c *ClusterFile) PrePareConfigs() error {
Expand Down Expand Up @@ -129,48 +128,45 @@ func (c *ClusterFile) PrePareCluster() error {
}

func (c *ClusterFile) DecodeCluster(data []byte) error {
cluster, err := GetClusterFromDataCompatV1(data)
cluster, err := yaml2.DecodeCRDFromByte(data, common.Cluster)
if err != nil {
return err
}
c.Cluster = *cluster
c.Cluster = *cluster.(*v2.Cluster)
return nil
}

func (c *ClusterFile) DecodeConfigs(data []byte) error {
configs, err := utils.DecodeV1CRDFromReader(bytes.NewReader(data), common.Config)
configs, err := yaml2.DecodeCRDFromByte(data, common.Config)
if err != nil {
if err == io.EOF {
return nil
}
return err
}
if configs == nil {
return ErrTypeNotFound
}
cfg := configs.([]v1.Config)
c.Configs = cfg
return nil
}

func (c *ClusterFile) DecodePlugins(data []byte) error {
plugs, err := utils.DecodeV1CRDFromReader(bytes.NewReader(data), common.Plugin)
plugs, err := yaml2.DecodeCRDFromByte(data, common.Plugin)
if err != nil {
if err == io.EOF {
return nil
}
return err
}
if plugs == nil {
return ErrTypeNotFound
}
plugins := plugs.([]v1.Plugin)
c.Plugins = plugins
return nil
}

func (c *ClusterFile) DecodeKubeadmConfig(data []byte) error {
kubeadmConfig, err := runtime.LoadKubeadmConfigs(string(data), runtime.DecodeCRDFromString)
kubeadmConfig, err := runtime.LoadKubeadmConfigs(string(data), yaml2.DecodeCRDFromString)
if err != nil {
return err
}
if kubeadmConfig == nil {
return ErrTypeNotFound
}
c.KubeConfig = kubeadmConfig
return nil
}
50 changes: 0 additions & 50 deletions pkg/clusterfile/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,12 @@ import (

yamlUtils "github.com/sealerio/sealer/utils/yaml"

k8sV1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/yaml"

"github.com/sealerio/sealer/common"
"github.com/sealerio/sealer/pkg/cert"
"github.com/sealerio/sealer/pkg/runtime"
v1 "github.com/sealerio/sealer/types/api/v1"
v2 "github.com/sealerio/sealer/types/api/v2"
k8sRuntime "k8s.io/apimachinery/pkg/runtime"
)

const typeV1 = "zlink.aliyun.com/v1alpha1"
const typeV2 = "sealer.cloud/v2"

var ErrClusterNotExist = fmt.Errorf("no cluster exist")

func GetDefaultClusterName() (string, error) {
Expand Down Expand Up @@ -80,48 +72,6 @@ func GetDefaultCluster() (cluster *v2.Cluster, err error) {
return GetClusterFromFile(fmt.Sprintf("%s/.sealer/%s/Clusterfile", userHome, name))
}

func GetClusterFromDataCompatV1(data []byte) (*v2.Cluster, error) {
var cluster *v2.Cluster
metaType := k8sV1.TypeMeta{}
err := yaml.Unmarshal(data, &metaType)
if err != nil {
return nil, err
}
if metaType.Kind != common.Cluster {
return nil, fmt.Errorf("not found type cluster from: \n%s", data)
}
if metaType.APIVersion == typeV1 {
cluster = &v2.Cluster{}
clusterV1 := &v1.Cluster{}
if err := yaml.Unmarshal(data, &clusterV1); err != nil {
return nil, err
}
var hosts []v2.Host
if len(clusterV1.Spec.Masters.IPList) != 0 {
hosts = append(hosts, v2.Host{IPS: clusterV1.Spec.Masters.IPList, Roles: []string{common.MASTER}})
}
if len(clusterV1.Spec.Nodes.IPList) != 0 {
hosts = append(hosts, v2.Host{IPS: clusterV1.Spec.Nodes.IPList, Roles: []string{common.NODE}})
}
cluster.APIVersion = typeV2
cluster.Spec.SSH = clusterV1.Spec.SSH
cluster.Spec.Env = clusterV1.Spec.Env
cluster.Spec.Hosts = hosts
cluster.Spec.Image = clusterV1.Spec.Image
cluster.Name = clusterV1.Name
cluster.Kind = clusterV1.Kind
} else {
c, err := runtime.DecodeCRDFromString(string(data), common.Cluster)
if err != nil {
return nil, err
} else if c == nil {
return nil, fmt.Errorf("not found type cluster from: \n%s", data)
}
cluster = c.(*v2.Cluster)
}
return cluster, nil
}

func SaveToDisk(cluster k8sRuntime.Object, clusterName string) error {
fileName := common.GetClusterWorkClusterfile(clusterName)
err := os.MkdirAll(filepath.Dir(fileName), os.ModePerm)
Expand Down
5 changes: 3 additions & 2 deletions pkg/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,11 @@ import (
"io/ioutil"
"testing"

"github.com/sealerio/sealer/utils"

"github.com/sealerio/sealer/common"
v1 "github.com/sealerio/sealer/types/api/v1"
v2 "github.com/sealerio/sealer/types/api/v2"
"github.com/sealerio/sealer/utils"
)

func TestDumper_Dump(t *testing.T) {
Expand Down Expand Up @@ -56,7 +57,7 @@ func TestDumper_Dump(t *testing.T) {
Cluster: &v2.Cluster{},
}
c.Cluster.Name = tt.fields.clusterName
configs, err := utils.DecodeV1CRD(tt.args.clusterfile, common.Config)
configs, err := utils.DecodeCRDFromFile(tt.args.clusterfile, common.Config)
if err != nil {
t.Error(err)
return
Expand Down
2 changes: 1 addition & 1 deletion pkg/plugin/labels.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ func (l LabelsNodes) formatData(data string, hosts []string) map[string][]label
return m
}
for _, v := range items {
tmps := strings.Split(v, " ")
tmps := strings.Split(strings.TrimSpace(v), " ")
if len(tmps) != 2 {
//logger.Warn("label data is no-compliance with the rules! label data: %v", v)
continue
Expand Down
7 changes: 4 additions & 3 deletions pkg/plugin/plugins.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,13 @@ import (
"plugin"
"strings"

"github.com/sealerio/sealer/utils"

"github.com/sealerio/sealer/utils/yaml"

"github.com/sealerio/sealer/common"
v1 "github.com/sealerio/sealer/types/api/v1"
v2 "github.com/sealerio/sealer/types/api/v2"
"github.com/sealerio/sealer/utils"
"github.com/sealerio/sealer/utils/platform"
strUtils "github.com/sealerio/sealer/utils/strings"
)
Expand Down Expand Up @@ -83,11 +84,11 @@ func (c *PluginsProcessor) Load() error {
Register(pt, p)
}
if yaml.Matcher(f.Name()) {
plugins, err := utils.DecodePlugins(filepath.Join(path, f.Name()))
plugins, err := utils.DecodeCRDFromFile(filepath.Join(path, f.Name()), common.Plugin)
if err != nil {
return fmt.Errorf("failed to load plugin %v", err)
}
c.Plugins = append(c.Plugins, plugins...)
c.Plugins = append(c.Plugins, plugins.([]v1.Plugin)...)
}
}
return nil
Expand Down
17 changes: 10 additions & 7 deletions pkg/runtime/kubeadm_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ package runtime

import (
"fmt"
"io"

"github.com/sealerio/sealer/utils"

osi "github.com/sealerio/sealer/utils/os"

Expand Down Expand Up @@ -62,13 +65,13 @@ func (k *KubeadmConfig) Merge(kubeadmYamlPath string) error {
err error
)
if kubeadmYamlPath == "" || !osi.IsFileExist(kubeadmYamlPath) {
defaultKubeadmConfig, err = LoadKubeadmConfigs(DefaultKubeadmConfig, DecodeCRDFromString)
defaultKubeadmConfig, err = LoadKubeadmConfigs(DefaultKubeadmConfig, utils.DecodeCRDFromString)
if err != nil {
return err
}
return mergo.Merge(k, defaultKubeadmConfig)
}
defaultKubeadmConfig, err = LoadKubeadmConfigs(kubeadmYamlPath, DecodeCRDFromFile)
defaultKubeadmConfig, err = LoadKubeadmConfigs(kubeadmYamlPath, utils.DecodeCRDFromFile)
if err != nil {
return fmt.Errorf("failed to found kubeadm config from %s: %v", kubeadmYamlPath, err)
}
Expand All @@ -84,31 +87,31 @@ func (k *KubeadmConfig) Merge(kubeadmYamlPath string) error {
func LoadKubeadmConfigs(arg string, decode func(arg string, kind string) (interface{}, error)) (*KubeadmConfig, error) {
kubeadmConfig := &KubeadmConfig{}
initConfig, err := decode(arg, InitConfiguration)
if err != nil {
if err != nil && err != io.EOF {
return nil, err
} else if initConfig != nil {
kubeadmConfig.InitConfiguration = *initConfig.(*v1beta2.InitConfiguration)
}
clusterConfig, err := decode(arg, ClusterConfiguration)
if err != nil {
if err != nil && err != io.EOF {
return nil, err
} else if clusterConfig != nil {
kubeadmConfig.ClusterConfiguration = *clusterConfig.(*v1beta2.ClusterConfiguration)
}
kubeProxyConfig, err := decode(arg, KubeProxyConfiguration)
if err != nil {
if err != nil && err != io.EOF {
return nil, err
} else if kubeProxyConfig != nil {
kubeadmConfig.KubeProxyConfiguration = *kubeProxyConfig.(*v1alpha1.KubeProxyConfiguration)
}
kubeletConfig, err := decode(arg, KubeletConfiguration)
if err != nil {
if err != nil && err != io.EOF {
return nil, err
} else if kubeletConfig != nil {
kubeadmConfig.KubeletConfiguration = *kubeletConfig.(*v1beta1.KubeletConfiguration)
}
joinConfig, err := decode(arg, JoinConfiguration)
if err != nil {
if err != nil && err != io.EOF {
return nil, err
} else if joinConfig != nil {
kubeadmConfig.JoinConfiguration = *joinConfig.(*v1beta2.JoinConfiguration)
Expand Down
4 changes: 3 additions & 1 deletion pkg/runtime/kubeadm_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import (
"os"
"testing"

"github.com/sealerio/sealer/utils"

"github.com/sealerio/sealer/utils/yaml"

"github.com/sealerio/sealer/logger"
Expand Down Expand Up @@ -359,7 +361,7 @@ func TestKubeadmConfig_LoadFromClusterfile(t *testing.T) {
t.Errorf("Remove %s error = %v, wantErr %v", testfile, err, tt.wantErr)
}
}()
KubeadmConfig, err := LoadKubeadmConfigs(testfile, DecodeCRDFromFile)
KubeadmConfig, err := LoadKubeadmConfigs(testfile, utils.DecodeCRDFromFile)
if err != nil {
t.Errorf("err: %v", err)
return
Expand Down
Loading

0 comments on commit ea12fef

Please sign in to comment.