Skip to content

K8SPG-833: add env and envFrom fields #1231

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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

Large diffs are not rendered by default.

679 changes: 674 additions & 5 deletions build/crd/percona/generated/pgv2.percona.com_perconapgclusters.yaml

Large diffs are not rendered by default.

787 changes: 728 additions & 59 deletions config/crd/bases/pgv2.percona.com_perconapgclusters.yaml

Large diffs are not rendered by default.

Large diffs are not rendered by default.

1,342 changes: 1,342 additions & 0 deletions deploy/bundle.yaml

Large diffs are not rendered by default.

24 changes: 24 additions & 0 deletions deploy/cr.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,12 @@ spec:
instances:
- name: instance1
replicas: 3
# env:
# - name: MY_ENV
# value: "1000"
# envFrom:
# - secretRef:
# name: instance-env-secret
# initContainer:
# image: docker.io/perconalab/percona-postgresql-operator:main
# resources:
Expand Down Expand Up @@ -307,6 +313,12 @@ spec:
pgBouncer:
replicas: 3
image: docker.io/perconalab/percona-postgresql-operator:main-pgbouncer17
# env:
# - name: MY_ENV
# value: "1000"
# envFrom:
# - secretRef:
# name: pgbouncer-env-secret
# exposeSuperusers: true
# resources:
# limits:
Expand Down Expand Up @@ -449,6 +461,12 @@ spec:
# - secret:
# name: cluster1-pgbackrest-secrets
# jobs:
# env:
# - name: MY_ENV
# value: "1000"
# envFrom:
# - secretRef:
# name: backup-env-secret
# restartPolicy: OnFailure
# backoffLimit: 2
# priorityClassName: high-priority
Expand Down Expand Up @@ -496,6 +514,12 @@ spec:
# repo3-path: /pgbackrest/postgres-operator/cluster1-multi-repo/repo3
# repo4-path: /pgbackrest/postgres-operator/cluster1-multi-repo/repo4
repoHost:
# env:
# - name: MY_ENV
# value: "1000"
# envFrom:
# - secretRef:
# name: repo-host-env-secret
# resources:
# limits:
# cpu: 200m
Expand Down
1,342 changes: 1,342 additions & 0 deletions deploy/crd.yaml

Large diffs are not rendered by default.

1,342 changes: 1,342 additions & 0 deletions deploy/cw-bundle.yaml

Large diffs are not rendered by default.

12 changes: 12 additions & 0 deletions internal/controller/postgrescluster/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -1243,6 +1243,18 @@ func (r *Reconciler) reconcileInstance(
addDevSHM(&instance.Spec.Template)
}

// K8SPG-833
if cluster.CompareVersion("2.8.0") >= 0 && (len(spec.Env) != 0 || len(spec.EnvFrom) != 0) {
for i := range instance.Spec.Template.Spec.Containers {
if len(spec.Env) != 0 {
instance.Spec.Template.Spec.Containers[i].Env = append(instance.Spec.Template.Spec.Containers[i].Env, spec.Env...)
}
if len(spec.EnvFrom) != 0 {
instance.Spec.Template.Spec.Containers[i].EnvFrom = append(instance.Spec.Template.Spec.Containers[i].EnvFrom, spec.EnvFrom...)
}
}
}

if err == nil {
err = errors.WithStack(r.apply(ctx, instance))
}
Expand Down
24 changes: 24 additions & 0 deletions internal/controller/postgrescluster/pgbackrest.go
Original file line number Diff line number Diff line change
Expand Up @@ -737,6 +737,18 @@ func (r *Reconciler) generateRepoHostIntent(ctx context.Context, postgresCluster

addTMPEmptyDir(&repo.Spec.Template, sizeLimit)

// K8SPG-833
if repoHost := postgresCluster.Spec.Backups.PGBackRest.RepoHost; postgresCluster.CompareVersion("2.8.0") >= 0 && (len(repoHost.Env) != 0 || len(repoHost.EnvFrom) != 0) {
for i := range repo.Spec.Template.Spec.Containers {
if len(repoHost.Env) != 0 {
repo.Spec.Template.Spec.Containers[i].Env = append(repo.Spec.Template.Spec.Containers[i].Env, repoHost.Env...)
}
if len(repoHost.EnvFrom) != 0 {
repo.Spec.Template.Spec.Containers[i].EnvFrom = append(repo.Spec.Template.Spec.Containers[i].EnvFrom, repoHost.EnvFrom...)
}
}
}

Comment on lines +740 to +751
Copy link
Contributor

Choose a reason for hiding this comment

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

Wondering if (len(repoHost.Env) != 0 || len(repoHost.EnvFrom) != 0) and all the related checks after it make any difference to the implementation. I think we are ok without them given that

	// K8SPG-833
	Env []corev1.EnvVar `json:"env,omitempty"`
	// K8SPG-833
	EnvFrom []corev1.EnvFromSource `json:"envFrom,omitempty"`

// set ownership references
if err := r.setControllerReference(postgresCluster, repo); err != nil {
return nil, err
Expand Down Expand Up @@ -924,6 +936,18 @@ func generateBackupJobSpecIntent(ctx context.Context, postgresCluster *v1beta1.P
if postgresCluster.Spec.Backups.PGBackRest.Jobs.BackoffLimit != nil {
jobSpec.BackoffLimit = postgresCluster.Spec.Backups.PGBackRest.Jobs.BackoffLimit
}

// K8SPG-833
if postgresCluster.CompareVersion("2.8.0") >= 0 && (len(postgresCluster.Spec.Backups.PGBackRest.Jobs.Env) != 0 || len(postgresCluster.Spec.Backups.PGBackRest.Jobs.EnvFrom) != 0) {
for i := range jobSpec.Template.Spec.Containers {
if len(postgresCluster.Spec.Backups.PGBackRest.Jobs.Env) != 0 {
jobSpec.Template.Spec.Containers[i].Env = append(jobSpec.Template.Spec.Containers[i].Env, postgresCluster.Spec.Backups.PGBackRest.Jobs.Env...)
}
if len(postgresCluster.Spec.Backups.PGBackRest.Jobs.EnvFrom) != 0 {
jobSpec.Template.Spec.Containers[i].EnvFrom = append(jobSpec.Template.Spec.Containers[i].EnvFrom, postgresCluster.Spec.Backups.PGBackRest.Jobs.EnvFrom...)
}
}
}
}

// Set the image pull secrets, if any exist.
Expand Down
12 changes: 12 additions & 0 deletions internal/pgbouncer/reconcile.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,18 @@ func Pod(
}

outPod.Volumes = []corev1.Volume{configVolume}

// K8SPG-833
if pgbouncer := inCluster.Spec.Proxy.PGBouncer; inCluster.CompareVersion("2.8.0") >= 0 && pgbouncer != nil && (len(pgbouncer.Env) != 0 || len(pgbouncer.EnvFrom) != 0) {
for i := range outPod.Containers {
if len(pgbouncer.Env) != 0 {
outPod.Containers[i].Env = append(outPod.Containers[i].Env, pgbouncer.Env...)
}
if len(pgbouncer.EnvFrom) != 0 {
outPod.Containers[i].EnvFrom = append(outPod.Containers[i].EnvFrom, pgbouncer.EnvFrom...)
}
}
}
}

// PostgreSQL populates outHBAs with any records needed to run PgBouncer.
Expand Down
191 changes: 191 additions & 0 deletions percona/controller/pgcluster/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1059,6 +1059,197 @@ var _ = Describe("Security context", Ordered, func() {
})
})

var _ = Describe("Envs", Ordered, func() {
ctx := context.Background()

const crName = "envs"
const ns = crName
crNamespacedName := types.NamespacedName{Name: crName, Namespace: ns}

namespace := &corev1.Namespace{
ObjectMeta: metav1.ObjectMeta{
Name: crName,
Namespace: ns,
},
}

BeforeAll(func() {
By("Creating the Namespace to perform the tests")
err := k8sClient.Create(ctx, namespace)
Expect(err).To(Not(HaveOccurred()))
})

AfterAll(func() {
By("Deleting the Namespace to perform the tests")
_ = k8sClient.Delete(ctx, namespace)
})

cr, err := readDefaultCR(crName, ns)
It("should read defautl cr.yaml", func() {
Expect(err).NotTo(HaveOccurred())
})

instanceEnv := []corev1.EnvVar{
{
Name: "INSTANCE_ENV",
Value: "VALUE1",
},
}
instanceEnvFrom := []corev1.EnvFromSource{
{
SecretRef: &corev1.SecretEnvSource{
LocalObjectReference: corev1.LocalObjectReference{
Name: "secret-instance-env",
},
},
},
}

pgbouncerEnv := []corev1.EnvVar{
{
Name: "PGBOUNCER_ENV",
Value: "VALUE2",
},
}
pgbouncerEnvFrom := []corev1.EnvFromSource{
{
SecretRef: &corev1.SecretEnvSource{
LocalObjectReference: corev1.LocalObjectReference{
Name: "secret-pgbouncer-env",
},
},
},
}

repoHostEnv := []corev1.EnvVar{
{
Name: "REPOHOST_ENV",
Value: "VALUE3",
},
}
repoHostEnvFrom := []corev1.EnvFromSource{
{
SecretRef: &corev1.SecretEnvSource{
LocalObjectReference: corev1.LocalObjectReference{
Name: "repohost-pgbouncer-env",
},
},
},
}

It("should create PerconaPGCluster", func() {
for i := range cr.Spec.InstanceSets {
cr.Spec.InstanceSets[i].Env = []corev1.EnvVar{
{
Name: "INSTANCE_ENV",
Value: "VALUE1",
},
}

cr.Spec.InstanceSets[i].EnvFrom = []corev1.EnvFromSource{
{
SecretRef: &corev1.SecretEnvSource{
LocalObjectReference: corev1.LocalObjectReference{
Name: "secret-instance-env",
},
},
},
}
}
cr.Spec.Proxy.PGBouncer.Env = []corev1.EnvVar{
{
Name: "PGBOUNCER_ENV",
Value: "VALUE2",
},
}
cr.Spec.Proxy.PGBouncer.EnvFrom = []corev1.EnvFromSource{
{
SecretRef: &corev1.SecretEnvSource{
LocalObjectReference: corev1.LocalObjectReference{
Name: "secret-pgbouncer-env",
},
},
},
}
cr.Spec.Backups.PGBackRest.RepoHost.Env = []corev1.EnvVar{
{
Name: "REPOHOST_ENV",
Value: "VALUE3",
},
}
cr.Spec.Backups.PGBackRest.RepoHost.EnvFrom = []corev1.EnvFromSource{
{
SecretRef: &corev1.SecretEnvSource{
LocalObjectReference: corev1.LocalObjectReference{
Name: "repohost-pgbouncer-env",
},
},
},
}
cr.Spec.CRVersion = "2.8.0"

status := cr.Status
Expect(k8sClient.Create(ctx, cr)).Should(Succeed())
cr.Status = status
Expect(k8sClient.Status().Update(ctx, cr)).Should(Succeed())
})

It("should reconcile", func() {
_, err := reconciler(cr).Reconcile(ctx, ctrl.Request{NamespacedName: crNamespacedName})
Expect(err).NotTo(HaveOccurred())
_, err = crunchyReconciler().Reconcile(ctx, ctrl.Request{NamespacedName: crNamespacedName})
Expect(err).NotTo(HaveOccurred())
})

It("Instances should have envs", func() {
stsList := &appsv1.StatefulSetList{}
labels := map[string]string{
"postgres-operator.crunchydata.com/data": "postgres",
"postgres-operator.crunchydata.com/cluster": crName,
}
err = k8sClient.List(ctx, stsList, client.InNamespace(cr.Namespace), client.MatchingLabels(labels))
Expect(err).NotTo(HaveOccurred())
Expect(stsList.Items).NotTo(BeEmpty())

for _, sts := range stsList.Items {
for _, c := range sts.Spec.Template.Spec.Containers {
Expect(c.Env).To(ContainElement(instanceEnv[0]))
Expect(c.EnvFrom).To(Equal(instanceEnvFrom))
}
}
})

It("PgBouncer should have envs", func() {
deployment := &appsv1.Deployment{
ObjectMeta: metav1.ObjectMeta{
Name: crName + "-pgbouncer",
Namespace: cr.Namespace,
},
}
err = k8sClient.Get(ctx, client.ObjectKeyFromObject(deployment), deployment)
Expect(err).NotTo(HaveOccurred())
for _, c := range deployment.Spec.Template.Spec.Containers {
Expect(c.Env).To(ContainElement(pgbouncerEnv[0]))
Expect(c.EnvFrom).To(Equal(pgbouncerEnvFrom))
}
})

It("PgBackrest Repo should have envs", func() {
sts := &appsv1.StatefulSet{
ObjectMeta: metav1.ObjectMeta{
Name: crName + "-repo-host",
Namespace: cr.Namespace,
},
}
err = k8sClient.Get(ctx, client.ObjectKeyFromObject(sts), sts)
Expect(err).NotTo(HaveOccurred())
for _, c := range sts.Spec.Template.Spec.Containers {
Expect(c.Env).To(ContainElement(repoHostEnv[0]))
Expect(c.EnvFrom).To(Equal(repoHostEnvFrom))
}
})
})

var _ = Describe("Operator-created sidecar container resources", Ordered, func() {
ctx := context.Background()

Expand Down
10 changes: 10 additions & 0 deletions pkg/apis/pgv2.percona.com/v2/perconapgcluster_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -805,6 +805,9 @@ type PGInstanceSetSpec struct {
// InitContainer defines the init container for the instance container of a PostgreSQL pod.
// +optional
InitContainer *crunchyv1beta1.InitContainerSpec `json:"initContainer,omitempty"`

Env []corev1.EnvVar `json:"env,omitempty"`
EnvFrom []corev1.EnvFromSource `json:"envFrom,omitempty"`
}

func (p PGInstanceSetSpec) ToCrunchy() crunchyv1beta1.PostgresInstanceSetSpec {
Expand All @@ -827,6 +830,8 @@ func (p PGInstanceSetSpec) ToCrunchy() crunchyv1beta1.PostgresInstanceSetSpec {
SecurityContext: p.SecurityContext,
TablespaceVolumes: p.TablespaceVolumes,
InitContainer: p.InitContainer,
Env: p.Env,
EnvFrom: p.EnvFrom,
}
}

Expand Down Expand Up @@ -993,6 +998,9 @@ type PGBouncerSpec struct {
// SecurityContext defines the security settings for PGBouncer pods.
// +optional
SecurityContext *corev1.PodSecurityContext `json:"securityContext,omitempty"`

Env []corev1.EnvVar `json:"env,omitempty"`
EnvFrom []corev1.EnvFromSource `json:"envFrom,omitempty"`
}

func (p *PGBouncerSpec) ToCrunchy(version string) *crunchyv1beta1.PGBouncerPodSpec {
Expand All @@ -1018,6 +1026,8 @@ func (p *PGBouncerSpec) ToCrunchy(version string) *crunchyv1beta1.PGBouncerPodSp
Tolerations: p.Tolerations,
TopologySpreadConstraints: p.TopologySpreadConstraints,
SecurityContext: p.SecurityContext,
Env: p.Env,
EnvFrom: p.EnvFrom,
}

spec.Default()
Expand Down
Loading
Loading