github.com/GoogleContainerTools/kaniko@v1.23.0/pkg/buildcontext/buildcontext.go (about)

     1  /*
     2  Copyright 2018 Google LLC
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package buildcontext
    18  
    19  import (
    20  	"errors"
    21  	"strings"
    22  
    23  	"github.com/GoogleContainerTools/kaniko/pkg/constants"
    24  	"github.com/GoogleContainerTools/kaniko/pkg/util"
    25  )
    26  
    27  const (
    28  	TarBuildContextPrefix = "tar://"
    29  )
    30  
    31  type BuildOptions struct {
    32  	GitBranch            string
    33  	GitSingleBranch      bool
    34  	GitRecurseSubmodules bool
    35  	InsecureSkipTLS      bool
    36  }
    37  
    38  // BuildContext unifies calls to download and unpack the build context.
    39  type BuildContext interface {
    40  	// Unpacks a build context and returns the directory where it resides
    41  	UnpackTarFromBuildContext() (string, error)
    42  }
    43  
    44  // GetBuildContext parses srcContext for the prefix and returns related buildcontext
    45  // parser
    46  func GetBuildContext(srcContext string, opts BuildOptions) (BuildContext, error) {
    47  	split := strings.SplitAfter(srcContext, "://")
    48  	if len(split) > 1 {
    49  		prefix := split[0]
    50  		context := split[1]
    51  
    52  		switch prefix {
    53  		case constants.GCSBuildContextPrefix:
    54  			return &GCS{context: srcContext}, nil
    55  		case constants.S3BuildContextPrefix:
    56  			return &S3{context: srcContext}, nil
    57  		case constants.LocalDirBuildContextPrefix:
    58  			return &Dir{context: context}, nil
    59  		case constants.GitBuildContextPrefix:
    60  			return &Git{context: context, opts: opts}, nil
    61  		case constants.HTTPSBuildContextPrefix:
    62  			if util.ValidAzureBlobStorageHost(srcContext) {
    63  				return &AzureBlob{context: srcContext}, nil
    64  			}
    65  			return &HTTPSTar{context: srcContext}, nil
    66  		case TarBuildContextPrefix:
    67  			return &Tar{context: context}, nil
    68  		}
    69  	}
    70  	return nil, errors.New("unknown build context prefix provided, please use one of the following: gs://, dir://, tar://, s3://, git://, https://")
    71  }