-
Notifications
You must be signed in to change notification settings - Fork 153
/
version.go
125 lines (102 loc) · 3.13 KB
/
version.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package server
import (
"context"
"fmt"
"github.com/weaveworks/weave-gitops/core/clustersmngr"
coretypes "github.com/weaveworks/weave-gitops/core/server/types"
pb "github.com/weaveworks/weave-gitops/pkg/api/core"
"github.com/weaveworks/weave-gitops/pkg/flux"
"github.com/weaveworks/weave-gitops/pkg/server/auth"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime/schema"
"sigs.k8s.io/controller-runtime/pkg/client"
)
// Variables that we'll set @ build time
var (
Version = "v0.0.0"
GitCommit = ""
Branch = ""
Buildtime = ""
)
const (
defaultVersion = ""
)
func (cs *coreServer) getScopedClient(ctx context.Context) (client.Client, error) {
clustersClient, err := cs.clustersManager.GetImpersonatedClientForCluster(ctx, auth.Principal(ctx), clustersmngr.DefaultCluster)
if err != nil {
return nil, fmt.Errorf("error getting impersonating client: %w", err)
}
scopedClient, err := clustersClient.Scoped(clustersmngr.DefaultCluster)
if err != nil {
return nil, fmt.Errorf("error getting scoped client: %w", err)
}
return scopedClient, nil
}
func (cs *coreServer) getFluxVersion(ctx context.Context, k8sClient client.Client) (string, error) {
listResult := unstructured.UnstructuredList{}
listResult.SetGroupVersionKind(schema.GroupVersionKind{
Group: "",
Version: "v1",
Kind: "Namespace",
})
opts := client.MatchingLabels{
coretypes.PartOfLabel: FluxNamespacePartOf,
}
u := unstructured.Unstructured{}
err := k8sClient.List(ctx, &listResult, opts)
if err != nil {
return "", fmt.Errorf("error getting list of objects")
} else {
for _, item := range listResult.Items {
if item.GetLabels()[flux.VersionLabelKey] != "" {
u = item
break
}
}
}
labels := u.GetLabels()
if labels == nil {
return "", fmt.Errorf("error getting labels")
}
fluxVersion := labels[flux.VersionLabelKey]
if fluxVersion == "" {
return "", fmt.Errorf("no flux version found")
}
return fluxVersion, nil
}
func (cs *coreServer) getKubeVersion(ctx context.Context) (string, error) {
dc, err := cs.clustersManager.GetImpersonatedDiscoveryClient(ctx, auth.Principal(ctx), clustersmngr.DefaultCluster)
if err != nil {
return "", fmt.Errorf("error creating discovery client: %w", err)
}
serverVersion, err := dc.ServerVersion()
if err != nil {
return "", fmt.Errorf("error getting server version: %w", err)
} else {
return serverVersion.GitVersion, nil
}
}
func (cs *coreServer) GetVersion(ctx context.Context, msg *pb.GetVersionRequest) (*pb.GetVersionResponse, error) {
k8sClient, err := cs.getScopedClient(ctx)
if err != nil {
cs.logger.Error(err, "error creating scoped client")
}
fluxVersion, err := cs.getFluxVersion(ctx, k8sClient)
if err != nil {
cs.logger.Error(err, "error getting flux version")
fluxVersion = defaultVersion
}
kubeVersion, err := cs.getKubeVersion(ctx)
if err != nil {
cs.logger.Error(err, "error getting k8s version")
kubeVersion = defaultVersion
}
return &pb.GetVersionResponse{
Semver: Version,
Commit: GitCommit,
Branch: Branch,
BuildTime: Buildtime,
FluxVersion: fluxVersion,
KubeVersion: kubeVersion,
}, nil
}