build.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /*
  2. Copyright 2016 The Kubernetes Authors.
  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. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package builder
  14. import (
  15. "flag"
  16. "fmt"
  17. "os"
  18. "os/exec"
  19. "path/filepath"
  20. "strings"
  21. "k8s.io/klog"
  22. "k8s.io/kubernetes/test/utils"
  23. )
  24. var k8sBinDir = flag.String("k8s-bin-dir", "", "Directory containing k8s kubelet binaries.")
  25. var buildTargets = []string{
  26. "cmd/kubelet",
  27. "test/e2e_node/e2e_node.test",
  28. "vendor/github.com/onsi/ginkgo/ginkgo",
  29. "cluster/gce/gci/mounter",
  30. }
  31. // BuildGo builds k8s binaries.
  32. func BuildGo() error {
  33. klog.Infof("Building k8s binaries...")
  34. k8sRoot, err := utils.GetK8sRootDir()
  35. if err != nil {
  36. return fmt.Errorf("failed to locate kubernetes root directory %v", err)
  37. }
  38. targets := strings.Join(buildTargets, " ")
  39. cmd := exec.Command("make", "-C", k8sRoot, fmt.Sprintf("WHAT=%s", targets))
  40. cmd.Stdout = os.Stdout
  41. cmd.Stderr = os.Stderr
  42. err = cmd.Run()
  43. if err != nil {
  44. return fmt.Errorf("failed to build go packages %v", err)
  45. }
  46. return nil
  47. }
  48. func getK8sBin(bin string) (string, error) {
  49. // Use commandline specified path
  50. if *k8sBinDir != "" {
  51. absPath, err := filepath.Abs(*k8sBinDir)
  52. if err != nil {
  53. return "", err
  54. }
  55. if _, err := os.Stat(filepath.Join(*k8sBinDir, bin)); err != nil {
  56. return "", fmt.Errorf("Could not find %s under directory %s", bin, absPath)
  57. }
  58. return filepath.Join(absPath, bin), nil
  59. }
  60. path, err := filepath.Abs(filepath.Dir(os.Args[0]))
  61. if err != nil {
  62. return "", fmt.Errorf("Could not find absolute path of directory containing the tests %s", filepath.Dir(os.Args[0]))
  63. }
  64. if _, err := os.Stat(filepath.Join(path, bin)); err == nil {
  65. return filepath.Join(path, bin), nil
  66. }
  67. buildOutputDir, err := utils.GetK8sBuildOutputDir()
  68. if err != nil {
  69. return "", err
  70. }
  71. if _, err := os.Stat(filepath.Join(buildOutputDir, bin)); err == nil {
  72. return filepath.Join(buildOutputDir, bin), nil
  73. }
  74. // Give up with error
  75. return "", fmt.Errorf("unable to locate %s, Can be defined using --k8s-path", bin)
  76. }
  77. // GetKubeletServerBin returns the path of kubelet binary.
  78. func GetKubeletServerBin() string {
  79. bin, err := getK8sBin("kubelet")
  80. if err != nil {
  81. klog.Fatalf("Could not locate kubelet binary %v.", err)
  82. }
  83. return bin
  84. }