forked from rook/rook
-
Notifications
You must be signed in to change notification settings - Fork 9
/
rook.go
121 lines (102 loc) · 3.87 KB
/
rook.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
/*
Copyright 2018 The Rook Authors. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package rook
import (
"fmt"
"os"
"strings"
"github.com/coreos/pkg/capnslog"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
rookclient "github.com/rook/rook/pkg/client/clientset/versioned"
"github.com/rook/rook/pkg/util/flags"
"github.com/rook/rook/pkg/version"
apiextensionsclient "k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset"
)
const (
RookEnvVarPrefix = "ROOK"
terminationLog = "/dev/termination-log"
)
var RootCmd = &cobra.Command{
Use: "rook",
Hidden: true,
}
var (
logLevelRaw string
Cfg = &Config{}
logger = capnslog.NewPackageLogger("github.com/rook/rook", "rookcmd")
)
type Config struct {
LogLevel capnslog.LogLevel
}
// Initialize the configuration parameters. The precedence from lowest to highest is:
// 1) default value (at compilation)
// 2) environment variables (upper case, replace - with _, and rook prefix. For example, discovery-url is ROOK_DISCOVERY_URL)
// 3) command line parameter
func init() {
RootCmd.PersistentFlags().StringVar(&logLevelRaw, "log-level", "INFO", "logging level for logging/tracing output (valid values: CRITICAL,ERROR,WARNING,NOTICE,INFO,DEBUG,TRACE)")
// load the environment variables
flags.SetFlagsFromEnv(RootCmd.Flags(), RookEnvVarPrefix)
flags.SetFlagsFromEnv(RootCmd.PersistentFlags(), RookEnvVarPrefix)
}
func SetLogLevel() {
// parse given log level string then set up corresponding global logging level
ll, err := capnslog.ParseLevel(logLevelRaw)
if err != nil {
logger.Warningf("failed to set log level %s. %+v", logLevelRaw, err)
}
Cfg.LogLevel = ll
capnslog.SetGlobalLogLevel(Cfg.LogLevel)
}
func LogStartupInfo(cmdFlags *pflag.FlagSet) {
// log the version number, arguments, and all final flag values (environment variable overrides
// have already been taken into account)
flagValues := flags.GetFlagsAndValues(cmdFlags, "secret|keyring")
logger.Infof("starting Rook %s with arguments '%s'", version.Version, strings.Join(os.Args, " "))
logger.Infof("flag values: %s", strings.Join(flagValues, ", "))
}
func GetClientset() (kubernetes.Interface, apiextensionsclient.Interface, rookclient.Interface, error) {
// create the k8s client
config, err := rest.InClusterConfig()
if err != nil {
return nil, nil, nil, fmt.Errorf("failed to get k8s config. %+v", err)
}
clientset, err := kubernetes.NewForConfig(config)
if err != nil {
return nil, nil, nil, fmt.Errorf("failed to create k8s clientset. %+v", err)
}
apiExtClientset, err := apiextensionsclient.NewForConfig(config)
if err != nil {
return nil, nil, nil, fmt.Errorf("failed to create k8s API extension clientset. %+v", err)
}
rookClientset, err := rookclient.NewForConfig(config)
if err != nil {
return nil, nil, nil, fmt.Errorf("failed to create rook clientset. %+v", err)
}
return clientset, apiExtClientset, rookClientset, nil
}
func TerminateFatal(reason error) {
fmt.Fprintln(os.Stderr, reason)
file, err := os.OpenFile(terminationLog, os.O_APPEND|os.O_WRONLY, 0644)
if err != nil {
fmt.Fprintln(os.Stderr, fmt.Errorf("failed to write message to termination log: %+v", err))
} else {
defer file.Close()
if _, err = file.WriteString(reason.Error()); err != nil {
fmt.Fprintln(os.Stderr, fmt.Errorf("failed to write message to termination log: %+v", err))
}
}
os.Exit(1)
}