zalando / postgres-operator

Postgres operator creates and manages PostgreSQL clusters running in Kubernetes
https://postgres-operator.readthedocs.io/
MIT License
4.37k stars 981 forks source link

Sidecar ports and env settings are not parsed and created in statefulset. #1245

Open sgrzemski-ias opened 3 years ago

sgrzemski-ias commented 3 years ago

Hi, I am trying to deploy my first database using your postgres-operator. I am creating a simple postgresql object with postgres_exporter as a sidecar for monitoring purposes.

kind: postgresql
metadata:
  name: acid-minimal-cluster
spec:
  teamId: "ACID"
  volume:
    size: 1Gi
  numberOfInstances: 2
  users:
    # database owner
    zalando:
    - superuser
    - createdb
  databases:
    foo: zalando
  postgresql:
    version: "10"
  sidecars:
    - name: postgres-exporter
      image: wrouesnel/postgres_exporter
      ports:
        - containerPort: 9187

It comes from minimal example that can be found here: https://postgres-operator.readthedocs.io/en/refactoring-sidecars/user/ After applying this resource, a statefulset is created, but it has some values missing:

        - name: POD_NAME
          valueFrom:
            fieldRef:
              apiVersion: v1
              fieldPath: metadata.name
        - name: POD_NAMESPACE
          valueFrom:
            fieldRef:
              apiVersion: v1
              fieldPath: metadata.namespace
        - name: POSTGRES_USER
          value: postgres
        - name: POSTGRES_PASSWORD
          valueFrom:
            secretKeyRef:
              key: password
              name: postgres.acid-minimal-cluster.credentials.postgresql.acid.zalan.do
        image: wrouesnel/postgres_exporter
        imagePullPolicy: IfNotPresent
        name: postgres-exporter
        ports:
        - containerPort: 0
          protocol: TCP
        resources:
          limits:
            cpu: "1"
            memory: 500Mi
          requests:
            cpu: 100m
            memory: 100Mi
        terminationMessagePath: /dev/termination-log
        terminationMessagePolicy: File
        volumeMounts:
        - mountPath: /home/postgres/pgdata
          name: pgdata

Same happens with variables, they are created as empty entries - {} and I cannot get my sidecar working. As a result, the statefulset is failing to create pods: Warning FailedCreate 38s (x17 over 6m6s) statefulset-controller create Pod acid-minimal-cluster-0 in StatefulSet acid-minimal-cluster failed error: Pod "acid-minimal-cluster-0" is invalid: spec.containers[1].ports[0].containerPort: Required value

FxKu commented 3 years ago

Looks duplicate to #1206

Have you updated your K8s version recently? Try to apply the latest version of the CRD and check that the x-kubernetes-preserve-unknown-fields properties are set wherever we allow full specs. Otherwise they will get pruned.

sgrzemski-ias commented 3 years ago

Yep, applied latest CRD and it works. x-kubernetes-preserve-unknown-fields is present in my current crd:

                      items:
                        type: string
                      nullable: true
                      type: array
                    volumeSource:
                      type: object
                      x-kubernetes-preserve-unknown-fields: true
                  required:
                  - name
                  - mountPath
                  - volumeSource
                  type: object
                type: array
--
                type: boolean
              enableShmVolume:
                type: boolean
              init_containers:
                items:
                  type: object
                  x-kubernetes-preserve-unknown-fields: true
                nullable: true
                type: array
              initContainers:
                items:
                  type: object
                  x-kubernetes-preserve-unknown-fields: true
                nullable: true
                type: array
              logicalBackupSchedule:
                pattern: ^(\d+|\*)(/\d+)?(\s+(\d+|\*)(/\d+)?){4}$
                type: string
              maintenanceWindows:
--
                additionalProperties:
                  type: string
                type: object
              sidecars:
                items:
                  type: object
                  x-kubernetes-preserve-unknown-fields: true
                nullable: true
                type: array
              spiloFSGroup:
                type: integer
              spiloRunAsGroup:
                type: integer
debu99 commented 3 years ago

@sgrzemski-ias have you checked customized environment variable for sidecar, i found i already used the latest CRD

sgrzemski-ias commented 3 years ago

No, I have to take my words back. I've just deployed a new cluster with 1.16.15-gke.4300 version and I cannot get it working.

Installed with:

    - git clone https://github.com/zalando/postgres-operator.git
    - cd postgres-operator
    - helm install postgres-operator ./charts/postgres-operator -f ./charts/postgres-operator/values-crd.yaml --namespace databases --create-namespace

Then applied this:

---
# Source: metadata-databases/templates/ranger.yml
apiVersion: "acid.zalan.do/v1"
kind: postgresql
metadata:
  name: gid-ranger-metadata
  namespace: databases
  labels:
    app.kubernetes.io/name: metadata-databases
    helm.sh/chart: metadata-databases-0.1.0
    app.kubernetes.io/managed-by: Helm
    app.kubernetes.io/instance: gidp-database
spec:
  teamId: gid
  volume:
    size: 5Gi
  numberOfInstances: 1
  users:
    ranger:
    - superuser
    - createdb
  databases:
    ranger: ranger
  postgresql:
    version: "11"
  enableLogicalBackup: false
  logicalBackupSchedule: "30 00 * * *"
  sidecars:
    - name: "pg-exporter"
      image: "wrouesnel/postgres_exporter"
      ports:
        - name: pg-exporter
          containerPort: 9187
          protocol: TCP
      resources:
        limits:
          cpu: 500m
          memory: 256M
        requests:
          cpu: 100m
          memory: 200M
      env:
        - name: "DATA_SOURCE_URI"
          value: "postgresql://localhost:5432/ranger?sslmode=disable"
        - name: "DATA_SOURCE_USER"
          value: $POSTGRES_USER
        - name: "DATA_SOURCE_PASS"
          value: $POSTGRES_PASSWORD

but no luck, fields are empty

FxKu commented 3 years ago

@sgrzemski-ias have you changed the operator version in helm's values-crd.yaml to use the latest version?