Skip to content

Commit 00d85c4

Browse files
arni-wxnclemstra.bhitman99pcallewaertTomas Adomavicius
authored
Pull from upstream fork (#2)
* Added host and database_name to Kubernetes secret * Fixed indentation * WIP: operator-sdk update from v0.14.0 to v0.17.1 (movetokube#55) * operator-sdk update from v0.14.0 to v0.15.2 * operator-sdk update from v0.15.2 to v0.16.0 * operator-sdk update from v0.16.0 to v0.17.1 * operator-sdk update from v0.14.0 to v0.17.1 (movetokube#55) * operator-sdk update from v0.14.0 to v0.15.2 * operator-sdk update from v0.15.2 to v0.16.0 * operator-sdk update from v0.16.0 to v0.17.1 * Override DropRole method for Azure (movetokube#62) * Override DropRole method for Azure, the newOwner can be an invalid format for postgres * Refactor * Create FUNDING.yml * update readme with contribution notes (movetokube#64) Co-authored-by: Tomas Adomavicius <tomas.adomavicius@devbridge.com> * change base image to ubi8 (movetokube#59) update container base image to ubi8 * use ubuntu-latest instead of ubuntu-16 (movetokube#68) * add JDBC compliant URI to kubernetes secret (movetokube#69) * Add generated secret details to README.md * Add optional annotations (movetokube#66) Co-authored-by: lemstra.b <lemstra.b@tkppensioen.nl> Co-authored-by: Tomas <admin@hitman.lt> Co-authored-by: Tomas <tomas@adomavicius.com> Co-authored-by: Pieter C <pieter.callewaert@nipro-group.com> Co-authored-by: Tomas Adomavicius <tomas.adomavicius@devbridge.com> Co-authored-by: eapri <a@pouli.nl> Co-authored-by: Steve Boardwell <steve.boardwell@datameer.com>
1 parent a2ab99b commit 00d85c4

File tree

520 files changed

+71511
-57310
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

520 files changed

+71511
-57310
lines changed

.github/FUNDING.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# These are supported funding model platforms
2+
3+
github: [hitman99]

.github/workflows/test.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,23 @@ on: [push, pull_request]
44

55
jobs:
66
test:
7-
runs-on: ubuntu-16.04
7+
runs-on: ubuntu-latest
88
name: Go test
99
steps:
1010
- uses: actions/checkout@master
1111
- uses: actions/setup-go@v1
1212
with:
13-
go-version: '1.13.3'
13+
go-version: '1.15.2'
1414
- run: |
1515
go test ./...
1616
build:
17-
runs-on: ubuntu-16.04
17+
runs-on: ubuntu-latest
1818
name: Go build
1919
steps:
2020
- uses: actions/checkout@master
2121
- uses: actions/setup-go@v1
2222
with:
23-
go-version: '1.13.3'
23+
go-version: '1.15.2'
2424
- run: |
2525
go build -mod=vendor -o operator github.com/movetokube/postgres-operator/cmd/manager
2626
file operator

README.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,3 +95,37 @@ This creates a user role `username-<hash>` and grants role `test-db-group`, `tes
9595
`PostgresUser` needs to reference a `Postgres` in the same namespace.
9696

9797
Two `Postgres` referencing the same database can exist in more than one namespace. The last CR referencing a database will drop the group role and transfer database ownership to the role used by the operator.
98+
Every PostgresUser has a generated Kubernetes secret attached to it, which contains the following data (i.e.):
99+
100+
| Key | Comment |
101+
|----------------------|---------------------|
102+
| `DATABASE_NAME` | Name of the database, same as in `Postgres` CR, copied for convenience |
103+
| `HOST` | PostgreSQL server host |
104+
| `PASSWORD` | Autogenerated password for user |
105+
| `ROLE` | Autogenerated role with login enabled (user) |
106+
| `LOGIN` | Same as `ROLE`. In case `POSTGRES_CLOUD_PROVIDER` is set to "Azure", `LOGIN` it will be set to `{role}@{serverName}`, serverName is extracted from `POSTGRES_USER` from operator's config. |
107+
| `POSTGRES_URL` | Connection string for Posgres, could be used for Go applications |
108+
| `POSTGRES_JDBC_URL` | JDBC compatible Postgres URI, formatter as `jdbc:postgresql://{POSTGRES_HOST}/{DATABASE_NAME}` |
109+
110+
111+
### Contribution
112+
You can contribute to this project by opening a PR to merge to `master`, or one of the `vX.X.X` branches.
113+
#### Branching
114+
`master` branch contains the latest source code with all the features. `vX.X.X` contains code for the specific major versions.
115+
i.e. `v0.4.x` contains the latest code for 0.4 version of the operator. See compatibility matrix below.
116+
117+
#### Tests
118+
Please write tests and fix any broken tests before you open a PR. Tests should cover at least 80% of your code.
119+
120+
### Compatibility
121+
Postgres operator uses Operator SDK, which uses kubernetes client. Kubernetes client compatibility with Kubernetes cluster
122+
can be found [here](https://github.com/kubernetes/client-go/blob/master/README.md#compatibility-matrix)
123+
124+
Postgres operator compatibility with Operator SDK version is in the table below
125+
126+
| | Operator SDK 0.17.x | Operator SDK 1.9.0 |
127+
|-------------------------------|---------------------|--------------------|
128+
| `postgres-operator 0.4.x` | ✓ | - |
129+
| `postgres-operator 1.0.x` | - | ✓ |
130+
| `HEAD` | ✓ | - |
131+

build/Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
FROM registry.access.redhat.com/ubi7/ubi-minimal:latest
1+
FROM registry.access.redhat.com/ubi8/ubi-minimal:latest
22

33
ENV OPERATOR=/usr/local/bin/postgres-operator \
44
USER_UID=1001 \

cmd/manager/main.go

Lines changed: 80 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,18 @@ package main
22

33
import (
44
"context"
5+
"errors"
56
"flag"
67
"fmt"
8+
kubemetrics "github.com/operator-framework/operator-sdk/pkg/kube-metrics"
9+
"github.com/operator-framework/operator-sdk/pkg/metrics"
10+
v1 "k8s.io/api/core/v1"
11+
"k8s.io/apimachinery/pkg/util/intstr"
12+
"k8s.io/client-go/rest"
713
"os"
814
"runtime"
15+
"sigs.k8s.io/controller-runtime/pkg/cache"
16+
"strings"
917

1018
// Import all Kubernetes client auth plugins (e.g. Azure, GCP, OIDC, etc.)
1119
_ "k8s.io/client-go/plugin/pkg/client/auth"
@@ -16,12 +24,8 @@ import (
1624
"github.com/operator-framework/operator-sdk/pkg/k8sutil"
1725
"github.com/operator-framework/operator-sdk/pkg/leader"
1826
"github.com/operator-framework/operator-sdk/pkg/log/zap"
19-
"github.com/operator-framework/operator-sdk/pkg/metrics"
20-
"github.com/operator-framework/operator-sdk/pkg/restmapper"
2127
sdkVersion "github.com/operator-framework/operator-sdk/version"
2228
"github.com/spf13/pflag"
23-
v1 "k8s.io/api/core/v1"
24-
"k8s.io/apimachinery/pkg/util/intstr"
2529
"sigs.k8s.io/controller-runtime/pkg/client/config"
2630
"sigs.k8s.io/controller-runtime/pkg/manager"
2731
logf "sigs.k8s.io/controller-runtime/pkg/runtime/log"
@@ -87,12 +91,23 @@ func main() {
8791
os.Exit(1)
8892
}
8993

90-
// Create a new Cmd to provide shared dependencies and start components
91-
mgr, err := manager.New(cfg, manager.Options{
94+
// Set default manager options
95+
options := manager.Options{
9296
Namespace: namespace,
93-
MapperProvider: restmapper.NewDynamicRESTMapper,
9497
MetricsBindAddress: fmt.Sprintf("%s:%d", metricsHost, metricsPort),
95-
})
98+
}
99+
100+
// Add support for MultiNamespace set in WATCH_NAMESPACE (e.g ns1,ns2)
101+
// Note that this is not intended to be used for excluding namespaces, this is better done via a Predicate
102+
// Also note that you may face performance issues when using this with a high number of namespaces.
103+
// More Info: https://godoc.org/github.com/kubernetes-sigs/controller-runtime/pkg/cache#MultiNamespacedCacheBuilder
104+
if strings.Contains(namespace, ",") {
105+
options.Namespace = ""
106+
options.NewCache = cache.MultiNamespacedCacheBuilder(strings.Split(namespace, ","))
107+
}
108+
109+
// Create a new manager to provide shared dependencies and start components
110+
mgr, err := manager.New(cfg, options)
96111
if err != nil {
97112
log.Error(err, "")
98113
os.Exit(1)
@@ -112,11 +127,42 @@ func main() {
112127
os.Exit(1)
113128
}
114129

130+
// Add the Metrics Service
131+
addMetrics(ctx, cfg)
132+
133+
log.Info("Starting the Cmd.")
134+
135+
// Start the Cmd
136+
if err := mgr.Start(signals.SetupSignalHandler()); err != nil {
137+
log.Error(err, "Manager exited non-zero")
138+
os.Exit(1)
139+
}
140+
}
141+
142+
// addMetrics will create the Services and Service Monitors to allow the operator export the metrics by using
143+
// the Prometheus operator
144+
// addMetrics will create the Services and Service Monitors to allow the operator export the metrics by using
145+
// the Prometheus operator
146+
func addMetrics(ctx context.Context, cfg *rest.Config) {
147+
// Get the namespace the operator is currently deployed in.
148+
operatorNs, err := k8sutil.GetOperatorNamespace()
149+
if err != nil {
150+
if errors.Is(err, k8sutil.ErrRunLocal) {
151+
log.Info("Skipping CR metrics server creation; not running in a cluster.")
152+
return
153+
}
154+
}
155+
156+
if err := serveCRMetrics(cfg, operatorNs); err != nil {
157+
log.Info("Could not generate and serve custom resource metrics", "error", err.Error())
158+
}
159+
115160
// Add to the below struct any other metrics ports you want to expose.
116161
servicePorts := []v1.ServicePort{
117162
{Port: metricsPort, Name: metrics.OperatorPortName, Protocol: v1.ProtocolTCP, TargetPort: intstr.IntOrString{Type: intstr.Int, IntVal: metricsPort}},
118163
{Port: operatorMetricsPort, Name: metrics.CRPortName, Protocol: v1.ProtocolTCP, TargetPort: intstr.IntOrString{Type: intstr.Int, IntVal: operatorMetricsPort}},
119164
}
165+
120166
// Create Service object to expose the metrics port(s).
121167
service, err := metrics.CreateMetricsService(ctx, cfg, servicePorts)
122168
if err != nil {
@@ -126,7 +172,9 @@ func main() {
126172
// CreateServiceMonitors will automatically create the prometheus-operator ServiceMonitor resources
127173
// necessary to configure Prometheus to scrape metrics from this operator.
128174
services := []*v1.Service{service}
129-
_, err = metrics.CreateServiceMonitors(cfg, namespace, services)
175+
176+
// The ServiceMonitor is created in the same namespace where the operator is deployed
177+
_, err = metrics.CreateServiceMonitors(cfg, operatorNs, services)
130178
if err != nil {
131179
log.Info("Could not create ServiceMonitor object", "error", err.Error())
132180
// If this operator is deployed to a cluster without the prometheus-operator running, it will return
@@ -135,12 +183,30 @@ func main() {
135183
log.Info("Install prometheus-operator in your cluster to create ServiceMonitor objects", "error", err.Error())
136184
}
137185
}
186+
}
138187

139-
log.Info("Starting the Cmd.")
188+
// serveCRMetrics gets the Operator/CustomResource GVKs and generates metrics based on those types.
189+
// It serves those metrics on "http://metricsHost:operatorMetricsPort".
190+
func serveCRMetrics(cfg *rest.Config, operatorNs string) error {
191+
// The function below returns a list of filtered operator/CR specific GVKs. For more control, override the GVK list below
192+
// with your own custom logic. Note that if you are adding third party API schemas, probably you will need to
193+
// customize this implementation to avoid permissions issues.
194+
filteredGVK, err := k8sutil.GetGVKsFromAddToScheme(apis.AddToScheme)
195+
if err != nil {
196+
return err
197+
}
140198

141-
// Start the Cmd
142-
if err := mgr.Start(signals.SetupSignalHandler()); err != nil {
143-
log.Error(err, "Manager exited non-zero")
144-
os.Exit(1)
199+
// The metrics will be generated from the namespaces which are returned here.
200+
// NOTE that passing nil or an empty list of namespaces in GenerateAndServeCRMetrics will result in an error.
201+
ns, err := kubemetrics.GetNamespacesForMetrics(operatorNs)
202+
if err != nil {
203+
return err
204+
}
205+
206+
// Generate and serve custom resource specific metrics.
207+
err = kubemetrics.GenerateAndServeCRMetrics(cfg, ns, filteredGVK, metricsHost, operatorMetricsPort)
208+
if err != nil {
209+
return err
145210
}
211+
return nil
146212
}

deploy/crds/db.movetokube.com_postgres_crd.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,12 @@ spec:
1919
apiVersion:
2020
description: 'APIVersion defines the versioned schema of this representation
2121
of an object. Servers should convert recognized schemas to the latest
22-
internal value, and may reject unrecognized values. More info: https://git.k8s.io/community/contributors/devel/api-conventions.md#resources'
22+
internal value, and may reject unrecognized values. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources'
2323
type: string
2424
kind:
2525
description: 'Kind is a string value representing the REST resource this
2626
object represents. Servers may infer this from the endpoint the client
27-
submits requests to. Cannot be updated. In CamelCase. More info: https://git.k8s.io/community/contributors/devel/api-conventions.md#types-kinds'
27+
submits requests to. Cannot be updated. In CamelCase. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds'
2828
type: string
2929
metadata:
3030
type: object

deploy/crds/db.movetokube.com_postgresusers_crd.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,12 @@ spec:
1919
apiVersion:
2020
description: 'APIVersion defines the versioned schema of this representation
2121
of an object. Servers should convert recognized schemas to the latest
22-
internal value, and may reject unrecognized values. More info: https://git.k8s.io/community/contributors/devel/api-conventions.md#resources'
22+
internal value, and may reject unrecognized values. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources'
2323
type: string
2424
kind:
2525
description: 'Kind is a string value representing the REST resource this
2626
object represents. Servers may infer this from the endpoint the client
27-
submits requests to. Cannot be updated. In CamelCase. More info: https://git.k8s.io/community/contributors/devel/api-conventions.md#types-kinds'
27+
submits requests to. Cannot be updated. In CamelCase. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds'
2828
type: string
2929
metadata:
3030
type: object

0 commit comments

Comments
 (0)