Course outline

Kubernetes / Basic: objects and execution boundaries / Lesson 7

Verify kubectl context and namespace

Compare Kubernetes cluster, context, namespace and identity before an authorized operation.

About 3 min readContent reviewed
On this page

Lesson goals

  • Explain the parts of a kubeconfig context.
  • Compare expected and observed target scope.
  • Distinguish target verification from authorization.

Understand a context

A kubeconfig context associates a cluster, user and optional namespace for client requests. A familiar context name is a label, not independent proof that it points to the intended cluster. Record the approved target and purpose before an operation. The records below are fictional and contain no credentials, server addresses or commands; no cluster is contacted by this lesson.

Write the expected target

The first record names the intended lab context, cluster, namespace and identity. A reviewer should compare each field with the approved change request, including whether the target is namespaced or cluster scoped. A namespace applies to many resources, but not every Kubernetes object. Treat a missing or surprising namespace as a reason to stop and investigate rather than create a replacement resource.

yaml Expected lab context
expected_context: course-lab
expected_cluster: synthetic-cluster
expected_namespace: training
identity: lab-reader

The record is a checklist, not a kubeconfig or a command.

Compare observed context

This invented observation matches the checklist labels, but the identity and API endpoint need confirmation under approved procedures. Kubeconfig can combine files and command options can change context. Protect credentials and review executable credential plugins in untrusted kubeconfig files.

yaml Synthetic observation
observed_context: course-lab
observed_cluster: synthetic-cluster
observed_namespace: training
authorization: separately-unverified

The record explicitly leaves authorization unverified; it is not output from kubectl.

Check permissions separately

Authentication identifies the caller, while authorization governs the specific verb and resource in a scope. A matching cluster and namespace does not grant permission or justify wider access. Determine the intended operation and consult the owner if it is denied. Cluster-scoped resources require particular care because a namespace label cannot limit their scope.

Verify and document

In an isolated authorized lab, compare expected and observed context immediately before a read-only test on a synthetic resource. Preserve a sanitized result with the time, target and actor, then verify that an adjacent unauthorized action remains denied when such a test is approved. Stop on a mismatch. A successful read proves only that request in that context, not permission for all later operations.

Common errors and fixes

A context label is assumed to prove the target

Why it happens
The label can refer to different cluster or user settings.
How to fix it
Compare the actual context fields and approved endpoint.
Prevent it next time
Record expected and observed target before operations.

A namespace is assumed to scope every object

Why it happens
Some resources are cluster scoped.
How to fix it
Confirm the resource scope and authorization.
Prevent it next time
Include scope in review and test cases.

A successful read is treated as permission to change

Why it happens
Authorization depends on the requested verb and resource.
How to fix it
Request review for the exact change rather than escalating broadly.
Prevent it next time
Test allowed and denied operations separately.

2 practice exercises

1. The context label matches but the cluster differs. What should happen?

Show worked solution

Stop. Identify the kubeconfig source and select the approved target through the authorized procedure before making any request.

2. A user can read a namespaced object. Does that establish permission to modify a cluster-scoped object?

Show worked solution

No. The verb, resource and scope differ; evaluate the specific request and applicable authorization.

Key takeaways

  • Context labels are not target verification.
  • Cluster, namespace, identity and authorization are separate checks.
  • Protect kubeconfig credentials and review untrusted plugins.

Official references