paths.go 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. /*
  2. Copyright 2018 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 utils
  14. import (
  15. "errors"
  16. "fmt"
  17. "os"
  18. "path/filepath"
  19. "runtime"
  20. "strings"
  21. )
  22. // GetK8sRootDir returns the root directory for kubernetes, if present in the gopath.
  23. func GetK8sRootDir() (string, error) {
  24. dir, err := RootDir()
  25. if err != nil {
  26. return "", err
  27. }
  28. return filepath.Join(dir, fmt.Sprintf("%s/", "k8s.io/kubernetes")), nil
  29. }
  30. // GetCAdvisorRootDir returns the root directory for cAdvisor, if present in the gopath.
  31. func GetCAdvisorRootDir() (string, error) {
  32. dir, err := RootDir()
  33. if err != nil {
  34. return "", err
  35. }
  36. return filepath.Join(dir, fmt.Sprintf("%s/", "github.com/google/cadvisor")), nil
  37. }
  38. // RootDir gets the on-disk kubernetes source directory, returning an error is none is found
  39. func RootDir() (string, error) {
  40. // Get the directory of the current executable
  41. _, testExec, _, _ := runtime.Caller(0)
  42. path := filepath.Dir(testExec)
  43. // Look for the kubernetes source root directory
  44. if strings.Contains(path, "k8s.io/kubernetes") {
  45. splitPath := strings.Split(path, "k8s.io/kubernetes")
  46. return splitPath[0], nil
  47. }
  48. return "", errors.New("could not find kubernetes source root directory")
  49. }
  50. // GetK8sBuildOutputDir returns the build output directory for k8s
  51. func GetK8sBuildOutputDir() (string, error) {
  52. k8sRoot, err := GetK8sRootDir()
  53. if err != nil {
  54. return "", err
  55. }
  56. buildOutputDir := filepath.Join(k8sRoot, "_output/local/go/bin")
  57. if _, err := os.Stat(buildOutputDir); err != nil {
  58. return "", err
  59. }
  60. return buildOutputDir, nil
  61. }