<p>I am trying to create a service for a set of pods based on certain selectors. For example, the below <code>get pods</code> command retrieves the right pods for my requirement - </p>

<pre><code>kubectl get pods --selector property1=dev,property2!=admin
</code></pre>

<p>Below is the extract of the service definition yaml where I am attempting to using the same selectors as above - </p>

<pre><code>apiVersion: v1
kind: Service
metadata:
  name: service1
spec:
  type: NodePort
  ports:
    - name: port1
      port: 30303
      targetPort: 30303
  selector:
    property1: dev
    &lt;&lt; property2: ???? &gt;&gt;&gt;

</code></pre>

<p>I have tried <code>matchExpressions</code> without realizing that <a href="https://kubernetes.io/docs/concepts/overview/working-with-objects/labels/#resources-that-support-set-based-requirements" rel="noreferrer">service is not among the resources that support set-based filters</a>. It resulted in the following error - </p>

<pre><code>error: error validating "STDIN": 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
</code></pre>

<p>I am running upstream Kubernetes 1.12.5</p>

<p>I've did some test but I am afraid it is not possible. As per <a href="https://kubernetes.io/docs/concepts/overview/working-with-objects/labels/#label-selectors" rel="nofollow noreferrer">docs</a> API supports two types of selectors:</p>

<ul>
<li><a href="https://kubernetes.io/docs/concepts/overview/working-with-objects/labels/#equality-based-requirement" rel="nofollow noreferrer">Equality-based</a></li>
<li><a href="https://kubernetes.io/docs/concepts/overview/working-with-objects/labels/#set-based-requirement" rel="nofollow noreferrer">Set-based</a></li>
</ul>

<p><code>kubeclt</code> allows to use operators like <code>=</code>,<code>==</code> and <code>!=</code>. So it works when you are using <code>$ kubectl get pods --selector property1=dev,property2!=admin</code>.</p>

<p>Configuration which you want to apply would work in <code>set-based</code> option as it supports <code>in</code>, <code>notin</code> and <code>exists</code>:</p>

<blockquote>
  <p>environment in (production, qa)</p>
  
  <p>tier notin (frontend, backend)</p>
  
  <p>partition</p>
  
  <p>!partition</p>
</blockquote>

<p>Unfortunately <code>set-based</code> is supported only by newer resurces as <code>Job</code>, <code>Deployment</code>, <code>Replica Set</code> and <code>Deamon Set</code> but is <strong>not supporting <code>services</code></strong>.  </p>

<p>More information about this can be found <a href="https://kubernetes.io/docs/concepts/overview/working-with-objects/labels/#resources-that-support-set-based-requirements" rel="nofollow noreferrer">here</a>. </p>

<p>Even if you will set selector in YAML as:</p>

<pre><code>property2: !value 
</code></pre>

<p>In service, <code>property2</code> will be without any value.</p>

<p><code>Selector:                 property1=dev,property2=</code></p>

<p>As additional information <code>,</code> is recognized as <code>AND</code> in <code>services</code>.</p>

<p>As I am not aware how you are managing your cluster, the only thing I can advise is to redefine labels to use only <code>AND</code> as logical operator.</p>
