<p>I am facing a weird behaviour with kubectl and --dry-run.</p>

<p>To simplify let's say that I have the following yaml file:</p>

<pre><code>apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  labels:
    run: nginx
  name: nginx
spec:
  replicas: 3
  selector:
    matchLabels:
      run: nginx
  strategy:
    rollingUpdate:
      maxSurge: 1
      maxUnavailable: 1
    type: RollingUpdate
  template:
    metadata:
      creationTimestamp: null
      labels:
        run: nginx
    spec:
      containers:
      - image: nginxsdf
        imagePullPolicy: Always
        name: nginx
</code></pre>

<p>Modifying for example the image or the number of replicas:</p>

<ul>
<li><p><code>kubectl apply -f Deployment.yaml  -o yaml  --dry-run</code> outputs me the resource having the <strong>OLD</strong> specifications</p></li>
<li><p><code>kubectl apply -f Deployment.yaml  -o yaml</code> outputs me the resource having <strong>NEW</strong> specifications</p></li>
</ul>

<p>According to the documentation:</p>

<blockquote>
  <p>--dry-run=false: If true, only print the object that would be sent, without sending it.</p>
</blockquote>

<p>However the object printed is the old one and not the one that will be sent to the ApiServer</p>

<p>Tested on minikube, gke v1.10.0</p>

<p>In the meanwhile I opened a new gitHub issue for it:</p>

<ul>
<li><a href="https://github.com/kubernetes/kubernetes/issues/72644" rel="noreferrer">https://github.com/kubernetes/kubernetes/issues/72644</a></li>
</ul>

GalloCedrone
<p>I got the following answer in the kubernetes issue page:</p>
<blockquote>
<p>When updating existing objects, kubectl apply doesn't send an entire object, just a patch. It is not exactly correct to print either the existing object or the new object in dry-run mode... the outcome of the merge is what should be printed.</p>
<p>For kubectl to be able to accurately reflect the result of the apply, it would need to have the server-side apply logic clientside, which is a non-goal.</p>
<p>Current efforts are directed at moving apply logic to the server. As part of that, the ability to dry-run server-side has been added. <code>kubectl apply --server-dry-run</code> will do what you want, printing the result of the apply merge, without actually persisting it.</p>
<p>@apelisse we should probably update the flag help for apply and possibly print a warning when using --dry-run when updating an object via apply to document the limitations of --dry-run and direct people to use --server-dry-run</p>
</blockquote>
