github.com/sealerio/sealer@v0.11.1-0.20240507115618-f4f89c5853ae/pkg/imageengine/buildah/engine.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 buildah
    16  
    17  import (
    18  	"path/filepath"
    19  	"runtime"
    20  
    21  	"github.com/containers/buildah/pkg/parse"
    22  	"github.com/containers/image/v5/types"
    23  
    24  	"github.com/sealerio/sealer/pkg/auth"
    25  
    26  	"github.com/BurntSushi/toml"
    27  	"github.com/containers/common/libimage"
    28  	"github.com/containers/storage"
    29  	"github.com/containers/storage/drivers/overlay"
    30  	types2 "github.com/containers/storage/types"
    31  	"github.com/sirupsen/logrus"
    32  	"github.com/spf13/cobra"
    33  
    34  	"github.com/sealerio/sealer/pkg/define/options"
    35  )
    36  
    37  type Engine struct {
    38  	*cobra.Command
    39  	libimageRuntime *libimage.Runtime
    40  	imageStore      storage.Store
    41  }
    42  
    43  func (engine *Engine) ImageRuntime() *libimage.Runtime {
    44  	return engine.libimageRuntime
    45  }
    46  
    47  func (engine *Engine) ImageStore() storage.Store {
    48  	return engine.imageStore
    49  }
    50  
    51  func (engine *Engine) SystemContext() *types.SystemContext {
    52  	return engine.libimageRuntime.SystemContext()
    53  }
    54  
    55  func checkOverlaySupported() {
    56  	conf := types2.TomlConfig{}
    57  	if _, err := toml.DecodeFile(storageConfPath, &conf); err != nil {
    58  		logrus.Warnf("failed to decode storage.conf, which may incur problems: %v", err)
    59  		return
    60  	}
    61  
    62  	if conf.Storage.RunRoot == "" || conf.Storage.GraphRoot == "" {
    63  		logrus.Warnf("runroot or graphroot is empty")
    64  		return
    65  	}
    66  
    67  	// this check aims to register "overlay" and "overlay2" driver.
    68  	// Otherwise, there will be "overlay" unsupported problem.
    69  	// This issue is relevant with low-level library problem.
    70  	// This is a weird problem. So fix it in this way currently.
    71  	if _, err := overlay.SupportsNativeOverlay(
    72  		filepath.Join(conf.Storage.GraphRoot, "overlay"),
    73  		filepath.Join(conf.Storage.RunRoot, "overlay")); err != nil {
    74  		logrus.Warnf("detect there is no native overlay supported: %v", err)
    75  	}
    76  }
    77  
    78  // TODO we can provide a configuration file to export those options.
    79  // the detailed information in the parse.SystemContextFromOptions
    80  func systemContext() *types.SystemContext {
    81  	// TODO
    82  	// options for the following
    83  	// DockerCertPath
    84  	// tls-verify
    85  	// os
    86  	// arch
    87  	// variant
    88  	return &types.SystemContext{
    89  		DockerRegistryUserAgent:           "Buildah/1.25.0",
    90  		AuthFilePath:                      auth.GetDefaultAuthFilePath(),
    91  		BigFilesTemporaryDir:              parse.GetTempDir(),
    92  		OSChoice:                          runtime.GOOS,
    93  		ArchitectureChoice:                runtime.GOARCH,
    94  		DockerInsecureSkipTLSVerify:       types.NewOptionalBool(false),
    95  		OCIInsecureSkipTLSVerify:          false,
    96  		DockerDaemonInsecureSkipTLSVerify: false,
    97  	}
    98  }
    99  
   100  func NewBuildahImageEngine(configurations options.EngineGlobalConfigurations) (*Engine, error) {
   101  	if err := initBuildah(); err != nil {
   102  		return nil, err
   103  	}
   104  
   105  	checkOverlaySupported()
   106  
   107  	store, err := getStore(&configurations)
   108  	if err != nil {
   109  		return nil, err
   110  	}
   111  
   112  	sysCxt := systemContext()
   113  	imageRuntime, err := libimage.RuntimeFromStore(store, &libimage.RuntimeOptions{SystemContext: sysCxt})
   114  	if err != nil {
   115  		return nil, err
   116  	}
   117  
   118  	return &Engine{
   119  		Command:         &cobra.Command{},
   120  		libimageRuntime: imageRuntime,
   121  		imageStore:      store,
   122  	}, nil
   123  }