Skip to content

Commit

Permalink
Propagate apiReader to ResolverDeploymentK8sObject
Browse files Browse the repository at this point in the history
  • Loading branch information
davidf146 committed May 29, 2023
1 parent e7f45ad commit fb0bdaa
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 25 deletions.
29 changes: 7 additions & 22 deletions cbcontainers/state/components/runtime_resolver_deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,14 @@ 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"
appsV1 "k8s.io/api/apps/v1"
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"
)

Expand All @@ -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,
}
}

Expand Down Expand Up @@ -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
}
4 changes: 2 additions & 2 deletions cbcontainers/state/state_applier.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand All @@ -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(),
Expand Down
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit fb0bdaa

Please sign in to comment.