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: guard against nil model #2059

Merged
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
4 changes: 4 additions & 0 deletions pkg/controller/direct/directbase/directbase_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ func AddController(mgr manager.Manager, gvk schema.GroupVersionKind, model Model
immediateReconcileRequests := make(chan event.GenericEvent, k8s.ImmediateReconcileRequestsBufferSize)
resourceWatcherRoutines := semaphore.NewWeighted(k8s.MaxNumResourceWatcherRoutines)

if model == nil {
return fmt.Errorf("model is nil for gvk %s", gvk)
}

reconciler, err := NewReconciler(mgr, immediateReconcileRequests, resourceWatcherRoutines, gvk, model, deps.JitterGenerator)
if err != nil {
return err
Expand Down
2 changes: 1 addition & 1 deletion pkg/controller/direct/registry/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ type ModelFactoryFunc func(ctx context.Context, config *config.ControllerConfig)

func GetModel(gk schema.GroupKind) (directbase.Model, error) {
registration := singleton.registrations[gk]
if registration == nil {
if registration == nil || registration.model == nil {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could also test this one separately and say "Init not called" (I guess?)

return nil, fmt.Errorf("no model registered for %s", gk)
}
return registration.model, nil
Expand Down
9 changes: 9 additions & 0 deletions pkg/test/controller/reconciler/testreconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"testing"
"time"

"github.com/GoogleCloudPlatform/k8s-config-connector/pkg/config"
dclcontroller "github.com/GoogleCloudPlatform/k8s-config-connector/pkg/controller/dcl"
"github.com/GoogleCloudPlatform/k8s-config-connector/pkg/controller/direct/directbase"
"github.com/GoogleCloudPlatform/k8s-config-connector/pkg/controller/direct/registry"
Expand Down Expand Up @@ -95,6 +96,14 @@ func NewTestReconciler(t *testing.T, mgr manager.Manager, provider *tfschema.Pro
}
serviceMetaLoader := metadata.New()
dclConverter := conversion.New(dclSchemaLoader, serviceMetaLoader)

// Initialize direct controllers
if err := registry.Init(context.TODO(), &config.ControllerConfig{
HTTPClient: httpClient,
}); err != nil {
log.Fatalf("error intializing direct registry: %v", err)
}

return &TestReconciler{
mgr: mgr,
t: t,
Expand Down
Loading