-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroot.go
91 lines (75 loc) · 2.78 KB
/
root.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
package cmd
import (
"fmt"
"os"
"github.com/balchua/humpback/controller"
"github.com/balchua/humpback/handler"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
)
var clientset *kubernetes.Clientset
var appParams handler.AppParameters
var rootCmd = &cobra.Command{
Use: "humpback",
Short: "Humpback is a helper command to deploy a pod and monitor till its completion.",
Long: `Humpback is a helper command to deploy a pod and monitor till its completion. This is useful to integrate with external job orchestration tool.`,
Run: func(cmd *cobra.Command, args []string) {
logrus.Info("Calling getClient()")
clientset, _ = getClient()
jobHandler := handler.Init(appParams, clientset)
jobHandler.Schedule()
if jobHandler.PodScheduled {
listOptions := metav1.ListOptions{
LabelSelector: jobHandler.Selector,
}
controller.Start(clientset, appParams.Namespace, listOptions)
} else {
logrus.Errorf("Unable to find app")
os.Exit(1)
}
},
}
func init() {
appParams = handler.AppParameters{}
logrus.SetFormatter(&logrus.TextFormatter{
FullTimestamp: true})
// Output to stdout instead of the default stderr, could also be a file.
logrus.SetOutput(os.Stdout)
// Only log the warning severity or above.
logrus.SetLevel(logrus.DebugLevel)
rootCmd.PersistentFlags().StringVarP(&appParams.Application, "application", "a", "", "The application to run.")
rootCmd.MarkPersistentFlagRequired("application")
rootCmd.PersistentFlags().StringVarP(&appParams.Command, "command", "c", "", "The Job to run.")
rootCmd.MarkPersistentFlagRequired("command")
rootCmd.PersistentFlags().StringVarP(&appParams.Namespace, "namespace", "n", "", "Deploy Pod to which namespace.")
rootCmd.MarkPersistentFlagRequired("namespace")
rootCmd.PersistentFlags().StringVarP(&appParams.KubeConfig, "kubeconfig", "k", "", "Kubernetes Configuration to use, when running outside the cluster.")
rootCmd.PersistentFlags().StringVarP(&appParams.ConfigPath, "appconfig-path", "p", "", "The path to the application config, i.e humpback.yaml. Do not include the filename, just the directory.")
}
func getClient() (*kubernetes.Clientset, error) {
var config *rest.Config
var err error
if appParams.KubeConfig == "" {
logrus.Debug("Using in cluster config")
config, err = rest.InClusterConfig()
// in cluster access
} else {
logrus.Debug("Using out of cluster config")
config, err = clientcmd.BuildConfigFromFlags("", appParams.KubeConfig)
}
if err != nil {
return nil, err
}
return kubernetes.NewForConfig(config)
}
//Execute this marks the entry point of execution
func Execute() {
if err := rootCmd.Execute(); err != nil {
fmt.Println(err)
os.Exit(1)
}
}