matchExpressions not working in setbased selectors ins Kubernetes

6/20/2020

I am trying to used set based selectors for replica set in kubernetes. I am not able to use match expressions for setting the filter condition.

Replica set yaml file.

# ngnix-replication-set.yaml
apiVersion: apps/v1
kind: ReplicaSet
metadata:
    name: nginx-rs
    # label not required for RC unless it's being referred to somewhere else.
spec:
    replicas: 3
    selector: # set based selector, can be used to select multiple options in single selection
         matchLabels :
            app : nginx
        ** matchExpressions :   # match expressions are not working  
         -{key:environment,operator:In,values:[dev]}
         -{key: tier, operator: NotIn, values: [frontend,backend]}  **
    template :
        metadata:
            name: nginx-pod
            labels:
                app: nginx  #this spec is used in selector
                tier: server
                environment : dev
        spec:
            containers:
            -   name: nginx-container
                image : nginx
                ports:
                -   containerPort: 80   

Error:

ngnix-replication-set.yaml": error validating data: ValidationError(ReplicaSet.spec.selector.matchExpressions): invalid type for io.k8s.apimachinery.pkg.apis.meta.v1.LabelSelector.matchExpressions: got "string", expected "array"; if you choose to ignore these errors, turn validation off with --validate=false

-- Raptor0009
google-kubernetes-engine
kubernetes
kubernetes-pod

1 Answer

6/20/2020

From the docs here this seems like a indentation issue. Below should work.

    ...
    selector:
      matchLabels:
        app: nginx
      matchExpressions:  
        - {key: environment, operator: In, values: [dev]}
        - {key: tier, operator: NotIn, values: [frontend,backend]}
    ...
-- Arghya Sadhu
Source: StackOverflow