forked from CrunchyData/postgres-operator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
runtime.go
87 lines (69 loc) · 2.82 KB
/
runtime.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
package runtime
/*
Copyright 2021 - 2022 Crunchy Data Solutions, Inc.
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 (
"time"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/kubernetes/scheme"
"k8s.io/client-go/rest"
"sigs.k8s.io/controller-runtime/pkg/client/config"
"sigs.k8s.io/controller-runtime/pkg/manager"
"github.com/adifri/postgres-operator/v5/pkg/apis/postgres-operator.crunchydata.com/v1beta1"
)
// default refresh interval in minutes
var refreshInterval = 60 * time.Minute
// CreateRuntimeManager creates a new controller runtime manager for the PostgreSQL Operator. The
// manager returned is configured specifically for the PostgreSQL Operator, and includes any
// controllers that will be responsible for managing PostgreSQL clusters using the
// 'postgrescluster' custom resource. Additionally, the manager will only watch for resources in
// the namespace specified, with an empty string resulting in the manager watching all namespaces.
func CreateRuntimeManager(namespace string, config *rest.Config,
disableMetrics bool) (manager.Manager, error) {
pgoScheme, err := CreatePostgresOperatorScheme()
if err != nil {
return nil, err
}
options := manager.Options{
Namespace: namespace, // if empty then watching all namespaces
SyncPeriod: &refreshInterval,
Scheme: pgoScheme,
}
if disableMetrics {
options.HealthProbeBindAddress = "0"
options.MetricsBindAddress = "0"
}
// create controller runtime manager
mgr, err := manager.New(config, options)
if err != nil {
return nil, err
}
return mgr, nil
}
// GetConfig creates a *rest.Config for talking to a Kubernetes API server.
func GetConfig() (*rest.Config, error) { return config.GetConfig() }
// CreatePostgresOperatorScheme creates a scheme containing the resource types required by the
// PostgreSQL Operator. This includes any custom resource types specific to the PostgreSQL
// Operator, as well as any standard Kubernetes resource types.
func CreatePostgresOperatorScheme() (*runtime.Scheme, error) {
// create a new scheme specifically for this manager
pgoScheme := runtime.NewScheme()
// add standard resource types to the scheme
if err := scheme.AddToScheme(pgoScheme); err != nil {
return nil, err
}
// add custom resource types to the default scheme
if err := v1beta1.AddToScheme(pgoScheme); err != nil {
return nil, err
}
return pgoScheme, nil
}