PyCharm 2023.2 Help

Kubernetes

Kubernetes is a container orchestration engine for the deployment, scaling, and management of containerized applications. Install and enable the Kubernetes plugin for PyCharm to add the following features for managing your Kubernetes cluster:

  • Coding assistance for Kubernetes resource configuration files.

  • Coding assistance for Helm charts and templates.

  • Validation of custom resources with custom resource definition (CRD) specifications.

  • Coding assistance for Kustomize files: field and local file path completion, quick documentation, and navigation between Kustomize files and patches. The list of related Kustomize files appears in the editor at the top of the open Kustomize patch.

  • Interaction with the cluster from PyCharm: use the Services tool window to see all resources of the Kubernetes cluster in the current context, jump to relevant resource definitions, view logs for containers running on pods, and much more.

The Kubernetes plugin supports Kubernetes versions 1.6 to 1.26 (you can select the applicable version).

Install the Kubernetes plugin

This functionality relies on the Kubernetes plugin, which you need to install and enable.

  1. Press Control+Alt+S to open the IDE settings and then select Plugins.

  2. Open the Marketplace tab, find the Kubernetes plugin, and click Install (restart the IDE if prompted).

Specify a custom path to kubectl and helm

PyCharm uses the default kubectl (and helm if you use it) executables as determined by the PATH environment variable. If you install Kubernetes or Helm in custom directories, you can manually specify the path to them.

  1. In the Settings dialog (Control+Alt+S), select Build, Execution, Deployment | Kubernetes.

  2. In the Path to kubectl executable field, specify the path to the kubectl executable file.

  3. In the Path to helm executable field, specify the path to the helm executable file.

Kubernetes settings window

Use custom Kubernetes configuration files

PyCharm detects the default kubeconfig file. You can use a different configuration file. You can either apply it globally or use a different file for each project.

  1. In the Settings dialog (Control+Alt+S), select Build, Execution, Deployment | Kubernetes.

  2. Under Configuration, click Add kubeconfig and specify the path to a kubeconfig file.

  3. If you want to use the configuration file in all projects, select Global in the Scope column. To use it only in the current project, keep the Project scope.

When you add a file, PyCharm validates its syntax and reports errors, if any.

Kubeconfig Configuration

Configure the Kubernetes API version

PyCharm provides completion for configuration key values, navigation to relevant selectors and definitions via gutter icons, specialized inspections that check for deprecated values and required keys, and other assistance features. These depend on the version of the API you are using. The Kubernetes plugin supports Kubernetes versions 1.6 to 1.26. By default, PyCharm sets it to the latest version. However, if your resources use an earlier version, you can change it.

  1. In the Settings dialog (Control+Alt+S), select Languages & Frameworks | Kubernetes.

  2. Change the Kubernetes API version and Kustomize version options as necessary.

Resource configuration files

The Kubernetes plugin provides rich support for resource configuration files in YAML, and only basic support for the JSON format.

Supported features

Feature

YAML

JSON

Auto-completion

Supported

Supported

Quick documentation

Supported

Supported

Inspections and quick fixes

  • Invalid, missing, and duplicated keys

  • Non-editable (read-only) keys and resources

  • Deprecated keys and resources

  • Invalid integer and enum key values

  • Invalid, missing, and duplicated properties

Live templates

Predefined templates for common configuration kinds:

  • kcm: ConfigMap

  • kdep: Deployment

  • kpod: Pod

  • kres: Generic resource

  • kser: Service

No predefined live templates

Smart completion

Supported

Not supported

Custom resource definitions

Supported

Not supported

Label definitions and selectors

Navigation using gutter icons, find usages, and renaming

Not supported

Enhancements of the original Kubernetes model

Enums instead of plain strings where applicable

None

PyCharm recognizes Kubernetes resource configuration files using the following mandatory fields:

  • apiVersion: identifies the versioned schema of the object representation

  • kind: identifies the resource type (for example, Service, Pod, Deployment, and so on)

If both of the previous fields are present in a YAML or JSON file, PyCharm will mark the file with the corresponding Kubernetes icon and enable all available features:

How a Kubernetes resource file is recognized

Create a resource file

With PyCharm, you can quickly create configuration files for some of the most popular resources in Kubernetes.

  1. In the Project tool window, right-click a folder, select New (or press Alt+Insert), and then select Kubernetes Resource.

  2. In the Name field, type your resource name and select the file template from the list.

    New Kubernetes Resource Window

    This will create a new file with its contents based on the selected file template.

  3. Click Apply icon to create the resource in a cluster. To change the context to which you apply it, click select context and select a context.

Alternatively, in YAML files, you can use predefined live templates:

  • kcm: Kubernetes ConfigMap

  • kdep: Kubernetes Deployment

  • kpod: Kubernetes Pod

  • kres: Kubernetes Generic Resource

  • kser: Kubernetes Service

Using a Live Template to create a Kubernetes deployment configuration

Disable Kubernetes schema validation

PyCharm validates your Kubernetes files against the Kubernetes API schema. This includes checking it for required keys or possible types of resource.

If your file contains apiVersion and kind, but it is not a Kubernetes file, you can disable such validation. You can suppress inspections and change their scope and severity in Settings | Editor | Inspections | Kubernetes. Or you can mark a file with a special directive to disable validation in it:

  • On top of a resource configuration file, add # nonk8s.

  • Alternatively, if you already have a warning about an unknown resource, right-click it in the Problems tool window and select Show Quick-Fixes | Mark this file as non-Kubernetes.

Manage your cluster

Use the Services tool window to view your cluster, switch between namespaces and contexts, and modify the cluster resources.

Edit kubeconfig

The kubeconfig file contains information about clusters, users, namespaces, and authentication.

  1. Open the Services tool window: select View | Tool Windows | Services or press Alt+8.

  2. Select a cluster and click Open kubeconfig File in Editor (Open kubeconfig File in Editor) on the toolbar. The action is also available when you right-click any Kubernetes object in the Services tool window.

    This will open the kubeconfig file in a new tab of the editor. To change the default file opened by this action, you can specify a path to another file in Path to kubeconfig file.

When you change your kubeconfig file, the configuration is automatically reloaded. You can also reload it manually or disable automatic reload by right-clicking a cluster or any Kubernetes object and selecting More | Reload Configuration Automatically.

Reload Configuration

Switch between namespaces

Kubernetes namespaces let you logically isolate resources within your cluster. With PyCharm, you can quickly switch between namespaces.

  1. Open the Services tool window: select View | Tool Windows | Services or press Alt+8.

  2. Right-click your cluster or any resource, select Namespaces (or click The Namespace icon on the toolbar), and select the required namespace.

    To view resources of all namespaces, select All Namespaces.

    Namespaces context menu

Change context

In the Services tool window, PyCharm shows all the contexts detected in the kubeconfig file.

When you create a resource file locally, you can select the context to which apply the resource by clicking select context. To speed up context selection, you can choose it in advance, in PyCharm settings.

  1. In the Settings dialog (Control+Alt+S), select Languages & Frameworks | Kubernetes.

  2. In the Context to use list, select a context to be used when applying local Kubernetes resources.

Modify cluster resources

  1. Open the Services tool window: select View | Tool Windows | Services or press Alt+8.

  2. Right-click a resource and select View YAML.

    This will open the configuration file of the selected resource in a new tab of the editor. When you change a field, modified lines are highlighted in the gutter. You can click the highlighted area in the gutter to quickly preview or rollback changes.

    View Diff
  3. Click Apply icon to apply changes to the current context and namespace.

When you modify a resource configuration file, a toolbar appears at the top right. It lets you apply the changes, delete the resource, preview the diff, and roll back the changes.

Delete resources

You can delete a resource in any of the following ways:

  • In the Services tool window, right-click a resource and select Delete.

  • In the Services tool window, select a resource and click Delete resource (Delete Resource) on the toolbar.

  • If the resource YAML file is opened in the editor, hover over it and click Delete resource. You can also click Select context here to change the context where you want to delete the resource.

View logs

You can view logs in pods and deployments.

  1. Open the Services tool window: select View | Tool Windows | Services or press Alt+8.

  2. Select a pod or a deployment. On the right side of the Services tool window, click one of the icons:

    • Follow Log icon (Follow Log) to live tail logs of the selected resource with the --follow=true flag.

    • Download Log icon (Download Log) to save the log file.

    Both actions are also available when you right-click a pod or a deployment.

    If you have multiple containers in the pod, you must select one from the displayed list of containers.

Kubernetes logs

You can configure log saving options in the IDE settings (Control+Alt+S) under Build, Execution, Deployment | Kubernetes | Logs:

  • If you want to save logs in a location other than Scratches, specify a directory in Path to download logs to. Or select Ask where to save log before downloading to prompt you for the location every time you save logs.

  • If you want to include the log timestamp in the filename, select Append timestamp to log file name.

Create Secrets

With PyCharm, you can quickly create Kubernetes Secrets.

  1. Open the Services tool window: select View | Tool Windows | Services or press Alt+8.

  2. Select a cluster, expand Configuration, right-click Secrets, and select Create New Secret.

  3. In the Create New Secret window that opens, specify the secret name, type, and namespace. To speed up secret creation, PyCharm adds required data or annotation keys depending on the selected secret type.

  4. In the Data and Annotations section, click Value and enter values for existing keys, or you can click Add icon and choose the way to provide values:

    • Add Manually to manually enter the value.

    • Use File Data to select a file (for example, an SSH key file or a Docker config.json) to use the file contents as the value.

    Create New Secret Window

You can view all your Secrets in the Services tool window, under Configuration. Once a Secret is created, you can start typing its name in secretName fields of your Pod configuration. This will invoke completion for Secret names available in the cluster or in your project.

Helm support

Helm is a tool for managing Kubernetes applications. Helm charts are packages of pre-configured resource definitions that you run inside a Kubernetes cluster. A chart contains a description of the package Chart.yaml and one or more templates used to generate Kubernetes manifest files.

Coding assistance for Helm charts and templates includes code completion, refactorings, inspections, quick-fixes, and quick documentation. Code completion includes values of dependencies from the specified repository (by default, from Helm Hub).

In Go template directives, PyCharm provides completion for Helm built-in objects and for values passed from a values.yaml file. You can press Control+B to navigate to a source of object values, for example, to a child chart value imported into a parent chart.

In template objects, you can use code folding: press Control+NumPad-+ and Control+NumPad-- to toggle between values and directives. You can also hover over a value to expand and show the directive.

Helm imported data

You can also use gutter icons for navigating between label definitions and label selectors, and between overridden and overriding values.

Create a new Helm chart

  1. In the Project tool window, right-click a folder and select New | Helm Chart.

  2. In the Create New Helm Chart window that opens, enter a name for the chart.

This runs the helm create command, which adds all the basic files required to get started:

  • .helmignore: Patterns to ignore when building packages

  • Chart.yaml: A basic chart description with metadata

  • values.yaml: Default values for chart templates

  • charts/: Directory for sub-charts

  • templates/: Directory for chart definitions

    • _helpers.tpl: Partials and functions for your templates

    • NOTES.txt: Information that is printed out after a chart is deployed

    • deployment.yaml: Example Kubernetes deployment definition

    • ingress.yaml: Example Kubernetes ingress definition

    • service.yaml: Example Kubernetes service definition

Preview the result of Helm template rendering

  • If a template file is opened in the editor, click Helm Template on the right side of the editor.

    Alternatively, right-click the template file in your project, point to Helm, and select Helm Template.

This runs the helm template command that renders a chart template. The rendered preview opens inside the diff viewer to compare it with the original template file.

Update external dependencies

  • Right-click the chart, point to Helm, and click Helm Dependency Update.

This runs the helm dependency update command.

In Helm 2, dependencies should be specified in the requirements.yaml file. This action also generates or updates requirements.lock.

In Helm 3, dependencies should be specified in the Chart.yaml file. If you specify the dependencies in the wrong file, PyCharm provides an inspection with a quick-fix to move them.

Examine a chart for possible issues

  • Right-click the chart, point to Helm, and click Helm Lint.

This runs the helm lint command that executes a series of tests to discover possible problems without actually installing the chart.

Custom resource definitions support

If you extend the Kubernetes cluster with custom resources, PyCharm can validate them with custom resource definition (CRD) specifications.

Specify the path to CRD specifications

  1. Press Control+Alt+S to open the IDE settings and then select Languages & Frameworks | Kubernetes.

  2. Click the Add button and either select a local CRD file or specify a URL. Then click OK.

Use the Up button and the Down button to rearrange the list of CRD files. This defines the priority for conflicting definitions: PyCharm will use the lowest one if it is defined in more than one file.

By default, CRDs are applied to the current project only. Change the Scope: option to IDE if you want a certain CRD to be available for any project that you open with this IDE instance.

To load the CRD from the running Kubernetes cluster, enable Use API schema from the active cluster if available.

You can view CRDs for the current cluster, including all their applied resources, under the Kubernetes node of the Services tool window.

The Kubernetes plugin supports CRD files of the following types:

CustomResourceDefinition files in YAML

The following example shows a simple CustomResourceDefinition specification for a custom resource stable.example.com/v1 of the kind CronTab. Obligatory fields to identify the resource are metadata.name, spec.group, spec.versions, and spec.names.

apiVersion: apiextensions.k8s.io/v1beta1 kind: CustomResourceDefinition metadata: name: crontabs.stable.example.com # Obligatory field to identify the resource spec: group: stable.example.com # Obligatory field to identify the resource versions: # Obligatory field to identify the resource - name: v1 served: true storage: true scope: Namespaced names: # Obligatory field to identify the resource plural: crontabs singular: crontab kind: CronTab shortNames: - ct validation: openAPIV3Schema: # Schema for validating custom objects properties: spec: properties: cronSpec: type: string pattern: '^(\d+|\*)(/\d+)?(\s+(\d+|\*)(/\d+)?){4}$' # not supported for validation replicas: type: integer minimum: 1 # not supported for validation maximum: 10 # not supported for validation

OpenAPI v2.0 schema in JSON

The following example shows a simple OpenAPI v2.0 schema with a CRD specification for a custom resource sample/v1 of the kind Config. The root definition of the custom resource must contain the x-kubernetes-group-version-kind field with the specified group, version, and kind. In the example, the root.Definition object uses a build property to reference the some.Definition object.

{ "swagger": "2.0", "info": { "title": "Sample Schema", "version": "sample/v1" }, "paths": {}, "definitions": { "some.Definition": { "description": "Example of a definition.", "properties": { "someProperty": { "type": "string" } } }, "root.Definition": { "description": "This is the root definition for the resource", "properties": { "build": { "$ref": "#/definitions/some.Definition" } }, "x-kubernetes-group-version-kind": [ { "group": "sample", "kind": "Config", "version": "v1" } ] } } }

CRD validation restrictions

PyCharm does not support the following OpenAPI v3 schema features:

  • multipleOf

  • maximum

  • exclusiveMaximum

  • minimum

  • exclusiveMinimum

  • maxLength

  • minLength

  • pattern

  • maxItems

  • minItems

  • uniqueItems

  • maxProperties

  • minProperties

  • allOf

  • oneOf

  • anyOf

  • not

  • format

  • default

  • nullable

  • readOnly

  • writeOnly

  • xml

  • externalDocs

  • example

  • deprecated

Productivity tips

If you use the features described here frequently, the following tips may be helpful:

Assign shortcuts

You can assign a keyboard shortcut for Kubernetes actions.

  1. In the Settings dialog (Control+Alt+S), select Keymap.

  2. Type kubernetes in the search field, then double-click an action to set a shortcut for it.

Alternatively, you can use the Find Action dialog Control+Shift+A, type the necessary action and, and press Alt+Enter.

For example, you can assign a shortcut to Interact with Cluster to quickly open the context menu while modifying a resource configuration file.

Configure code folding

By default, PyCharm uses code folding to render value references in Helm templates and definitions in Kubernetes configuration files as the actual values. You can click the value to expand it or press Control+NumPad-+ and Control+NumPad-- to toggle folding. If you want to see the references and definitions expanded by default, do the following:

  1. In the Settings dialog (Control+Alt+S), select Editor | General | Code Folding.

  2. On the Code Folding page, clear the necessary checkboxes:

    • Kubernetes: value references in Helm templates

    • Kubernetes: EnvVar definitions in YAML files

    • Kubernetes: ExecAction definitions in YAML files

Last modified: 15 September 2023