forked from HYDPublic/ops-kube-db-operator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
135 lines (109 loc) · 3.42 KB
/
main.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package main
import (
"flag"
"os"
"strings"
"github.com/golang/glog"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
clientset "github.com/MYOB-Technology/ops-kube-db-operator/pkg/client/clientset/versioned"
"github.com/MYOB-Technology/ops-kube-db-operator/pkg/controller"
"time"
"github.com/MYOB-Technology/ops-kube-db-operator/pkg/client/informers/externalversions"
"github.com/MYOB-Technology/ops-kube-db-operator/pkg/k8s"
"github.com/MYOB-Technology/ops-kube-db-operator/pkg/rds"
"github.com/MYOB-Technology/ops-kube-db-operator/pkg/signals"
"github.com/MYOB-Technology/ops-kube-db-operator/pkg/worker"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
rds2 "github.com/aws/aws-sdk-go/service/rds"
"github.com/aws/aws-sdk-go/service/rds/rdsiface"
)
var kubeconfig string
var region string
var subnetGroup string
var sgIDs []*string
var nsSuffix string
func main() {
if subnetGroup == "" {
glog.Fatalf("please provide a subnet group")
}
if len(sgIDs) == 0 {
glog.Fatalf("please provide a comma separated list of security group ids with at least one id.")
}
// set up signals so we handle the first shutdown signal gracefully
stopCh := signals.SetupSignalHandler()
var config *rest.Config
var err error
// if flag has not been passed and env not set, presume running in cluster
if kubeconfig != "" {
glog.Infof("using kubeconfig %v", kubeconfig)
config, err = clientcmd.BuildConfigFromFlags("", kubeconfig)
} else {
glog.Infof("running inside cluster")
config, err = rest.InClusterConfig()
}
if nil != err {
glog.Errorf("error creating config, %v", err)
return
}
k8sClient, err := kubernetes.NewForConfig(config)
if err != nil {
glog.Fatalf("error building k8s clientset: %s", err.Error())
}
crdClient, err := clientset.NewForConfig(config)
if err != nil {
glog.Fatalf("error building CRD clientset: %s", err.Error())
}
rdsClient, err := getRDSClient()
if err != nil {
glog.Fatalf("error cannot get rds client: %s", err.Error())
}
rdsConfig := rds.NewRDSTransformerConfig(&subnetGroup, sgIDs)
rdsTransformer := rds.NewBumblebee(rdsConfig)
wrkr := worker.NewDBWorker(
rds.NewRDSImpure(rdsClient, rdsTransformer),
k8s.NewStoreCreds(k8sClient),
k8s.NewMetricsExporter(k8sClient),
worker.NewConfig(100000, nsSuffix),
worker.NewPostgresDBValidator(),
worker.NewLogger(),
worker.NewOptimus(),
k8s.NewCRDClient(crdClient),
)
factory := externalversions.NewSharedInformerFactory(crdClient, time.Second*30)
go factory.Start(stopCh)
crdController := controller.New(factory, wrkr)
crdController.Run(stopCh)
}
func getRDSClient() (rdsiface.RDSAPI, error) {
c := aws.NewConfig().WithRegion(region)
s, err := session.NewSession(c)
if err != nil {
return nil, err
}
return rds2.New(s), nil
}
func init() {
flag.StringVar(&kubeconfig, "kubeconfig", "", "kubeconfig file")
flag.StringVar(&nsSuffix, "ns-suffix", "", "namespace suffix (or env NS_SUFFIX)")
flag.Parse()
// if no flag has been passed, read kubeconfig file from environment
if kubeconfig == "" {
kubeconfig = os.Getenv("KUBECONFIG")
}
region = os.Getenv("AWS_REGION")
subnetGroup = os.Getenv("DB_SUBNET_GROUP")
sgList := os.Getenv("DB_SECURITY_GROUP_IDS")
if region == "" {
region = "ap-southeast-2"
}
t := strings.Split(sgList, ",")
for _, v := range t {
sgIDs = append(sgIDs, &v)
}
if nsSuffix == "" {
nsSuffix = os.Getenv("NS_SUFFIX")
}
}