All Projects → gkarthiks → k8s-discovery

gkarthiks / k8s-discovery

Licence: MIT license
Auto discover the running environment and serves the kubernetes clientset interface for interaction

Programming Languages

go
31211 projects - #10 most used programming language

Projects that are alternatives of or similar to k8s-discovery

Coerce Rs
Coerce - an asynchronous (async/await) Actor runtime and cluster framework for Rust
Stars: ✭ 231 (+425%)
Mutual labels:  cluster
Multi-Node-TimescaleDB
The multi-node setup of TimescaleDB 🐯🐯🐯 🐘 🐯🐯🐯
Stars: ✭ 42 (-4.55%)
Mutual labels:  cluster
clusterhat-image
Converts Raspbian/Raspberry Pi OS images to support Cluster HAT
Stars: ✭ 71 (+61.36%)
Mutual labels:  cluster
Turing Pi Cluster
Turing Pi cluster configuration for Raspberry Pi Compute Modules
Stars: ✭ 233 (+429.55%)
Mutual labels:  cluster
Magento-2-aws-cluster-terraform
Magento 2 AWS autoscaling cluster with Terraform and Packer or ImageBuilder. Adobe Commerce Cloud alternative. The best ecommerce infrastructure. Drive more sales online. Transparent billing. Developer-friendly. No hidden bottlenecks.
Stars: ✭ 107 (+143.18%)
Mutual labels:  cluster
neth-proxy
Stratum <-> Stratum Proxy and optimizer for ethminer
Stars: ✭ 35 (-20.45%)
Mutual labels:  cluster
Pyslurm
Python Interface to Slurm
Stars: ✭ 222 (+404.55%)
Mutual labels:  cluster
urb-k8s
Kubernetes adapter for Universal Resource Broker
Stars: ✭ 19 (-56.82%)
Mutual labels:  cluster
node-codis
Codis client for Node.js.
Stars: ✭ 18 (-59.09%)
Mutual labels:  cluster
k3d-action
A GitHub Action to run lightweight ephemeral Kubernetes clusters during workflow. Fundamental advantage of this action is a full customization of embedded k3s clusters. In addition, it provides a private image registry and multi-cluster support.
Stars: ✭ 137 (+211.36%)
Mutual labels:  cluster
Raspi Cluster
Notes and scripts for setting up (yet another) Raspberry Pi computing cluster
Stars: ✭ 235 (+434.09%)
Mutual labels:  cluster
Mosquitto Cluster
a built-in, autonomous Mosquitto Cluster implementation. MQTT集群.
Stars: ✭ 238 (+440.91%)
Mutual labels:  cluster
soa-checklist
Microservice Oriented Architecture checklist
Stars: ✭ 92 (+109.09%)
Mutual labels:  cluster
Redis Cluster
Redis Cluster setup running on Kubernetes
Stars: ✭ 230 (+422.73%)
Mutual labels:  cluster
terraform-aws-eks-workers
Terraform module to provision an AWS AutoScaling Group, IAM Role, and Security Group for EKS Workers
Stars: ✭ 82 (+86.36%)
Mutual labels:  cluster
Reading And Comprehense Redis Cluster
分布式NOSQL redis源码阅读中文分析注释,带详尽注释以及相关流程调用注释,提出改造点,redis cluster集群功能、节点扩容、槽位迁移、failover故障切换、一致性选举完整分析,对理解redis源码很有帮助,解决了source insight中文注释乱码问题,更新完毕(redis源码学习交流QQ群:568892619)
Stars: ✭ 224 (+409.09%)
Mutual labels:  cluster
caddy-tlsconsul
🔒 Consul K/V storage for Caddy Web Server / Certmagic TLS data
Stars: ✭ 89 (+102.27%)
Mutual labels:  cluster
meteor-cluster
worker pool for meteor using node js native `cluster` module
Stars: ✭ 18 (-59.09%)
Mutual labels:  cluster
docker-volume-hetzner
Docker Volume Plugin for accessing Hetzner Cloud Volumes
Stars: ✭ 81 (+84.09%)
Mutual labels:  cluster
ha cluster exporter
Prometheus exporter for Pacemaker based Linux HA clusters
Stars: ✭ 63 (+43.18%)
Mutual labels:  cluster

K8s-Discovery

PkgGoDev Release language Go Report Card License CII Best Practices

K8s Discovery, is an effort to reduce the boiler plate code for the client-go or kubernetes based cloud native GoLang developers.

The main aspect of this is around saving and cleaning the code for in-cluster and out-cluster configurations.

Usage

Run go get to get the k8s-discovery module as follows.

go get github.com/gkarthiks/k8s-discovery

Declare a variable as var k8s *discovery.K8s and initialize it as k8s, _ = discovery.NewK8s(). Now the k8s will hold the interface that will provide the clientset for Kubernetes communication that is pulled either via in-cluster or via kubeconfig file.

Available APIs at the moment

NewK8s: Will return a new kubernetes clientset's interface that is formulated either via in-cluster configuration or kubeconfog file.

GetVersion: Queries the Kubernetes for the version in v0.0.0-master+$Format:%h$

GetNamespace: Gets the namespace of the running pod if running inside the cluster, if outside returns based on the POD_NAMESPACE environment variable. This environment variable also takes precedence if provided in a pod.

Available client

K8s Discovery provides the client set for kubernetes client with hassle free configuration as well as the metrics client. The MetricsClientSet can be used to query the metrics against the containers. There is also a RestConfig exposed via discovery to make use of the rest api.

Example

package main

import (
	"context"
	"fmt"
	"log"
	"strconv"

	discovery "github.com/gkarthiks/k8s-discovery"
	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
	metricsTypes "k8s.io/metrics/pkg/apis/metrics/v1beta1"
)

var (
	k8s *discovery.K8s
)

func main() {
	k8s, _ = discovery.NewK8s()
	namespace, _ := k8s.GetNamespace()
	version, _ := k8s.GetVersion()
	fmt.Printf("Specified Namespace: %s\n", namespace)
	fmt.Printf("Version of running Kubernetes: %s\n", version)
	
	
	
	
	cronJobs, err := k8s.Clientset.BatchV1beta1().CronJobs(namespace).List(context.Background(), metav1.ListOptions{})
	if err != nil {
		log.Panic(err.Error())
	}
	for idx, crons := range cronJobs.Items {
		fmt.Printf("%d -> %s\n", idx, crons.Name)
	}
	
	
	
	
	fmt.Println("==== Moving towards the metrics query ====")
	
	podMetrics, err := k8s.MetricsClientSet.MetricsV1beta1().PodMetricses(namespace).List(context.Background(), metav1.ListOptions{})
	if err != nil {
		panic(err)
	}

	var podMetric metricsTypes.PodMetrics
	getPodUsageMetrics := func(pod metricsTypes.PodMetrics) {
		for _, container := range pod.Containers {
			cpuQuantityDec := container.Usage.Cpu().AsDec().String()
			cpuUsageFloat, _ := strconv.ParseFloat(cpuQuantityDec, 64)

			fmt.Printf( "CPU Usage Float: %v\n", cpuUsageFloat)

			memoryQuantityDec := container.Usage.Memory().AsDec().String()
			memoryUsageFloat, _ := strconv.ParseFloat(memoryQuantityDec, 64)
			fmt.Printf("Memory Usage Float: %v\n\n", memoryUsageFloat)
		}
	}

	for _, podMetric = range podMetrics.Items {
		getPodUsageMetrics(podMetric)
	}
	
	
	
}

Note:

For GCP or managed kubernetes, you have to import the auth module, else an error message stating no Auth Provider found for name "gcp" will be thrown. The import looks like the below for the sample program. Special mention @ringods.

import (
	"context"
	"fmt"
	"log"
	"strconv"

	discovery "github.com/gkarthiks/k8s-discovery"
	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
	metricsTypes "k8s.io/metrics/pkg/apis/metrics/v1beta1"
	_ "k8s.io/client-go/plugin/pkg/client/auth"
)

Contributing

For contributions please refer the contributing guidelines.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].