k8s.io/kubernetes@v1.29.3/test/e2e_node/builder/build.go (about)

     1  /*
     2  Copyright 2016 The Kubernetes 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 builder
    18  
    19  import (
    20  	"flag"
    21  	"fmt"
    22  	"os"
    23  	"os/exec"
    24  	"path/filepath"
    25  	"strings"
    26  
    27  	"k8s.io/klog/v2"
    28  	"k8s.io/kubernetes/test/utils"
    29  )
    30  
    31  var k8sBinDir = flag.String("k8s-bin-dir", "", "Directory containing k8s kubelet binaries.")
    32  var useDockerizedBuild = flag.Bool("use-dockerized-build", false, "Use dockerized build for test artifacts")
    33  var targetBuildArch = flag.String("target-build-arch", "linux/amd64", "Target architecture for the test artifacts for dockerized build")
    34  
    35  var buildCGOTargets = []string{
    36  	"cmd/kubelet",
    37  }
    38  
    39  var buildNoCGOTargets = []string{
    40  	"test/e2e_node/e2e_node.test",
    41  	"github.com/onsi/ginkgo/v2/ginkgo",
    42  	"cluster/gce/gci/mounter",
    43  	"test/e2e_node/plugins/gcp-credential-provider",
    44  }
    45  
    46  // BuildGo builds k8s binaries.
    47  func BuildGo() error {
    48  	if err := BuildTargets(true); err != nil {
    49  		return fmt.Errorf("unable to build cgo targets : %w", err)
    50  	}
    51  	if err := BuildTargets(false); err != nil {
    52  		return fmt.Errorf("unable to build non-cgo targets : %w", err)
    53  	}
    54  	return nil
    55  }
    56  
    57  // BuildGo builds k8s binaries.
    58  func BuildTargets(cgo bool) error {
    59  	klog.Infof("Building k8s binaries...")
    60  	k8sRoot, err := utils.GetK8sRootDir()
    61  	if err != nil {
    62  		return fmt.Errorf("failed to locate kubernetes root directory %v", err)
    63  	}
    64  	targets := buildCGOTargets
    65  	if !cgo {
    66  		targets = buildNoCGOTargets
    67  	}
    68  	what := strings.Join(targets, " ")
    69  	cmd := exec.Command("make", "-C", k8sRoot,
    70  		fmt.Sprintf("WHAT=%s", what))
    71  	if cgo {
    72  		cmd.Args = append(cmd.Args, "CGO_ENABLED=1")
    73  	} else {
    74  		cmd.Args = append(cmd.Args, "CGO_ENABLED=0")
    75  	}
    76  	if IsDockerizedBuild() {
    77  		klog.Infof("Building dockerized k8s binaries targets %s for architecture %s", targets, GetTargetBuildArch())
    78  		// Multi-architecture build is only supported in dockerized build
    79  		cmd = exec.Command(filepath.Join(k8sRoot, "build/run.sh"), "make", fmt.Sprintf("WHAT=%s", what), fmt.Sprintf("KUBE_BUILD_PLATFORMS=%s", GetTargetBuildArch()))
    80  	}
    81  	cmd.Stdout = os.Stdout
    82  	cmd.Stderr = os.Stderr
    83  	err = cmd.Run()
    84  	if err != nil {
    85  		return fmt.Errorf("failed to build go packages %v", err)
    86  	}
    87  	return nil
    88  }
    89  
    90  // IsDockerizedBuild returns if test needs to use dockerized build
    91  func IsDockerizedBuild() bool {
    92  	return *useDockerizedBuild
    93  }
    94  
    95  // GetTargetBuildArch returns the target build architecture for dockerized build
    96  func GetTargetBuildArch() string {
    97  	return *targetBuildArch
    98  }
    99  
   100  // IsTargetArchArm64 returns if the target is for linux/arm64 platform
   101  func IsTargetArchArm64() bool {
   102  	return GetTargetBuildArch() == "linux/arm64"
   103  }
   104  
   105  func getK8sBin(bin string) (string, error) {
   106  	// Use commandline specified path
   107  	if *k8sBinDir != "" {
   108  		absPath, err := filepath.Abs(*k8sBinDir)
   109  		if err != nil {
   110  			return "", err
   111  		}
   112  		if _, err := os.Stat(filepath.Join(*k8sBinDir, bin)); err != nil {
   113  			return "", fmt.Errorf("Could not find %s under directory %s", bin, absPath)
   114  		}
   115  		return filepath.Join(absPath, bin), nil
   116  	}
   117  
   118  	path, err := filepath.Abs(filepath.Dir(os.Args[0]))
   119  	if err != nil {
   120  		return "", fmt.Errorf("Could not find absolute path of directory containing the tests %s", filepath.Dir(os.Args[0]))
   121  	}
   122  	if _, err := os.Stat(filepath.Join(path, bin)); err == nil {
   123  		return filepath.Join(path, bin), nil
   124  	}
   125  
   126  	buildOutputDir, err := utils.GetK8sBuildOutputDir(IsDockerizedBuild(), GetTargetBuildArch())
   127  	if err != nil {
   128  		return "", err
   129  	}
   130  	if _, err := os.Stat(filepath.Join(buildOutputDir, bin)); err == nil {
   131  		return filepath.Join(buildOutputDir, bin), nil
   132  	}
   133  
   134  	// Give up with error
   135  	return "", fmt.Errorf("unable to locate %s, Can be defined using --k8s-path", bin)
   136  }
   137  
   138  // GetKubeletServerBin returns the path of kubelet binary.
   139  func GetKubeletServerBin() string {
   140  	bin, err := getK8sBin("kubelet")
   141  	if err != nil {
   142  		klog.Fatalf("Could not locate kubelet binary %v.", err)
   143  	}
   144  	return bin
   145  }