Skip to content

Commit

Permalink
add discovery client
Browse files Browse the repository at this point in the history
  • Loading branch information
zanetworker committed Jan 16, 2024
1 parent 6d0715b commit ccf862e
Show file tree
Hide file tree
Showing 4 changed files with 287 additions and 12 deletions.
99 changes: 99 additions & 0 deletions discovery.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
package main

import (
"context"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
"os"
"path/filepath"

corev1 "k8s.io/api/core/v1"
)

// InitializeInternalClient InitializeKubernetesClient initializes a Kubernetes client from the environment
func InitializeInternalClient() (*kubernetes.Clientset, error) {
config, err := rest.InClusterConfig()
if err != nil {
return nil, err
}
clientset, err := kubernetes.NewForConfig(config)
if err != nil {
return nil, err
}
return clientset, nil
}

// InitializeKubernetesClientForExternalUse initializes a Kubernetes client from a KubeConfig
func InitializeKubernetesClientForExternalUse() (*kubernetes.Clientset, error) {
kubeconfig := os.Getenv("KUBECONFIG")

if kubeconfig == "" {
kubeconfig = filepath.Join(os.Getenv("HOME"), ".kube", "config")
}

config, err := clientcmd.BuildConfigFromFlags("", kubeconfig)
if err != nil {
return nil, err
}

clientset, err := kubernetes.NewForConfig(config)
if err != nil {
return nil, err
}

return clientset, nil
}

type NodeResourceInfo struct {
NodeName string
CPU float64 // CPUs in the node
Memory float64 // Memory in GiB
MaxPods int // Max Pods, needs custom logic for OpenShift
}

func FetchClusterDataTwo(clientset *kubernetes.Clientset) ([]NodeResourceInfo, error) {
labelSelector := "node-role.kubernetes.io/control-plane="
nodes, err := clientset.CoreV1().Nodes().List(context.TODO(), metav1.ListOptions{
LabelSelector: labelSelector,
})
if err != nil {
return nil, err
}

var nodesResourceInfo []NodeResourceInfo

for _, node := range nodes.Items {
cpu := node.Status.Allocatable.Cpu().MilliValue()
memory := node.Status.Allocatable.Memory().Value()
memoryInGiB := float64(memory) / (1024 * 1024 * 1024)
// Placeholder for maxPods logic
maxPods := inferMaxPodsFromNode(node) // Implement this function based on your setup

nodesResourceInfo = append(nodesResourceInfo, NodeResourceInfo{
NodeName: node.Name,
CPU: float64(cpu) / 1000, // Convert milliCPU to CPU
Memory: memoryInGiB,
MaxPods: maxPods,
})
}

return nodesResourceInfo, nil
}

func inferMaxPodsFromNode(node corev1.Node) int {
// Retrieve the allocatable pods from the node status
if allocatablePods, exists := node.Status.Allocatable["pods"]; exists {
// The value is a Quantity, which needs to be parsed to an int
maxPods, b := allocatablePods.AsInt64()
if b != true {
// Handle error or use a default value if parsing fails
return 250 // Example default value
}
return int(maxPods)
}

// Default value if the allocatable pods are not set
return 250 // Adjust this default value based on your needs
}
52 changes: 46 additions & 6 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,20 +1,60 @@
module github.com/zanetworker/hcp-sizer

go 1.20
go 1.21

toolchain go1.21.5

require (
github.com/fatih/color v1.16.0
github.com/manifoldco/promptui v0.9.0
github.com/spf13/cobra v1.8.0
k8s.io/apimachinery v0.29.0
k8s.io/client-go v0.29.0
)

require (
github.com/AlecAivazis/survey/v2 v2.3.7 // indirect
github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e // indirect
github.com/fatih/color v1.16.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/emicklei/go-restful/v3 v3.11.0 // indirect
github.com/go-logr/logr v1.3.0 // indirect
github.com/go-openapi/jsonpointer v0.19.6 // indirect
github.com/go-openapi/jsonreference v0.20.2 // indirect
github.com/go-openapi/swag v0.22.3 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/google/gnostic-models v0.6.8 // indirect
github.com/google/gofuzz v1.2.0 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/imdario/mergo v0.3.6 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 // indirect
github.com/manifoldco/promptui v0.9.0 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/mgutz/ansi v0.0.0-20170206155736-9520e82c474b // indirect
github.com/spf13/cobra v1.8.0 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/spf13/pflag v1.0.5 // indirect
golang.org/x/net v0.17.0 // indirect
golang.org/x/oauth2 v0.10.0 // indirect
golang.org/x/sys v0.14.0 // indirect
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211 // indirect
golang.org/x/text v0.4.0 // indirect
golang.org/x/term v0.13.0 // indirect
golang.org/x/text v0.13.0 // indirect
golang.org/x/time v0.3.0 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/protobuf v1.31.0 // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
k8s.io/api v0.29.0 // indirect
k8s.io/klog/v2 v2.110.1 // indirect
k8s.io/kube-openapi v0.0.0-20231010175941-2dd684a91f00 // indirect
k8s.io/utils v0.0.0-20230726121419-3b25d923346b // indirect
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd // indirect
sigs.k8s.io/structured-merge-diff/v4 v4.4.1 // indirect
sigs.k8s.io/yaml v1.3.0 // indirect
)
Loading

0 comments on commit ccf862e

Please sign in to comment.