/
main.go
269 lines (231 loc) · 7.87 KB
/
main.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
package main
import (
"context"
"encoding/json"
"fmt"
"log"
"net"
"path/filepath"
"strings"
"go.bytebuilders.dev/license-verifier/info"
v "gomodules.xyz/x/version"
core "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/util/sets"
"k8s.io/apimachinery/pkg/version"
genericapiserver "k8s.io/apiserver/pkg/server"
"k8s.io/client-go/informers"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/cache"
"k8s.io/client-go/tools/clientcmd"
"k8s.io/client-go/util/homedir"
"k8s.io/klog/v2"
meta_util "kmodules.xyz/client-go/meta"
"kmodules.xyz/client-go/tools/clusterid"
"kmodules.xyz/resource-metrics/api"
)
func main() {
masterURL := ""
kubeconfigPath := filepath.Join(homedir.HomeDir(), ".kube", "config")
kubeconfigPath = "/home/tamal/Downloads/mysql-test-kubeconfig.yaml"
config, err := clientcmd.BuildConfigFromFlags(masterURL, kubeconfigPath)
if err != nil {
log.Fatalf("Could not get Kubernetes config: %s", err)
}
kc := kubernetes.NewForConfigOrDie(config)
factory := informers.NewSharedInformerFactory(kc, 0)
nodeInformer := factory.Core().V1().Nodes().Informer()
nodeLister := factory.Core().V1().Nodes().Lister()
nodeInformer.AddEventHandlerWithResyncPeriod(&ResourceEventPublisher{}, 0) // c.Auditor.ForGVK(api.SchemeGroupVersion.WithKind(api.ResourceKindPostgres)))
stopCh := genericapiserver.SetupSignalHandler()
factory.Start(stopCh)
// Wait for all involved caches to be synced, before processing items from the queue is started
for t, ok := range factory.WaitForCacheSync(stopCh) {
if !ok {
klog.Fatalf("%v timed out waiting for caches to sync", t)
return
}
}
nodes, err := nodeLister.List(labels.Everything())
if err != nil {
panic(err)
}
si, err := GenerateSiteInfo(config, kc, nodes, "")
if err != nil {
panic(err)
}
data, err := json.MarshalIndent(si, "", " ")
if err != nil {
panic(err)
}
fmt.Println(string(data))
}
func GenerateSiteInfo(cfg *rest.Config, kc kubernetes.Interface, nodes []*core.Node, licenseID string) (*SiteInfo, error) {
var si SiteInfo
if info.ProductName != "" || v.Version.Version != "" || licenseID != "" {
si.Product = &ProductInfo{}
si.Product.LicenseID = licenseID
si.Product.ProductOwnerName = info.ProductOwnerName
si.Product.ProductOwnerUID = info.ProductOwnerUID
si.Product.ProductName = info.ProductName
si.Product.ProductUID = info.ProductUID
si.Product.Version = Version{
Version: v.Version.Version,
VersionStrategy: v.Version.VersionStrategy,
CommitHash: v.Version.CommitHash,
GitBranch: v.Version.GitBranch,
GitTag: v.Version.GitTag,
CommitTimestamp: v.Version.CommitTimestamp,
GoVersion: v.Version.GoVersion,
Compiler: v.Version.Compiler,
Platform: v.Version.Platform,
}
}
var err error
si.Kubernetes.ClusterName = clusterid.ClusterName()
si.Kubernetes.ClusterUID, err = clusterid.ClusterUID(kc.CoreV1().Namespaces())
if err != nil {
return nil, err
}
si.Kubernetes.Version, err = kc.Discovery().ServerVersion()
if err != nil {
return nil, err
}
cert, err := meta_util.APIServerCertificate(cfg)
if err != nil {
return nil, err
} else {
si.Kubernetes.ControlPlane = &ControlPlaneInfo{
NotBefore: metav1.NewTime(cert.NotBefore),
NotAfter: metav1.NewTime(cert.NotAfter),
// DNSNames: cert.DNSNames,
EmailAddresses: cert.EmailAddresses,
// IPAddresses: cert.IPAddresses,
// URIs: cert.URIs,
}
dnsNames := sets.NewString(cert.DNSNames...)
ips := sets.NewString()
if len(cert.Subject.CommonName) > 0 {
if ip := net.ParseIP(cert.Subject.CommonName); ip != nil {
if !skipIP(ip) {
ips.Insert(ip.String())
}
} else {
dnsNames.Insert(cert.Subject.CommonName)
}
}
for _, host := range dnsNames.UnsortedList() {
if host == "kubernetes" ||
host == "kubernetes.default" ||
host == "kubernetes.default.svc" ||
strings.HasSuffix(host, ".svc.cluster.local") ||
host == "localhost" ||
!strings.ContainsRune(host, '.') {
dnsNames.Delete(host)
}
}
si.Kubernetes.ControlPlane.DNSNames = dnsNames.List()
for _, ip := range cert.IPAddresses {
if !skipIP(ip) {
ips.Insert(ip.String())
}
}
si.Kubernetes.ControlPlane.IPAddresses = ips.List()
uris := make([]string, 0, len(cert.URIs))
for _, u := range cert.URIs {
uris = append(uris, u.String())
}
si.Kubernetes.ControlPlane.URIs = uris
}
if len(nodes) == 0 {
result, err := kc.CoreV1().Nodes().List(context.TODO(), metav1.ListOptions{})
if err != nil {
return nil, err
}
nodes = make([]*core.Node, len(result.Items))
for i := range result.Items {
nodes[i] = &result.Items[i]
}
}
si.Kubernetes.NodeStats.Count = len(nodes)
var capacity core.ResourceList
var allocatable core.ResourceList
for _, node := range nodes {
capacity = api.AddResourceList(capacity, node.Status.Capacity)
allocatable = api.AddResourceList(allocatable, node.Status.Allocatable)
}
si.Kubernetes.NodeStats.Capacity = capacity
si.Kubernetes.NodeStats.Allocatable = allocatable
return &si, nil
}
type SiteInfo struct {
Product *ProductInfo `json:"product,omitempty"`
Kubernetes KubernetesInfo `json:"kubernetes"`
}
type Version struct {
Version string `json:"version,omitempty"`
VersionStrategy string `json:"versionStrategy,omitempty"`
CommitHash string `json:"commitHash,omitempty"`
GitBranch string `json:"gitBranch,omitempty"`
GitTag string `json:"gitTag,omitempty"`
CommitTimestamp string `json:"commitTimestamp,omitempty"`
GoVersion string `json:"goVersion,omitempty"`
Compiler string `json:"compiler,omitempty"`
Platform string `json:"platform,omitempty"`
}
type ProductInfo struct {
Version Version `json:"version"`
LicenseID string `json:"licenseID,omitempty"`
ProductOwnerName string `json:"productOwnerName,omitempty"`
ProductOwnerUID string `json:"productOwnerUID,omitempty"`
// This has been renamed to Features
ProductName string `json:"productName,omitempty"`
ProductUID string `json:"productUID,omitempty"`
}
type KubernetesInfo struct {
// https://github.com/kmodules/client-go/blob/master/tools/clusterid/lib.go
ClusterName string `json:"clusterName,omitempty"`
ClusterUID string `json:"clusterUID,omitempty"`
Version *version.Info `json:"version,omitempty"`
ControlPlane *ControlPlaneInfo `json:"controlPlane,omitempty"`
NodeStats NodeStats `json:"nodeStats"`
}
// https://github.com/kmodules/client-go/blob/kubernetes-1.16.3/tools/analytics/analytics.go#L66
type ControlPlaneInfo struct {
DNSNames []string `json:"dnsNames,omitempty"`
EmailAddresses []string `json:"emailAddresses,omitempty"`
IPAddresses []string `json:"ipAddresses,omitempty"`
URIs []string `json:"uris,omitempty"`
NotBefore metav1.Time `json:"notBefore"`
NotAfter metav1.Time `json:"notAfter"`
}
type NodeStats struct {
Count int `json:"count,omitempty"`
// Capacity represents the total resources of a node.
// More info: https://kubernetes.io/docs/concepts/storage/persistent-volumes#capacity
// +optional
Capacity core.ResourceList `json:"capacity,omitempty"`
// Allocatable represents the resources of a node that are available for scheduling.
// Defaults to Capacity.
// +optional
Allocatable core.ResourceList `json:"allocatable,omitempty"`
}
func skipIP(ip net.IP) bool {
return ip.IsLoopback() ||
ip.IsMulticast() ||
ip.IsGlobalUnicast() ||
ip.IsInterfaceLocalMulticast() ||
ip.IsLinkLocalMulticast() ||
ip.IsLinkLocalUnicast()
}
var _ cache.ResourceEventHandler = &ResourceEventPublisher{}
type ResourceEventPublisher struct {
}
func (p ResourceEventPublisher) OnAdd(obj interface{}) {
}
func (p ResourceEventPublisher) OnUpdate(oldObj, newObj interface{}) {
}
func (p ResourceEventPublisher) OnDelete(obj interface{}) {
}