feat: Implement Kubernetes client and example
- Add Kubernetes client module for interacting with kubectl - Implement methods to get cluster info, pods, deployments, and services - Create a Kubernetes example script demonstrating client usage - Add JSON response structs for parsing kubectl output - Define runtime resource structs (Pod, Deployment, Service) for structured data - Include comprehensive unit tests for data structures and client logic
This commit is contained in:
@@ -1,8 +1,5 @@
|
||||
module kubernetes
|
||||
|
||||
import incubaid.herolib.data.encoderhero
|
||||
import os
|
||||
|
||||
// K8s API Version and Kind tracking
|
||||
@[params]
|
||||
pub struct K8sMetadata {
|
||||
@@ -150,7 +147,7 @@ pub mut:
|
||||
pub struct KubeConfig {
|
||||
pub mut:
|
||||
kubeconfig_path string
|
||||
context string = ''
|
||||
context string
|
||||
namespace string = 'default'
|
||||
api_server string
|
||||
ca_cert_path string
|
||||
@@ -179,3 +176,189 @@ pub mut:
|
||||
running_pods int
|
||||
api_server string
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// Kubectl JSON Response Structs
|
||||
// These structs match the JSON structure returned by kubectl commands
|
||||
// ============================================================================
|
||||
|
||||
// Version response from 'kubectl version -o json'
|
||||
struct KubectlVersionResponse {
|
||||
server_version ServerVersionInfo @[json: serverVersion]
|
||||
}
|
||||
|
||||
struct ServerVersionInfo {
|
||||
git_version string @[json: gitVersion]
|
||||
major string
|
||||
minor string
|
||||
}
|
||||
|
||||
// Generic list response structure
|
||||
struct KubectlListResponse {
|
||||
items []KubectlItemMetadata
|
||||
}
|
||||
|
||||
struct KubectlItemMetadata {
|
||||
metadata KubectlMetadata
|
||||
}
|
||||
|
||||
struct KubectlMetadata {
|
||||
name string
|
||||
}
|
||||
|
||||
// Pod list response from 'kubectl get pods -o json'
|
||||
struct KubectlPodListResponse {
|
||||
items []KubectlPodItem
|
||||
}
|
||||
|
||||
struct KubectlPodItem {
|
||||
metadata KubectlPodMetadata
|
||||
spec KubectlPodSpec
|
||||
status KubectlPodStatus
|
||||
}
|
||||
|
||||
struct KubectlPodMetadata {
|
||||
name string
|
||||
namespace string
|
||||
labels map[string]string
|
||||
creation_timestamp string @[json: creationTimestamp]
|
||||
}
|
||||
|
||||
struct KubectlPodSpec {
|
||||
node_name string @[json: nodeName]
|
||||
containers []KubectlContainer
|
||||
}
|
||||
|
||||
struct KubectlContainer {
|
||||
name string
|
||||
image string
|
||||
}
|
||||
|
||||
struct KubectlPodStatus {
|
||||
phase string
|
||||
pod_ip string @[json: podIP]
|
||||
}
|
||||
|
||||
// Deployment list response from 'kubectl get deployments -o json'
|
||||
struct KubectlDeploymentListResponse {
|
||||
items []KubectlDeploymentItem
|
||||
}
|
||||
|
||||
struct KubectlDeploymentItem {
|
||||
metadata KubectlDeploymentMetadata
|
||||
spec KubectlDeploymentSpec
|
||||
status KubectlDeploymentStatus
|
||||
}
|
||||
|
||||
struct KubectlDeploymentMetadata {
|
||||
name string
|
||||
namespace string
|
||||
labels map[string]string
|
||||
creation_timestamp string @[json: creationTimestamp]
|
||||
}
|
||||
|
||||
struct KubectlDeploymentSpec {
|
||||
replicas int
|
||||
}
|
||||
|
||||
struct KubectlDeploymentStatus {
|
||||
ready_replicas int @[json: readyReplicas]
|
||||
available_replicas int @[json: availableReplicas]
|
||||
updated_replicas int @[json: updatedReplicas]
|
||||
}
|
||||
|
||||
// Service list response from 'kubectl get services -o json'
|
||||
struct KubectlServiceListResponse {
|
||||
items []KubectlServiceItem
|
||||
}
|
||||
|
||||
struct KubectlServiceItem {
|
||||
metadata KubectlServiceMetadata
|
||||
spec KubectlServiceSpec
|
||||
status KubectlServiceStatus
|
||||
}
|
||||
|
||||
struct KubectlServiceMetadata {
|
||||
name string
|
||||
namespace string
|
||||
labels map[string]string
|
||||
creation_timestamp string @[json: creationTimestamp]
|
||||
}
|
||||
|
||||
struct KubectlServiceSpec {
|
||||
service_type string @[json: type]
|
||||
cluster_ip string @[json: clusterIP]
|
||||
external_ips []string @[json: externalIPs]
|
||||
ports []KubectlServicePort
|
||||
}
|
||||
|
||||
struct KubectlServicePort {
|
||||
port int
|
||||
protocol string
|
||||
}
|
||||
|
||||
struct KubectlServiceStatus {
|
||||
load_balancer KubectlLoadBalancerStatus @[json: loadBalancer]
|
||||
}
|
||||
|
||||
struct KubectlLoadBalancerStatus {
|
||||
ingress []KubectlLoadBalancerIngress
|
||||
}
|
||||
|
||||
struct KubectlLoadBalancerIngress {
|
||||
ip string
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// Runtime resource structs (returned from kubectl get commands)
|
||||
// ============================================================================
|
||||
|
||||
// Pod runtime information
|
||||
pub struct Pod {
|
||||
pub mut:
|
||||
name string
|
||||
namespace string
|
||||
status string
|
||||
node string
|
||||
ip string
|
||||
containers []string
|
||||
labels map[string]string
|
||||
created_at string
|
||||
}
|
||||
|
||||
// Deployment runtime information
|
||||
pub struct Deployment {
|
||||
pub mut:
|
||||
name string
|
||||
namespace string
|
||||
replicas int
|
||||
ready_replicas int
|
||||
available_replicas int
|
||||
updated_replicas int
|
||||
labels map[string]string
|
||||
created_at string
|
||||
}
|
||||
|
||||
// Service runtime information
|
||||
pub struct Service {
|
||||
pub mut:
|
||||
name string
|
||||
namespace string
|
||||
service_type string
|
||||
cluster_ip string
|
||||
external_ip string
|
||||
ports []string
|
||||
labels map[string]string
|
||||
created_at string
|
||||
}
|
||||
|
||||
// Version information from kubectl version command
|
||||
pub struct VersionInfo {
|
||||
pub mut:
|
||||
major string
|
||||
minor string
|
||||
git_version string
|
||||
git_commit string
|
||||
build_date string
|
||||
platform string
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user