PyCharm 2020.3 Help

Kubernetes

Kubernetes enables deployment, scaling, and management of containerized applications. PyCharm provides support for Kubernetes resource configuration files via the Kubernetes plugin.

The Kubernetes plugin supports Kubernetes API starting from version 1.5. It provides rich support for resource configuration files in YAML, and only basic support for the JSON format.

FeatureYAMLJSON
Auto-completionSupportedSupported
Quick documentationSupportedSupported
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 completionSupportedNot supported
Custom resource definitionsSupportedNot supported
Label definitions and selectorsNavigation using gutter icons, find usages, and renamingNot supported
Enhancements of the original Kubernetes modelEnums instead of plain strings where applicableNone

The Kubernetes plugin also adds support for path completion in Kustomize files 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.

You can use the Services tool window to see all resources of the Kubernetes cluster in the current context, jump to relevant resource definitions, and view logs for containers running on pods.

Resource configuration files

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

  • apiVersion: identifies the versioned schema of the object representation

  • kind: identifies the object kind (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

For YAML files, you can also use predefined Live Templates to create the necessary type of configuration:

  • 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

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 knowledge about the version of the API you are using. By default, PyCharm assumes it is the latest version.

  1. In the Settings/Preferences dialog Ctrl+Alt+S, select Languages & Frameworks | Kubernetes.

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

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. Also, it is possible to use gutter icons for navigating between label definitions and label selectors, and between overridden and overriding values. Code completion includes values of dependencies from the specified repository (by default, from Helm Hub ).

By default, Go template directives are rendered as the actual values in Helm templates. You can click the value to expand and show the directive. This functionality is implemented using code folding. Press Ctrl+NumPad + and Ctrl+NumPad - to toggle between values and directives.

Create a new Helm chart

  • Right-click any directory in the Project tool window, point to Kubernetes, and click Helm Create 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

  • Right-click the template file, point to Kubernetes, and click Helm Template.

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

Update external dependencies

  • Right-click the chart, point to Kubernetes, 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 Kubernetes, 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 your Kubernetes cluster is extended with custom resources, PyCharm can validate them with custom resource definition (CRD) specifcations.

Specify the path to CRD specifications

  1. In the Settings/Preferences dialog Ctrl+Alt+S, 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: the lowest one is used 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

The following OpenAPI v3 schema features are not supported:

  • 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/Preferences dialog Ctrl+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 Ctrl+Shift+A, type kubernetes, select the necessary action, and press Alt+Enter.

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 Ctrl+NumPad + and Ctrl+NumPad - to toggle folding. If you want to see the references and defintions expanded by default, do the following:

  1. In the Settings/Preferences dialog Ctrl+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

Use multiple Kubernetes configuration files

  • PyCharm detects the default kubeconfig file. To use a different configuration file, open Settings/Preferences, select Build, Execution, Deployment | Kubernetes, and set the necessary path. If you want to use different configuration files in different projects, select Set this path only for the current project.

Last modified: 08 March 2021