github.com/Racer159/jackal@v0.32.7-0.20240401174413-0bd2339e4f2e/src/pkg/k8s/hpa.go (about)

     1  // SPDX-License-Identifier: Apache-2.0
     2  // SPDX-FileCopyrightText: 2021-Present The Jackal Authors
     3  
     4  // Package k8s provides a client for interacting with a Kubernetes cluster.
     5  package k8s
     6  
     7  import (
     8  	"context"
     9  
    10  	autoscalingV2 "k8s.io/api/autoscaling/v2"
    11  	corev1 "k8s.io/api/core/v1"
    12  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    13  )
    14  
    15  // GetAllHPAs returns a list of horizontal pod autoscalers for all namespaces.
    16  func (k *K8s) GetAllHPAs() (*autoscalingV2.HorizontalPodAutoscalerList, error) {
    17  	return k.GetHPAs(corev1.NamespaceAll)
    18  }
    19  
    20  // GetHPAs returns a list of horizontal pod autoscalers in a given namespace.
    21  func (k *K8s) GetHPAs(namespace string) (*autoscalingV2.HorizontalPodAutoscalerList, error) {
    22  	metaOptions := metav1.ListOptions{}
    23  	return k.Clientset.AutoscalingV2().HorizontalPodAutoscalers(namespace).List(context.TODO(), metaOptions)
    24  }
    25  
    26  // GetHPA returns a single horizontal pod autoscaler by namespace and name.
    27  func (k *K8s) GetHPA(namespace, name string) (*autoscalingV2.HorizontalPodAutoscaler, error) {
    28  	metaOptions := metav1.GetOptions{}
    29  	return k.Clientset.AutoscalingV2().HorizontalPodAutoscalers(namespace).Get(context.TODO(), name, metaOptions)
    30  }
    31  
    32  // UpdateHPA updates the given horizontal pod autoscaler in the cluster.
    33  func (k *K8s) UpdateHPA(hpa *autoscalingV2.HorizontalPodAutoscaler) (*autoscalingV2.HorizontalPodAutoscaler, error) {
    34  	metaOptions := metav1.UpdateOptions{}
    35  	return k.Clientset.AutoscalingV2().HorizontalPodAutoscalers(hpa.Namespace).Update(context.TODO(), hpa, metaOptions)
    36  }