Bootstrap FreeKB - OpenShift - matchLabels vs matchExpressions
OpenShift - matchLabels vs matchExpressions

Updated:   |  OpenShift articles

Let's say you have a pod with the following labels.

metadata:
  labels:
    region: east
    type: webapp

 

And you want some other resource to be applied to the pod. This can be done with either matchLabels or matchExpressions. For the sake of this article, let's say you want a Pod Disruption Budget applied to the pod if the pod has labels region: east and type: webapp.

With matchLabels, the Pod Disruption Budget will only be applied to pods that have the same labels as matchLabels. In this example, the Pod Disruption Budget would be applied to the pod since the Pod Disruption Budget has both region: east and type: webapp.

apiVersion: policy/v1
kind: PodDisruptionBudget
metadata:
  name: my-pod-disruption-budget
  namespace: my-namespace
spec:
  minAvailable: 1
  selector:
    matchLabels:
      region: east
      environment: production

 

With matchExpressions, there are 4 possible Operators.

  • In or NotIn
  • Exists or DoesNotExist

With the In Operator, the Pod Disruption Budget will be applied to the pod if the Pod Disruption Budget has both region: east and type: webapp.

apiVersion: policy/v1
kind: PodDisruptionBudget
metadata:
  name: my-pod-disruption-budget
  namespace: my-namespace
spec:
  minAvailable: 1
  selector:
    matchExpressions:
    - { key: region, operator: In, values: [east] }
    - { key: type, operator: In, values: [webapp] }

 

With the NotIn Operator, the Pod Disruption Budget will be applied to the pod if the Pod Disruption Budget does not have region: west and type: service.

apiVersion: policy/v1
kind: PodDisruptionBudget
metadata:
  name: my-pod-disruption-budget
  namespace: my-namespace
spec:
  minAvailable: 1
  selector:
    matchExpressions:
    - { key: region, operator: NotIn, values: [west] }
    - { key: type, operator: NotIn, values: [service] }

 

With the Exists Operator, the Pod Disruption Budget will be applied to the pod if the Pod Disruption Budget has labels region and type, regardless of the values of the keys.

apiVersion: policy/v1
kind: PodDisruptionBudget
metadata:
  name: my-pod-disruption-budget
  namespace: my-namespace
spec:
  minAvailable: 1
  selector:
    matchExpressions:
    - { key: region, operator: Exists }
    - { key: type, operator: Exists }

 

With the DoesNotExist Operator, the Pod Disruption Budget will be applied to the pod if the Pod Disruption Budget does not have labels region and type, regardless of the values of the keys.

apiVersion: policy/v1
kind: PodDisruptionBudget
metadata:
  name: my-pod-disruption-budget
  namespace: my-namespace
spec:
  minAvailable: 1
  selector:
    matchExpressions:
    - { key: region, operator: DoesNotExist }
    - { key: type, operator: DoesNotExist }

 




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


Please enter 78db85 in the box below so that we can be sure you are a human.