forked from CrunchyData/postgres-operator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
postgres-operator.go
132 lines (107 loc) · 4.25 KB
/
postgres-operator.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
package main
/*
Copyright 2017 - 2020 Crunchy Data
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.
*/
import (
"fmt"
"os"
"time"
"github.com/kubernetes/sample-controller/pkg/signals"
"github.com/crunchydata/postgres-operator/config"
"github.com/crunchydata/postgres-operator/controller"
"github.com/crunchydata/postgres-operator/controller/manager"
nscontroller "github.com/crunchydata/postgres-operator/controller/namespace"
crunchylog "github.com/crunchydata/postgres-operator/logging"
"github.com/crunchydata/postgres-operator/ns"
"github.com/crunchydata/postgres-operator/operator/operatorupgrade"
log "github.com/sirupsen/logrus"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
kubeinformers "k8s.io/client-go/informers"
"k8s.io/client-go/kubernetes"
"github.com/crunchydata/postgres-operator/kubeapi"
"github.com/crunchydata/postgres-operator/operator"
)
func main() {
debugFlag := os.Getenv("CRUNCHY_DEBUG")
//add logging configuration
crunchylog.CrunchyLogger(crunchylog.SetParameters())
if debugFlag == "true" {
log.SetLevel(log.DebugLevel)
log.Debug("debug flag set to true")
} else {
log.Info("debug flag set to false")
}
//give time for pgo-event to start up
time.Sleep(time.Duration(5) * time.Second)
clients, err := kubeapi.NewControllerClients()
if err != nil {
log.Error(err)
os.Exit(2)
}
kubeClientset := clients.Kubeclientset
pgoRESTclient := clients.PGORestclient
operator.Initialize(kubeClientset)
// Configure namespaces for the Operator. This includes determining the namespace
// operating mode, creating/updating namespaces (if permitted), and obtaining a valid
// list of target namespaces for the operator install
namespaceList, err := operator.SetupNamespaces(kubeClientset)
if err != nil {
log.Errorf("Error configuring operator namespaces: %w", err)
os.Exit(2)
}
// set up signals so we handle the first shutdown signal gracefully
stopCh := signals.SetupSignalHandler()
// create a new controller manager with controllers for all current namespaces and then run
// all of those controllers
controllerManager, err := manager.NewControllerManager(namespaceList)
if err != nil {
log.Error(err)
os.Exit(2)
}
controllerManager.RunAll()
log.Debug("controller manager created and all included controllers are now running")
// if the namespace operating mode is not 'disabled', then create and start a namespace
// controller
if operator.NamespaceOperatingMode() != ns.NamespaceOperatingModeDisabled {
if err := createAndStartNamespaceController(kubeClientset, controllerManager,
stopCh); err != nil {
log.Error(err)
os.Exit(2)
}
}
defer controllerManager.StopAll()
operatorupgrade.OperatorUpdateCRPgoVersion(kubeClientset, pgoRESTclient, namespaceList)
log.Info("PostgreSQL Operator initialized and running, waiting for signal to exit")
<-stopCh
log.Infof("Signal received, now exiting")
}
// createAndStartNamespaceController creates a namespace controller and then starts it
func createAndStartNamespaceController(kubeClientset *kubernetes.Clientset,
controllerManager controller.ManagerInterface, stopCh <-chan struct{}) error {
nsKubeInformerFactory := kubeinformers.NewSharedInformerFactoryWithOptions(kubeClientset,
operator.NamespaceRefreshInterval,
kubeinformers.WithTweakListOptions(func(options *metav1.ListOptions) {
options.LabelSelector = fmt.Sprintf("%s=%s,%s=%s",
config.LABEL_VENDOR, config.LABEL_CRUNCHY,
config.LABEL_PGO_INSTALLATION_NAME, operator.InstallationName)
}))
nsController, err := nscontroller.NewNamespaceController(controllerManager,
nsKubeInformerFactory.Core().V1().Namespaces())
if err != nil {
return err
}
nsController.AddNamespaceEventHandler()
// start the namespace controller
nsKubeInformerFactory.Start(stopCh)
log.Debug("namespace controller is now running")
return nil
}