Skip to content

Commit

Permalink
Struct changes
Browse files Browse the repository at this point in the history
  • Loading branch information
sssash18 committed Nov 21, 2023
1 parent c406c16 commit 8e3a4fe
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 36 deletions.
40 changes: 4 additions & 36 deletions cluster-autoscaler/cloudprovider/mcm/mcm_cloud_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,25 +24,18 @@ package mcm
import (
"context"
"fmt"
v1 "k8s.io/api/apps/v1"
apiv1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
"k8s.io/apimachinery/pkg/fields"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/types"
"k8s.io/autoscaler/cluster-autoscaler/cloudprovider"
"k8s.io/autoscaler/cluster-autoscaler/config"
"k8s.io/autoscaler/cluster-autoscaler/config/dynamic"
"k8s.io/autoscaler/cluster-autoscaler/utils/errors"
"k8s.io/client-go/tools/cache"
"k8s.io/client-go/tools/clientcmd"
"k8s.io/klog/v2"
schedulerframework "k8s.io/kubernetes/pkg/scheduler/framework"
"os"
"strings"
"time"

"k8s.io/autoscaler/cluster-autoscaler/cloudprovider"
"k8s.io/autoscaler/cluster-autoscaler/config"
"k8s.io/autoscaler/cluster-autoscaler/config/dynamic"
v1appslister "k8s.io/client-go/listers/apps/v1"
)

const (
Expand Down Expand Up @@ -198,37 +191,12 @@ func (mcm *mcmCloudProvider) GetResourceLimiter() (*cloudprovider.ResourceLimite
return mcm.resourceLimiter, nil
}

var deploymentLister *v1appslister.DeploymentLister

func init() {
deploymentLister = newDeploymentLister()
}

func newDeploymentLister() *v1appslister.DeploymentLister {
namespace := os.Getenv("CONTROL_NAMESPACE")
controlKubeconfig, err := clientcmd.BuildConfigFromFlags("", "")
if err != nil {
klog.Errorf(err.Error())
}
controlCoreClientBuilder := CoreControllerClientBuilder{
ClientConfig: controlKubeconfig,
}
deployKubeClient := controlCoreClientBuilder.ClientOrDie("deploykubeclient")
selector := fields.Everything()
deploymentListWatch := cache.NewListWatchFromClient(deployKubeClient.AppsV1().RESTClient(), "deployments", namespace, selector)
store, reflector := cache.NewNamespaceKeyedIndexerAndReflector(deploymentListWatch, &v1.Deployment{}, time.Hour)
deploymentLister := v1appslister.NewDeploymentLister(store)
stopCh := make(chan struct{})
go reflector.Run(stopCh)
return &deploymentLister
}

// Refresh is called before every main loop and can be used to dynamically update cloud provider state.
// In particular the list of node groups returned by NodeGroups can change as a result of CloudProvider.Refresh().
func (mcm *mcmCloudProvider) Refresh() error {

namespace := os.Getenv("CONTROL_NAMESPACE")
deployment, err := (*deploymentLister).Deployments(namespace).Get("machine-controller-manager")
deployment, err := mcm.mcmManager.deploymentLister.Deployments(namespace).Get("machine-controller-manager")
if err != nil {
klog.Errorf("failed to get machine-controller-manager deployment: ", err.Error())
return err
Expand Down
14 changes: 14 additions & 0 deletions cluster-autoscaler/cloudprovider/mcm/mcm_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ import (
"errors"
"flag"
"fmt"
appsv1 "k8s.io/api/apps/v1"
"k8s.io/apimachinery/pkg/fields"
v1appslister "k8s.io/client-go/listers/apps/v1"
"math/rand"
"net/http"
"os"
Expand Down Expand Up @@ -115,6 +118,7 @@ type McmManager struct {
namespace string
interrupt chan struct{}
discoveryOpts cloudprovider.NodeGroupDiscoveryOptions
deploymentLister v1appslister.DeploymentLister
machineClient machineapi.MachineV1alpha1Interface
machineDeploymentLister machinelisters.MachineDeploymentLister
machineSetLister machinelisters.MachineSetLister
Expand Down Expand Up @@ -167,6 +171,15 @@ func createMCMManagerInternal(discoveryOpts cloudprovider.NodeGroupDiscoveryOpti
controlCoreClientBuilder := CoreControllerClientBuilder{
ClientConfig: controlKubeconfig,
}

deployKubeClient := controlCoreClientBuilder.ClientOrDie("deploykubeclient")
selector := fields.Everything()
deploymentListWatch := cache.NewListWatchFromClient(deployKubeClient.AppsV1().RESTClient(), "deployments", namespace, selector)
store, reflector := cache.NewNamespaceKeyedIndexerAndReflector(deploymentListWatch, &appsv1.Deployment{}, time.Hour)
deploymentLister := v1appslister.NewDeploymentLister(store)
stopCh := make(chan struct{})
go reflector.Run(stopCh)

availableResources, err := getAvailableResources(controlCoreClientBuilder)
if err != nil {
return nil, err
Expand Down Expand Up @@ -232,6 +245,7 @@ func createMCMManagerInternal(discoveryOpts cloudprovider.NodeGroupDiscoveryOpti
m := &McmManager{
namespace: namespace,
interrupt: make(chan struct{}),
deploymentLister: deploymentLister,
machineClient: controlMachineClient,
machineClassLister: machineClassLister,
machineLister: machineSharedInformers.Machines().Lister(),
Expand Down

0 comments on commit 8e3a4fe

Please sign in to comment.