github.com/alibaba/sealer@v0.8.6-0.20220430115802-37a2bdaa8173/pkg/clusterfile/util.go (about)

     1  // Copyright © 2021 Alibaba Group Holding Ltd.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package clusterfile
    16  
    17  import (
    18  	"fmt"
    19  	"io/ioutil"
    20  	"os"
    21  	"strings"
    22  
    23  	"github.com/alibaba/sealer/pkg/runtime"
    24  	k8sV1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    25  	"sigs.k8s.io/yaml"
    26  
    27  	"github.com/alibaba/sealer/utils"
    28  
    29  	v1 "github.com/alibaba/sealer/types/api/v1"
    30  	v2 "github.com/alibaba/sealer/types/api/v2"
    31  
    32  	"github.com/alibaba/sealer/common"
    33  	"github.com/alibaba/sealer/pkg/cert"
    34  )
    35  
    36  const typeV1 = "zlink.aliyun.com/v1alpha1"
    37  const typeV2 = "sealer.cloud/v2"
    38  
    39  var ErrClusterNotExist = fmt.Errorf("no cluster exist")
    40  
    41  func GetDefaultClusterName() (string, error) {
    42  	files, err := ioutil.ReadDir(fmt.Sprintf("%s/.sealer", cert.GetUserHomeDir()))
    43  	if err != nil {
    44  		return "", err
    45  	}
    46  	var clusters []string
    47  	for _, f := range files {
    48  		if f.IsDir() {
    49  			clusters = append(clusters, f.Name())
    50  		}
    51  	}
    52  	if len(clusters) == 1 {
    53  		return clusters[0], nil
    54  	} else if len(clusters) > 1 {
    55  		return "", fmt.Errorf("Select a cluster through the -c parameter: " + strings.Join(clusters, ","))
    56  	}
    57  
    58  	return "", ErrClusterNotExist
    59  }
    60  
    61  func GetClusterFromFile(filepath string) (cluster *v2.Cluster, err error) {
    62  	cluster = &v2.Cluster{}
    63  	if err = utils.UnmarshalYamlFile(filepath, cluster); err != nil {
    64  		return nil, fmt.Errorf("failed to get cluster from %s, %v", filepath, err)
    65  	}
    66  	cluster.SetAnnotations(common.ClusterfileName, filepath)
    67  	return cluster, nil
    68  }
    69  
    70  func GetDefaultCluster() (cluster *v2.Cluster, err error) {
    71  	name, err := GetDefaultClusterName()
    72  	if err != nil {
    73  		return nil, err
    74  	}
    75  	userHome, err := os.UserHomeDir()
    76  	if err != nil {
    77  		return nil, err
    78  	}
    79  	var filepath = fmt.Sprintf("%s/.sealer/%s/Clusterfile", userHome, name)
    80  
    81  	return GetClusterFromFile(filepath)
    82  }
    83  
    84  func GetClusterFromDataCompatV1(data []byte) (*v2.Cluster, error) {
    85  	var cluster *v2.Cluster
    86  	metaType := k8sV1.TypeMeta{}
    87  	err := yaml.Unmarshal(data, &metaType)
    88  	if err != nil {
    89  		return nil, err
    90  	}
    91  	if metaType.Kind != common.Cluster {
    92  		return nil, fmt.Errorf("not found type cluster from: \n%s", data)
    93  	}
    94  	if metaType.APIVersion == typeV1 {
    95  		cluster = &v2.Cluster{}
    96  		clusterV1 := &v1.Cluster{}
    97  		if err := yaml.Unmarshal(data, &clusterV1); err != nil {
    98  			return nil, err
    99  		}
   100  		var hosts []v2.Host
   101  		if len(clusterV1.Spec.Masters.IPList) != 0 {
   102  			hosts = append(hosts, v2.Host{IPS: clusterV1.Spec.Masters.IPList, Roles: []string{common.MASTER}})
   103  		}
   104  		if len(clusterV1.Spec.Nodes.IPList) != 0 {
   105  			hosts = append(hosts, v2.Host{IPS: clusterV1.Spec.Nodes.IPList, Roles: []string{common.NODE}})
   106  		}
   107  		cluster.APIVersion = typeV2
   108  		cluster.Spec.SSH = clusterV1.Spec.SSH
   109  		cluster.Spec.Env = clusterV1.Spec.Env
   110  		cluster.Spec.Hosts = hosts
   111  		cluster.Spec.Image = clusterV1.Spec.Image
   112  		cluster.Name = clusterV1.Name
   113  		cluster.Kind = clusterV1.Kind
   114  	} else {
   115  		c, err := runtime.DecodeCRDFromString(string(data), common.Cluster)
   116  		if err != nil {
   117  			return nil, err
   118  		} else if c == nil {
   119  			return nil, fmt.Errorf("not found type cluster from: \n%s", data)
   120  		}
   121  		cluster = c.(*v2.Cluster)
   122  	}
   123  	return cluster, nil
   124  }