This documentation provides a step-by-step guide for building and starting the Kubernetes Python client project.
Step 1: Clone the Repository
Begin by cloning the repository from its source. Open a terminal and execute:
git clone https://github.com/kubernetes-client/python.git
cd python
Ensure that you replace the URL with the correct repository location if it differs from the example.
Step 2: Install Dependencies
Navigate to the test requirements file and install the necessary dependencies using pip:
pip install -r ../test-requirements.txt
This command will install all the required packages specified in the test-requirements.txt
file.
Step 3: Set up the Documentation Environment (Optional)
If you intend to build the documentation, you must also have Sphinx installed. However, this step is optional and can be skipped if documentation building is not necessary. To build the documentation, you will need to use make
:
make html
This command will generate the documentation in HTML format. The Makefile contains instructions for building the documentation components. Make sure you have the Makefile
located in your current directory.
Additional Info from Makefile
If further modifications or custom configurations are required, refer to the Makefile in the doc
directory. Key variables defined include:
SPHINXAPIDOC = sphinx-apidoc
SPHINXBUILD = sphinx-build
SOURCEDIR = source
BUILDDIR = build
Step 4: Configure API Client
To configure the API client, follow the example provided in the kubernetes/README.md
. Below is a code snippet demonstrating how to set up API key authorization and host configuration:
from __future__ import print_function
import kubernetes.client
from kubernetes.client.rest import ApiException
configuration = kubernetes.client.Configuration()
# Configure API key authorization: BearerToken
configuration.api_key['authorization'] = 'YOUR_API_KEY'
# Uncomment below to setup prefix (e.g. Bearer) for API key, if needed
# configuration.api_key_prefix['authorization'] = 'Bearer'
# Defining host is optional and defaults to http://localhost
configuration.host = "http://localhost"
Make sure to replace 'YOUR_API_KEY'
with the actual API key needed for authentication.
Step 5: Running a Sample Program
To ensure that everything is set up correctly, you can run a sample script. The following example shows how to load the Kubernetes configuration and access the API:
def main():
# Fetching and loading local Kubernetes Information
from kubernetes import client, config
config.load_kube_config() # Load kube config from default location
apps_v1_api = client.AppsV1Api()
networking_v1_api = client.NetworkingV1Api()
# Further operations using apps_v1_api and networking_v1_api can be performed here
This sample demonstrates how to initialize the APIs for interaction with Kubernetes.
Conclusion
Following these steps will successfully set up the Kubernetes Python client project. Ensure to handle dependencies properly and reference relevant documentation when building components or troubleshooting issues.
For further details and examples, refer to the source documentation files mentioned throughout this guide.
Sources
doc/README.md
doc/Makefile
kubernetes/README.md
- Python code examples