github.com/GoogleContainerTools/skaffold@v1.39.18/pkg/skaffold/initializer/analyze/builder.go (about)

     1  /*
     2  Copyright 2020 The Skaffold Authors
     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 analyze
    18  
    19  import (
    20  	"context"
    21  	"path/filepath"
    22  	"strings"
    23  
    24  	"github.com/GoogleContainerTools/skaffold/pkg/skaffold/build/buildpacks"
    25  	"github.com/GoogleContainerTools/skaffold/pkg/skaffold/build/jib"
    26  	"github.com/GoogleContainerTools/skaffold/pkg/skaffold/docker"
    27  	"github.com/GoogleContainerTools/skaffold/pkg/skaffold/initializer/build"
    28  )
    29  
    30  type builderAnalyzer struct {
    31  	directoryAnalyzer
    32  	enableJibInit        bool
    33  	enableJibGradleInit  bool
    34  	enableBuildpacksInit bool
    35  	findBuilders         bool
    36  	buildpacksBuilder    string
    37  	foundBuilders        []build.InitBuilder
    38  
    39  	parentDirToStopFindJibSettings string
    40  }
    41  
    42  func (a *builderAnalyzer) analyzeFile(ctx context.Context, filePath string) error {
    43  	if a.findBuilders {
    44  		lookForJib := a.parentDirToStopFindJibSettings == "" || a.parentDirToStopFindJibSettings == a.currentDir
    45  		builderConfigs, lookForJib := a.detectBuilders(ctx, filePath, lookForJib)
    46  		a.foundBuilders = append(a.foundBuilders, builderConfigs...)
    47  		if !lookForJib {
    48  			a.parentDirToStopFindJibSettings = a.currentDir
    49  		}
    50  	}
    51  	return nil
    52  }
    53  
    54  func (a *builderAnalyzer) exitDir(dir string) {
    55  	if a.parentDirToStopFindJibSettings == dir {
    56  		a.parentDirToStopFindJibSettings = ""
    57  	}
    58  }
    59  
    60  // detectBuilders checks if a path is a builder config, and if it is, returns the InitBuilders representing the
    61  // configs. Also returns a boolean marking search completion for subdirectories (true = subdirectories should
    62  // continue to be searched, false = subdirectories should not be searched for more builders)
    63  func (a *builderAnalyzer) detectBuilders(ctx context.Context, path string, detectJib bool) ([]build.InitBuilder, bool) {
    64  	var results []build.InitBuilder
    65  	searchSubDirectories := true
    66  
    67  	// TODO: Remove backwards compatibility if statement (not entire block)
    68  	if a.enableJibInit && detectJib {
    69  		// Check for jib
    70  		if builders := jib.Validate(ctx, path, a.enableJibGradleInit); builders != nil {
    71  			for i := range builders {
    72  				results = append(results, builders[i])
    73  			}
    74  			searchSubDirectories = false
    75  		}
    76  	}
    77  
    78  	// Check for Dockerfile
    79  	base := filepath.Base(path)
    80  	if strings.Contains(strings.ToLower(base), "dockerfile") {
    81  		if docker.Validate(path) {
    82  			results = append(results, docker.ArtifactConfig{
    83  				// Docker expects forward slashes (for Linux containers at least)
    84  				File: filepath.ToSlash(path),
    85  			})
    86  		}
    87  	}
    88  
    89  	// TODO: Remove backwards compatibility if statement (not entire block)
    90  	if a.enableBuildpacksInit {
    91  		// Check for buildpacks
    92  		if buildpacks.Validate(path) {
    93  			results = append(results, buildpacks.ArtifactConfig{
    94  				File:    path,
    95  				Builder: a.buildpacksBuilder,
    96  			})
    97  		}
    98  	}
    99  
   100  	return results, searchSubDirectories
   101  }