Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix-informer-sync #1156

Merged
merged 6 commits into from
Aug 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 14 additions & 14 deletions radix-operator/alert/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,28 +27,28 @@ const (
)

// NewController creates a new controller that handles RadixAlerts
func NewController(ctx context.Context, client kubernetes.Interface,
radixClient radixclient.Interface, handler common.Handler,
func NewController(ctx context.Context,
kubeClient kubernetes.Interface,
radixClient radixclient.Interface,
handler common.Handler,
kubeInformerFactory kubeinformers.SharedInformerFactory,
radixInformerFactory informers.SharedInformerFactory,
waitForChildrenToSync bool,
recorder record.EventRecorder) *common.Controller {
logger := log.With().Str("controller", controllerAgentName).Logger()
alertInformer := radixInformerFactory.Radix().V1().RadixAlerts()
registrationInformer := radixInformerFactory.Radix().V1().RadixRegistrations()

controller := &common.Controller{
Name: controllerAgentName,
HandlerOf: crType,
KubeClient: client,
RadixClient: radixClient,
Informer: alertInformer.Informer(),
KubeInformerFactory: kubeInformerFactory,
WorkQueue: common.NewRateLimitedWorkQueue(ctx, crType),
Handler: handler,
WaitForChildrenToSync: waitForChildrenToSync,
Recorder: recorder,
LockKeyAndIdentifier: common.NamespacePartitionKey,
Name: controllerAgentName,
HandlerOf: crType,
KubeClient: kubeClient,
RadixClient: radixClient,
KubeInformerFactory: kubeInformerFactory,
RadixInformerFactory: radixInformerFactory,
WorkQueue: common.NewRateLimitedWorkQueue(ctx, crType),
Handler: handler,
Recorder: recorder,
LockKeyAndIdentifier: common.NamespacePartitionKey,
}

logger.Info().Msg("Setting up event handlers")
Expand Down
4 changes: 2 additions & 2 deletions radix-operator/alert/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func (s *controllerTestSuite) Test_RadixAlertEvents() {
alertName, namespace := "any-alert", "any-ns"
alert := &v1.RadixAlert{ObjectMeta: metav1.ObjectMeta{Name: alertName}}

sut := NewController(context.Background(), s.KubeClient, s.RadixClient, s.Handler, s.KubeInformerFactory, s.RadixInformerFactory, false, s.EventRecorder)
sut := NewController(context.Background(), s.KubeClient, s.RadixClient, s.Handler, s.KubeInformerFactory, s.RadixInformerFactory, s.EventRecorder)
s.RadixInformerFactory.Start(s.Ctx.Done())
s.KubeInformerFactory.Start(s.Ctx.Done())
go func() {
Expand Down Expand Up @@ -69,7 +69,7 @@ func (s *controllerTestSuite) Test_RadixRegistrationEvents() {
s.Require().NoError(err)
}

sut := NewController(context.Background(), s.KubeClient, s.RadixClient, s.Handler, s.KubeInformerFactory, s.RadixInformerFactory, false, s.EventRecorder)
sut := NewController(context.Background(), s.KubeClient, s.RadixClient, s.Handler, s.KubeInformerFactory, s.RadixInformerFactory, s.EventRecorder)
s.RadixInformerFactory.Start(s.Ctx.Done())
s.KubeInformerFactory.Start(s.Ctx.Done())
go func() {
Expand Down
28 changes: 14 additions & 14 deletions radix-operator/application/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,28 +26,28 @@ const (
)

// NewController creates a new controller that handles RadixApplications
func NewController(ctx context.Context, client kubernetes.Interface,
radixClient radixclient.Interface, handler common.Handler,
func NewController(ctx context.Context,
kubeClient kubernetes.Interface,
radixClient radixclient.Interface,
handler common.Handler,
kubeInformerFactory kubeinformers.SharedInformerFactory,
radixInformerFactory informers.SharedInformerFactory,
waitForChildrenToSync bool,
recorder record.EventRecorder) *common.Controller {
logger := log.With().Str("controller", controllerAgentName).Logger()
applicationInformer := radixInformerFactory.Radix().V1().RadixApplications()
registrationInformer := radixInformerFactory.Radix().V1().RadixRegistrations()

controller := &common.Controller{
Name: controllerAgentName,
HandlerOf: crType,
KubeClient: client,
RadixClient: radixClient,
Informer: applicationInformer.Informer(),
KubeInformerFactory: kubeInformerFactory,
WorkQueue: common.NewRateLimitedWorkQueue(ctx, crType),
Handler: handler,
WaitForChildrenToSync: waitForChildrenToSync,
Recorder: recorder,
LockKeyAndIdentifier: common.NamespacePartitionKey,
Name: controllerAgentName,
HandlerOf: crType,
KubeClient: kubeClient,
RadixClient: radixClient,
KubeInformerFactory: kubeInformerFactory,
RadixInformerFactory: radixInformerFactory,
WorkQueue: common.NewRateLimitedWorkQueue(ctx, crType),
Handler: handler,
Recorder: recorder,
LockKeyAndIdentifier: common.NamespacePartitionKey,
}

logger.Info().Msg("Setting up event handlers")
Expand Down
4 changes: 2 additions & 2 deletions radix-operator/application/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func (s *controllerTestSuite) Test_Controller_Calls_Handler() {
namespace := utils.GetAppNamespace(appName)
appNamespace := test.CreateAppNamespace(s.KubeClient, appName)

sut := NewController(context.Background(), s.KubeClient, s.RadixClient, s.Handler, s.KubeInformerFactory, s.RadixInformerFactory, false, s.EventRecorder)
sut := NewController(context.Background(), s.KubeClient, s.RadixClient, s.Handler, s.KubeInformerFactory, s.RadixInformerFactory, s.EventRecorder)
s.RadixInformerFactory.Start(s.Ctx.Done())
s.KubeInformerFactory.Start(s.Ctx.Done())

Expand All @@ -53,7 +53,7 @@ func (s *controllerTestSuite) Test_Controller_Calls_Handler_On_Admin_Or_Reader_C
s.Require().NoError(err)
}

sut := NewController(context.Background(), s.KubeClient, s.RadixClient, s.Handler, s.KubeInformerFactory, s.RadixInformerFactory, false, s.EventRecorder)
sut := NewController(context.Background(), s.KubeClient, s.RadixClient, s.Handler, s.KubeInformerFactory, s.RadixInformerFactory, s.EventRecorder)
s.RadixInformerFactory.Start(s.Ctx.Done())
s.KubeInformerFactory.Start(s.Ctx.Done())

Expand Down
28 changes: 14 additions & 14 deletions radix-operator/batch/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,28 +23,28 @@ const (
)

// NewController creates a new controller that handles RadixBatches
func NewController(ctx context.Context, client kubernetes.Interface,
radixClient radixclient.Interface, handler common.Handler,
func NewController(ctx context.Context,
kubeClient kubernetes.Interface,
radixClient radixclient.Interface,
handler common.Handler,
kubeInformerFactory kubeinformers.SharedInformerFactory,
radixInformerFactory informers.SharedInformerFactory,
waitForChildrenToSync bool,
recorder record.EventRecorder) *common.Controller {
logger := log.With().Str("controller", controllerAgentName).Logger()
batchInformer := radixInformerFactory.Radix().V1().RadixBatches()
jobInformer := kubeInformerFactory.Batch().V1().Jobs()

controller := &common.Controller{
Name: controllerAgentName,
HandlerOf: crType,
KubeClient: client,
RadixClient: radixClient,
Informer: batchInformer.Informer(),
KubeInformerFactory: kubeInformerFactory,
WorkQueue: common.NewRateLimitedWorkQueue(ctx, crType),
Handler: handler,
WaitForChildrenToSync: waitForChildrenToSync,
Recorder: recorder,
LockKeyAndIdentifier: common.NamespacePartitionKey,
Name: controllerAgentName,
HandlerOf: crType,
KubeClient: kubeClient,
RadixClient: radixClient,
KubeInformerFactory: kubeInformerFactory,
RadixInformerFactory: radixInformerFactory,
WorkQueue: common.NewRateLimitedWorkQueue(ctx, crType),
Handler: handler,
Recorder: recorder,
LockKeyAndIdentifier: common.NamespacePartitionKey,
}

logger.Info().Msg("Setting up event handlers")
Expand Down
2 changes: 1 addition & 1 deletion radix-operator/batch/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func (s *controllerTestSuite) Test_RadixBatchEvents() {
batchName, namespace := "a-batch-job", "a-ns"
jobName := "a-job"

sut := NewController(context.Background(), s.KubeClient, s.RadixClient, s.Handler, s.KubeInformerFactory, s.RadixInformerFactory, false, s.EventRecorder)
sut := NewController(context.Background(), s.KubeClient, s.RadixClient, s.Handler, s.KubeInformerFactory, s.RadixInformerFactory, s.EventRecorder)
s.RadixInformerFactory.Start(s.Ctx.Done())
s.KubeInformerFactory.Start(s.Ctx.Done())
go func() {
Expand Down
50 changes: 17 additions & 33 deletions radix-operator/common/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

"github.com/equinor/radix-operator/pkg/apis/metrics"
radixclient "github.com/equinor/radix-operator/pkg/client/clientset/versioned"
informers "github.com/equinor/radix-operator/pkg/client/informers/externalversions"
"github.com/rs/zerolog/log"
"golang.org/x/sync/errgroup"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand All @@ -21,18 +22,17 @@ import (

// Controller Instance variables
type Controller struct {
Name string
HandlerOf string
KubeClient kubernetes.Interface
RadixClient radixclient.Interface
WorkQueue workqueue.RateLimitingInterface
Informer cache.SharedIndexInformer
KubeInformerFactory kubeinformers.SharedInformerFactory
Handler Handler
WaitForChildrenToSync bool
Recorder record.EventRecorder
LockKeyAndIdentifier LockKeyAndIdentifierFunc
locker resourceLocker
Name string
HandlerOf string
KubeClient kubernetes.Interface
RadixClient radixclient.Interface
WorkQueue workqueue.RateLimitingInterface
KubeInformerFactory kubeinformers.SharedInformerFactory
RadixInformerFactory informers.SharedInformerFactory
Handler Handler
Recorder record.EventRecorder
LockKeyAndIdentifier LockKeyAndIdentifierFunc
locker resourceLocker
}

// Run starts the shared informer, which will be stopped when stopCh is closed.
Expand All @@ -48,24 +48,12 @@ func (c *Controller) Run(ctx context.Context, threadiness int) error {
logger := log.Ctx(ctx)
logger.Debug().Msgf("Starting")

cacheSyncs := []cache.InformerSynced{
c.hasSynced,
}

logger.Debug().Msg("Start WaitForChildrenToSync")
if c.WaitForChildrenToSync {
cacheSyncs = append(cacheSyncs,
c.KubeInformerFactory.Core().V1().Namespaces().Informer().HasSynced,
c.KubeInformerFactory.Core().V1().Secrets().Informer().HasSynced,
)
}
logger.Debug().Msg("Completed WaitForChildrenToSync")

// Wait for the caches to be synced before starting workers
logger.Info().Msg("Waiting for informer caches to sync")
if ok := cache.WaitForCacheSync(ctx.Done(), cacheSyncs...); !ok {
return fmt.Errorf("failed to wait for caches to sync")
}
logger.Info().Msg("Waiting for Kube objects caches to sync")
c.KubeInformerFactory.WaitForCacheSync(ctx.Done())
logger.Info().Msg("Waiting for Radix objects caches to sync")
c.RadixInformerFactory.WaitForCacheSync(ctx.Done())
logger.Info().Msg("Completed syncing informer caches")

logger.Info().Msg("Starting workers")

Expand Down Expand Up @@ -252,7 +240,3 @@ func (c *Controller) HandleObject(ctx context.Context, obj interface{}, ownerKin
return
}
}

func (c *Controller) hasSynced() bool {
return c.Informer.HasSynced()
}
Loading
Loading