This repository has been archived by the owner on Dec 1, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
root.go
97 lines (78 loc) · 2.3 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
92
93
94
95
96
97
package cmd
import (
"flag"
"fmt"
"github.com/juju/loggo"
"github.com/soheilhy/cmux"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/samsung-cnct/cma-aks/pkg/apiserver"
"github.com/samsung-cnct/cma-aks/pkg/util"
"net"
"os"
"strings"
"sync"
)
var (
logger loggo.Logger
RootCmd = &cobra.Command{
Use: "cma-aks",
Short: "The CMA AKS Helper",
Long: `The CMA AKS Helper
Running this by itself will invoke the webserver to run.
See subcommands for additional features`,
Run: func(cmd *cobra.Command, args []string) {
runWebServer()
},
TraverseChildren: true,
}
)
func runWebServer() {
logger := util.GetModuleLogger("cmd.cmaaks", loggo.INFO)
// get flags
portNumber := viper.GetInt("port")
var wg sync.WaitGroup
stop := make(chan struct{})
logger.Infof("Creating Web Server")
tcpMux := createWebServer(&apiserver.ServerOptions{PortNumber: portNumber})
wg.Add(1)
go func() {
defer wg.Done()
logger.Infof("Starting to serve requests on port %d", portNumber)
tcpMux.Serve()
}()
<-stop
logger.Infof("Waiting for controllers to shut down gracefully")
wg.Wait()
}
func createWebServer(options *apiserver.ServerOptions) cmux.CMux {
conn, err := net.Listen("tcp", fmt.Sprintf(":%d", options.PortNumber))
if err != nil {
panic(err)
}
tcpMux := cmux.New(conn)
apiserver.AddServersToMux(tcpMux, options)
return tcpMux
}
func init() {
viper.SetEnvPrefix("cmaaks")
replacer := strings.NewReplacer("-", "_")
viper.SetEnvKeyReplacer(replacer)
// using standard library "flag" package
RootCmd.Flags().Int("port", 9030, "Port to listen on")
RootCmd.PersistentFlags().String("kubeconfig", "", "Location of kubeconfig file")
RootCmd.PersistentFlags().String("kubectl", "kubectl", "Location of kubectl file")
RootCmd.PersistentFlags().String("kubernetes-namespace", "default", "What namespace to operate on")
viper.BindPFlag("port", RootCmd.Flags().Lookup("port"))
viper.BindPFlag("kubeconfig", RootCmd.PersistentFlags().Lookup("kubeconfig"))
viper.BindPFlag("kubectl", RootCmd.PersistentFlags().Lookup("kubectl"))
viper.BindPFlag("kubernetes-namespace", RootCmd.PersistentFlags().Lookup("kubernetes-namespace"))
viper.AutomaticEnv()
RootCmd.Flags().AddGoFlagSet(flag.CommandLine)
}
func Execute() {
if err := RootCmd.Execute(); err != nil {
fmt.Println(err)
os.Exit(1)
}
}