forked from openshift/online-archivist
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmonitor.go
113 lines (93 loc) · 3.3 KB
/
monitor.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
package cmd
import (
"os"
"path/filepath"
"github.com/openshift/online-archivist/pkg/clustermonitor"
"github.com/openshift/online-archivist/pkg/config"
buildclient "github.com/openshift/client-go/build/clientset/versioned"
"k8s.io/client-go/kubernetes"
restclient "k8s.io/client-go/rest"
kclientcmd "k8s.io/client-go/tools/clientcmd"
log "github.com/Sirupsen/logrus"
arkclient "github.com/heptio/ark/pkg/client"
"github.com/spf13/cobra"
)
func init() {
RootCmd.AddCommand(clusterMonitorCmd)
}
var clusterMonitorCmd = &cobra.Command{
Use: "monitor",
Short: "Monitors OpenShift cluster capacity and initiates archival.",
Long: `Monitors the capacity of an OpenShift cluster, project last activity, and submits requests to archive dormant projects as necessary.`,
Run: func(cmd *cobra.Command, args []string) {
log.SetOutput(os.Stdout)
archivistCfg := loadConfig(cfgFile)
restConfig, kc, err := createClients()
if err != nil {
log.Panicf("error creating OpenShift/Kubernetes clients: %s", err)
}
// TODO: in Ark this appears to be the binary name when launching the CLI.
// Not sure how this is being used.
arkFactory := arkclient.NewFactory("ark")
arkClient, err := arkFactory.Client()
if err != nil {
log.Panicf("error creating Ark client: %s", err)
}
log.Debugf("got ark client: %v", arkClient)
buildClient := buildclient.NewForConfigOrDie(restConfig)
activityMonitor := clustermonitor.NewClusterMonitor(archivistCfg, archivistCfg.Clusters[0],
buildClient, kc, arkClient)
activityMonitor.Run()
log.Infoln("cluster monitor running")
},
}
func createClients() (*restclient.Config, kubernetes.Interface, error) {
return CreateClientsForConfig()
}
// CreateClientsForConfig creates and returns OpenShift and Kubernetes clients (as well as other useful
// client objects) for the given client config.
func CreateClientsForConfig() (*restclient.Config, kubernetes.Interface, error) {
clientConfig, err := restclient.InClusterConfig()
if err != nil {
log.Warnf("error creating in-cluster config: %s", err)
log.Warnf("attempting switch to external kubeconfig")
// For external use with the current user's kubeconfig context: (development)
clientConfig, err = kclientcmd.BuildConfigFromFlags("", filepath.Join(homeDir(), ".kube", "config"))
if err != nil {
log.Panicf("error creating cluster config: %s", err)
}
}
log.WithFields(log.Fields{
"APIPath": clientConfig.APIPath,
"Host": clientConfig.Host,
"Username": clientConfig.Username,
"BearerToken": clientConfig.BearerToken,
}).Infoln("Created OpenShift client clientConfig:")
kc := kubernetes.NewForConfigOrDie(clientConfig)
return clientConfig, kc, err
}
func loadConfig(configFile string) config.ArchivistConfig {
var archivistCfg config.ArchivistConfig
if configFile != "" {
var err error
archivistCfg, err = config.NewArchivistConfigFromFile(configFile)
if err != nil {
log.Panicf("invalid configuration: %s", err)
}
} else {
archivistCfg = config.NewDefaultArchivistConfig()
}
if lvl, err := log.ParseLevel(archivistCfg.LogLevel); err != nil {
log.Panic(err)
} else {
log.SetLevel(lvl)
}
log.Infoln("using configuration:", archivistCfg)
return archivistCfg
}
func homeDir() string {
if h := os.Getenv("HOME"); h != "" {
return h
}
return os.Getenv("USERPROFILE") // windows
}