-
Notifications
You must be signed in to change notification settings - Fork 64
/
k8s.go
73 lines (61 loc) · 1.96 KB
/
k8s.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
package main
import (
"fmt"
"os"
"strings"
globalLogger "github.com/rs/zerolog/log"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/util/validation"
k8sVersion "k8s.io/apimachinery/pkg/version"
"k8s.io/client-go/discovery"
"k8s.io/client-go/rest"
)
var defaultNamespacesToWatch = []string{v1.NamespaceDefault}
const allNamespacesLabel = "__all__"
func getNamespacesToWatch() (watchAll bool, namespaces []string, err error) {
watchNamespacesRaw := strings.TrimSpace(os.Getenv("SENTRY_K8S_WATCH_NAMESPACES"))
// Nothing in the env variable => use the default value
if watchNamespacesRaw == "" {
return false, defaultNamespacesToWatch, nil
}
// Special label => watch all namespaces
if watchNamespacesRaw == allNamespacesLabel {
return true, []string{}, nil
}
rawNamespaces := strings.Split(watchNamespacesRaw, ",")
namespaces = make([]string, 0, len(rawNamespaces))
for _, rawNamespace := range rawNamespaces {
namespace := strings.TrimSpace(rawNamespace)
if namespace == "" {
continue
}
errors := validation.IsValidLabelValue(namespace)
if len(errors) != 0 {
// Not a valid namespace name
return false, []string{}, fmt.Errorf(errors[0])
}
namespaces = append(namespaces, namespace)
}
namespaces = removeDuplicates(namespaces)
if len(namespaces) == 0 {
return false, namespaces, fmt.Errorf("no namespaces specified")
}
return false, namespaces, nil
}
func getClusterVersion(config *rest.Config) (*k8sVersion.Info, error) {
versionInfo := &k8sVersion.Info{}
discoveryClient, err := discovery.NewDiscoveryClientForConfig(config)
if err != nil {
return versionInfo, err
}
globalLogger.Debug().Msgf("Fetching cluster version...")
versionInfo, err = discoveryClient.ServerVersion()
globalLogger.Debug().Msgf("Cluster version: %s", versionInfo)
return versionInfo, err
}
func getObjectNameTag(object *v1.ObjectReference) string {
if object.Kind == "" {
return "object_name"
}
return fmt.Sprintf("%s_name", strings.ToLower(object.Kind))
}