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: namespaced kruise-controller panic due to multiNamespaceCache #1773

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
18 changes: 12 additions & 6 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -247,19 +247,25 @@ func main() {
setupLog.Error(err, "unable to wait webhook ready")
os.Exit(1)
}

setupLog.Info("setup controllers")
if err = controller.SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to setup controllers")
os.Exit(1)
}
}()

setupLog.Info("setup controllers")
if err = controller.SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to setup controllers")
os.Exit(1)
}

setupLog.Info("starting manager")
if err := mgr.Start(ctx); err != nil {
setupLog.Error(err, "problem running manager")
os.Exit(1)
}

setupLog.Info("setup controllers that need manager started")
if err = controller.SetupAfterStart(mgr); err != nil {
setupLog.Error(err, "unable to setup controllers after manager start")
os.Exit(1)
}
}

func setRestConfig(c *rest.Config) {
Expand Down
19 changes: 17 additions & 2 deletions pkg/controller/controllers.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ import (
)

var controllerAddFuncs []func(manager.Manager) error
var controllerAddAfterStarts []func(manager.Manager) error

func init() {
controllerAddFuncs = append(controllerAddFuncs, advancedcronjob.Add)
Expand All @@ -58,15 +59,16 @@ func init() {
controllerAddFuncs = append(controllerAddFuncs, statefulset.Add)
controllerAddFuncs = append(controllerAddFuncs, uniteddeployment.Add)
controllerAddFuncs = append(controllerAddFuncs, podunavailablebudget.Add)
controllerAddFuncs = append(controllerAddFuncs, workloadspread.Add)
controllerAddFuncs = append(controllerAddFuncs, resourcedistribution.Add)
controllerAddFuncs = append(controllerAddFuncs, ephemeraljob.Add)
controllerAddFuncs = append(controllerAddFuncs, containerlauchpriority.Add)
controllerAddFuncs = append(controllerAddFuncs, persistentpodstate.Add)
controllerAddFuncs = append(controllerAddFuncs, sidecarterminator.Add)
controllerAddFuncs = append(controllerAddFuncs, podprobemarker.Add)
controllerAddFuncs = append(controllerAddFuncs, nodepodprobe.Add)
controllerAddFuncs = append(controllerAddFuncs, imagelistpulljob.Add)

controllerAddAfterStarts = append(controllerAddAfterStarts, workloadspread.Add)
controllerAddAfterStarts = append(controllerAddAfterStarts, persistentpodstate.Add)
}

func SetupWithManager(m manager.Manager) error {
Expand All @@ -81,3 +83,16 @@ func SetupWithManager(m manager.Manager) error {
}
return nil
}

func SetupAfterStart(m manager.Manager) error {
for _, f := range controllerAddAfterStarts {
if err := f(m); err != nil {
if kindMatchErr, ok := err.(*meta.NoKindMatchError); ok {
klog.InfoS("CRD is not installed, its controller will perform noops!", "CRD", kindMatchErr.GroupKind)
continue
}
return err
}
}
return nil
}
10 changes: 5 additions & 5 deletions pkg/controller/daemonset/daemonset_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@
v1core "k8s.io/client-go/kubernetes/typed/core/v1"
appslisters "k8s.io/client-go/listers/apps/v1"
corelisters "k8s.io/client-go/listers/core/v1"
"k8s.io/client-go/tools/cache"
"k8s.io/client-go/tools/record"
"k8s.io/client-go/util/flowcontrol"
"k8s.io/client-go/util/retry"
Expand All @@ -66,6 +65,7 @@
kruiseclientset "github.com/openkruise/kruise/pkg/client/clientset/versioned"
"github.com/openkruise/kruise/pkg/client/clientset/versioned/scheme"
kruiseappslisters "github.com/openkruise/kruise/pkg/client/listers/apps/v1alpha1"
controllerutil "github.com/openkruise/kruise/pkg/controller/util"
"github.com/openkruise/kruise/pkg/features"
kruiseutil "github.com/openkruise/kruise/pkg/util"
utilclient "github.com/openkruise/kruise/pkg/util/client"
Expand Down Expand Up @@ -172,10 +172,10 @@
return nil, err
}

dsLister := kruiseappslisters.NewDaemonSetLister(dsInformer.(cache.SharedIndexInformer).GetIndexer())
historyLister := appslisters.NewControllerRevisionLister(revInformer.(cache.SharedIndexInformer).GetIndexer())
podLister := corelisters.NewPodLister(podInformer.(cache.SharedIndexInformer).GetIndexer())
nodeLister := corelisters.NewNodeLister(nodeInformer.(cache.SharedIndexInformer).GetIndexer())
dsLister := kruiseappslisters.NewDaemonSetLister(controllerutil.GetCacheIndexer(dsInformer))
historyLister := appslisters.NewControllerRevisionLister(controllerutil.GetCacheIndexer(revInformer))
podLister := corelisters.NewPodLister(controllerutil.GetCacheIndexer(podInformer))
nodeLister := corelisters.NewNodeLister(controllerutil.GetCacheIndexer(nodeInformer))

Check warning on line 178 in pkg/controller/daemonset/daemonset_controller.go

View check run for this annotation

Codecov / codecov/patch

pkg/controller/daemonset/daemonset_controller.go#L175-L178

Added lines #L175 - L178 were not covered by tests
failedPodsBackoff := flowcontrol.NewBackOff(1*time.Second, 15*time.Minute)
revisionAdapter := revisionadapter.NewDefaultImpl()

Expand Down
12 changes: 6 additions & 6 deletions pkg/controller/statefulset/statefulset_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
appslisters "k8s.io/client-go/listers/apps/v1"
corelisters "k8s.io/client-go/listers/core/v1"
storagelisters "k8s.io/client-go/listers/storage/v1"
toolscache "k8s.io/client-go/tools/cache"
"k8s.io/client-go/tools/record"
"k8s.io/klog/v2"
kubecontroller "k8s.io/kubernetes/pkg/controller"
Expand All @@ -54,6 +53,7 @@
"github.com/openkruise/kruise/pkg/client"
kruiseclientset "github.com/openkruise/kruise/pkg/client/clientset/versioned"
kruiseappslisters "github.com/openkruise/kruise/pkg/client/listers/apps/v1beta1"
controllerutil "github.com/openkruise/kruise/pkg/controller/util"
"github.com/openkruise/kruise/pkg/features"
"github.com/openkruise/kruise/pkg/util"
utilclient "github.com/openkruise/kruise/pkg/util/client"
Expand Down Expand Up @@ -133,10 +133,10 @@
return nil, err
}

statefulSetLister := kruiseappslisters.NewStatefulSetLister(statefulSetInformer.(toolscache.SharedIndexInformer).GetIndexer())
podLister := corelisters.NewPodLister(podInformer.(toolscache.SharedIndexInformer).GetIndexer())
pvcLister := corelisters.NewPersistentVolumeClaimLister(pvcInformer.(toolscache.SharedIndexInformer).GetIndexer())
scLister := storagelisters.NewStorageClassLister(scInformer.(toolscache.SharedIndexInformer).GetIndexer())
statefulSetLister := kruiseappslisters.NewStatefulSetLister(controllerutil.GetCacheIndexer(statefulSetInformer))
podLister := corelisters.NewPodLister(controllerutil.GetCacheIndexer(podInformer))
pvcLister := corelisters.NewPersistentVolumeClaimLister(controllerutil.GetCacheIndexer(pvcInformer))
scLister := storagelisters.NewStorageClassLister(controllerutil.GetCacheIndexer(scInformer))

Check warning on line 139 in pkg/controller/statefulset/statefulset_controller.go

View check run for this annotation

Codecov / codecov/patch

pkg/controller/statefulset/statefulset_controller.go#L136-L139

Added lines #L136 - L139 were not covered by tests

genericClient := client.GetGenericClientWithName("statefulset-controller")
eventBroadcaster := record.NewBroadcaster()
Expand All @@ -159,7 +159,7 @@
inplaceupdate.New(utilclient.NewClientFromManager(mgr, "statefulset-controller"), revisionadapter.NewDefaultImpl()),
lifecycle.New(utilclient.NewClientFromManager(mgr, "statefulset-controller")),
NewRealStatefulSetStatusUpdater(genericClient.KruiseClient, statefulSetLister),
history.NewHistory(genericClient.KubeClient, appslisters.NewControllerRevisionLister(revInformer.(toolscache.SharedIndexInformer).GetIndexer())),
history.NewHistory(genericClient.KubeClient, appslisters.NewControllerRevisionLister(controllerutil.GetCacheIndexer(revInformer))),

Check warning on line 162 in pkg/controller/statefulset/statefulset_controller.go

View check run for this annotation

Codecov / codecov/patch

pkg/controller/statefulset/statefulset_controller.go#L162

Added line #L162 was not covered by tests
recorder,
),
podControl: kubecontroller.RealPodControl{KubeClient: genericClient.KubeClient, Recorder: recorder},
Expand Down
20 changes: 20 additions & 0 deletions pkg/controller/util/cache.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package util

import (
"k8s.io/client-go/tools/cache"
toolscache "sigs.k8s.io/controller-runtime/pkg/cache"
)

// GetCacheIndexer helps to get the cache indexer from the informer
func GetCacheIndexer(informer toolscache.Informer) cache.Indexer {
shardIndexInformer, ok := informer.(cache.SharedIndexInformer)
if ok {
return shardIndexInformer.GetIndexer()
}
indexers := cache.Indexers{}
err := informer.(toolscache.Informer).AddIndexers(indexers)
if err != nil {
panic(err)
}
return cache.NewIndexer(cache.DeletionHandlingMetaNamespaceKeyFunc, indexers)
Copy link
Member

Choose a reason for hiding this comment

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

is it the recommended way to retrieve indexer of multi namespaced informer ? it seems rather tricky.

}
Loading