sigs.k8s.io/cluster-api@v1.7.1/controllers/remote/restconfig.go (about) 1 /* 2 Copyright 2021 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 remote 18 19 import ( 20 "fmt" 21 "os" 22 "path/filepath" 23 gruntime "runtime" 24 "strings" 25 26 "sigs.k8s.io/cluster-api/version" 27 ) 28 29 const ( 30 unknowString = "unknown" 31 ) 32 33 func buildUserAgent(command, version, sourceName, os, arch, commit string) string { 34 return fmt.Sprintf( 35 "%s/%s %s (%s/%s) cluster.x-k8s.io/%s", command, version, sourceName, os, arch, commit) 36 } 37 38 // DefaultClusterAPIUserAgent returns a User-Agent string built from static global vars. 39 func DefaultClusterAPIUserAgent(sourceName string) string { 40 return buildUserAgent( 41 adjustCommand(os.Args[0]), 42 adjustVersion(version.Get().GitVersion), 43 adjustSourceName(sourceName), 44 gruntime.GOOS, 45 gruntime.GOARCH, 46 adjustCommit(version.Get().GitCommit)) 47 } 48 49 // adjustSourceName returns the name of the source calling the client. 50 func adjustSourceName(c string) string { 51 if c == "" { 52 return unknowString 53 } 54 return c 55 } 56 57 // adjustCommit returns sufficient significant figures of the commit's git hash. 58 func adjustCommit(c string) string { 59 if c == "" { 60 return unknowString 61 } 62 if len(c) > 7 { 63 return c[:7] 64 } 65 return c 66 } 67 68 // adjustVersion strips "alpha", "beta", etc. from version in form 69 // major.minor.patch-[alpha|beta|etc]. 70 func adjustVersion(v string) string { 71 if v == "" { 72 return unknowString 73 } 74 seg := strings.SplitN(v, "-", 2) 75 return seg[0] 76 } 77 78 // adjustCommand returns the last component of the 79 // OS-specific command path for use in User-Agent. 80 func adjustCommand(p string) string { 81 // Unlikely, but better than returning "". 82 if p == "" { 83 return unknowString 84 } 85 return filepath.Base(p) 86 }