Skip to content

Commit 1a009c2

Browse files
authored
Add default container annotation (#4031)
* Add default container annotation Since K8s 1.21, the kubectl.kubernetes.io/default-container annotation can be added to set a default container, removing the need for a -c flag or eliminating the message about defaulting Note: the `-c` flag can still be used to refer to a different, non-default container. Issues: [PGO-1941]
1 parent ec99117 commit 1a009c2

File tree

12 files changed

+76
-10
lines changed

12 files changed

+76
-10
lines changed

internal/controller/pgupgrade/jobs.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
"k8s.io/apimachinery/pkg/labels"
1717

1818
"github.com/crunchydata/postgres-operator/internal/initialize"
19+
"github.com/crunchydata/postgres-operator/internal/naming"
1920
"github.com/crunchydata/postgres-operator/pkg/apis/postgres-operator.crunchydata.com/v1beta1"
2021
)
2122

@@ -129,7 +130,6 @@ func (r *PGUpgradeReconciler) generateUpgradeJob(
129130
job.Namespace = upgrade.Namespace
130131
job.Name = pgUpgradeJob(upgrade).Name
131132

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

148+
job.Annotations = Merge(upgrade.Spec.Metadata.GetAnnotationsOrNil(),
149+
map[string]string{
150+
naming.DefaultContainerAnnotation: database.Name,
151+
})
152+
148153
// Copy the pod template from the startup instance StatefulSet. This includes
149154
// the service account, volumes, DNS policies, and scheduling constraints.
150155
startup.Spec.Template.DeepCopyInto(&job.Spec.Template)
@@ -241,7 +246,6 @@ func (r *PGUpgradeReconciler) generateRemoveDataJob(
241246
job.Namespace = upgrade.Namespace
242247
job.Name = upgrade.Name + "-" + sts.Name
243248

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

@@ -254,6 +258,11 @@ func (r *PGUpgradeReconciler) generateRemoveDataJob(
254258
}
255259
}
256260

261+
job.Annotations = Merge(upgrade.Spec.Metadata.GetAnnotationsOrNil(),
262+
map[string]string{
263+
naming.DefaultContainerAnnotation: database.Name,
264+
})
265+
257266
// Copy the pod template from the sts instance StatefulSet. This includes
258267
// the service account, volumes, DNS policies, and scheduling constraints.
259268
sts.Spec.Template.DeepCopyInto(&job.Spec.Template)

internal/controller/pgupgrade/jobs_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ func TestGenerateUpgradeJob(t *testing.T) {
6262
apiVersion: batch/v1
6363
kind: Job
6464
metadata:
65+
annotations:
66+
kubectl.kubernetes.io/default-container: database
6567
creationTimestamp: null
6668
labels:
6769
postgres-operator.crunchydata.com/cluster: pg5
@@ -81,6 +83,8 @@ spec:
8183
backoffLimit: 0
8284
template:
8385
metadata:
86+
annotations:
87+
kubectl.kubernetes.io/default-container: database
8488
creationTimestamp: null
8589
labels:
8690
postgres-operator.crunchydata.com/cluster: pg5
@@ -193,6 +197,8 @@ func TestGenerateRemoveDataJob(t *testing.T) {
193197
apiVersion: batch/v1
194198
kind: Job
195199
metadata:
200+
annotations:
201+
kubectl.kubernetes.io/default-container: database
196202
creationTimestamp: null
197203
labels:
198204
postgres-operator.crunchydata.com/cluster: pg5
@@ -211,6 +217,8 @@ spec:
211217
backoffLimit: 0
212218
template:
213219
metadata:
220+
annotations:
221+
kubectl.kubernetes.io/default-container: database
214222
creationTimestamp: null
215223
labels:
216224
postgres-operator.crunchydata.com/cluster: pg5

internal/controller/postgrescluster/instance.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1265,6 +1265,9 @@ func generateInstanceStatefulSetIntent(_ context.Context,
12651265
sts.Spec.Template.Annotations = naming.Merge(
12661266
cluster.Spec.Metadata.GetAnnotationsOrNil(),
12671267
spec.Metadata.GetAnnotationsOrNil(),
1268+
map[string]string{
1269+
naming.DefaultContainerAnnotation: naming.ContainerDatabase,
1270+
},
12681271
)
12691272
sts.Spec.Template.Labels = naming.Merge(
12701273
cluster.Spec.Metadata.GetLabelsOrNil(),

internal/controller/postgrescluster/pgadmin.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,11 @@ func (r *Reconciler) reconcilePGAdminStatefulSet(
258258
}
259259
sts.Spec.Template.Annotations = naming.Merge(
260260
cluster.Spec.Metadata.GetAnnotationsOrNil(),
261-
cluster.Spec.UserInterface.PGAdmin.Metadata.GetAnnotationsOrNil())
261+
cluster.Spec.UserInterface.PGAdmin.Metadata.GetAnnotationsOrNil(),
262+
map[string]string{
263+
naming.DefaultContainerAnnotation: naming.ContainerPGAdmin,
264+
},
265+
)
262266
sts.Spec.Template.Labels = naming.Merge(
263267
cluster.Spec.Metadata.GetLabelsOrNil(),
264268
cluster.Spec.UserInterface.PGAdmin.Metadata.GetLabelsOrNil(),

internal/controller/postgrescluster/pgadmin_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -500,6 +500,8 @@ func TestReconcilePGAdminStatefulSet(t *testing.T) {
500500
template.Spec.Volumes = nil
501501

502502
assert.Assert(t, cmp.MarshalMatches(template.ObjectMeta, `
503+
annotations:
504+
kubectl.kubernetes.io/default-container: pgadmin
503505
creationTimestamp: null
504506
labels:
505507
postgres-operator.crunchydata.com/cluster: test-cluster
@@ -613,6 +615,7 @@ terminationGracePeriodSeconds: 30
613615
assert.Assert(t, cmp.MarshalMatches(template.ObjectMeta, `
614616
annotations:
615617
annotation1: annotationvalue
618+
kubectl.kubernetes.io/default-container: pgadmin
616619
creationTimestamp: null
617620
labels:
618621
label1: labelvalue

internal/controller/postgrescluster/pgbackrest.go

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -572,7 +572,11 @@ func (r *Reconciler) generateRepoHostIntent(ctx context.Context, postgresCluster
572572

573573
annotations := naming.Merge(
574574
postgresCluster.Spec.Metadata.GetAnnotationsOrNil(),
575-
postgresCluster.Spec.Backups.PGBackRest.Metadata.GetAnnotationsOrNil())
575+
postgresCluster.Spec.Backups.PGBackRest.Metadata.GetAnnotationsOrNil(),
576+
map[string]string{
577+
naming.DefaultContainerAnnotation: naming.PGBackRestRepoContainerName,
578+
},
579+
)
576580
labels := naming.Merge(
577581
postgresCluster.Spec.Metadata.GetLabelsOrNil(),
578582
postgresCluster.Spec.Backups.PGBackRest.Metadata.GetLabelsOrNil(),
@@ -792,6 +796,14 @@ func generateBackupJobSpecIntent(ctx context.Context, postgresCluster *v1beta1.P
792796
container.Resources = postgresCluster.Spec.Backups.PGBackRest.Jobs.Resources
793797
}
794798

799+
if annotations != nil {
800+
annotations[naming.DefaultContainerAnnotation] = naming.PGBackRestRepoContainerName
801+
} else {
802+
annotations = map[string]string{
803+
naming.DefaultContainerAnnotation: naming.PGBackRestRepoContainerName,
804+
}
805+
}
806+
795807
jobSpec := &batchv1.JobSpec{
796808
Template: corev1.PodTemplateSpec{
797809
ObjectMeta: metav1.ObjectMeta{Labels: labels, Annotations: annotations},
@@ -1259,7 +1271,10 @@ func (r *Reconciler) generateRestoreJobIntent(cluster *v1beta1.PostgresCluster,
12591271
annotations := naming.Merge(
12601272
cluster.Spec.Metadata.GetAnnotationsOrNil(),
12611273
cluster.Spec.Backups.PGBackRest.Metadata.GetAnnotationsOrNil(),
1262-
map[string]string{naming.PGBackRestConfigHash: configHash})
1274+
map[string]string{
1275+
naming.PGBackRestConfigHash: configHash,
1276+
naming.DefaultContainerAnnotation: naming.PGBackRestRestoreContainerName,
1277+
})
12631278
labels := naming.Merge(
12641279
cluster.Spec.Metadata.GetLabelsOrNil(),
12651280
cluster.Spec.Backups.PGBackRest.Metadata.GetLabelsOrNil(),

internal/controller/postgrescluster/pgbouncer.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -371,7 +371,11 @@ func (r *Reconciler) generatePGBouncerDeployment(
371371
}
372372
deploy.Spec.Template.Annotations = naming.Merge(
373373
cluster.Spec.Metadata.GetAnnotationsOrNil(),
374-
cluster.Spec.Proxy.PGBouncer.Metadata.GetAnnotationsOrNil())
374+
cluster.Spec.Proxy.PGBouncer.Metadata.GetAnnotationsOrNil(),
375+
map[string]string{
376+
naming.DefaultContainerAnnotation: naming.ContainerPGBouncer,
377+
},
378+
)
375379
deploy.Spec.Template.Labels = naming.Merge(
376380
cluster.Spec.Metadata.GetLabelsOrNil(),
377381
cluster.Spec.Proxy.PGBouncer.Metadata.GetLabelsOrNil(),

internal/controller/postgrescluster/pgbouncer_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -443,6 +443,7 @@ namespace: ns3
443443
// Annotations present in the pod template.
444444
assert.DeepEqual(t, deploy.Spec.Template.Annotations, map[string]string{
445445
"a": "v1",
446+
"kubectl.kubernetes.io/default-container": "pgbouncer",
446447
})
447448

448449
// Labels present in the pod template.

internal/controller/postgrescluster/volumes.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -469,7 +469,9 @@ func (r *Reconciler) reconcileMovePGDataDir(ctx context.Context,
469469

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

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

709713
jobSpec := &batchv1.JobSpec{
710714
Template: corev1.PodTemplateSpec{
711-
ObjectMeta: metav1.ObjectMeta{Labels: labels},
715+
ObjectMeta: metav1.ObjectMeta{Labels: labels, Annotations: map[string]string{
716+
naming.DefaultContainerAnnotation: naming.ContainerJobMovePGBackRestRepoDir,
717+
}},
712718
Spec: corev1.PodSpec{
713719
// Set the image pull secrets, if any exist.
714720
// This is set here rather than using the service account due to the lack

internal/controller/standalone_pgadmin/statefulset.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,12 @@ func statefulset(
7474
sts.Spec.Selector = &metav1.LabelSelector{
7575
MatchLabels: naming.StandalonePGAdminLabels(pgadmin.Name),
7676
}
77-
sts.Spec.Template.Annotations = pgadmin.Spec.Metadata.GetAnnotationsOrNil()
77+
sts.Spec.Template.Annotations = naming.Merge(
78+
pgadmin.Spec.Metadata.GetAnnotationsOrNil(),
79+
map[string]string{
80+
naming.DefaultContainerAnnotation: naming.ContainerPGAdmin,
81+
},
82+
)
7883
sts.Spec.Template.Labels = naming.Merge(
7984
pgadmin.Spec.Metadata.GetLabelsOrNil(),
8085
naming.StandalonePGAdminDataLabels(pgadmin.Name),

0 commit comments

Comments
 (0)