k8s.io/kubernetes@v1.29.3/test/utils/paths.go (about) 1 /* 2 Copyright 2018 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 utils 18 19 import ( 20 "errors" 21 "fmt" 22 "os" 23 "path/filepath" 24 "runtime" 25 "strings" 26 ) 27 28 // GetK8sRootDir returns the root directory for kubernetes, if present in the gopath. 29 func GetK8sRootDir() (string, error) { 30 dir := os.Getenv("KUBE_ROOT") 31 if len(dir) > 0 { 32 return dir, nil 33 } 34 35 dir, err := RootDir() 36 if err != nil { 37 return "", err 38 } 39 return filepath.Join(dir, fmt.Sprintf("%s/", "k8s.io/kubernetes")), nil 40 } 41 42 // GetCAdvisorRootDir returns the root directory for cAdvisor, if present in the gopath. 43 func GetCAdvisorRootDir() (string, error) { 44 dir, err := RootDir() 45 if err != nil { 46 return "", err 47 } 48 return filepath.Join(dir, fmt.Sprintf("%s/", "github.com/google/cadvisor")), nil 49 } 50 51 // RootDir gets the on-disk kubernetes source directory, returning an error is none is found 52 func RootDir() (string, error) { 53 // Get the directory of the current executable 54 _, testExec, _, _ := runtime.Caller(0) 55 path := filepath.Dir(testExec) 56 57 // Look for the kubernetes source root directory 58 if strings.Contains(path, "k8s.io/kubernetes") { 59 splitPath := strings.Split(path, "k8s.io/kubernetes") 60 return splitPath[0], nil 61 } 62 63 return "", errors.New("could not find kubernetes source root directory") 64 } 65 66 // GetK8sBuildOutputDir returns the build output directory for k8s 67 // For dockerized build, targetArch (eg: 'linux/arm64', 'linux/amd64') must be explicitly specified 68 // For non dockerized build, targetArch is ignored 69 func GetK8sBuildOutputDir(isDockerizedBuild bool, targetArch string) (string, error) { 70 k8sRoot, err := GetK8sRootDir() 71 if err != nil { 72 return "", err 73 } 74 buildOutputDir := filepath.Join(k8sRoot, "_output/local/go/bin") 75 if isDockerizedBuild { 76 buildOutputDir = filepath.Join(k8sRoot, "_output/dockerized/bin/", targetArch) 77 } 78 if _, err := os.Stat(buildOutputDir); err != nil { 79 return "", err 80 } 81 return buildOutputDir, nil 82 }