Skip to content

Commit

Permalink
Use a discoveryAgent go prevent creating clients on init()
Browse files Browse the repository at this point in the history
Signed-off-by: Raul Sevilla <[email protected]>
  • Loading branch information
rsevilla87 committed Dec 2, 2022
1 parent 3bb8618 commit dca1acd
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 45 deletions.
4 changes: 3 additions & 1 deletion cmd/kube-burner/ocp.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (

_ "embed"

"github.com/cloud-bulldozer/kube-burner/pkg/discovery"
"github.com/cloud-bulldozer/kube-burner/pkg/workloads"
uid "github.com/satori/go.uuid"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -54,7 +55,8 @@ func openShiftCmd() *cobra.Command {
"BURST": fmt.Sprintf("%d", *burst),
"INDEXING": fmt.Sprintf("%v", indexing),
}
wh = workloads.NewWorkloadHelper(envVars, *alerting, OCPConfig)
discoveryAgent := discovery.NewDiscoveryAgent()
wh = workloads.NewWorkloadHelper(envVars, *alerting, OCPConfig, discoveryAgent)
wh.Metadata.UUID = *uuid
if *esServer != "" {
err := wh.GatherMetadata()
Expand Down
56 changes: 32 additions & 24 deletions pkg/discovery/discovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,15 @@ import (
)

var tokenExpiration = 10 * time.Hour
var clientset *kubernetes.Clientset
var dynamicClient dynamic.Interface

func init() {
type Agent struct {
clientSet *kubernetes.Clientset
dynamicClient dynamic.Interface
}

var discoveryAgent Agent

func NewDiscoveryAgent() Agent {
var kubeconfig string
if os.Getenv("KUBECONFIG") != "" {
kubeconfig = os.Getenv("KUBECONFIG")
Expand All @@ -49,25 +54,28 @@ func init() {
if err != nil {
log.Fatal(err)
}
clientset = kubernetes.NewForConfigOrDie(restConfig)
dynamicClient = dynamic.NewForConfigOrDie(restConfig)
discoveryAgent = Agent{
clientSet: kubernetes.NewForConfigOrDie(restConfig),
dynamicClient: dynamic.NewForConfigOrDie(restConfig),
}
return discoveryAgent
}

// GetPrometheus Returns Prometheus URL and valid Bearer token
func GetPrometheus() (string, string, error) {
prometheusURL, err := getPrometheusURL()
func (da *Agent) GetPrometheus() (string, string, error) {
prometheusURL, err := getPrometheusURL(da.dynamicClient)
if err != nil {
return "", "", err
}
prometheusToken, err := getBearerToken()
prometheusToken, err := getBearerToken(da.clientSet)
if err != nil {
return "", "", err
}
return prometheusURL, prometheusToken, nil
}

// getPrometheusURL Returns a valid prometheus endpoint from the openshift-monitoring/prometheus-k8s route
func getPrometheusURL() (string, error) {
func getPrometheusURL(dynamicClient dynamic.Interface) (string, error) {
route, err := dynamicClient.Resource(schema.GroupVersionResource{
Group: routeGroup,
Version: routeVersion,
Expand All @@ -89,7 +97,7 @@ func getPrometheusURL() (string, error) {
}

// getBearerToken returns a valid bearer token from the openshift-monitoring/prometheus-k8s service account
func getBearerToken() (string, error) {
func getBearerToken(clientset *kubernetes.Clientset) (string, error) {
request := authenticationv1.TokenRequest{
Spec: authenticationv1.TokenRequestSpec{
ExpirationSeconds: pointer.Int64Ptr(int64(tokenExpiration.Seconds())),
Expand All @@ -104,21 +112,21 @@ func getBearerToken() (string, error) {
}

// GetWorkerNodeCount returns the number of worker nodes
func GetWorkerNodeCount() (int, error) {
nodeList, err := clientset.CoreV1().Nodes().List(context.TODO(), v1.ListOptions{LabelSelector: "node-role.kubernetes.io/worker="})
func (da *Agent) GetWorkerNodeCount() (int, error) {
nodeList, err := da.clientSet.CoreV1().Nodes().List(context.TODO(), v1.ListOptions{LabelSelector: "node-role.kubernetes.io/worker="})
log.Debug("Node count: ", len(nodeList.Items))
return len(nodeList.Items), err
}

// GetCurrentPodCount returns the number of current running pods across all worker nodes
func GetCurrentPodCount() (int, error) {
func (da *Agent) GetCurrentPodCount() (int, error) {
var podCount int
nodeList, err := clientset.CoreV1().Nodes().List(context.TODO(), v1.ListOptions{LabelSelector: "node-role.kubernetes.io/worker="})
nodeList, err := da.clientSet.CoreV1().Nodes().List(context.TODO(), v1.ListOptions{LabelSelector: "node-role.kubernetes.io/worker="})
if err != nil {
return podCount, err
}
for _, node := range nodeList.Items {
podList, err := clientset.CoreV1().Pods(v1.NamespaceAll).List(context.TODO(), v1.ListOptions{FieldSelector: "status.phase=Running,spec.nodeName=" + node.Name})
podList, err := da.clientSet.CoreV1().Pods(v1.NamespaceAll).List(context.TODO(), v1.ListOptions{FieldSelector: "status.phase=Running,spec.nodeName=" + node.Name})
if err != nil {
return podCount, err
}
Expand All @@ -129,9 +137,9 @@ func GetCurrentPodCount() (int, error) {
}

// GetInfraDetails returns cluster anme and platform
func GetInfraDetails() (InfraObj, error) {
func (da *Agent) GetInfraDetails() (InfraObj, error) {
var infraJSON InfraObj
infra, err := dynamicClient.Resource(schema.GroupVersionResource{
infra, err := da.dynamicClient.Resource(schema.GroupVersionResource{
Group: "config.openshift.io",
Version: "v1",
Resource: "infrastructures",
Expand All @@ -145,15 +153,15 @@ func GetInfraDetails() (InfraObj, error) {
}

// GetVersionInfo obtains OCP and k8s version information
func GetVersionInfo() (VersionObj, error) {
func (da *Agent) GetVersionInfo() (VersionObj, error) {
var cv clusterVersion
var versionInfo VersionObj
version, err := clientset.ServerVersion()
version, err := da.clientSet.ServerVersion()
versionInfo.K8sVersion = version.GitVersion
if err != nil {
return versionInfo, err
}
clusterVersion, err := dynamicClient.Resource(
clusterVersion, err := da.dynamicClient.Resource(
schema.GroupVersionResource{
Group: "config.openshift.io",
Version: "v1",
Expand All @@ -175,9 +183,9 @@ func GetVersionInfo() (VersionObj, error) {
}

// GetNodesInfo returns node information
func GetNodesInfo() (NodeInfo, error) {
func (da *Agent) GetNodesInfo() (NodeInfo, error) {
var nodeInfoData NodeInfo
nodes, err := clientset.CoreV1().Nodes().List(context.TODO(), v1.ListOptions{})
nodes, err := da.clientSet.CoreV1().Nodes().List(context.TODO(), v1.ListOptions{})
if err != nil {
return nodeInfoData, err
}
Expand All @@ -200,8 +208,8 @@ func GetNodesInfo() (NodeInfo, error) {
}

// GetSDNInfo returns SDN type
func GetSDNInfo() (string, error) {
networkData, err := dynamicClient.Resource(schema.GroupVersionResource{
func (da *Agent) GetSDNInfo() (string, error) {
networkData, err := da.dynamicClient.Resource(schema.GroupVersionResource{
Group: "config.openshift.io",
Version: "v1",
Resource: "networks",
Expand Down
20 changes: 11 additions & 9 deletions pkg/workloads/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ type WorkloadHelper struct {
Metadata clusterMetadata
alerting bool
ocpConfig embed.FS
discoveryAgent discovery.Agent
}

type clusterMetadata struct {
Expand All @@ -70,17 +71,18 @@ type clusterMetadata struct {
}

// NewWorkloadHelper initializes workloadHelper
func NewWorkloadHelper(envVars map[string]string, alerting bool, ocpConfig embed.FS) WorkloadHelper {
func NewWorkloadHelper(envVars map[string]string, alerting bool, ocpConfig embed.FS, da discovery.Agent) WorkloadHelper {
return WorkloadHelper{
envVars: envVars,
alerting: alerting,
ocpConfig: ocpConfig,
envVars: envVars,
alerting: alerting,
ocpConfig: ocpConfig,
discoveryAgent: da,
}
}

// SetKubeBurnerFlags configures the required environment variables and flags for kube-burner
func (wh *WorkloadHelper) SetKubeBurnerFlags() {
prometheusURL, prometheusToken, err := discovery.GetPrometheus()
prometheusURL, prometheusToken, err := wh.discoveryAgent.GetPrometheus()
if err != nil {
log.Fatal("Error obtaining Prometheus information:", err.Error())
}
Expand All @@ -92,19 +94,19 @@ func (wh *WorkloadHelper) SetKubeBurnerFlags() {
}

func (wh *WorkloadHelper) GatherMetadata() error {
infra, err := discovery.GetInfraDetails()
infra, err := wh.discoveryAgent.GetInfraDetails()
if err != nil {
return err
}
version, err := discovery.GetVersionInfo()
version, err := wh.discoveryAgent.GetVersionInfo()
if err != nil {
return err
}
nodeInfo, err := discovery.GetNodesInfo()
nodeInfo, err := wh.discoveryAgent.GetNodesInfo()
if err != nil {
return err
}
sdnType, err := discovery.GetSDNInfo()
sdnType, err := wh.discoveryAgent.GetSDNInfo()
if err != nil {
return err
}
Expand Down
5 changes: 2 additions & 3 deletions pkg/workloads/node-density-cni.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (

"github.com/cloud-bulldozer/kube-burner/log"

"github.com/cloud-bulldozer/kube-burner/pkg/discovery"
"github.com/spf13/cobra"
)

Expand All @@ -40,12 +39,12 @@ func NewNodeDensityCNI(wh *WorkloadHelper) *cobra.Command {
os.Exit(0)
}
wh.Metadata.Benchmark = cmd.Name()
workerNodeCount, err := discovery.GetWorkerNodeCount()
workerNodeCount, err := wh.discoveryAgent.GetWorkerNodeCount()
if err != nil {
log.Fatal("Error obtaining worker node count:", err)
}
totalPods := workerNodeCount * podsPerNode
podCount, err := discovery.GetCurrentPodCount()
podCount, err := wh.discoveryAgent.GetCurrentPodCount()
if err != nil {
log.Fatal(err)
}
Expand Down
5 changes: 2 additions & 3 deletions pkg/workloads/node-density-heavy.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (

"github.com/cloud-bulldozer/kube-burner/log"

"github.com/cloud-bulldozer/kube-burner/pkg/discovery"
"github.com/spf13/cobra"
)

Expand All @@ -42,12 +41,12 @@ func NewNodeDensityHeavy(wh *WorkloadHelper) *cobra.Command {
os.Exit(0)
}
wh.Metadata.Benchmark = cmd.Name()
workerNodeCount, err := discovery.GetWorkerNodeCount()
workerNodeCount, err := wh.discoveryAgent.GetWorkerNodeCount()
if err != nil {
log.Fatal("Error obtaining worker node count:", err)
}
totalPods := workerNodeCount * podsPerNode
podCount, err := discovery.GetCurrentPodCount()
podCount, err := wh.discoveryAgent.GetCurrentPodCount()
if err != nil {
log.Fatal(err)
}
Expand Down
8 changes: 3 additions & 5 deletions pkg/workloads/node-density.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (

"github.com/cloud-bulldozer/kube-burner/log"

"github.com/cloud-bulldozer/kube-burner/pkg/discovery"
"github.com/spf13/cobra"
)

Expand All @@ -43,17 +42,16 @@ func NewNodeDensity(wh *WorkloadHelper) *cobra.Command {
os.Exit(0)
}
wh.Metadata.Benchmark = cmd.Name()
workerNodeCount, err := discovery.GetWorkerNodeCount()
workerNodeCount, err := wh.discoveryAgent.GetWorkerNodeCount()
if err != nil {
log.Fatal("Error obtaining worker node count:", err)
}
totalPods := workerNodeCount * podsPerNode
podCount, err := discovery.GetCurrentPodCount()
podCount, err := wh.discoveryAgent.GetCurrentPodCount()
if err != nil {
log.Fatal(err)
}
jobIterations := totalPods - podCount
os.Setenv("JOB_ITERATIONS", fmt.Sprint(jobIterations))
os.Setenv("JOB_ITERATIONS", fmt.Sprint(totalPods-podCount))
os.Setenv("POD_READY_THRESHOLD", fmt.Sprintf("%v", podReadyThreshold))
os.Setenv("CONTAINER_IMAGE", containerImage)
},
Expand Down

0 comments on commit dca1acd

Please sign in to comment.