github.com/sealerio/sealer@v0.11.1-0.20240507115618-f4f89c5853ae/pkg/infra/infra.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 infra
    16  
    17  import (
    18  	"fmt"
    19  
    20  	"github.com/sealerio/sealer/pkg/infra/aliyun"
    21  	"github.com/sealerio/sealer/pkg/infra/container"
    22  	v1 "github.com/sealerio/sealer/types/api/v1"
    23  )
    24  
    25  type Interface interface {
    26  	// Apply IAAS resources and save metadata info like vpc instance id to cluster status
    27  	// https://github.com/fanux/sealgate/tree/master/cloud
    28  	Apply() error
    29  }
    30  
    31  func NewDefaultProvider(cluster *v1.Cluster) (Interface, error) {
    32  	switch cluster.Spec.Provider {
    33  	case aliyun.AliCloud:
    34  		return NewAliProvider(cluster)
    35  	case container.CONTAINER:
    36  		return NewContainerProvider(cluster)
    37  	default:
    38  		return nil, fmt.Errorf("the provider is invalid, please set the provider correctly")
    39  	}
    40  }
    41  
    42  func NewAliProvider(cluster *v1.Cluster) (Interface, error) {
    43  	config := new(aliyun.Config)
    44  	err := aliyun.LoadConfig(config)
    45  	if err != nil {
    46  		return nil, err
    47  	}
    48  	aliProvider := new(aliyun.AliProvider)
    49  	aliProvider.Config = *config
    50  	aliProvider.Cluster = cluster
    51  	err = aliProvider.NewClient()
    52  	if err != nil {
    53  		return nil, err
    54  	}
    55  	return aliProvider, nil
    56  }
    57  
    58  func NewContainerProvider(cluster *v1.Cluster) (Interface, error) {
    59  	if container.IsDockerAvailable() {
    60  		return nil, fmt.Errorf("please install docker on your system")
    61  	}
    62  
    63  	cli, err := container.NewClientWithCluster(cluster)
    64  	if err != nil {
    65  		return nil, fmt.Errorf("new container client failed")
    66  	}
    67  
    68  	return cli, nil
    69  }