Why kubernetes services do not support matchExpressions?

I already asked this on Stack Overflow:

In Kuberntes,
resources, such as Job, Deployment, ReplicaSet, and DaemonSet, support matchExpressions.
but Service resources do not support matchExpressions in spec.selector.

What I tried:

I tried to create a service with matchExpressions in selector:

vim test-svc.yaml

apiVersion: v1
kind: Service
metadata:
  name: my-service
spec:
  selector:
    matchExpressions:
      - { key: appname, operator: In, values: [myapp, myniceapp, mygreatapp]}
  ports:
  - name: tcp
    port: 8080
    targetPort: 8080
  type: ClusterIP

then

kunectl apply -f test-svc.yaml

and I get error:

error: error validating "test-svc.yaml": error validating data: ValidationError(Service.spec.selector.matchExpressions): invalid type for io.k8s.api.core.v1.ServiceSpec.selector: got "array", expected "string"; if you choose to ignore these errors, turn validation off with --validate=false

What I expected:

I expected to be able to create a service with matchExpressions as selector.
and I’m eager to know what is the reasoning behind this limit.

The service API predates “modern” selectors, and because of API compatibility has not yet tried to be updated.

1 Like