DEV Community

Cover image for What Happens when you type kubectl create namespace command?
Nidhin BM
Nidhin BM

Posted on

What Happens when you type kubectl create namespace command?

Summary

From kubectl documentation

Kubernetes provides a command line tool for communicating with a Kubernetes cluster's control plane, using the Kubernetes API.

This tool is named kubectl.
For configuration, kubectl looks for a file named config in the $HOME/.kube directory.

kubectl

This is the first file runs when you type kubectl https://github.com/kubernetes/kubernetes/blob/master/cmd/kubectl/kubectl.go

Here the main function is the entry point

func main() {
    command := cmd.NewDefaultKubectlCommand()
    if err := cli.RunNoErrOutput(command); err != nil {
        // Pretty-print the error and exit with an error.
        util.CheckErr(err)
    }
}
Enter fullscreen mode Exit fullscreen mode

which depends on cmd and cli packages. Lets see cmd package for now

https://github.com/kubernetes/kubernetes/blob/master/staging/src/k8s.io/kubectl/pkg/cmd/cmd.go

Inside this package

  • NewDefaultKubectlCommand creates the kubectl command with default arguments
  • Then it calls NewDefaultKubectlCommandWithArgs
  • NewDefaultKubectlCommandWithArgs creates the kubectl command with arguments

  • NewKubectlCommand creates the kubectl command and its nested children. Which has following commands

kubectl controls the Kubernetes cluster manager.

 Find more information at: https://kubernetes.io/docs/reference/kubectl/

Basic Commands (Beginner):
  create          Create a resource from a file or from stdin
  expose          Take a replication controller, service, deployment or pod and expose it as a new Kubernetes service
  run             Run a particular image on the cluster
  set             Set specific features on objects

Basic Commands (Intermediate):
  explain         Get documentation for a resource
  get             Display one or many resources
  edit            Edit a resource on the server
  delete          Delete resources by file names, stdin, resources and names, or by resources and label selector

Deploy Commands:
  rollout         Manage the rollout of a resource
  scale           Set a new size for a deployment, replica set, or replication controller
  autoscale       Auto-scale a deployment, replica set, stateful set, or replication controller

Cluster Management Commands:
  certificate     Modify certificate resources
  cluster-info    Display cluster information
  top             Display resource (CPU/memory) usage
  cordon          Mark node as unschedulable
  uncordon        Mark node as schedulable
  drain           Drain node in preparation for maintenance
  taint           Update the taints on one or more nodes

Troubleshooting and Debugging Commands:
  describe        Show details of a specific resource or group of resources
  logs            Print the logs for a container in a pod
  attach          Attach to a running container
  exec            Execute a command in a container
  port-forward    Forward one or more local ports to a pod
  proxy           Run a proxy to the Kubernetes API server
  cp              Copy files and directories to and from containers
  auth            Inspect authorization
  debug           Create debugging sessions for troubleshooting workloads and nodes
  events          List events

Advanced Commands:
  diff            Diff the live version against a would-be applied version
  apply           Apply a configuration to a resource by file name or stdin
  patch           Update fields of a resource
  replace         Replace a resource by file name or stdin
  wait            Experimental: Wait for a specific condition on one or many resources
  kustomize       Build a kustomization target from a directory or URL

Settings Commands:
  label           Update the labels on a resource
  annotate        Update the annotations on a resource
  completion      Output shell completion code for the specified shell (bash, zsh, fish, or powershell)

Subcommands provided by plugins:

Other Commands:
  api-resources   Print the supported API resources on the server
  api-versions    Print the supported API versions on the server, in the form of "group/version"
  config          Modify kubeconfig files
  plugin          Provides utilities for interacting with plugins
  version         Print the client and server version information

Usage:
  kubectl [flags] [options]

Use "kubectl <command> --help" for more information about a given command.
Use "kubectl options" for a list of global command-line options (applies to all commands).
Enter fullscreen mode Exit fullscreen mode

create

entry point of kubectl create package is https://github.com/kubernetes/kubernetes/blob/master/staging/src/k8s.io/kubectl/pkg/cmd/create/create.go

  • CreateOptions is the commandline options for 'create' sub command
  • NewCreateOptions returns an initialized CreateOptions instance
  • NewCmdCreate returns new initialized instance of create sub command
  • Under this following sub commands are available
Available Commands:
  clusterrole           Create a cluster role
  clusterrolebinding    Create a cluster role binding for a particular cluster role
  configmap             Create a config map from a local file, directory or literal value
  cronjob               Create a cron job with the specified name
  deployment            Create a deployment with the specified name
  ingress               Create an ingress with the specified name
  job                   Create a job with the specified name
  namespace             Create a namespace with the specified name
  poddisruptionbudget   Create a pod disruption budget with the specified name
  priorityclass         Create a priority class with the specified name
  quota                 Create a quota with the specified name
  role                  Create a role with single rule
  rolebinding           Create a role binding for a particular role or cluster role
  secret                Create a secret using a specified subcommand
  service               Create a service using a specified subcommand
  serviceaccount        Create a service account with the specified name
  token                 Request a service account token
Enter fullscreen mode Exit fullscreen mode

namespace

Entry point https://github.com/kubernetes/kubernetes/blob/master/staging/src/k8s.io/kubectl/pkg/cmd/create/create_namespace.go#L76

  • Complete completes all the required options
  • Validate validates required fields are set to support structured generation
  • Run calls the CreateSubcommandOptions.Run in NamespaceOpts instance
  • createNamespace outputs a namespace object using the configured fields
  • Then o.Client.Namespaces().Create(context.TODO(), namespace, createOptions) creates namespace

Next post

How control-plane is handling this api call

Top comments (0)