github.com/argoproj/argo-cd@v1.8.7/server/version/version.go (about) 1 package version 2 3 import ( 4 "errors" 5 "fmt" 6 "os/exec" 7 "regexp" 8 "strings" 9 10 "github.com/argoproj/gitops-engine/pkg/utils/tracing" 11 "github.com/golang/protobuf/ptypes/empty" 12 "github.com/google/go-jsonnet" 13 "github.com/sirupsen/logrus" 14 "golang.org/x/net/context" 15 16 "github.com/argoproj/argo-cd/common" 17 "github.com/argoproj/argo-cd/pkg/apiclient/version" 18 "github.com/argoproj/argo-cd/server/settings" 19 "github.com/argoproj/argo-cd/util/helm" 20 ksutil "github.com/argoproj/argo-cd/util/ksonnet" 21 "github.com/argoproj/argo-cd/util/kustomize" 22 "github.com/argoproj/argo-cd/util/log" 23 sessionmgr "github.com/argoproj/argo-cd/util/session" 24 ) 25 26 type server struct { 27 ksonnetVersion string 28 kustomizeVersion string 29 helmVersion string 30 kubectlVersion string 31 jsonnetVersion string 32 authenticator settings.Authenticator 33 disableAuth func() (bool, error) 34 } 35 36 func NewServer(authenticator settings.Authenticator, disableAuth func() (bool, error)) *server { 37 return &server{authenticator: authenticator, disableAuth: disableAuth} 38 } 39 40 func getVersion() (string, error) { 41 span := tracing.NewLoggingTracer(log.NewLogrusLogger(logrus.New())).StartSpan("Version") 42 defer span.Finish() 43 cmd := exec.Command("kubectl", "version", "--client") 44 out, err := cmd.CombinedOutput() 45 if err != nil { 46 return "", fmt.Errorf("could not get kubectl version: %s", err) 47 } 48 49 re := regexp.MustCompile(`GitVersion:"([a-zA-Z0-9\.\-]+)"`) 50 matches := re.FindStringSubmatch(string(out)) 51 if len(matches) != 2 { 52 return "", errors.New("could not get kubectl version") 53 } 54 version := matches[1] 55 if version[0] != 'v' { 56 version = "v" + version 57 } 58 return strings.TrimSpace(version), nil 59 } 60 61 // Version returns the version of the API server 62 func (s *server) Version(ctx context.Context, _ *empty.Empty) (*version.VersionMessage, error) { 63 vers := common.GetVersion() 64 disableAuth, err := s.disableAuth() 65 if err != nil { 66 return nil, err 67 } 68 69 if !sessionmgr.LoggedIn(ctx) && !disableAuth { 70 return &version.VersionMessage{Version: vers.Version}, nil 71 } 72 73 if s.ksonnetVersion == "" { 74 ksonnetVersion, err := ksutil.Version() 75 if err == nil { 76 s.ksonnetVersion = ksonnetVersion 77 } else { 78 s.ksonnetVersion = err.Error() 79 } 80 } 81 if s.kustomizeVersion == "" { 82 kustomizeVersion, err := kustomize.Version(true) 83 if err == nil { 84 s.kustomizeVersion = kustomizeVersion 85 } else { 86 s.kustomizeVersion = err.Error() 87 } 88 } 89 if s.helmVersion == "" { 90 helmVersion, err := helm.Version(true) 91 if err == nil { 92 s.helmVersion = helmVersion 93 } else { 94 s.helmVersion = err.Error() 95 } 96 } 97 if s.kubectlVersion == "" { 98 kubectlVersion, err := getVersion() 99 if err == nil { 100 s.kubectlVersion = kubectlVersion 101 } else { 102 s.kubectlVersion = err.Error() 103 } 104 } 105 s.jsonnetVersion = jsonnet.Version() 106 return &version.VersionMessage{ 107 Version: vers.Version, 108 BuildDate: vers.BuildDate, 109 GitCommit: vers.GitCommit, 110 GitTag: vers.GitTag, 111 GitTreeState: vers.GitTreeState, 112 GoVersion: vers.GoVersion, 113 Compiler: vers.Compiler, 114 Platform: vers.Platform, 115 KsonnetVersion: s.ksonnetVersion, 116 KustomizeVersion: s.kustomizeVersion, 117 HelmVersion: s.helmVersion, 118 KubectlVersion: s.kubectlVersion, 119 JsonnetVersion: s.jsonnetVersion, 120 }, nil 121 } 122 123 // AuthFuncOverride allows the version to be returned without auth 124 func (s *server) AuthFuncOverride(ctx context.Context, fullMethodName string) (context.Context, error) { 125 if s.authenticator != nil { 126 // this authenticates the user, but ignores any error, so that we have claims populated 127 ctx, _ = s.authenticator.Authenticate(ctx) 128 } 129 return ctx, nil 130 }