Skip to content

Commit 8b68b8b

Browse files
author
Jonathan S. Katz
committed
Allow for Operator generation of deafult pgBackRest configuration
The "pgo-backrest-repo-config" consists of several default files that can be generated when the Operator is initially loaded (and reconciled on initialization as well). This removes additional manual steps required when deploying the Operator through methods such as OLM, making it easier to get started.
1 parent 19a8b2c commit 8b68b8b

File tree

9 files changed

+126
-83
lines changed

9 files changed

+126
-83
lines changed

build/postgres-operator/Dockerfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ RUN if [ "$DFSET" = "rhel" ] ; then \
2828
fi
2929

3030
ADD bin/postgres-operator /usr/local/bin
31+
ADD installers/ansible/roles/pgo-operator/files/pgo-backrest-repo /default-pgo-backrest-repo
3132
ADD installers/ansible/roles/pgo-operator/files/pgo-configs /default-pgo-config
3233
ADD conf/postgres-operator/pgo.yaml /default-pgo-config/pgo.yaml
3334

deploy/deploy.sh

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -46,14 +46,14 @@ fi
4646
pgbackrest_aws_s3_key=$(awsKeySecret "aws-s3-key")
4747
pgbackrest_aws_s3_key_secret=$(awsKeySecret "aws-s3-key-secret")
4848

49-
$PGO_CMD --namespace=$PGO_OPERATOR_NAMESPACE create secret generic pgo-backrest-repo-config \
50-
--from-file=config=${PGO_CONF_DIR}/pgo-backrest-repo/config \
51-
--from-file=sshd_config=${PGO_CONF_DIR}/pgo-backrest-repo/sshd_config \
52-
--from-file=aws-s3-ca.crt=${PGO_CONF_DIR}/pgo-backrest-repo/aws-s3-ca.crt \
53-
--from-literal=aws-s3-key="${pgbackrest_aws_s3_key}" \
54-
--from-literal=aws-s3-key-secret="${pgbackrest_aws_s3_key_secret}"
55-
$PGO_CMD --namespace=$PGO_OPERATOR_NAMESPACE label secret pgo-backrest-repo-config \
56-
vendor=crunchydata
49+
if [[ ! -z $pgbackrest_aws_s3_key ]] || [[ ! -z $pgbackrest_aws_s3_key_secret ]]
50+
then
51+
$PGO_CMD --namespace=$PGO_OPERATOR_NAMESPACE create secret generic pgo-backrest-repo-config \
52+
--from-literal=aws-s3-key="${pgbackrest_aws_s3_key}" \
53+
--from-literal=aws-s3-key-secret="${pgbackrest_aws_s3_key_secret}"
54+
$PGO_CMD --namespace=$PGO_OPERATOR_NAMESPACE label secret pgo-backrest-repo-config \
55+
vendor=crunchydata
56+
fi
5757

5858
#
5959
# credentials for pgo-apiserver TLS REST API

docs/content/installation/other/operator-hub.md

Lines changed: 3 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -15,44 +15,14 @@ that is available in OperatorHub.io.
1515

1616
## Before You Begin
1717

18-
There are a few manual steps that the cluster administrator must perform prior to installing PGO.
19-
At the very least, it must be provided with an initial configuration.
18+
There are some optional Secrets you can add before installing the PostgreSQL Operator into your cluster.
2019

21-
First, make sure OLM and the OperatorHub.io catalog are installed by running
22-
`kubectl get CatalogSources --all-namespaces`. You should see something similar to the following:
20+
### Secrets (optional)
2321

24-
```
25-
NAMESPACE NAME DISPLAY TYPE PUBLISHER
26-
olm operatorhubio-catalog Community Operators grpc OperatorHub.io
27-
```
28-
29-
Take note of the name and namespace above, you will need them later on.
30-
31-
Next, select a namespace in which to install the PostgreSQL Operator. PostgreSQL clusters will also be deployed here.
32-
If it does not exist, create it now.
33-
34-
```
35-
export PGO_OPERATOR_NAMESPACE=pgo
36-
kubectl create namespace "$PGO_OPERATOR_NAMESPACE"
37-
```
38-
39-
Next, clone the PostgreSQL Operator repository locally.
40-
41-
```
42-
git clone -b v{{< param operatorVersion >}} https://github.com/CrunchyData/postgres-operator.git
43-
cd postgres-operator
44-
```
45-
46-
### Secrets
47-
48-
Configure pgBackRest for your environment. If you do not plan to use AWS S3 to store backups, you can omit
49-
the `aws-s3` keys below.
22+
If you plan to use AWS S3 to store backups and would like to have the keys available for every backup, you can create a Secret as described below:
5023

5124
```
5225
kubectl -n "$PGO_OPERATOR_NAMESPACE" create secret generic pgo-backrest-repo-config \
53-
--from-file=./installers/ansible/roles/pgo-operator/files/pgo-backrest-repo/config \
54-
--from-file=./installers/ansible/roles/pgo-operator/files/pgo-backrest-repo/sshd_config \
55-
--from-file=./installers/ansible/roles/pgo-operator/files/pgo-backrest-repo/aws-s3-ca.crt \
5626
--from-literal=aws-s3-key="<your-aws-s3-key>" \
5727
--from-literal=aws-s3-key-secret="<your-aws-s3-key-secret>"
5828
kubectl -n "$PGO_OPERATOR_NAMESPACE" label secret pgo-backrest-repo-config \
@@ -71,9 +41,6 @@ kubectl -n "$PGO_OPERATOR_NAMESPACE" create secret tls pgo.tls \
7141
--key=/path/to/server.key
7242
```
7343

74-
Once these resources are in place, the PostgreSQL Operator can be installed into the cluster.
75-
76-
7744
## Installation
7845

7946
Create an `OperatorGroup` and a `Subscription` in your chosen namespace.

installers/ansible/roles/pgo-operator/tasks/main.yml

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@
102102
when: pgorole_pgoadmin_result.rc == 1
103103

104104
- name: PGO Service Account
105-
when:
105+
when:
106106
- create_rbac|bool
107107
tags:
108108
- install
@@ -124,7 +124,7 @@
124124
when: pgo_service_account_result.rc == 1
125125

126126
- name: Cluster RBAC (namespace_mode 'dynamic')
127-
when:
127+
when:
128128
- create_rbac|bool
129129
- namespace_mode == "dynamic"
130130
tags:
@@ -147,7 +147,7 @@
147147
when: cluster_rbac_result.rc == 1
148148

149149
- name: Cluster RBAC (namespace_mode 'readonly')
150-
when:
150+
when:
151151
- create_rbac|bool
152152
- namespace_mode == "readonly"
153153
tags:
@@ -175,7 +175,7 @@
175175
tags:
176176
- install
177177
- update
178-
when:
178+
when:
179179
- create_rbac|bool
180180
- namespace_mode == "disabled"
181181

@@ -263,13 +263,13 @@
263263
- name: Create PGO BackRest Repo Secret
264264
command: |
265265
{{ kubectl_or_oc }} create secret generic pgo-backrest-repo-config \
266-
--from-file=config='{{ role_path }}/files/pgo-backrest-repo/config' \
267-
--from-file=sshd_config='{{ role_path }}/files/pgo-backrest-repo/sshd_config' \
268-
--from-file=aws-s3-ca.crt='{{ role_path }}/files/pgo-backrest-repo/aws-s3-ca.crt' \
269266
--from-literal=aws-s3-key='{{ backrest_aws_s3_key }}' \
270267
--from-literal=aws-s3-key-secret='{{ backrest_aws_s3_secret }}' \
271268
-n {{ pgo_operator_namespace }}
272-
when: pgo_backrest_repo_config_result.rc == 1
269+
when:
270+
- pgo_backrest_repo_config_result.rc == 1
271+
- (backrest_aws_s3_key | default('') != '') or
272+
(backrest_aws_s3_secret | default('') != '')
273273

274274
- name: PGO ConfigMap
275275
tags:
@@ -286,7 +286,7 @@
286286
shell: "{{ kubectl_or_oc }} get configmap pgo-config -n {{ pgo_operator_namespace }}"
287287
register: pgo_config_result
288288
failed_when: false
289-
289+
290290
- name: Create PGO ConfigMap
291291
command: |
292292
{{ kubectl_or_oc }} create configmap pgo-config \
@@ -382,8 +382,8 @@
382382
shell: "{{ kubectl_or_oc }} get -f {{ output_dir }}/pgo-client.json"
383383
register: pgo_client_json_result
384384
failed_when: false
385-
385+
386386
- name: Create PGO-Client deployment
387387
command: |
388388
{{ kubectl_or_oc }} create --filename='{{ output_dir }}/pgo-client.json'
389-
when: pgo_client_json_result.rc == 1
389+
when: pgo_client_json_result.rc == 1

installers/olm/description.openshift.md

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -56,20 +56,12 @@ edit `conf/postgres-operator/pgo.yaml` and set `DisableFSGroup` to `true`.
5656

5757
[Security Context Constraint]: https://docs.openshift.com/container-platform/latest/authentication/managing-security-context-constraints.html
5858

59-
### Secrets
59+
### Secrets (optional)
6060

61-
Configure pgBackRest for your environment. If you do not plan to use AWS S3 to store backups, you can omit
62-
the `aws-s3` keys below.
61+
If you plan to use AWS S3 to store backups, you can configure your environment to automatically provide your AWS S3 credentials to all newly created PostgreSQL clusters:
6362

6463
```
65-
curl https://raw.githubusercontent.com/CrunchyData/postgres-operator/v${PGO_VERSION}/installers/ansible/roles/pgo-operator/files/pgo-backrest-repo/config > config
66-
curl https://raw.githubusercontent.com/CrunchyData/postgres-operator/v${PGO_VERSION}/installers/ansible/roles/pgo-operator/files/pgo-backrest-repo/sshd_config > sshd_config
67-
curl https://raw.githubusercontent.com/CrunchyData/postgres-operator/v${PGO_VERSION}/installers/ansible/roles/pgo-operator/files/pgo-backrest-repo/aws-s3-ca.crt > aws-s3-ca.crt
68-
6964
oc -n "$PGO_OPERATOR_NAMESPACE" create secret generic pgo-backrest-repo-config \
70-
--from-file=./config \
71-
--from-file=./sshd_config \
72-
--from-file=./aws-s3-ca.crt \
7365
--from-literal=aws-s3-key="<your-aws-s3-key>" \
7466
--from-literal=aws-s3-key-secret="<your-aws-s3-key-secret>"
7567
oc -n "$PGO_OPERATOR_NAMESPACE" label secret pgo-backrest-repo-config vendor=crunchydata

installers/olm/description.upstream.md

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -49,20 +49,12 @@ export PGO_OPERATOR_NAMESPACE=pgo
4949
kubectl create namespace "$PGO_OPERATOR_NAMESPACE"
5050
```
5151

52-
### Secrets
52+
### Secrets (optional)
5353

54-
Configure pgBackRest for your environment. If you do not plan to use AWS S3 to store backups, you can omit
55-
the `aws-s3` keys below.
54+
If you plan to use AWS S3 to store backups, you can configure your environment to automatically provide your AWS S3 credentials to all newly created PostgreSQL clusters:
5655

5756
```
58-
curl https://raw.githubusercontent.com/CrunchyData/postgres-operator/v${PGO_VERSION}/installers/ansible/roles/pgo-operator/files/pgo-backrest-repo/config > config
59-
curl https://raw.githubusercontent.com/CrunchyData/postgres-operator/v${PGO_VERSION}/installers/ansible/roles/pgo-operator/files/pgo-backrest-repo/sshd_config > sshd_config
60-
curl https://raw.githubusercontent.com/CrunchyData/postgres-operator/v${PGO_VERSION}/installers/ansible/roles/pgo-operator/files/pgo-backrest-repo/aws-s3-ca.crt > aws-s3-ca.crt
61-
6257
kubectl -n "$PGO_OPERATOR_NAMESPACE" create secret generic pgo-backrest-repo-config \
63-
--from-file=./config \
64-
--from-file=./sshd_config \
65-
--from-file=./aws-s3-ca.crt \
6658
--from-literal=aws-s3-key="<your-aws-s3-key>" \
6759
--from-literal=aws-s3-key-secret="<your-aws-s3-key-secret>"
6860
kubectl -n "$PGO_OPERATOR_NAMESPACE" label secret pgo-backrest-repo-config vendor=crunchydata

internal/config/secrets.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package config
2+
3+
/*
4+
Copyright 2020 Crunchy Data Solutions, Inc.
5+
Licensed under the Apache License, Version 2.0 (the "License");
6+
you may not use this file except in compliance with the License.
7+
You may obtain a copy of the License at
8+
9+
http://www.apache.org/licenses/LICENSE-2.0
10+
11+
Unless required by applicable law or agreed to in writing, software
12+
distributed under the License is distributed on an "AS IS" BASIS,
13+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
See the License for the specific language governing permissions and
15+
limitations under the License.
16+
*/
17+
18+
const SecretOperatorBackrestRepoConfig = "pgo-backrest-repo-config"

internal/operator/common.go

Lines changed: 80 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@ package operator
1818
import (
1919
"bytes"
2020
"encoding/json"
21+
"io/ioutil"
2122
"os"
23+
"path"
2224
"strings"
2325

2426
"github.com/crunchydata/postgres-operator/internal/config"
@@ -27,10 +29,15 @@ import (
2729
log "github.com/sirupsen/logrus"
2830

2931
v1 "k8s.io/api/core/v1"
32+
kerrors "k8s.io/apimachinery/pkg/api/errors"
33+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
3034
"k8s.io/client-go/kubernetes"
3135
)
3236

3337
const (
38+
// defaultBackrestRepoConfigPath contains the default configuration that are used
39+
// to set up a pgBackRest repository
40+
defaultBackrestRepoConfigPath = "/default-pgo-backrest-repo/"
3441
// defaultRegistry is the default registry to pull the container images from
3542
defaultRegistry = "registry.developers.crunchydata.com/crunchydata"
3643
)
@@ -62,6 +69,10 @@ type containerResourcesTemplateFields struct {
6269
RequestsMemory, RequestsCPU string
6370
}
6471

72+
// defaultBackrestRepoConfigKeys are the default keys expected to be in the
73+
// pgBackRest repo config secret
74+
var defaultBackrestRepoConfigKeys = []string{"config", "sshd_config", "aws-s3-ca.crt"}
75+
6576
func Initialize(clientset kubernetes.Interface) {
6677

6778
tmp := os.Getenv("CRUNCHY_DEBUG")
@@ -89,16 +100,15 @@ func Initialize(clientset kubernetes.Interface) {
89100
os.Exit(2)
90101
}
91102

92-
var err error
93-
94-
err = Pgo.GetConfig(clientset, PgoNamespace)
95-
if err != nil {
103+
if err := Pgo.GetConfig(clientset, PgoNamespace); err != nil {
96104
log.Error(err)
97-
log.Error("pgo-config files and templates did not load")
98-
os.Exit(2)
105+
log.Fatal("pgo-config files and templates did not load")
99106
}
100107

101-
log.Printf("PrimaryStorage=%v\n", Pgo.Storage["storage1"])
108+
// initialize the general pgBackRest secret
109+
if err := initializeOperatorBackrestSecret(clientset, PgoNamespace); err != nil {
110+
log.Fatal(err)
111+
}
102112

103113
if Pgo.Cluster.CCPImagePrefix == "" {
104114
log.Debugf("pgo.yaml CCPImagePrefix not set, using default %q", defaultRegistry)
@@ -360,6 +370,69 @@ func initializeControllerWorkerCounts() {
360370
}
361371
}
362372

373+
// initializeOperatorBackrestSecret ensures the generic pgBackRest configuration
374+
// is available
375+
func initializeOperatorBackrestSecret(clientset kubernetes.Interface, namespace string) error {
376+
var isNew, isModified bool
377+
378+
// determine if the Secret already exists
379+
secret, err := clientset.
380+
CoreV1().Secrets(namespace).
381+
Get(config.SecretOperatorBackrestRepoConfig, metav1.GetOptions{})
382+
383+
// if there is a true error, return. Otherwise, initialize a new Secret
384+
if err != nil {
385+
if !kerrors.IsNotFound(err) {
386+
return err
387+
}
388+
389+
secret = &v1.Secret{
390+
ObjectMeta: metav1.ObjectMeta{
391+
Name: config.SecretOperatorBackrestRepoConfig,
392+
},
393+
Data: map[string][]byte{},
394+
}
395+
isNew = true
396+
}
397+
398+
// set any missing defaults
399+
for _, filename := range defaultBackrestRepoConfigKeys {
400+
// skip if there is already content
401+
if len(secret.Data[filename]) != 0 {
402+
continue
403+
}
404+
405+
file := path.Join(defaultBackrestRepoConfigPath, filename)
406+
407+
// if we can't read the contents of the file for whatever reason, warn,
408+
// but continue
409+
// otherwise, update the entry in the Secret
410+
if contents, err := ioutil.ReadFile(file); err != nil {
411+
log.Warn(err)
412+
continue
413+
} else {
414+
secret.Data[filename] = contents
415+
}
416+
417+
isModified = true
418+
}
419+
420+
// do not make any updates if the secret is not modified at all
421+
if !isModified {
422+
return nil
423+
}
424+
425+
// make the API calls based on if we are creating or updating
426+
if isNew {
427+
_, err := clientset.CoreV1().Secrets(namespace).Create(secret)
428+
return err
429+
}
430+
431+
_, err = clientset.CoreV1().Secrets(namespace).Update(secret)
432+
433+
return err
434+
}
435+
363436
// SetupNamespaces is responsible for the initial namespace configuration for the Operator
364437
// install. This includes setting the proper namespace operating mode, creating and/or updating
365438
// namespaces as needed (or as permitted by the current operator mode), and returning a valid list

internal/util/cluster.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ func CreateBackrestRepoSecrets(clientset kubernetes.Interface,
150150
// SSHD(...?) and possible S3 credentials
151151
configs, configErr := clientset.
152152
CoreV1().Secrets(backrestRepoConfig.OperatorNamespace).
153-
Get("pgo-backrest-repo-config", metav1.GetOptions{})
153+
Get(config.SecretOperatorBackrestRepoConfig, metav1.GetOptions{})
154154

155155
if configErr != nil {
156156
log.Error(configErr)

0 commit comments

Comments
 (0)