@kotaicode/kubeconfig-utils
v1.1.0
Published
A TypeScript library for managing Kubernetes kubeconfig files
Readme
kubeconfig-utils-ts
A TypeScript library for managing Kubernetes kubeconfig files. This library provides a simple and type-safe way to manipulate kubeconfig files programmatically.
Features
- Type-safe kubeconfig manipulation
- Runtime validation using Zod
- Comprehensive test coverage
- Full TypeScript support
- Detailed documentation
Installation
npm install kubeconfig-utilsUsage
Basic Usage
import { KubeconfigEditor } from 'kubeconfig-utils';
// Create an editor instance with a kubeconfig
const editor = new KubeconfigEditor(kubeconfigYaml);
// Chain operations
const finalKubeconfig = editor
.mergeWith(anotherKubeconfigYaml)
.deduplicate()
.renameContext("old-name", "new-name")
.setContextNamespace("context-name", "namespace")
.setCurrentContext("context-name")
.toString();Merging Multiple Configs
import { KubeconfigEditor } from 'kubeconfig-utils';
import * as fs from 'fs';
// Load multiple kubeconfig files
const configs = [
fs.readFileSync('~/.kube/config', 'utf8'),
fs.readFileSync('prod-config.yaml', 'utf8'),
fs.readFileSync('staging-config.yaml', 'utf8')
];
// Merge all configs and organize them
const finalConfig = new KubeconfigEditor(configs[0])
.mergeWith(configs[1])
.mergeWith(configs[2])
.deduplicate()
.renameContext('minikube', 'local-dev')
.renameContext('prod-cluster', 'production')
.setContextNamespace('production', 'prod')
.setCurrentContext('production')
.toString();
// Save the merged config
fs.writeFileSync('merged-config.yaml', finalConfig);Filtering and Organizing
import { KubeconfigEditor } from 'kubeconfig-utils';
// Load a kubeconfig with multiple clusters
const editor = new KubeconfigEditor(kubeconfigYaml);
// Keep only production contexts and organize them
const prodConfig = editor
.filterContextsByClusterName('prod-cluster')
.renameContext('prod-cluster', 'production')
.setContextNamespace('production', 'prod')
.removeContext('unused-context')
.toString();Documentation
For detailed documentation, visit https://kotaicode.github.io/kubeconfig-utils-ts/
Contributing
Contributions are welcome! Please read our Contributing Guide for details on our code of conduct and the process for submitting pull requests.
License
This project is licensed under the MIT License - see the LICENSE file for details.
Release Process
This project uses release-it for automated versioning, changelog generation, and GitHub releases. Publishing to npm is handled by GitHub Actions.
How to create a new release
- Ensure your working directory is clean and on the
mainbranch. - Run the release script:
npm run release- This will prompt for the next version, update the changelog, commit, tag, push, and create a GitHub release.
- Wait for GitHub Actions to publish to npm:
- The
publish.ymlworkflow will automatically publish the package to npm when a new GitHub release is created.
- The
- Verify the release:
- Check the GitHub Releases page.
- Check the package on npmjs.com.
Requirements
- You must have push access to the repository.
- The
NPM_TOKENsecret must be set in the repository settings for npm publishing. - The
GITHUB_TOKENis provided automatically by GitHub Actions.
