Kubernetes TypeScript Types

1.30.0 · active · verified Sun Apr 19

The `kubernetes-types` package provides comprehensive TypeScript definitions for Kubernetes API resources. It generates these definitions directly from the official Kubernetes OpenAPI specifications, ensuring accuracy and up-to-date type information. The current stable version is 1.30.0, aligning with Kubernetes API version 1.30.x. This package adheres to a strict versioning policy: its major and minor versions directly correspond to the target Kubernetes API version, while the patch version is reserved for updates to the generated types themselves. This approach ensures developers can reliably match their type definitions to their Kubernetes cluster's API version, preventing common runtime errors related to schema mismatches. It is a critical tool for building type-safe Kubernetes clients, controllers, and automation scripts in TypeScript, distinguishing itself by its direct generation from the official OpenAPI specs and strict version adherence rather than manual maintenance.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to import and define basic Kubernetes Pod and Deployment resources using the provided TypeScript types, including common metadata and container specifications, and environment variables.

import { Pod } from 'kubernetes-types/core/v1'
import { ObjectMeta } from 'kubernetes-types/meta/v1'
import { Deployment } from 'kubernetes-types/apps/v1'

const metadata: ObjectMeta = {
  name: 'my-app',
  labels: { app: 'my-app' }
};

const myPod: Pod = {
  apiVersion: 'v1',
  kind: 'Pod',
  metadata: {
    ...metadata,
    annotations: { 'example.com/description': 'A simple example pod' }
  },
  spec: {
    containers: [
      {
        name: 'nginx-container',
        image: 'nginx:latest',
        ports: [{ containerPort: 80 }],
        resources: { 
          requests: { cpu: '100m', memory: '128Mi' },
          limits: { cpu: '200m', memory: '256Mi' }
        }
      }
    ]
  }
};

const myDeployment: Deployment = {
  apiVersion: 'apps/v1',
  kind: 'Deployment',
  metadata: {
    ...metadata,
    name: 'my-app-deployment'
  },
  spec: {
    replicas: 3,
    selector: { matchLabels: { app: 'my-app' } },
    template: {
      metadata: { labels: { app: 'my-app' } },
      spec: {
        containers: [
          {
            name: 'my-app-container',
            image: 'my-org/my-app:1.0.0',
            ports: [{ containerPort: 3000 }],
            env: [
              { name: 'NODE_ENV', value: 'production' },
              { name: 'API_KEY', value: process.env.MY_API_KEY ?? '' }
            ],
            resources: {
              requests: { cpu: '200m', memory: '256Mi' },
              limits: { cpu: '500m', memory: '512Mi' }
            }
          }
        ]
      }
    }
  }
};

console.log('Defined Pod:', JSON.stringify(myPod, null, 2));
console.log('Defined Deployment:', JSON.stringify(myDeployment, null, 2));

view raw JSON →