github.com/alibaba/sealer@v0.8.6-0.20220430115802-37a2bdaa8173/build/buildinstruction/context.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 buildinstruction
    16  
    17  import (
    18  	"github.com/opencontainers/go-digest"
    19  
    20  	"github.com/alibaba/sealer/common"
    21  	"github.com/alibaba/sealer/pkg/image"
    22  	"github.com/alibaba/sealer/pkg/image/cache"
    23  	"github.com/alibaba/sealer/pkg/image/store"
    24  	v1 "github.com/alibaba/sealer/types/api/v1"
    25  )
    26  
    27  type ExecContext struct {
    28  	BuildContext string
    29  	BuildArgs    map[string]string
    30  	//cache flag,will change for each layer ctx
    31  	ContinueCache bool
    32  	//cache chain to hit,will change for each layer ctx
    33  	ParentID cache.ChainID
    34  	//static method to do cache
    35  	CacheSvc cache.Service
    36  	Prober   image.Prober
    37  	//used to gen layer
    38  	LayerStore store.LayerStore
    39  }
    40  
    41  type InstructionContext struct {
    42  	// dynamic method to init different instruction
    43  	CurrentLayer *v1.Layer
    44  	BaseLayers   []v1.Layer
    45  	Platform     v1.Platform
    46  }
    47  
    48  type Out struct {
    49  	LayerID       digest.Digest
    50  	ParentID      cache.ChainID
    51  	ContinueCache bool
    52  }
    53  
    54  func NewInstruction(ic InstructionContext) (Interface, error) {
    55  	// init each inst via layer type
    56  	switch ic.CurrentLayer.Type {
    57  	case common.CMDCOMMAND, common.RUNCOMMAND:
    58  		return NewCmdInstruction(ic)
    59  	case common.COPYCOMMAND:
    60  		return NewCopyInstruction(ic)
    61  	}
    62  
    63  	return nil, nil
    64  }
    65  
    66  func NewExecContext(buildContext string, buildArgs map[string]string, useCache bool, layerStore store.LayerStore) ExecContext {
    67  	if !useCache {
    68  		return ExecContext{
    69  			LayerStore:   layerStore,
    70  			BuildContext: buildContext,
    71  			BuildArgs:    buildArgs,
    72  		}
    73  	}
    74  	chainSvc, err := cache.NewService()
    75  	if err != nil {
    76  		return ExecContext{}
    77  	}
    78  
    79  	service, err := image.NewImageService()
    80  	if err != nil {
    81  		return ExecContext{}
    82  	}
    83  
    84  	prober := image.NewImageProber(service, true)
    85  	return ExecContext{
    86  		LayerStore:    layerStore,
    87  		BuildContext:  buildContext,
    88  		BuildArgs:     buildArgs,
    89  		CacheSvc:      chainSvc,
    90  		ParentID:      cache.ChainID(""),
    91  		Prober:        prober,
    92  		ContinueCache: true,
    93  	}
    94  }