<p>After fixing the problem from this topic <a href="https://stackoverflow.com/questions/58556424/cant-use-google-cloud-kubernetes-substitutions/58560129?noredirect=1#comment103494292_58560129">Can&#39;t use Google Cloud Kubernetes substitutions</a> (yaml files are all there, to not copy-paste them once again) I got a new problem. Making a new topic because there is the correct answer for the previous one.</p>

<blockquote>
  <p>Step #2: Running: kubectl apply -f deployment.yaml
  <br>
  Step #2: Warning:
  kubectl apply should be used on resource created by either kubectl
  create --save-config or kubectl apply
  <br>
  Step #2: The Deployment
  "myproject" is invalid: spec.selector: Invalid value:
  v1.LabelSelector{MatchLabels:map[string]string{"app":"myproject",
  "run":"myproject"},
  MatchExpressions:[]v1.LabelSelectorRequirement(nil)}: field is
  immutable<br></p>
</blockquote>

<p>I've checked similar issues but hasn't been able to find anything related.<br><br>
Also, is that possible that this error related to upgrading App Engine -> Docker -> Kubernetes? I created valid  configuration on each step. Maybe there are some things that were created and immutable now? What should I do in this case?<br></p>

<p>One more note, maybe that matters, it says "kubectl apply should be used on resource created by either kubectl create --save-config or kubectl apply" (you can see above), but executing<br></p>

<pre><code>kubectl create deployment myproject --image=gcr.io/myproject/myproject
</code></pre>

<p>gives me this</p>

<blockquote>
  <p>Error from server (AlreadyExists): deployments.apps "myproject" already exists</p>
</blockquote>

<p>which is actually expected, but, at the same time, controversial with warning above (at least from my prospective)<br><br></p>

<p>Any idea?</p>

<p>Output of <code>kubectl version</code></p>

<pre><code>Client Version: version.Info{Major:"1", Minor:"14", GitVersion:"v1.14.7", GitCommit:"8fca2ec50a6133511b771a11559e24191b1aa2b4", GitTreeState:"clean", BuildDate:"2019-09-18T14:47:22Z", GoVersion:"go1.12.9", Compiler:"gc", Platform:"darwin/amd64"}
Server Version: version.Info{Major:"1", Minor:"13+", GitVersion:"v1.13.11-gke.14", GitCommit:"56d89863d1033f9668ddd6e1c1aea81cd846ef88", GitTreeState:"clean", BuildDate:"2019-11-07T19:12:22Z", GoVersion:"go1.12.11b4", Compiler:"gc", Platform:"linux/amd64"}
</code></pre>

<p><br><br>
Current YAML file:</p>

<pre><code>steps:
  - name: 'gcr.io/cloud-builders/docker'
    entrypoint: 'bash'
    args: [
      '-c',
      'docker pull gcr.io/$PROJECT_ID/myproject:latest || exit 0'
    ]
  - name: 'gcr.io/cloud-builders/docker'
    args: [
      'build',
      '-t',
      'gcr.io/$PROJECT_ID/myproject:$BRANCH_NAME-$COMMIT_SHA',
      '-t',
      'gcr.io/$PROJECT_ID/myproject:latest',
      '.'
    ]
  - name: 'gcr.io/cloud-builders/kubectl'
    args: [ 'apply', '-f', 'deployment.yaml' ]
    env:
      - 'CLOUDSDK_COMPUTE_ZONE=&lt;region&gt;'
      - 'CLOUDSDK_CONTAINER_CLUSTER=myproject'
  - name: 'gcr.io/cloud-builders/kubectl'
    args: [
      'set',
      'image',
      'deployment',
      'myproject',
      'myproject=gcr.io/$PROJECT_ID/myproject:$BRANCH_NAME-$COMMIT_SHA'
    ]
    env:
      - 'CLOUDSDK_COMPUTE_ZONE=&lt;region&gt;'
      - 'CLOUDSDK_CONTAINER_CLUSTER=myproject'
      - 'DB_PORT=5432'
      - 'DB_SCHEMA=public'
      - 'TYPEORM_CONNECTION=postgres'
      - 'FE=myproject'
      - 'V=1'
      - 'CLEAR_DB=true'
      - 'BUCKET_NAME=myproject'
      - 'BUCKET_TYPE=google'
      - 'KMS_KEY_NAME=storagekey'
timeout: 1600s
images:
  - 'gcr.io/$PROJECT_ID/myproject:$BRANCH_NAME-$COMMIT_SHA'
  - 'gcr.io/$PROJECT_ID/myproject:latest
</code></pre>

<p>deployment.yaml</p>

<pre><code>apiVersion: apps/v1
kind: Deployment
metadata:
  name: myproject
spec:
  replicas: 1
  selector:
    matchLabels:
      app: myproject
  template:
    metadata:
      labels:
        app: myproject
    spec:
      containers:
        - name: myproject
          image: gcr.io/myproject/github.com/weekendman/{{repo name here}}:latest
          ports:
            - containerPort: 80
</code></pre>

<p>From apps/v1 on,  a Deployment’s label selector is immutable after it gets created.</p>

<p>excerpt from Kubernetes's <a href="https://kubernetes.io/docs/concepts/workloads/controllers/deployment/#label-selector-updates" rel="noreferrer">document</a>:</p>

<blockquote>
  <p>Note: In API version apps/v1, a Deployment’s label selector is
  immutable after it gets created.</p>
</blockquote>

<p>So, you can delete this deployment first, then apply it.</p>
