-
Notifications
You must be signed in to change notification settings - Fork 62
K8SPG-594 delete custom extensions from installed #967
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 23 commits
Commits
Show all changes
41 commits
Select commit
Hold shift + click to select a range
bdfa932
K8SPG-594 delete custom extensions from installed
nmarukovich d1c9434
update extensions check
nmarukovich a265b86
fix checks
nmarukovich d370c4a
fix checks
nmarukovich 71c00fb
delete unused
nmarukovich afb1f0c
Merge branch 'main' into K8SPG-594_delete_installed_ext
nmarukovich 6f450e1
Merge branch 'main' into K8SPG-594_delete_installed_ext
nmarukovich 08695c6
fix PR coments
nmarukovich cf29171
Merge branch 'K8SPG-594_delete_installed_ext' of github.com:percona/p…
nmarukovich 4c6bf22
fix PR comments
nmarukovich b6a2f72
Merge branch 'main' into K8SPG-594_delete_installed_ext
nmarukovich f232f36
fix PR
nmarukovich f3999bd
Merge branch 'K8SPG-594_delete_installed_ext' of github.com:percona/p…
nmarukovich 0a2524a
delete logs
nmarukovich e174f7b
update conditions
nmarukovich 41e0fb4
Merge branch 'main' into K8SPG-594_delete_installed_ext
nmarukovich 08e178d
Merge branch 'main' into K8SPG-594_delete_installed_ext
nmarukovich a4c54f2
update annotations adding
nmarukovich 0dc2bbd
Merge branch 'main' into K8SPG-594_delete_installed_ext
hors fbe03d3
use status instead of annotations
nmarukovich bf0818c
Merge branch 'K8SPG-594_delete_installed_ext' of github.com:percona/p…
nmarukovich b6ce78d
fix PR
nmarukovich bbf10e0
delete unused logs
nmarukovich 061f592
fix PR comments
nmarukovich a74161a
fix PR comments
nmarukovich bd04f98
fix PR comments
nmarukovich f9ea789
fix test
nmarukovich 37b3e4e
fix the test
nmarukovich eb26df5
Merge branch 'main' into K8SPG-594_delete_installed_ext
nmarukovich c688307
fix vulnarabilities
nmarukovich d94deba
fix vulnarabilities
nmarukovich ed21ea6
update test
nmarukovich 75a3372
fix logging
nmarukovich 4f86a07
fix test
nmarukovich 4f1bac0
fix PR comments
nmarukovich 7128445
Merge branch 'main' into K8SPG-594_delete_installed_ext
nmarukovich 07979c3
rename package with common functions
nmarukovich a899835
Merge branch 'K8SPG-594_delete_installed_ext' of github.com:percona/p…
nmarukovich 51242e9
Merge branch 'main' into K8SPG-594_delete_installed_ext
hors 05a27e0
fix imports
nmarukovich 10d9c83
Merge branch 'K8SPG-594_delete_installed_ext' of github.com:percona/p…
nmarukovich File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
apiVersion: kuttl.dev/v1beta1 | ||
kind: TestAssert | ||
timeout: 30 | ||
--- | ||
kind: ConfigMap | ||
apiVersion: v1 | ||
metadata: | ||
name: 10-check-extensions | ||
data: | ||
data: |2- | ||
pg_stat_monitor | ||
pgaudit |
13 changes: 13 additions & 0 deletions
13
e2e-tests/tests/custom-extensions/09-check-installed-extensions.yaml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
apiVersion: kuttl.dev/v1beta1 | ||
kind: TestStep | ||
timeout: 30 | ||
commands: | ||
- script: |- | ||
set -o errexit | ||
set -o xtrace | ||
|
||
source ../../functions | ||
|
||
data=$(kubectl -n ${NAMESPACE} exec $(get_client_pod) -- psql -v ON_ERROR_STOP=1 -t -q postgres://postgres:$(get_psql_user_pass custom-extensions-pguser-postgres)@$(get_psql_user_host custom-extensions-pguser-postgres) -c "\c postgres" -c "select name from pg_extensions order by name") | ||
|
||
kubectl create configmap -n "${NAMESPACE}" 10-check-extensions --from-literal=data="${data}" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,10 +4,14 @@ | |
"context" | ||
"crypto/md5" | ||
"fmt" | ||
"github.com/percona/percona-postgresql-operator/internal/controller/runtime" | ||
"io" | ||
"reflect" | ||
"strings" | ||
"time" | ||
|
||
"github.com/percona/percona-postgresql-operator/internal/postgres" | ||
|
||
"github.com/pkg/errors" | ||
"go.opentelemetry.io/otel/trace" | ||
batchv1 "k8s.io/api/batch/v1" | ||
|
@@ -49,8 +53,12 @@ | |
|
||
// Reconciler holds resources for the PerconaPGCluster reconciler | ||
type PGClusterReconciler struct { | ||
Client client.Client | ||
Owner client.FieldOwner | ||
Client client.Client | ||
Owner client.FieldOwner | ||
PodExec func( | ||
ctx context.Context, namespace, pod, container string, | ||
stdin io.Reader, stdout, stderr io.Writer, command ...string, | ||
) error | ||
Recorder record.EventRecorder | ||
Tracer trace.Tracer | ||
Platform string | ||
|
@@ -65,6 +73,13 @@ | |
|
||
// SetupWithManager adds the PerconaPGCluster controller to the provided runtime manager | ||
func (r *PGClusterReconciler) SetupWithManager(mgr manager.Manager) error { | ||
if r.PodExec == nil { | ||
var err error | ||
r.PodExec, err = runtime.NewPodExecutor(mgr.GetConfig()) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
if err := r.CrunchyController.Watch(source.Kind(mgr.GetCache(), &corev1.Secret{}, r.watchSecrets())); err != nil { | ||
return errors.Wrap(err, "unable to watch secrets") | ||
} | ||
|
@@ -241,7 +256,9 @@ | |
return reconcile.Result{}, errors.Wrap(err, "failed to handle monitor user password change") | ||
} | ||
|
||
r.reconcileCustomExtensions(cr) | ||
if err := r.reconcileCustomExtensions(ctx, cr, postgresCluster); err != nil { | ||
return reconcile.Result{}, errors.Wrap(err, "reconcile custom extensions") | ||
} | ||
|
||
if err := r.reconcileScheduledBackups(ctx, cr); err != nil { | ||
return reconcile.Result{}, errors.Wrap(err, "reconcile scheduled backups") | ||
|
@@ -524,15 +541,64 @@ | |
return nil | ||
} | ||
|
||
func (r *PGClusterReconciler) reconcileCustomExtensions(cr *v2.PerconaPGCluster) { | ||
func (r *PGClusterReconciler) reconcileCustomExtensions(ctx context.Context, cr *v2.PerconaPGCluster, postgresCluster *v1beta1.PostgresCluster) error { | ||
log := logging.FromContext(ctx).WithValues("cluster", cr.Name, "namespace", cr.Namespace) | ||
|
||
if err := r.Client.Get(ctx, client.ObjectKeyFromObject(postgresCluster), postgresCluster); err != nil { | ||
nmarukovich marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return errors.Wrap(err, "get PostgresCluster") | ||
} | ||
|
||
if cr.Spec.Extensions.Storage.Secret == nil { | ||
return | ||
return nil | ||
} | ||
|
||
extensionKeys := make([]string, 0) | ||
extensionNames := make([]string, 0) | ||
|
||
for _, extension := range cr.Spec.Extensions.Custom { | ||
key := extensions.GetExtensionKey(cr.Spec.PostgresVersion, extension.Name, extension.Version) | ||
extensionKeys = append(extensionKeys, key) | ||
extensionNames = append(extensionNames, extension.Name) | ||
} | ||
|
||
if cr.CompareVersion("2.6.0") >= 0 { | ||
nmarukovich marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// custom extensions to be removed | ||
var removedExtension []string | ||
// list of installed custom extensions | ||
var installedExtensions []string | ||
installedExtensions = cr.Status.InstalledCustomExtensions | ||
crExtensions := make(map[string]struct{}) | ||
for _, ext := range extensionNames { | ||
crExtensions[ext] = struct{}{} | ||
} | ||
// Check for missing entries in crExtensions | ||
for _, ext := range installedExtensions { | ||
// If an object exists in installedExtensions but not in crExtensions, the extension should be deleted. | ||
if _, exists := crExtensions[ext]; !exists { | ||
nmarukovich marked this conversation as resolved.
Show resolved
Hide resolved
|
||
removedExtension = append(removedExtension, ext) | ||
} | ||
} | ||
log.Info("Extension to delete", "removedExtension", removedExtension) | ||
|
||
if len(removedExtension) > 0 { | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. unnecessary empty line |
||
action := func(ctx context.Context, exec postgres.Executor) error { | ||
return errors.WithStack(DisableCustomExtensionsInPostgreSQL(ctx, exec, removedExtension)) | ||
} | ||
|
||
primary, err := getPrimaryPod(ctx, r.Client, cr) | ||
|
||
if primary == nil { | ||
return errors.New("Pod is nil") | ||
} | ||
|
||
err = action(ctx, func(ctx context.Context, stdin io.Reader, stdout, stderr io.Writer, command ...string) error { | ||
return r.PodExec(ctx, primary.Namespace, primary.Name, naming.ContainerDatabase, stdin, stdout, stderr, command...) | ||
}) | ||
if err != nil { | ||
return errors.Wrap(err, "deletion extension from installed") | ||
} | ||
} | ||
} | ||
|
||
for i := 0; i < len(cr.Spec.InstanceSets); i++ { | ||
|
@@ -549,6 +615,33 @@ | |
)) | ||
set.VolumeMounts = append(set.VolumeMounts, extensions.ExtensionVolumeMounts(cr.Spec.PostgresVersion)...) | ||
} | ||
return nil | ||
} | ||
|
||
func DisableCustomExtensionsInPostgreSQL(ctx context.Context, exec postgres.Executor, customExtensionsForDeletion []string) error { | ||
nmarukovich marked this conversation as resolved.
Show resolved
Hide resolved
|
||
log := logging.FromContext(ctx) | ||
|
||
for _, extensionName := range customExtensionsForDeletion { | ||
|
||
sqlCommand := fmt.Sprintf( | ||
`SET client_min_messages = WARNING; DROP EXTENSION IF EXISTS %s;`, | ||
extensionName, | ||
) | ||
|
||
stdout, stderr, err := exec.ExecInAllDatabases(ctx, | ||
sqlCommand, | ||
map[string]string{ | ||
"ON_ERROR_STOP": "on", // Abort when any one command fails. | ||
"QUIET": "on", // Do not print successful commands to stdout. | ||
}, | ||
) | ||
|
||
log.V(1).Info("disabled", "extensionName", extensionName, "stdout", stdout, "stderr", stderr) | ||
|
||
return errors.Wrap(err, "custom extension deletion") | ||
|
||
} | ||
return nil | ||
} | ||
|
||
func isBackupRunning(ctx context.Context, cl client.Reader, cr *v2.PerconaPGCluster) (bool, error) { | ||
|
@@ -659,3 +752,27 @@ | |
|
||
return nil | ||
} | ||
|
||
func getPrimaryPod(ctx context.Context, cli client.Client, cr *v2.PerconaPGCluster) (*corev1.Pod, error) { | ||
podList := &corev1.PodList{} | ||
err := cli.List(ctx, podList, &client.ListOptions{ | ||
Namespace: cr.Namespace, | ||
LabelSelector: labels.SelectorFromSet(map[string]string{ | ||
"app.kubernetes.io/instance": cr.Name, | ||
"postgres-operator.crunchydata.com/role": "master", | ||
}), | ||
}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if len(podList.Items) == 0 { | ||
return nil, errors.New("no primary pod found") | ||
} | ||
|
||
if len(podList.Items) > 1 { | ||
return nil, errors.New("multiple primary pods found") | ||
} | ||
|
||
return &podList.Items[0], nil | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.