Import from Kubernetes
Documentation for Kubernetes catalog population scripts
In modern cloud-native environments, applications run across dozens of Kubernetes namespaces, each hosting multiple Deployments, Services, and other resources. Manually onboarding these Kubernetes workloads into the Harness Software Catalog quickly becomes error-prone and unsustainable.
This script helps you discover and add your Kubernetes resources to the Harness Internal Developer Portal catalog automatically. It is especially useful when you have many Kubernetes resources across multiple namespaces that would be time-consuming to add manually.
The script follows a comprehensive workflow:
Scans your Kubernetes cluster and finds all your resources
Generates IDP-compatible YAML files for each resource
Commits these files to a central Git repository (GitHub)
Registers them with Harness IDP through the Entities API
This workflow ensures you have version control for all your catalog entities and can track changes over time.
By automatically analyzing Deployments, Services, and their interdependencies, the script ensures the catalog reflects a near real-time view of your cluster without requiring manual intervention.
Script source
curl -o kubernetes-harness-idp-catalog-sync.py https://raw.githubusercontent.com/harness-community/idp-samples/refs/heads/main/IDP-2.0-Samples/catalog-scripts/kubernetes-harness-idp-catalog-sync.pyBefore you begin
Local environment
This script is designed to run on your local machine or in a CI/CD pipeline with access to both your Kubernetes cluster and GitHub. You will need:
Python 3 with the following libraries installed:
pip install requests python-dotenv kubernetes
Kubernetes access
Access to your Kubernetes cluster via properly configured
kubectland kubeconfigPermissions to list and get deployments, services, and other resources
For local use, make sure you are connected to the right cluster context:
A
.envfile configured with the following environment variables:
HARNESS_API_KEYmust have write access to IDP entities.GITHUB_TOKENrequiresrepoandread:orgscopes.CONNECTOR_REFis the Git connector reference in Harness pointing to yourCENTRAL_REPO.
Execution
Run the script with namespace and dependency analysis flags:
Options:
--namespace(optional): Limit discovery to a specific namespace. Defaults to all namespaces.--resource-kind(optional): Filter by resource type (Deployment,Service,Pod). Defaults to Deployments and Services.--analyze-dependencies(flag): Enables detection of service-to-deployment dependencies based on selectors and environment variables.
What the script does
Connects to your Kubernetes cluster using kubeconfig
Discovers Deployments, Services (and optionally Pods)
Generates Harness-compatible
idp.yamlfor each resourcePushes each YAML file into a GitHub central repo at a structured path
Registers the entity in Harness IDP via the Entities API
Resource discovery logic
The script intelligently discovers Kubernetes resources using the official Kubernetes Python client:
It uses different API methods based on resource types and filters:
For Deployments:
apps_v1.list_namespaced_deployment()orapps_v1.list_deployment_for_all_namespaces()For Services:
v1.list_namespaced_service()orv1.list_service_for_all_namespaces()
Each resource is extracted with its complete metadata including:
Name, namespace, kind
Labels and selectors
Environment variables (for Deployments)
Dependency detection mechanism
The script employs two sophisticated methods to detect dependencies between resources:
Service-to-Deployment Mapping:
This identifies which Deployments implement each Service by comparing Service selectors with Deployment labels.
Environment Variable Analysis:
This detects when one resource references another via environment variables, revealing implicit dependencies.
YAML generation and entity creation
The script dynamically generates Harness-compatible entity definitions with these key features:
Deterministic Identifiers:
Creates stable, consistent identifiers using namespace, kind, and name.
Rich Metadata:
Includes descriptive information and automatic tagging.
Dependency Relationships:
Maps the discovered dependencies into Harness relationship format.
GitHub integration
The script interfaces with GitHub's API to store entity definitions:
Path Organization:
Creates a logical folder structure based on Kubernetes hierarchy.
Smart File Operations:
Checks if files already exist before creating or updating them.
Harness catalog registration
The script registers entities with Harness using the Entities API:
API Integration:
Intelligent Retries:
The script automatically retries with UPSERT mode if entities already exist.
Output structure
The GitHub repo will store files in the following format:
Each YAML will look like:
Logs & troubleshooting
Logs are printed to stdout for each resource:
Discovery status (
Found N resources)Entity creation (
✓ Registered in Harness successfully)Dependency detection (
Detected dependency: ...)
Failures include Harness API error codes and response details.
If entity already exists, the script automatically retries with UPSERT mode.
If running on a personal GitHub account instead of an org, change the GitHub API call from:
to:
Last updated
Was this helpful?