kubernetesdeploymentkubectl

Why do I need save-config with kubectl apply?


kubectl apply <file.yaml> --save-config creates or updates a deployment and saves the deployment as metadata.

In the documentation it says

--save-config[=false]: If true, the configuration of current object will be saved in its annotation. This is useful when you want to perform kubectl apply on this object in the future.

Why do I need save-config? I can still update my deployment using kubectl apply if I do not --save-config.


Solution

  • kubectl apply

    kubectl apply use the data in an annotation kubectl.kubernetes.io/last-applied-configuration to see e.g. if any fields has been removed since the last apply. This is needed because some fields or annotations may have been added live in the cluster by e.g. a controller or mutating webhook.

    See e.g. Understanding the Kubectl Apply Command

    I can still update my deployment using kubectl apply if I do not --save-config

    Yes, --save-config is only used when migrating from an imperative workflow. See more details below. The following kubectl apply commands does not need --save-config flag because the annotation is already there.

    Workflows with kubectl

    When working with configurations for Kubernetes, this can be done in multiple ways, they are both imperative or declarative:

    kubectl apply is used for declarative configuration management.

    Migrating from imperative to declarative config mangement

    Using kubectl with the --save-config flag is a way to write config to the annotation kubectl.kubernetes.io/last-applied-configuration that kubectl apply uses. This is useful when migrating from an imperative to an declarative workflow.