mayadata-io / d-operators

Declarative patterns to write kubernetes controllers
Apache License 2.0
10 stars 7 forks source link

yaml based assertion #6

Open AmitKumarDas opened 4 years ago

AmitKumarDas commented 4 years ago

As a developer I want to use better assertion logic than the following:

- name: Fetch OpenEBS control plane pods ready status
  shell: kubectl get pods -n openebs | grep {{ item }} | awk '{print $2}' | awk -F'/' '{print $1}' && kubectl get pods -n openebs | grep {{ item }} | awk '{print $2}' | awk -F'/' '{print $2}'
  register: ready_status
  with_items:
    - '{{ openebs_components }}'
  until: '{{ ready_status.stdout_lines | unique | length == 1 }}'
  retries: 20
  delay: 5

It will be great to assert list of values in a declarative manner.

For example below yaml should be enough to assert any Pods with the given label to be in Running state. It should return error if there are any Pods that are not Running.

kind: Pod
metadata:
  labels:
    name: app
status:
  phase: Running
AmitKumarDas commented 4 years ago

UseCase 1: To further our expectation of declarative assertion, let us try to assert any Deployment that has one of its containers with a nginx image with name my-nginx. Solution: The assertion yaml should look like below:

apiVersion: apps/v1
kind: Deployment
spec:
  template:
    spec:
      containers:
      - name: my-nginx
        image: nginx

UseCase 2: Assert any Deployment that has nginx image with container port 80 Solution: The assertion yaml should look like following:

apiVersion: apps/v1
kind: Deployment
spec:
  template:
    spec:
      containers:
      - image: nginx
        ports:
        - containerPort: 80

UseCase 3: Assert 3 Pods with label app:run in Running state Solution: The assertion yaml should look like following:

kind: PodList
count: 3
items:
- kind: Pod
  metadata:
    labels:
      app: run
  status:
    phase: Running

UseCase 4: Assert 2 Pods with label app:run & app:running in Running state Solution: The assertion yaml should look like following:

kind: PodList
items:
- kind: Pod
  metadata:
    labels:
      app: run
  status:
    phase: Running
- kind: Pod
  metadata:
    labels:
      app: running
  status:
    phase: Running