# Set once kubectl set-context --namespace=payments kubectl get pods kubectl get services kubectl get deployments Override for a single command kubectl get pods --namespace=audit # operates in 'audit' 4.2 Interaction with --all-namespaces The --all-namespaces flag (or -A ) explicitly overrides any namespace setting—including the context default—and operates across all namespaces. This is consistent with the precedence rule: the imperative flag takes highest priority. 4.3 Namespace Existence and Validation Crucially, kubectl set-context --namespace does not verify that the specified namespace exists in the target cluster. The validation occurs at the moment of a resource operation (e.g., get , create ). If the namespace does not exist, the API server returns:
kubectl set-context prod-ops --namespace=monitoring The context becomes: kubectl set-context namespace
contexts: - name: prod-ops context: cluster: prod-eks user: ops-user namespace: monitoring # <--- added field If a namespace field already exists, it is overwritten. Setting --namespace="" removes the field entirely, reverting to fallback behavior. The command is strictly idempotent: re-applying the same --namespace results in no net change. However, changing the namespace overwrites the previous value without warning. Unlike kubectl config set-context (the older syntax), set-context does not modify other fields (cluster, user) unless explicitly instructed. 4. Operational Semantics 4.1 Persistent Default vs. Transient Override Once a namespace is bound to a context, it persists across shell sessions and even across cluster credential renewal, as long as the kubeconfig remains intact. The validation occurs at the moment of a
contexts: - name: prod-ops context: cluster: prod-eks user: ops-user After executing: The command is strictly idempotent: re-applying the same