pkaramol
pkaramol

Reputation: 19342

Unknown and noisy fields when executing kubectl run and outputting to yaml

I am trying to create a pod with dru-run and check it in yaml as follows:

k run --restart=Never --image=busybox busybox --dry-run=server -o yaml -- env > mypod.yaml

The produced yaml file has a ton of fields unknown to me such as:

apiVersion: v1
kind: Pod
metadata:
  creationTimestamp: "2020-11-23T15:33:39Z"
  labels:
    run: busybox
  managedFields:
  - apiVersion: v1
    fieldsType: FieldsV1
    fieldsV1:
      f:metadata:
        f:labels:
          .: {}
          f:run: {}
      f:spec:
        f:containers:
          k:{"name":"busybox"}:
            .: {}
            f:args: {}
            f:image: {}
            f:imagePullPolicy: {}
            f:name: {}
            f:resources: {}
            f:terminationMessagePath: {}
            f:terminationMessagePolicy: {}
        f:dnsPolicy: {}
        f:enableServiceLinks: {}
        f:restartPolicy: {}
        f:schedulerName: {}
        f:securityContext: {}
        f:terminationGracePeriodSeconds: {}
    manager: kubectl-run
    operation: Update
    time: "2020-11-23T15:33:39Z"
  name: busybox
  namespace: mynamespace
  selfLink: /api/v1/namespaces/mynamespace/pods/busybox
  uid: 79e514a9-3c55-4d62-8068-9244252efaa3
spec:
  containers:
  - args:
    - env
    image: busybox
    imagePullPolicy: Always
    name: busybox
    resources: {}
    terminationMessagePath: /dev/termination-log
    terminationMessagePolicy: File
    volumeMounts:
    - mountPath: /var/run/secrets/kubernetes.io/serviceaccount
      name: default-token-h57fg
      readOnly: true
  dnsPolicy: ClusterFirst
  enableServiceLinks: true
  preemptionPolicy: PreemptLowerPriority
  priority: 0
  restartPolicy: Never
  schedulerName: default-scheduler
  securityContext: {}
  serviceAccount: default
  serviceAccountName: default
  terminationGracePeriodSeconds: 30
  tolerations:
  - effect: NoExecute
    key: node.kubernetes.io/not-ready
    operator: Exists
    tolerationSeconds: 300
  - effect: NoExecute
    key: node.kubernetes.io/unreachable
    operator: Exists
    tolerationSeconds: 300
  volumes:
  - name: default-token-h57fg
    secret:
      defaultMode: 420
      secretName: default-token-h57fg
status:
  phase: Pending
  qosClass: BestEffort

Is there a way to avoid these fields being printed out? (and what is their purpose)

working with

k version
Client Version: version.Info{Major:"1", Minor:"19", GitVersion:"v1.19.0", GitCommit:"e19964183377d0ec2052d1f1fa930c4d7575bd50", GitTreeState:"clean", BuildDate:"2020-08-26T14:30:33Z", GoVersion:"go1.15", Compiler:"gc", Platform:"linux/amd64"}
Server Version: version.Info{Major:"1", Minor:"19", GitVersion:"v1.19.0", GitCommit:"e19964183377d0ec2052d1f1fa930c4d7575bd50", GitTreeState:"clean", BuildDate:"2020-08-26T14:23:04Z", GoVersion:"go1.15", Compiler:"gc", Platform:"linux/amd64"}

Upvotes: 1

Views: 773

Answers (1)

Olesya Bolobova
Olesya Bolobova

Reputation: 1653

Update
In Kubernetes 1.21 managedFields has been removed from default kubectl output. https://github.com/kubernetes/kubernetes/pull/96878
Use kubectl --show-managed-fields to display it.


That's a new feature called Server-side Apply, enabled by default since Kubernetes 1.18.
It is intended to improve object merge algorithms by tracking fields ownership.

Server-side Apply works by keeping track of which actor of the system has changed each field of an object. It does so by diffing all updates to objects, and recording all the fields that have changed as well the time of the operation. All this information is stored in the managedFields in the metadata of objects. Since objects can have many fields, this field can be quite large.

When someone applies, we can then use the information stored within managedFields to report relevant conflicts and help the merge algorithm to do the right thing. https://kubernetes.io/blog/2020/04/01/kubernetes-1.18-feature-server-side-apply-beta-2/

A lot of ppl are complaining about noisy output.
Here is the main discussion.
https://github.com/kubernetes/kubernetes/issues/90066

For now there is no any suitable way to remove extra fields from the output.

  1. You could disable the whole feature. https://github.com/kubernetes/kubernetes/issues/90066#issuecomment-639512202
    But that's not recommended, as it was established for a reason, and new tools and components are likely to rely on a certain behavior it enables.

  2. You could use jq or yq in conjunction with scripts, aliases etc.
    https://github.com/kubernetes/kubernetes/issues/89080#issuecomment-626127959

kyaml() {
  kubectl -o yaml "$@" \
    | yq d - 'items[*].metadata.managedFields' \
    | yq d - '.metadata.managedFields' \
    | yq d - 'items[*].metadata.ownerReferences' \
    | yq d - 'metadata.ownerReferences' \
    | yq d - 'items[*].status' \
    | yq d - 'status'
}
kyaml get pods

Upvotes: 2

Related Questions