github.com/sealerio/sealer@v0.11.1-0.20240507115618-f4f89c5853ae/pkg/container-runtime/installer.go (about)

     1  // Copyright © 2022 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 containerruntime
    16  
    17  import (
    18  	"fmt"
    19  	"net"
    20  	"path/filepath"
    21  
    22  	"github.com/sealerio/sealer/common"
    23  	"github.com/sealerio/sealer/pkg/infradriver"
    24  	v2 "github.com/sealerio/sealer/types/api/v2"
    25  )
    26  
    27  const (
    28  	DefaultDockerCRISocket     = "/var/run/dockershim.sock"
    29  	DefaultCgroupDriver        = "systemd"
    30  	DefaultDockerCertsDir      = "/etc/docker/certs.d"
    31  	DefaultContainerdCRISocket = "/run/containerd/containerd.sock"
    32  	DefaultContainerdCertsDir  = "/etc/containerd/certs.d"
    33  	DockerConfigFileName       = "config.json"
    34  )
    35  
    36  const (
    37  	CgroupDriverArg = "CgroupDriver"
    38  )
    39  
    40  type Installer interface {
    41  	InstallOn(hosts []net.IP) error
    42  
    43  	GetInfo() (Info, error)
    44  
    45  	UnInstallFrom(hosts []net.IP) error
    46  
    47  	//Upgrade() (ContainerRuntimeInfo, error)
    48  	//Rollback() (ContainerRuntimeInfo, error)
    49  }
    50  
    51  type Info struct {
    52  	v2.ContainerRuntimeConfig
    53  	CgroupDriver   string
    54  	CRISocket      string
    55  	CertsDir       string
    56  	ConfigFilePath string
    57  }
    58  
    59  func NewInstaller(conf v2.ContainerRuntimeConfig, driver infradriver.InfraDriver) (Installer, error) {
    60  	switch conf.Type {
    61  	case common.Docker, "":
    62  		conf.Type = common.Docker
    63  		ret := &DefaultInstaller{
    64  			rootfs: driver.GetClusterRootfsPath(),
    65  			driver: driver,
    66  			envs:   driver.GetClusterEnv(),
    67  			Info: Info{
    68  				CertsDir:               DefaultDockerCertsDir,
    69  				CRISocket:              DefaultDockerCRISocket,
    70  				ContainerRuntimeConfig: conf,
    71  				ConfigFilePath:         filepath.Join(common.GetHomeDir(), ".docker", DockerConfigFileName),
    72  			},
    73  		}
    74  		ret.Info.CgroupDriver = DefaultCgroupDriver
    75  		if cd, ok := ret.envs[CgroupDriverArg]; ok && cd != "" {
    76  			ret.Info.CgroupDriver = cd
    77  		}
    78  
    79  		return ret, nil
    80  	case common.Containerd:
    81  		ret := &DefaultInstaller{
    82  			rootfs: driver.GetClusterRootfsPath(),
    83  			driver: driver,
    84  			envs:   driver.GetClusterEnv(),
    85  			Info: Info{
    86  				CertsDir:               DefaultContainerdCertsDir,
    87  				CRISocket:              DefaultContainerdCRISocket,
    88  				ContainerRuntimeConfig: conf,
    89  			},
    90  		}
    91  		ret.Info.CgroupDriver = DefaultCgroupDriver
    92  		if cd, ok := ret.envs[CgroupDriverArg]; ok && cd != "" {
    93  			ret.Info.CgroupDriver = cd
    94  		}
    95  
    96  		return ret, nil
    97  	default:
    98  		return nil, fmt.Errorf("invalid container runtime type")
    99  	}
   100  }