Skip to content
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

K8SPG-487: add global annotations and labels for cr.yaml #956

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions deploy/cr.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ metadata:
# - percona.com/delete-ssl
spec:
crVersion: 2.6.0
# metadata:
# annotations:
# test-annotation: value
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we want to have a test prefix here in the CR? Maybe something like example-annotation or annotation-key. Test feels like a leftover from some testing.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@pooknull wdyt about this?

# labels:
# test-label: value
# secrets:
# customRootCATLSSecret:
# name: cluster1-ca-cert
Expand Down
5 changes: 4 additions & 1 deletion e2e-tests/tests/init-deploy/01-create-cluster.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,7 @@ commands:

source ../../functions

get_cr | kubectl -n "${NAMESPACE}" apply -f -
get_cr \
| yq '.spec.metadata.labels.test-label = "test"' \
| yq '.spec.metadata.annotations.test-annotation = "test"' \
| kubectl -n "${NAMESPACE}" apply -f -
231 changes: 231 additions & 0 deletions e2e-tests/tests/init-deploy/05-assert.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,231 @@
apiVersion: kuttl.dev/v1beta1
kind: TestAssert
timeout: 300
---
apiVersion: v1
kind: Secret
metadata:
annotations:
test-annotation: test
labels:
app.kubernetes.io/instance: init-deploy
app.kubernetes.io/managed-by: percona-postgresql-operator
app.kubernetes.io/name: percona-postgresql
app.kubernetes.io/part-of: percona-postgresql
pgv2.percona.com/version: 2.6.0
postgres-operator.crunchydata.com/cluster: init-deploy
postgres-operator.crunchydata.com/pgbackrest: ""
postgres-operator.crunchydata.com/pgbackrest-config: ""
test-label: test
name: init-deploy-pgbackrest
type: Opaque
---
apiVersion: v1
kind: Secret
metadata:
annotations:
test-annotation: test
labels:
app.kubernetes.io/instance: init-deploy
app.kubernetes.io/managed-by: percona-postgresql-operator
app.kubernetes.io/name: percona-postgresql
app.kubernetes.io/part-of: percona-postgresql
postgres-operator.crunchydata.com/cluster: init-deploy
postgres-operator.crunchydata.com/cluster-certificate: replication-client-tls
test-label: test
name: init-deploy-replication-cert
type: Opaque
---
apiVersion: v1
kind: Secret
metadata:
annotations:
test-annotation: test
labels:
app.kubernetes.io/instance: init-deploy
app.kubernetes.io/managed-by: percona-postgresql-operator
app.kubernetes.io/name: percona-postgresql
app.kubernetes.io/part-of: percona-postgresql
postgres-operator.crunchydata.com/cluster: init-deploy
postgres-operator.crunchydata.com/pguser: postgres
postgres-operator.crunchydata.com/role: pguser
test-label: test
name: init-deploy-pguser-postgres
type: Opaque
---
apiVersion: v1
kind: Secret
metadata:
annotations:
test-annotation: test
labels:
app.kubernetes.io/instance: init-deploy
app.kubernetes.io/managed-by: percona-postgresql-operator
app.kubernetes.io/name: percona-postgresql
app.kubernetes.io/part-of: percona-postgresql
postgres-operator.crunchydata.com/cluster: init-deploy
postgres-operator.crunchydata.com/pguser: init-deploy
postgres-operator.crunchydata.com/role: pguser
test-label: test
name: init-deploy-pguser-init-deploy
type: Opaque
---
apiVersion: v1
kind: Secret
metadata:
annotations:
test-annotation: test
labels:
app.kubernetes.io/component: pgbouncer
app.kubernetes.io/instance: init-deploy
app.kubernetes.io/managed-by: percona-postgresql-operator
app.kubernetes.io/name: percona-postgresql
app.kubernetes.io/part-of: percona-postgresql
pgv2.percona.com/version: 2.6.0
postgres-operator.crunchydata.com/cluster: init-deploy
postgres-operator.crunchydata.com/role: pgbouncer
test-label: test
name: init-deploy-pgbouncer
type: Opaque
---
apiVersion: v1
kind: Secret
metadata:
annotations:
test-annotation: test
labels:
app.kubernetes.io/instance: init-deploy
app.kubernetes.io/managed-by: percona-postgresql-operator
app.kubernetes.io/name: percona-postgresql
app.kubernetes.io/part-of: percona-postgresql
postgres-operator.crunchydata.com/cluster: init-deploy
postgres-operator.crunchydata.com/cluster-certificate: postgres-tls
test-label: test
name: init-deploy-cluster-cert
type: Opaque
---
apiVersion: v1
kind: Secret
metadata:
annotations:
test-annotation: test
labels:
test-label: test
name: init-deploy-cluster-ca-cert
ownerReferences:
- apiVersion: postgres-operator.crunchydata.com/v1beta1
kind: PostgresCluster
name: init-deploy
type: Opaque
---
apiVersion: v1
kind: ConfigMap
metadata:
annotations:
test-annotation: test
labels:
postgres-operator.crunchydata.com/cluster: init-deploy
test-label: test
name: init-deploy-config
ownerReferences:
- apiVersion: postgres-operator.crunchydata.com/v1beta1
blockOwnerDeletion: true
controller: true
kind: PostgresCluster
name: init-deploy
---
apiVersion: v1
kind: ConfigMap
metadata:
annotations:
test-annotation: test
labels:
app.kubernetes.io/instance: init-deploy
app.kubernetes.io/managed-by: percona-postgresql-operator
app.kubernetes.io/name: percona-postgresql
app.kubernetes.io/part-of: percona-postgresql
pgv2.percona.com/version: 2.6.0
postgres-operator.crunchydata.com/cluster: init-deploy
postgres-operator.crunchydata.com/pgbackrest: ""
postgres-operator.crunchydata.com/pgbackrest-config: ""
test-label: test
name: init-deploy-pgbackrest-config
ownerReferences:
- apiVersion: postgres-operator.crunchydata.com/v1beta1
blockOwnerDeletion: true
controller: true
kind: PostgresCluster
name: init-deploy
---
apiVersion: v1
kind: ConfigMap
metadata:
annotations:
test-annotation: test
labels:
app.kubernetes.io/component: pgbouncer
app.kubernetes.io/instance: init-deploy
app.kubernetes.io/managed-by: percona-postgresql-operator
app.kubernetes.io/name: percona-postgresql
app.kubernetes.io/part-of: percona-postgresql
pgv2.percona.com/version: 2.6.0
postgres-operator.crunchydata.com/cluster: init-deploy
postgres-operator.crunchydata.com/role: pgbouncer
test-label: test
name: init-deploy-pgbouncer
ownerReferences:
- apiVersion: postgres-operator.crunchydata.com/v1beta1
blockOwnerDeletion: true
controller: true
kind: PostgresCluster
name: init-deploy
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
annotations:
test-annotation: test
generation: 1
labels:
app.kubernetes.io/instance: init-deploy
app.kubernetes.io/managed-by: percona-postgresql-operator
app.kubernetes.io/name: percona-postgresql
app.kubernetes.io/part-of: percona-postgresql
pgv2.percona.com/version: 2.6.0
postgres-operator.crunchydata.com/cluster: init-deploy
postgres-operator.crunchydata.com/data: pgbackrest
postgres-operator.crunchydata.com/pgbackrest: ""
postgres-operator.crunchydata.com/pgbackrest-dedicated: ""
test-label: test
name: init-deploy-repo-host
ownerReferences:
- apiVersion: postgres-operator.crunchydata.com/v1beta1
blockOwnerDeletion: true
controller: true
kind: PostgresCluster
name: init-deploy
spec:
template:
metadata:
annotations:
test-annotation: test
labels:
app.kubernetes.io/instance: init-deploy
app.kubernetes.io/managed-by: percona-postgresql-operator
app.kubernetes.io/name: percona-postgresql
app.kubernetes.io/part-of: percona-postgresql
pgv2.percona.com/version: 2.6.0
postgres-operator.crunchydata.com/cluster: init-deploy
postgres-operator.crunchydata.com/data: pgbackrest
postgres-operator.crunchydata.com/pgbackrest: ""
postgres-operator.crunchydata.com/pgbackrest-dedicated: ""
test-label: test
status:
availableReplicas: 1
collisionCount: 0
currentReplicas: 1
observedGeneration: 1
readyReplicas: 1
replicas: 1
updatedReplicas: 1

7 changes: 7 additions & 0 deletions internal/controller/postgrescluster/pki.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package postgrescluster
import (
"context"

gover "github.com/hashicorp/go-version"
"github.com/pkg/errors"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
Expand Down Expand Up @@ -109,6 +110,12 @@ func (r *Reconciler) reconcileRootCertificate(
intent.Data = make(map[string][]byte)
intent.ObjectMeta.OwnerReferences = existing.ObjectMeta.OwnerReferences

currVersion, err := gover.NewVersion(cluster.Labels[naming.LabelVersion])
if err == nil && currVersion.GreaterThanOrEqual(gover.Must(gover.NewVersion("2.6.0"))) && cluster.Spec.Metadata != nil {
intent.Labels = cluster.Spec.Metadata.Labels
intent.Annotations = cluster.Spec.Metadata.Annotations
}

// A root secret is scoped to the namespace where postgrescluster(s)
// are deployed. For operator deployments with postgresclusters in more than
// one namespace, there will be one root per namespace.
Expand Down
4 changes: 4 additions & 0 deletions percona/controller/pgcluster/backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,10 @@ func reconcileBackupJob(ctx context.Context, cl client.Client, cr *v2.PerconaPGC
RepoName: repoName,
},
}
if cr.CompareVersion("2.6.0") >= 0 && cr.Spec.Metadata != nil {
pb.Annotations = naming.Merge(cr.Spec.Metadata.Annotations, pb.Annotations)
pb.Labels = cr.Spec.Metadata.Labels
}

err = cl.Create(ctx, pb)
if err != nil {
Expand Down
7 changes: 7 additions & 0 deletions percona/controller/pgcluster/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,7 @@ func (r *PGClusterReconciler) reconcileOldCACert(ctx context.Context, cr *v2.Per
// K8SPG-555: We should create an empty secret with old name, so that crunchy part can populate it
// instead of creating secrets unique to the cluster
// TODO: remove when 2.4.0 will become unsupported

if err := r.Client.Create(ctx, oldCASecret); err != nil {
return errors.Wrap(err, "failed to create ca secret")
}
Expand Down Expand Up @@ -409,6 +410,12 @@ func (r *PGClusterReconciler) reconcileOldCACert(ctx context.Context, cr *v2.Per
if !k8serrors.IsNotFound(err) {
newCASecret.Data = oldCASecret.Data
}

if cr.CompareVersion("2.6.0") >= 0 && cr.Spec.Metadata != nil {
newCASecret.Annotations = cr.Spec.Metadata.Annotations
newCASecret.Labels = cr.Spec.Metadata.Labels
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't catch this place, if we will have different annotations (for example for extentions too), should we put it to secret annotations?


if err := r.Client.Create(ctx, newCASecret); err != nil {
return errors.Wrap(err, "failed to create updated CA secret")
}
Expand Down
5 changes: 5 additions & 0 deletions percona/controller/pgcluster/restore.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
k8serrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

"github.com/percona/percona-postgresql-operator/internal/naming"
pNaming "github.com/percona/percona-postgresql-operator/percona/naming"
v2 "github.com/percona/percona-postgresql-operator/pkg/apis/pgv2.percona.com/v2"
)
Expand Down Expand Up @@ -41,6 +42,10 @@ func (r *PGClusterReconciler) createBootstrapRestoreObject(ctx context.Context,
RepoName: repoName,
},
}
if cr.CompareVersion("2.6.0") >= 0 && cr.Spec.Metadata != nil {
pgr.Annotations = naming.Merge(cr.Spec.Metadata.Annotations, pgr.Annotations)
pgr.Labels = cr.Spec.Metadata.Labels
}

err := r.Client.Create(ctx, pgr)
if err != nil && !k8serrors.IsAlreadyExists(err) {
Expand Down
5 changes: 5 additions & 0 deletions percona/controller/pgcluster/schedule.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,11 @@ func (r *PGClusterReconciler) createScheduledBackup(log logr.Logger, backupName,
},
}

if pb.CompareVersion("2.6.0") >= 0 && cr.Spec.Metadata != nil {
pb.Annotations = cr.Spec.Metadata.Annotations
pb.Labels = cr.Spec.Metadata.Labels
}

err := r.Client.Create(ctx, pb)
if err != nil {
return errors.Wrapf(err, "failed to create PerconaPGBackup %s", backupName)
Expand Down
Loading