Rancher Kubernetes API compatible with RKE, RKE2 and maybe others?

Overview

kctl

Rancher Kubernetes API compatible with RKE, RKE2 and maybe others?


Documentation is WIP.


Quickstart

pip install --upgrade kctl

Usage

https://ranchercluster.com/k8s/clusters/c-m-xxxxxx """ KctlClient.v1.list_apps_deployment() """ All v1 methods will now return the specified cluster context { 'type': 'collection', 'links': {'self': 'https://ranchercluster.com/k8s/clusters/c-m-xxxxxx/v1/apps.deployments/fleet-default'}, 'createTypes': {'apps.deployment': 'https://ranchercluster.com/k8s/clusters/c-m-xxxxxx/v1/apps.deployments'}, 'actions': {}, 'resourceType': 'apps.deployment', 'revision': '6671325', 'data': [] } """ ">
from lazycls.envs import set_env_from_dict


"""
---
Primary Configuration that takes env variables first.
---

host = envToStr('KCTL_HOST', 'http://localhost')
api_key = envToStr('KCTL_API_KEY')
api_key_prefix = envToStr('KCTL_API_KEY_PREFIX', 'token:')
api_token = envToStr('KCTL_API_TOKEN')
username = envToStr('KCTL_API_USERNAME')
password = envToStr('KCTL_API_PASSWORD')
auth_prefix = envToStr('KTCL_AUTH_PREFIX', 'authorization')

---
Rancher Specific Configuration
---

host: str = KctlCfg.host,
api_version: str = 'v1',
api_token: str = KctlCfg.api_token,
ssl_verify: bool = True,
strict: bool = False,
cache_time: int = 86400,
cache_dir: Union[Path, str] = DefaultCacheDir,
rancher_default_cluster: str = None,
rancher_fleet_name: str = 'fleet-default',
clusters_enabled: List[str] = [],
clusters_disabled: List[str] = []

---
Then validates against env variables during initialization, prioritizing env variables.
---

api_version = envToStr('KCTL_API_VERSION', api_version or 'v1')
ssl_verify = envToBool('KCTL_SSL_VERIFY', str(ssl_verify))
strict = envToBool('KCTL_STRICT', str(strict))
cache_time = envToInt('KCTL_CACHE_TIME', cache_time)
cache_dir = to_path(envToStr('KCTL_CACHE_DIR', None) or cache_dir)

rancher_default_cluster = envToStr('KCTL_RANCHER_DEFAULT_CLUSTER', rancher_default_cluster)
rancher_fleet_name = envToStr('KCTL_RANCHER_FLEET_NAME', rancher_fleet_name)

# If both are empty, then it will assume all clusters are enabled.
clusters_enabled = envToList('KCTL_CLUSTERS_ENABLED', clusters_enabled)
clusters_disabled = envToList('KCTL_CLUSTERS_DISABLED', clusters_disabled)

"""

data = {
    'KCTL_HOST': 'https://ranchercluster.com',
    'KCTL_API_TOKEN': 'token-xxxx:yyyyyyyyyyyyyyyyyyyyyyyyyyyy'
}

set_env_from_dict(data, override=True)
from kctl.client import KctlClient

## Rancher Specific
## This will build the object class dynamically
## enabling v1/v3 api methods.
## v3 is typically the management cluster
## v1 is cluster specific

KctlClient.build_rancher_ctx()

## KctlClient is a Class that doesnt require initialization

## Sync Method
cs = KctlClient.v3.list_cluster()

## Async Method
cs = await KctlClient.v3.async_list_cluster()

cs.data[-1].name

"""
local
"""

## Change Cluster Context

KctlClient.set_cluster('staging-cluster')

KctlClient.v1.url
"""
Now the primary api url will be called using the proper k8s path

-> https://ranchercluster.com/k8s/clusters/c-m-xxxxxx

"""

KctlClient.v1.list_apps_deployment()

"""
All v1 methods will now return the specified cluster context

{
    'type': 'collection', 
    'links': {'self': 'https://ranchercluster.com/k8s/clusters/c-m-xxxxxx/v1/apps.deployments/fleet-default'}, 
    'createTypes': {'apps.deployment': 'https://ranchercluster.com/k8s/clusters/c-m-xxxxxx/v1/apps.deployments'}, 
    'actions': {}, 
    'resourceType': 'apps.deployment', 
    'revision': '6671325', 
    'data': []
}
"""

Enhancements

This library borrows the dynamic initialization method from the primary rancher-client python library with several enhancements.

  • Async and Sync support via httpx / lazyapi, which can be initialized from a sync environment.

  • All async methods are accessed with async_ prefix of the same sync methods.

  • Inclusion of kubernetes python client which can be called via KctlClient.api, allowing setting of credentials once. Although this use case has not been extensively tested. (or rather, at all.)

  • Dynamic Access of downstream clusters without requiring reinitialization of the client

  • Enables context switching between clusters

  • Lazy caching of the api schema in kctl/kctl/.kctlcache

  • Enables access of v1/v3 api methods within the same client.

  • All returned results are objects, allowing dynamic access of object attributes, rather than strictly dict based.

  • Adds support for fleet namespaces to ensure proper url handling


Credits / Libraries Used

  • rancher-client: inspired dynamic schema initialization

  • kubernetes: included to manage credentials

  • lazyapi: personal library that is used to create the httpx clients.

  • lazycls: personal library that contains a lot of utility functions that enables this library to be as slim as possible.

You might also like...
A Kubernetes operator that creates UptimeRobot monitors for your ingresses

This operator automatically creates uptime monitors at UptimeRobot for your Kubernetes Ingress resources. This allows you to easily integrate uptime monitoring of your services into your Kubernetes deployments.

A Simple script to hunt unused Kubernetes resources.

K8SPurger A Simple script to hunt unused Kubernetes resources. Release History Release 0.3 Added Ingress Added Services Account Adding RoleBindding Re

Run Oracle on Kubernetes with El Carro

El Carro is a new project that offers a way to run Oracle databases in Kubernetes as a portable, open source, community driven, no vendor lock-in container orchestration system. El Carro provides a powerful declarative API for comprehensive and consistent configuration and deployment as well as for real-time operations and monitoring.

Linux, Jenkins, AWS, SRE, Prometheus, Docker, Python, Ansible, Git, Kubernetes, Terraform, OpenStack, SQL, NoSQL, Azure, GCP, DNS, Elastic, Network, Virtualization. DevOps Interview Questions
Linux, Jenkins, AWS, SRE, Prometheus, Docker, Python, Ansible, Git, Kubernetes, Terraform, OpenStack, SQL, NoSQL, Azure, GCP, DNS, Elastic, Network, Virtualization. DevOps Interview Questions

Linux, Jenkins, AWS, SRE, Prometheus, Docker, Python, Ansible, Git, Kubernetes, Terraform, OpenStack, SQL, NoSQL, Azure, GCP, DNS, Elastic, Network, Virtualization. DevOps Interview Questions

Chartreuse: Automated Alembic migrations within kubernetes
Chartreuse: Automated Alembic migrations within kubernetes

Chartreuse: Automated Alembic SQL schema migrations within kubernetes "How to automate management of Alembic database schema migration at scale using

Caboto, the Kubernetes semantic analysis tool
Caboto, the Kubernetes semantic analysis tool

Caboto Caboto, the Kubernetes semantic analysis toolkit. It contains a lightweight Python library for semantic analysis of plain Kubernetes manifests

Hubble - Network, Service & Security Observability for Kubernetes using eBPF
Hubble - Network, Service & Security Observability for Kubernetes using eBPF

Network, Service & Security Observability for Kubernetes What is Hubble? Getting Started Features Service Dependency Graph Metrics & Monitoring Flow V

A charmed operator for running PGbouncer on kubernetes.

operator-template Description TODO: Describe your charm in a few paragraphs of Markdown Usage TODO: Provide high-level usage, such as required config

Quick & dirty controller to schedule Kubernetes Jobs later (once)

K8s Jobber Operator Quickly implemented Kubernetes controller to enable scheduling of Jobs at a later time. Usage: To schedule a Job later, Set .spec.

Releases(v0.0.1)
Owner
Chief Architect @ Growth Engine
null
Google Kubernetes Engine (GKE) with a Snyk Kubernetes controller installed/configured for Snyk App

Google Kubernetes Engine (GKE) with a Snyk Kubernetes controller installed/configured for Snyk App This example provisions a Google Kubernetes Engine

Pas Apicella 2 Feb 9, 2022
Coding For Entrepreneurs 100 Jan 1, 2023
Helperpod - A CLI tool to run a Kubernetes utility pod with pre-installed tools that can be used for debugging/testing purposes inside a Kubernetes cluster

Helperpod is a CLI tool to run a Kubernetes utility pod with pre-installed tools that can be used for debugging/testing purposes inside a Kubernetes cluster.

Atakan Tatlı 2 Feb 5, 2022
This repository contains code examples and documentation for learning how applications can be developed with Kubernetes

BigBitBus KAT Components Click on the diagram to enlarge, or follow this link for detailed documentation Introduction Welcome to the BigBitBus Kuberne

null 51 Oct 16, 2022
sysctl/sysfs settings on a fly for Kubernetes Cluster. No restarts are required for clusters and nodes.

SysBindings Daemon Little toolkit for control the sysctl/sysfs bindings on Kubernetes Cluster on the fly and without unnecessary restarts of cluster o

Wallarm 19 May 6, 2022
Copy a Kubernetes pod and run commands in its environment

copypod Utility for copying a running Kubernetes pod so you can run commands in a copy of its environment, without worrying about it the pod potential

Memrise 4 Apr 8, 2022
Checkmk kube agent - Checkmk Kubernetes Cluster and Node Collectors

Checkmk Kubernetes Cluster and Node Collectors Checkmk cluster and node collecto

tribe29 GmbH 15 Dec 26, 2022
A basic instruction for Kubernetes setup and understanding.

A basic instruction for Kubernetes setup and understanding Module ID Module Guide - Install Kubernetes Cluster k8s-install 3 Docker Core Technology mo

null 648 Jan 2, 2023
A Blazing fast Security Auditing tool for Kubernetes

A Blazing fast Security Auditing tool for kubernetes!! Basic Overview Kubestriker performs numerous in depth checks on kubernetes infra to identify th

Vasant Chinnipilli 934 Jan 4, 2023
Official Python client library for kubernetes

Kubernetes Python Client Python client for the kubernetes API. Installation From source: git clone --recursive https://github.com/kubernetes-client/py

Kubernetes Clients 5.4k Jan 2, 2023