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

Add default container annotation #4031

Merged
merged 4 commits into from
Nov 20, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
13 changes: 11 additions & 2 deletions internal/controller/pgupgrade/jobs.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"k8s.io/apimachinery/pkg/labels"

"github.com/crunchydata/postgres-operator/internal/initialize"
"github.com/crunchydata/postgres-operator/internal/naming"
"github.com/crunchydata/postgres-operator/pkg/apis/postgres-operator.crunchydata.com/v1beta1"
)

Expand Down Expand Up @@ -129,7 +130,6 @@ func (r *PGUpgradeReconciler) generateUpgradeJob(
job.Namespace = upgrade.Namespace
job.Name = pgUpgradeJob(upgrade).Name

job.Annotations = upgrade.Spec.Metadata.GetAnnotationsOrNil()
job.Labels = Merge(upgrade.Spec.Metadata.GetLabelsOrNil(),
commonLabels(pgUpgrade, upgrade), //FIXME role pgupgrade
map[string]string{
Expand All @@ -145,6 +145,11 @@ func (r *PGUpgradeReconciler) generateUpgradeJob(
}
}

job.Annotations = Merge(upgrade.Spec.Metadata.GetAnnotationsOrNil(),
map[string]string{
naming.DefaultContainerAnnotation: database.Name,
})

// Copy the pod template from the startup instance StatefulSet. This includes
// the service account, volumes, DNS policies, and scheduling constraints.
startup.Spec.Template.DeepCopyInto(&job.Spec.Template)
Expand Down Expand Up @@ -241,7 +246,6 @@ func (r *PGUpgradeReconciler) generateRemoveDataJob(
job.Namespace = upgrade.Namespace
job.Name = upgrade.Name + "-" + sts.Name

job.Annotations = upgrade.Spec.Metadata.GetAnnotationsOrNil()
job.Labels = labels.Merge(upgrade.Spec.Metadata.GetLabelsOrNil(),
commonLabels(removeData, upgrade)) //FIXME role removedata

Expand All @@ -254,6 +258,11 @@ func (r *PGUpgradeReconciler) generateRemoveDataJob(
}
}

job.Annotations = Merge(upgrade.Spec.Metadata.GetAnnotationsOrNil(),
map[string]string{
naming.DefaultContainerAnnotation: database.Name,
})

// Copy the pod template from the sts instance StatefulSet. This includes
// the service account, volumes, DNS policies, and scheduling constraints.
sts.Spec.Template.DeepCopyInto(&job.Spec.Template)
Expand Down
8 changes: 8 additions & 0 deletions internal/controller/pgupgrade/jobs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ func TestGenerateUpgradeJob(t *testing.T) {
apiVersion: batch/v1
kind: Job
metadata:
annotations:
kubectl.kubernetes.io/default-container: database
creationTimestamp: null
labels:
postgres-operator.crunchydata.com/cluster: pg5
Expand All @@ -81,6 +83,8 @@ spec:
backoffLimit: 0
template:
metadata:
annotations:
kubectl.kubernetes.io/default-container: database
creationTimestamp: null
labels:
postgres-operator.crunchydata.com/cluster: pg5
Expand Down Expand Up @@ -193,6 +197,8 @@ func TestGenerateRemoveDataJob(t *testing.T) {
apiVersion: batch/v1
kind: Job
metadata:
annotations:
kubectl.kubernetes.io/default-container: database
creationTimestamp: null
labels:
postgres-operator.crunchydata.com/cluster: pg5
Expand All @@ -211,6 +217,8 @@ spec:
backoffLimit: 0
template:
metadata:
annotations:
kubectl.kubernetes.io/default-container: database
creationTimestamp: null
labels:
postgres-operator.crunchydata.com/cluster: pg5
Expand Down
3 changes: 3 additions & 0 deletions internal/controller/postgrescluster/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -1268,6 +1268,9 @@ func generateInstanceStatefulSetIntent(_ context.Context,
sts.Spec.Template.Annotations = naming.Merge(
cluster.Spec.Metadata.GetAnnotationsOrNil(),
spec.Metadata.GetAnnotationsOrNil(),
map[string]string{
naming.DefaultContainerAnnotation: naming.ContainerDatabase,
},
)
sts.Spec.Template.Labels = naming.Merge(
cluster.Spec.Metadata.GetLabelsOrNil(),
Expand Down
6 changes: 5 additions & 1 deletion internal/controller/postgrescluster/pgadmin.go
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,11 @@ func (r *Reconciler) reconcilePGAdminStatefulSet(
}
sts.Spec.Template.Annotations = naming.Merge(
cluster.Spec.Metadata.GetAnnotationsOrNil(),
cluster.Spec.UserInterface.PGAdmin.Metadata.GetAnnotationsOrNil())
cluster.Spec.UserInterface.PGAdmin.Metadata.GetAnnotationsOrNil(),
map[string]string{
naming.DefaultContainerAnnotation: naming.ContainerPGAdmin,
},
)
sts.Spec.Template.Labels = naming.Merge(
cluster.Spec.Metadata.GetLabelsOrNil(),
cluster.Spec.UserInterface.PGAdmin.Metadata.GetLabelsOrNil(),
Expand Down
3 changes: 3 additions & 0 deletions internal/controller/postgrescluster/pgadmin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -500,6 +500,8 @@ func TestReconcilePGAdminStatefulSet(t *testing.T) {
template.Spec.Volumes = nil

assert.Assert(t, cmp.MarshalMatches(template.ObjectMeta, `
annotations:
kubectl.kubernetes.io/default-container: pgadmin
creationTimestamp: null
labels:
postgres-operator.crunchydata.com/cluster: test-cluster
Expand Down Expand Up @@ -613,6 +615,7 @@ terminationGracePeriodSeconds: 30
assert.Assert(t, cmp.MarshalMatches(template.ObjectMeta, `
annotations:
annotation1: annotationvalue
kubectl.kubernetes.io/default-container: pgadmin
creationTimestamp: null
labels:
label1: labelvalue
Expand Down
19 changes: 17 additions & 2 deletions internal/controller/postgrescluster/pgbackrest.go
Original file line number Diff line number Diff line change
Expand Up @@ -583,7 +583,11 @@ func (r *Reconciler) generateRepoHostIntent(ctx context.Context, postgresCluster

annotations := naming.Merge(
postgresCluster.Spec.Metadata.GetAnnotationsOrNil(),
postgresCluster.Spec.Backups.PGBackRest.Metadata.GetAnnotationsOrNil())
postgresCluster.Spec.Backups.PGBackRest.Metadata.GetAnnotationsOrNil(),
map[string]string{
naming.DefaultContainerAnnotation: naming.PGBackRestRepoContainerName,
},
)
labels := naming.Merge(
postgresCluster.Spec.Metadata.GetLabelsOrNil(),
postgresCluster.Spec.Backups.PGBackRest.Metadata.GetLabelsOrNil(),
Expand Down Expand Up @@ -805,6 +809,14 @@ func generateBackupJobSpecIntent(ctx context.Context, postgresCluster *v1beta1.P
container.Resources = postgresCluster.Spec.Backups.PGBackRest.Jobs.Resources
}

if annotations != nil {
annotations[naming.DefaultContainerAnnotation] = naming.PGBackRestRepoContainerName
} else {
annotations = map[string]string{
naming.DefaultContainerAnnotation: naming.PGBackRestRepoContainerName,
}
}

jobSpec := &batchv1.JobSpec{
Template: corev1.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{Labels: labels, Annotations: annotations},
Expand Down Expand Up @@ -1272,7 +1284,10 @@ func (r *Reconciler) generateRestoreJobIntent(cluster *v1beta1.PostgresCluster,
annotations := naming.Merge(
cluster.Spec.Metadata.GetAnnotationsOrNil(),
cluster.Spec.Backups.PGBackRest.Metadata.GetAnnotationsOrNil(),
map[string]string{naming.PGBackRestConfigHash: configHash})
map[string]string{
naming.PGBackRestConfigHash: configHash,
naming.DefaultContainerAnnotation: naming.PGBackRestRestoreContainerName,
})
labels := naming.Merge(
cluster.Spec.Metadata.GetLabelsOrNil(),
cluster.Spec.Backups.PGBackRest.Metadata.GetLabelsOrNil(),
Expand Down
6 changes: 5 additions & 1 deletion internal/controller/postgrescluster/pgbouncer.go
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,11 @@ func (r *Reconciler) generatePGBouncerDeployment(
}
deploy.Spec.Template.Annotations = naming.Merge(
cluster.Spec.Metadata.GetAnnotationsOrNil(),
cluster.Spec.Proxy.PGBouncer.Metadata.GetAnnotationsOrNil())
cluster.Spec.Proxy.PGBouncer.Metadata.GetAnnotationsOrNil(),
map[string]string{
naming.DefaultContainerAnnotation: naming.ContainerPGBouncer,
},
)
deploy.Spec.Template.Labels = naming.Merge(
cluster.Spec.Metadata.GetLabelsOrNil(),
cluster.Spec.Proxy.PGBouncer.Metadata.GetLabelsOrNil(),
Expand Down
1 change: 1 addition & 0 deletions internal/controller/postgrescluster/pgbouncer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,7 @@ namespace: ns3
// Annotations present in the pod template.
assert.DeepEqual(t, deploy.Spec.Template.Annotations, map[string]string{
"a": "v1",
"kubectl.kubernetes.io/default-container": "pgbouncer",
})

// Labels present in the pod template.
Expand Down
12 changes: 9 additions & 3 deletions internal/controller/postgrescluster/volumes.go
Original file line number Diff line number Diff line change
Expand Up @@ -469,7 +469,9 @@ func (r *Reconciler) reconcileMovePGDataDir(ctx context.Context,

jobSpec := &batchv1.JobSpec{
Template: corev1.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{Labels: labels},
ObjectMeta: metav1.ObjectMeta{Labels: labels, Annotations: map[string]string{
naming.DefaultContainerAnnotation: naming.ContainerJobMovePGDataDir,
}},
Spec: corev1.PodSpec{
// Set the image pull secrets, if any exist.
// This is set here rather than using the service account due to the lack
Expand Down Expand Up @@ -586,7 +588,9 @@ func (r *Reconciler) reconcileMoveWALDir(ctx context.Context,

jobSpec := &batchv1.JobSpec{
Template: corev1.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{Labels: labels},
ObjectMeta: metav1.ObjectMeta{Labels: labels, Annotations: map[string]string{
naming.DefaultContainerAnnotation: naming.ContainerJobMovePGWALDir,
}},
Spec: corev1.PodSpec{
// Set the image pull secrets, if any exist.
// This is set here rather than using the service account due to the lack
Expand Down Expand Up @@ -708,7 +712,9 @@ func (r *Reconciler) reconcileMoveRepoDir(ctx context.Context,

jobSpec := &batchv1.JobSpec{
Template: corev1.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{Labels: labels},
ObjectMeta: metav1.ObjectMeta{Labels: labels, Annotations: map[string]string{
naming.DefaultContainerAnnotation: naming.ContainerJobMovePGBackRestRepoDir,
}},
Spec: corev1.PodSpec{
// Set the image pull secrets, if any exist.
// This is set here rather than using the service account due to the lack
Expand Down
7 changes: 6 additions & 1 deletion internal/controller/standalone_pgadmin/statefulset.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,12 @@ func statefulset(
sts.Spec.Selector = &metav1.LabelSelector{
MatchLabels: naming.StandalonePGAdminLabels(pgadmin.Name),
}
sts.Spec.Template.Annotations = pgadmin.Spec.Metadata.GetAnnotationsOrNil()
sts.Spec.Template.Annotations = naming.Merge(
pgadmin.Spec.Metadata.GetAnnotationsOrNil(),
map[string]string{
naming.DefaultContainerAnnotation: naming.ContainerPGAdmin,
},
)
sts.Spec.Template.Labels = naming.Merge(
pgadmin.Spec.Metadata.GetLabelsOrNil(),
naming.StandalonePGAdminDataLabels(pgadmin.Name),
Expand Down
3 changes: 3 additions & 0 deletions internal/controller/standalone_pgadmin/statefulset_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ func TestReconcilePGAdminStatefulSet(t *testing.T) {
template.Spec.Volumes = nil

assert.Assert(t, cmp.MarshalMatches(template.ObjectMeta, `
annotations:
kubectl.kubernetes.io/default-container: pgadmin
creationTimestamp: null
labels:
postgres-operator.crunchydata.com/data: pgadmin
Expand Down Expand Up @@ -170,6 +172,7 @@ terminationGracePeriodSeconds: 30
assert.Assert(t, cmp.MarshalMatches(template.ObjectMeta, `
annotations:
annotation1: annotationvalue
kubectl.kubernetes.io/default-container: pgadmin
creationTimestamp: null
labels:
label1: labelvalue
Expand Down
5 changes: 5 additions & 0 deletions internal/naming/annotations.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,9 @@ const (
// to a cluster without backups. As usual with the operator, we do not
// touch cloud-based backups.
AuthorizeBackupRemovalAnnotation = annotationPrefix + "authorizeBackupRemoval"

// Used from Kubernetes v1.21+ to define a default container used when the
// `-c` flag is not passed.
// --https://kubernetes.io/docs/reference/labels-annotations-taints/#kubectl-kubernetes-io-default-container
DefaultContainerAnnotation = "kubectl.kubernetes.io/default-container"
)