diff --git a/cbcontainers/state/components/runtime_resolver_deployment.go b/cbcontainers/state/components/runtime_resolver_deployment.go index d245866a..4a42ca3f 100644 --- a/cbcontainers/state/components/runtime_resolver_deployment.go +++ b/cbcontainers/state/components/runtime_resolver_deployment.go @@ -3,10 +3,6 @@ package components import ( "context" "fmt" - "k8s.io/client-go/kubernetes" - "k8s.io/client-go/rest" - "math" - cbContainersV1 "github.com/vmware/cbcontainers-operator/api/v1" "github.com/vmware/cbcontainers-operator/cbcontainers/state/applyment" commonState "github.com/vmware/cbcontainers-operator/cbcontainers/state/common" @@ -14,6 +10,7 @@ import ( coreV1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/types" + "math" "sigs.k8s.io/controller-runtime/pkg/client" ) @@ -36,11 +33,13 @@ var ( type ResolverDeploymentK8sObject struct { // Namespace is the Namespace in which the Deployment will be created. Namespace string + APIReader client.Reader } -func NewResolverDeploymentK8sObject() *ResolverDeploymentK8sObject { +func NewResolverDeploymentK8sObject(apiReader client.Reader) *ResolverDeploymentK8sObject { return &ResolverDeploymentK8sObject{ Namespace: commonState.DataPlaneNamespaceName, + APIReader: apiReader, } } @@ -234,25 +233,11 @@ func (obj *ResolverDeploymentK8sObject) mutateVolumesMounts(container *coreV1.Co } func (obj *ResolverDeploymentK8sObject) getDynamicReplicasCount(nodesToReplicasRatio int32) (*int32, error) { - // Get the in-cluster config - config, err := rest.InClusterConfig() - if err != nil { - return nil, fmt.Errorf("error getting in-cluster config: %v", err) - } - - // Create a Kubernetes client - clientSet, err := kubernetes.NewForConfig(config) - if err != nil { - return nil, fmt.Errorf("error creating Kubernetes client: %v", err) - } - - // Get the list of nodes in the cluster - nodes, err := clientSet.CoreV1().Nodes().List(context.Background(), metav1.ListOptions{}) - if err != nil { + nodesList := &coreV1.NodeList{} + if err := obj.APIReader.List(context.Background(), nodesList); err != nil || nodesList.Items == nil || len(nodesList.Items) < 1 { return nil, fmt.Errorf("error getting list of nodes: %v", err) } - - nodesCount := int32(math.Ceil(float64(len(nodes.Items)) / float64(nodesToReplicasRatio))) + nodesCount := int32(math.Ceil(float64(len(nodesList.Items)) / float64(nodesToReplicasRatio))) return &nodesCount, nil } diff --git a/cbcontainers/state/state_applier.go b/cbcontainers/state/state_applier.go index 5639756b..6cbc6741 100644 --- a/cbcontainers/state/state_applier.go +++ b/cbcontainers/state/state_applier.go @@ -41,7 +41,7 @@ type StateApplier struct { log logr.Logger } -func NewStateApplier(agentComponentApplier AgentComponentApplier, k8sVersion string, tlsSecretsValuesCreator components.TlsSecretsValuesCreator, log logr.Logger) *StateApplier { +func NewStateApplier(apiReader client.Reader, agentComponentApplier AgentComponentApplier, k8sVersion string, tlsSecretsValuesCreator components.TlsSecretsValuesCreator, log logr.Logger) *StateApplier { return &StateApplier{ desiredConfigMap: components.NewConfigurationK8sObject(), desiredRegistrySecret: components.NewRegistrySecretK8sObject(), @@ -53,7 +53,7 @@ func NewStateApplier(agentComponentApplier AgentComponentApplier, k8sVersion str enforcerValidatingWebhook: components.NewEnforcerValidatingWebhookK8sObject(k8sVersion), enforcerMutatingWebhook: components.NewEnforcerMutatingWebhookK8sObject(k8sVersion), stateReporterDeployment: components.NewStateReporterDeploymentK8sObject(), - resolverDeployment: components.NewResolverDeploymentK8sObject(), + resolverDeployment: components.NewResolverDeploymentK8sObject(apiReader), resolverService: components.NewResolverServiceK8sObject(), sensorDaemonSet: components.NewSensorDaemonSetK8sObject(), imageScanningReporterDeployment: components.NewImageScanningReporterDeploymentK8sObject(), diff --git a/main.go b/main.go index 2cc33d83..4c44a08c 100644 --- a/main.go +++ b/main.go @@ -119,7 +119,7 @@ func main() { Scheme: mgr.GetScheme(), K8sVersion: k8sVersion, ClusterProcessor: processors.NewAgentProcessor(cbContainersAgentLogger, processors.NewDefaultGatewayCreator(), operator.NewEnvVersionProvider(), clusterIdentifier), - StateApplier: state.NewStateApplier(agent_applyment.NewAgentComponent(applyment.NewComponentApplier(mgr.GetClient())), k8sVersion, certificatesUtils.NewCertificateCreator(), cbContainersAgentLogger), + StateApplier: state.NewStateApplier(mgr.GetAPIReader(), agent_applyment.NewAgentComponent(applyment.NewComponentApplier(mgr.GetClient())), k8sVersion, certificatesUtils.NewCertificateCreator(), cbContainersAgentLogger), }).SetupWithManager(mgr); err != nil { setupLog.Error(err, "unable to create controller", "controller", "CBContainersAgent") os.Exit(1)