get_client() function provides a flexible way to create a Kubernetes client for interacting with your cluster. It supports multiple authentication methods and configuration options.
Basic Client Creation
The simplest way to get a client is to callget_client() without arguments:
ocp_resources/resource.py:195 and returns a DynamicClient instance.
Configuration Methods
There are several ways to configure your client connection:Method 1: Default Kubeconfig
Method 2: Custom Kubeconfig File
Method 3: Kubeconfig Dictionary
Method 4: Specific Context
Method 5: Token Authentication
Method 6: Basic Authentication
Client Configuration Parameters
Theget_client() function accepts these parameters:
Advanced Configuration
Custom Client Configuration
SSL Configuration
Proxy Configuration
Proxy settings can be configured through environment variables or client configuration:In-Cluster Configuration
When running inside a Kubernetes pod:Authentication Methods
Service Account Token
OAuth Token
Certificate Authentication
Multiple Clusters
Managing connections to multiple clusters:Testing with Fake Client
For testing without a real cluster:Client Reuse
Create the client once and reuse it across your application for better performance.
Good: Reuse Client
Bad: Create Multiple Clients
Environment Variables
The client respects these environment variables:Example Usage
Connection Validation
Validate your client connection:Troubleshooting
Issue: Connection Refused
Issue: SSL Certificate Verification Failed
Issue: Context Not Found
Issue: Token Expired
Security Best Practices
Use Environment Variables
Use Secret Management
Rotate Credentials
Common Patterns
Pattern 1: Multi-Environment Configuration
Pattern 2: Client Factory
Next Steps
Resource Class
Learn about the base Resource class
Working with Pods
Execute commands and retrieve logs
Testing Guide
Test your code with the fake client
Fake Client
Test without a real Kubernetes cluster