-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcmd.go
90 lines (72 loc) · 2.22 KB
/
cmd.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
package main
import (
"flag"
"fmt"
"os"
"path/filepath"
"github.com/EP4/kubernetes-ssh-container-exposer/internal/handlers"
internalLogger "github.com/EP4/kubernetes-ssh-container-exposer/internal/logger"
"github.com/EP4/kubernetes-ssh-container-exposer/internal/registry"
controller "github.com/philipgough/kube-kontroller"
"go.uber.org/zap"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
)
var logger, _ = zap.NewDevelopment()
const VERSION = "0.2.0"
func newClient(outOfCluster bool) (kubernetes.Interface, error) {
if !outOfCluster {
config, err := rest.InClusterConfig()
if err != nil {
return nil, err
}
return kubernetes.NewForConfig(config)
}
homeDir := func() string {
if h := os.Getenv("HOME"); h != "" {
return h
}
return os.Getenv("USERPROFILE") // windows
}
var kubeconfig *string
if home := homeDir(); home != "" {
kubeconfig = flag.String("kubeconfig", filepath.Join(home, ".kube", "config"), "(optional) absolute path to the kubeconfig file")
} else {
kubeconfig = flag.String("kubeconfig", "", "absolute path to the kubeconfig file")
}
flag.Parse()
config, err := clientcmd.BuildConfigFromFlags("", *kubeconfig)
if err != nil {
panic(err.Error())
}
return kubernetes.NewForConfig(config)
}
func initializeRegistry() (*registry.Registry, error) {
registry := registry.NewRegistry(logger)
if err := registry.ConnectDatabase(); err != nil {
return nil, err
}
if err := registry.TruncateAll(); err != nil {
return nil, err
}
return registry, nil
}
func main() {
logger.Info("Started", zap.String("version", VERSION))
logger.WithOptions()
registry, err := initializeRegistry()
if err != nil {
logger.Fatal(fmt.Sprintf("failed to initialize registry - %v", err.Error()))
}
kubeClient, err := newClient(false)
if err != nil {
logger.Fatal(fmt.Sprintf("failed to create Kubernetes client - %v", err.Error()))
}
ctrlLogger := internalLogger.NewLogger(logger)
ctrl := controller.NewSecretController(kubeClient, controller.GetDefaultOptions(), controller.GetDefaultListOpts(), ctrlLogger)
ctrl.SetHandlerFactory(handlers.NewSecretHandler(kubeClient, registry, logger))
stopCh := make(chan struct{})
go ctrl.Run(stopCh)
<-stopCh
}