2016-11-05 17:09:05 -07:00
|
|
|
/*
|
|
|
|
|
Copyright 2016 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.
|
|
|
|
|
*/
|
2016-09-29 17:09:21 -07:00
|
|
|
package main
|
2016-08-10 09:57:29 -07:00
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"fmt"
|
2016-09-29 16:49:00 -07:00
|
|
|
"os"
|
|
|
|
|
"strings"
|
2016-08-10 09:57:29 -07:00
|
|
|
|
2017-09-14 09:40:34 -07:00
|
|
|
"k8s.io/client-go/kubernetes"
|
|
|
|
|
"k8s.io/client-go/rest"
|
|
|
|
|
|
2016-11-11 17:37:08 -08:00
|
|
|
"github.com/coreos/pkg/capnslog"
|
2016-09-29 16:49:00 -07:00
|
|
|
"github.com/spf13/cobra"
|
2017-08-23 22:20:14 -07:00
|
|
|
"github.com/spf13/pflag"
|
2016-09-29 16:49:00 -07:00
|
|
|
|
2017-12-04 08:33:19 -08:00
|
|
|
rookalpha "github.com/rook/rook/pkg/apis/rook.io/v1alpha1"
|
2017-12-04 08:37:39 -08:00
|
|
|
rookclient "github.com/rook/rook/pkg/client/clientset/versioned"
|
2016-10-26 00:38:39 +02:00
|
|
|
"github.com/rook/rook/pkg/clusterd"
|
2017-12-04 08:33:19 -08:00
|
|
|
"github.com/rook/rook/pkg/daemon/ceph/mon"
|
2017-02-01 15:41:46 -08:00
|
|
|
"github.com/rook/rook/pkg/util/exec"
|
2016-10-26 00:38:39 +02:00
|
|
|
"github.com/rook/rook/pkg/util/flags"
|
2017-08-23 22:20:14 -07:00
|
|
|
"github.com/rook/rook/pkg/version"
|
2017-09-14 09:40:34 -07:00
|
|
|
apiextensionsclient "k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset"
|
2017-08-23 22:20:14 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
const (
|
|
|
|
|
RookEnvVarPrefix = "ROOK"
|
2017-10-31 17:00:04 -07:00
|
|
|
terminationLog = "/dev/termination-log"
|
2016-08-10 09:57:29 -07:00
|
|
|
)
|
|
|
|
|
|
2016-09-29 16:49:00 -07:00
|
|
|
var rootCmd = &cobra.Command{
|
2017-09-27 14:01:29 -07:00
|
|
|
Use: "rook",
|
|
|
|
|
Hidden: true,
|
2016-09-29 16:49:00 -07:00
|
|
|
}
|
2017-01-27 10:48:26 -08:00
|
|
|
var cfg = &config{}
|
2017-02-01 11:00:09 -08:00
|
|
|
var clusterInfo mon.ClusterInfo
|
2016-09-29 16:49:00 -07:00
|
|
|
|
2016-11-11 17:37:08 -08:00
|
|
|
var logLevelRaw string
|
2017-06-25 10:19:59 -07:00
|
|
|
var logger = capnslog.NewPackageLogger("github.com/rook/rook", "rook")
|
2016-11-11 17:37:08 -08:00
|
|
|
|
2016-09-29 16:49:00 -07:00
|
|
|
type config struct {
|
2017-01-04 09:01:47 -08:00
|
|
|
nodeID string
|
|
|
|
|
discoveryURL string
|
|
|
|
|
etcdMembers string
|
|
|
|
|
devices string
|
2017-03-23 17:14:05 -07:00
|
|
|
directories string
|
2017-01-11 20:19:47 -08:00
|
|
|
metadataDevice string
|
2017-01-04 09:01:47 -08:00
|
|
|
dataDir string
|
|
|
|
|
forceFormat bool
|
|
|
|
|
location string
|
|
|
|
|
logLevel capnslog.LogLevel
|
|
|
|
|
cephConfigOverride string
|
2017-12-04 08:33:19 -08:00
|
|
|
storeConfig rookalpha.StoreConfig
|
2017-01-30 21:13:05 -08:00
|
|
|
networkInfo clusterd.NetworkInfo
|
2017-01-20 09:41:27 -08:00
|
|
|
monEndpoints string
|
2017-07-21 14:35:56 -07:00
|
|
|
nodeName string
|
2016-09-29 16:49:00 -07:00
|
|
|
}
|
|
|
|
|
|
2016-08-10 09:57:29 -07:00
|
|
|
func main() {
|
2016-09-29 16:49:00 -07:00
|
|
|
addCommands()
|
|
|
|
|
|
|
|
|
|
if err := rootCmd.Execute(); err != nil {
|
2017-06-25 10:19:59 -07:00
|
|
|
fmt.Printf("rook error: %+v\n", err)
|
2016-08-10 09:57:29 -07:00
|
|
|
}
|
|
|
|
|
}
|
2016-09-29 16:49:00 -07:00
|
|
|
|
|
|
|
|
// Initialize the configuration parameters. The precedence from lowest to highest is:
|
|
|
|
|
// 1) default value (at compilation)
|
2017-08-04 09:51:23 -07:00
|
|
|
// 2) environment variables (upper case, replace - with _, and rook prefix. For example, discovery-url is ROOK_DISCOVERY_URL)
|
2016-09-29 16:49:00 -07:00
|
|
|
// 3) command line parameter
|
|
|
|
|
func init() {
|
2016-11-11 17:37:08 -08:00
|
|
|
rootCmd.PersistentFlags().StringVar(&logLevelRaw, "log-level", "INFO", "logging level for logging/tracing output (valid values: CRITICAL,ERROR,WARNING,NOTICE,INFO,DEBUG,TRACE)")
|
2016-10-26 09:48:25 -07:00
|
|
|
|
2016-09-29 16:49:00 -07:00
|
|
|
// load the environment variables
|
2017-08-23 22:20:14 -07:00
|
|
|
flags.SetFlagsFromEnv(rootCmd.Flags(), RookEnvVarPrefix)
|
|
|
|
|
flags.SetFlagsFromEnv(rootCmd.PersistentFlags(), RookEnvVarPrefix)
|
2016-09-29 16:49:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func addCommands() {
|
|
|
|
|
rootCmd.AddCommand(versionCmd)
|
2017-01-05 14:03:21 -08:00
|
|
|
rootCmd.AddCommand(monCmd)
|
|
|
|
|
rootCmd.AddCommand(osdCmd)
|
2017-06-13 12:33:08 -07:00
|
|
|
rootCmd.AddCommand(mgrCmd)
|
2017-01-05 14:03:21 -08:00
|
|
|
rootCmd.AddCommand(rgwCmd)
|
|
|
|
|
rootCmd.AddCommand(mdsCmd)
|
2017-03-30 12:45:11 -07:00
|
|
|
rootCmd.AddCommand(apiCmd)
|
2017-09-14 09:40:34 -07:00
|
|
|
rootCmd.AddCommand(agentCmd)
|
2017-03-30 12:45:11 -07:00
|
|
|
rootCmd.AddCommand(operatorCmd)
|
|
|
|
|
}
|
|
|
|
|
|
2017-01-20 09:41:27 -08:00
|
|
|
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)
|
|
|
|
|
}
|
2017-02-01 15:41:46 -08:00
|
|
|
|
2017-08-23 22:20:14 -07:00
|
|
|
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")
|
|
|
|
|
logger.Infof("starting Rook %s with arguments '%s'", version.Version, strings.Join(os.Args, " "))
|
|
|
|
|
logger.Infof("flag values: %s", strings.Join(flagValues, ", "))
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-14 14:24:31 -07:00
|
|
|
func createContext() *clusterd.Context {
|
2017-02-01 15:41:46 -08:00
|
|
|
executor := &exec.CommandExecutor{}
|
2017-06-02 13:44:54 -07:00
|
|
|
return &clusterd.Context{
|
2017-02-01 15:41:46 -08:00
|
|
|
Executor: executor,
|
|
|
|
|
ConfigDir: cfg.dataDir,
|
|
|
|
|
ConfigFileOverride: cfg.cephConfigOverride,
|
|
|
|
|
LogLevel: cfg.logLevel,
|
2017-08-01 22:33:00 -07:00
|
|
|
NetworkInfo: cfg.networkInfo,
|
2017-02-01 15:41:46 -08:00
|
|
|
}
|
|
|
|
|
}
|
2017-09-14 09:40:34 -07:00
|
|
|
|
2017-12-04 08:37:39 -08:00
|
|
|
func getClientset() (kubernetes.Interface, apiextensionsclient.Interface, rookclient.Interface, error) {
|
2017-09-14 09:40:34 -07:00
|
|
|
// create the k8s client
|
|
|
|
|
config, err := rest.InClusterConfig()
|
|
|
|
|
if err != nil {
|
2017-12-04 08:37:39 -08:00
|
|
|
return nil, nil, nil, fmt.Errorf("failed to get k8s config. %+v", err)
|
2017-09-14 09:40:34 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
clientset, err := kubernetes.NewForConfig(config)
|
|
|
|
|
if err != nil {
|
2017-12-04 08:37:39 -08:00
|
|
|
return nil, nil, nil, fmt.Errorf("failed to create k8s clientset. %+v", err)
|
2017-09-14 09:40:34 -07:00
|
|
|
}
|
|
|
|
|
apiExtClientset, err := apiextensionsclient.NewForConfig(config)
|
|
|
|
|
if err != nil {
|
2017-12-04 08:37:39 -08:00
|
|
|
return nil, nil, nil, fmt.Errorf("failed to create k8s API extension clientset. %+v", err)
|
2017-09-14 09:40:34 -07:00
|
|
|
}
|
2017-12-04 08:37:39 -08:00
|
|
|
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
|
2017-09-14 09:40:34 -07:00
|
|
|
}
|
2017-10-31 17:00:04 -07:00
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
}
|