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

refactor(informer-manager): use gvk as keys and support TweakListOptions #151

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
9 changes: 9 additions & 0 deletions pkg/apis/core/v1alpha1/extensions_federatedtypeconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,15 @@ func (f *FederatedTypeConfig) GetSourceTypeGVR() schema.GroupVersionResource {
}
}

func (f *FederatedTypeConfig) GetSourceTypeGVK() schema.GroupVersionKind {
apiResource := f.GetSourceType()
return schema.GroupVersionKind{
Group: apiResource.Group,
Version: apiResource.Version,
Kind: apiResource.Kind,
}
}

func (f *FederatedTypeConfig) GetStatusCollectionEnabled() bool {
return f.Spec.StatusCollection != nil
}
Expand Down
22 changes: 0 additions & 22 deletions pkg/controllers/util/federatedinformer.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
fedcorev1a1 "github.com/kubewharf/kubeadmiral/pkg/apis/core/v1alpha1"
"github.com/kubewharf/kubeadmiral/pkg/client/generic"
"github.com/kubewharf/kubeadmiral/pkg/controllers/common"
"github.com/kubewharf/kubeadmiral/pkg/controllers/util/managedlabel"

Check failure on line 44 in pkg/controllers/util/federatedinformer.go

View workflow job for this annotation

GitHub Actions / test (1.19)

no required module provides package github.com/kubewharf/kubeadmiral/pkg/controllers/util/managedlabel; to add it:

Check failure on line 44 in pkg/controllers/util/federatedinformer.go

View workflow job for this annotation

GitHub Actions / test (1.20)

no required module provides package github.com/kubewharf/kubeadmiral/pkg/controllers/util/managedlabel; to add it:
"github.com/kubewharf/kubeadmiral/pkg/controllers/util/schema"
)

Expand Down Expand Up @@ -289,28 +289,6 @@
return federatedInformer, err
}

func IsClusterReady(clusterStatus *fedcorev1a1.FederatedClusterStatus) bool {
for _, condition := range clusterStatus.Conditions {
if condition.Type == fedcorev1a1.ClusterReady {
if condition.Status == corev1.ConditionTrue {
return true
}
}
}
return false
}

func IsClusterJoined(clusterStatus *fedcorev1a1.FederatedClusterStatus) bool {
for _, condition := range clusterStatus.Conditions {
if condition.Type == fedcorev1a1.ClusterJoined {
if condition.Status == corev1.ConditionTrue {
return true
}
}
}
return false
}

type informer struct {
controller cache.Controller
store cache.Store
Expand Down
46 changes: 46 additions & 0 deletions pkg/util/cluster/util.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
Copyright 2023 The KubeAdmiral Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package cluster

import (
corev1 "k8s.io/api/core/v1"

fedcorev1a1 "github.com/kubewharf/kubeadmiral/pkg/apis/core/v1alpha1"
)


Check failure on line 25 in pkg/util/cluster/util.go

View workflow job for this annotation

GitHub Actions / lint

File is not `gci`-ed with --skip-generated -s standard -s default -s prefix(github.com/kubewharf/kubeadmiral) --custom-order (gci)

Check failure on line 25 in pkg/util/cluster/util.go

View workflow job for this annotation

GitHub Actions / lint

File is not `gofumpt`-ed (gofumpt)
func IsClusterReady(clusterStatus *fedcorev1a1.FederatedClusterStatus) bool {
for _, condition := range clusterStatus.Conditions {
if condition.Type == fedcorev1a1.ClusterReady {
if condition.Status == corev1.ConditionTrue {
return true
}
}
}
return false
}

func IsClusterJoined(clusterStatus *fedcorev1a1.FederatedClusterStatus) bool {
Copy link
Member

Choose a reason for hiding this comment

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

Are we intentionally leaving the pkg/controllers/util counterparts behind?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Removed

for _, condition := range clusterStatus.Conditions {
if condition.Type == fedcorev1a1.ClusterJoined {
if condition.Status == corev1.ConditionTrue {
return true
}
}
}
return false
}
25 changes: 25 additions & 0 deletions pkg/util/informermanager/common.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
Copyright 2023 The KubeAdmiral Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package informermanager

import fedcorev1a1 "github.com/kubewharf/kubeadmiral/pkg/apis/core/v1alpha1"

// RegisterOncePredicate can be used to as an EventHandlerGenerator predicate
// to generate and register event handlers exactly once for each FTC.
func RegisterOncePredicate(old, _ *fedcorev1a1.FederatedTypeConfig) bool {
return old == nil
}
26 changes: 20 additions & 6 deletions pkg/util/informermanager/federatedinformermanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"sync"

apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/client-go/dynamic"
Expand All @@ -34,8 +35,9 @@ import (
fedcorev1a1 "github.com/kubewharf/kubeadmiral/pkg/apis/core/v1alpha1"
fedcorev1a1informers "github.com/kubewharf/kubeadmiral/pkg/client/informers/externalversions/core/v1alpha1"
fedcorev1a1listers "github.com/kubewharf/kubeadmiral/pkg/client/listers/core/v1alpha1"
"github.com/kubewharf/kubeadmiral/pkg/controllers/util"
clusterutil "github.com/kubewharf/kubeadmiral/pkg/util/cluster"
"github.com/kubewharf/kubeadmiral/pkg/util/logging"
"github.com/kubewharf/kubeadmiral/pkg/util/managedlabel"
)

type federatedInformerManager struct {
Expand Down Expand Up @@ -83,7 +85,7 @@ func NewFederatedInformerManager(
clusterInformer.Informer().AddEventHandler(cache.FilteringResourceEventHandler{
FilterFunc: func(obj interface{}) bool {
cluster := obj.(*fedcorev1a1.FederatedCluster)
return util.IsClusterJoined(&cluster.Status)
return clusterutil.IsClusterJoined(&cluster.Status)
},
Handler: cache.ResourceEventHandlerFuncs{
AddFunc: func(obj interface{}) { manager.enqueue(obj) },
Expand Down Expand Up @@ -129,7 +131,7 @@ func (m *federatedInformerManager) worker(ctx context.Context) {
m.queue.AddRateLimited(key)
return
}
if apierrors.IsNotFound(err) || !util.IsClusterJoined(&cluster.Status) {
if apierrors.IsNotFound(err) || !clusterutil.IsClusterJoined(&cluster.Status) {
if err := m.processClusterDeletion(ctx, name); err != nil {
logger.Error(err, "Failed to process FederatedCluster, will retry")
m.queue.AddRateLimited(key)
Expand Down Expand Up @@ -185,7 +187,19 @@ func (m *federatedInformerManager) processCluster(
return fmt.Errorf("failed to get client for cluster %s: %w", clusterName, err), true
}

manager := NewInformerManager(clusterClient, m.ftcInformer)
manager := NewInformerManager(
clusterClient,
m.ftcInformer,
func(opts *metav1.ListOptions) {
selector := &metav1.LabelSelector{}
metav1.AddLabelToSelector(
selector,
managedlabel.ManagedByKubeAdmiralLabelKey,
managedlabel.ManagedByKubeAdmiralLabelValue,
)
opts.LabelSelector = metav1.FormatLabelSelector(selector)
},
)

ctx, cancel := context.WithCancel(ctx)
for _, generator := range m.eventHandlerGenerators {
Expand Down Expand Up @@ -268,7 +282,7 @@ func (m *federatedInformerManager) GetFederatedTypeConfigLister() fedcorev1a1lis
}

func (m *federatedInformerManager) GetResourceLister(
gvr schema.GroupVersionResource,
gvk schema.GroupVersionKind,
cluster string,
) (lister cache.GenericLister, informerSynced cache.InformerSynced, exists bool) {
m.lock.RLock()
Expand All @@ -279,7 +293,7 @@ func (m *federatedInformerManager) GetResourceLister(
return nil, nil, false
}

return manager.GetResourceLister(gvr)
return manager.GetResourceLister(gvk)
}

func (m *federatedInformerManager) HasSynced() bool {
Expand Down
24 changes: 12 additions & 12 deletions pkg/util/informermanager/federatedinformermanager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,11 +182,11 @@ func TestFederatedInformerManager(t *testing.T) {
// 2. Verify that listers for existing FTCs and clusters are eventually available

for _, ftc := range defaultFTCs {
gvr := ftc.GetSourceTypeGVR()
gvk := ftc.GetSourceTypeGVK()

for _, cluster := range defaultClusters {
g.Eventually(func(g gomega.Gomega) {
lister, informerSynced, exists := manager.GetResourceLister(gvr, cluster.Name)
lister, informerSynced, exists := manager.GetResourceLister(gvk, cluster.Name)

g.Expect(exists).To(gomega.BeTrue())
g.Expect(lister).ToNot(gomega.BeNil())
Expand All @@ -197,17 +197,17 @@ func TestFederatedInformerManager(t *testing.T) {

// 3. Verify that the lister for non-existent FTCs or clusters are not available

lister, informerSynced, exists := manager.GetResourceLister(common.DaemonSetGVR, "cluster-1")
lister, informerSynced, exists := manager.GetResourceLister(daemonsetGVK, "cluster-1")
g.Expect(exists).To(gomega.BeFalse())
g.Expect(lister).To(gomega.BeNil())
g.Expect(informerSynced).To(gomega.BeNil())

lister, informerSynced, exists = manager.GetResourceLister(common.DeploymentGVR, "cluster-4")
lister, informerSynced, exists = manager.GetResourceLister(deploymentGVK, "cluster-4")
g.Expect(exists).To(gomega.BeFalse())
g.Expect(lister).To(gomega.BeNil())
g.Expect(informerSynced).To(gomega.BeNil())

lister, informerSynced, exists = manager.GetResourceLister(common.DaemonSetGVR, "cluster-4")
lister, informerSynced, exists = manager.GetResourceLister(daemonsetGVK, "cluster-4")
g.Expect(exists).To(gomega.BeFalse())
g.Expect(lister).To(gomega.BeNil())
g.Expect(informerSynced).To(gomega.BeNil())
Expand Down Expand Up @@ -245,13 +245,13 @@ func TestFederatedInformerManager(t *testing.T) {
}()

ftc := daemonsetFTC
gvr := ftc.GetSourceTypeGVR()
gvk := ftc.GetSourceTypeGVK()

// 2. Verify that listers for daemonsets FTCs is not available at the start

g.Consistently(func(g gomega.Gomega) {
for _, cluster := range defaultClusters {
lister, informerSynced, exists := manager.GetResourceLister(common.DeploymentGVR, cluster.Name)
lister, informerSynced, exists := manager.GetResourceLister(deploymentGVK, cluster.Name)
g.Expect(exists).To(gomega.BeFalse())
g.Expect(lister).To(gomega.BeNil())
g.Expect(informerSynced).To(gomega.BeNil())
Expand All @@ -267,7 +267,7 @@ func TestFederatedInformerManager(t *testing.T) {

g.Eventually(func(g gomega.Gomega) {
for _, cluster := range defaultClusters {
lister, informerSynced, exists := manager.GetResourceLister(gvr, cluster.Name)
lister, informerSynced, exists := manager.GetResourceLister(gvk, cluster.Name)
g.Expect(exists).To(gomega.BeTrue())
g.Expect(lister).ToNot(gomega.BeNil())
g.Expect(informerSynced()).To(gomega.BeTrue())
Expand Down Expand Up @@ -308,9 +308,9 @@ func TestFederatedInformerManager(t *testing.T) {

g.Consistently(func(g gomega.Gomega) {
for _, ftc := range defaultFTCs {
gvr := ftc.GetSourceTypeGVR()
gvk := ftc.GetSourceTypeGVK()

lister, informerSynced, exists := manager.GetResourceLister(gvr, cluster.Name)
lister, informerSynced, exists := manager.GetResourceLister(gvk, cluster.Name)
g.Expect(exists).To(gomega.BeFalse())
g.Expect(lister).To(gomega.BeNil())
g.Expect(informerSynced).To(gomega.BeNil())
Expand All @@ -326,9 +326,9 @@ func TestFederatedInformerManager(t *testing.T) {

g.Eventually(func(g gomega.Gomega) {
for _, ftc := range defaultFTCs {
gvr := ftc.GetSourceTypeGVR()
gvk := ftc.GetSourceTypeGVK()

lister, informerSynced, exists := manager.GetResourceLister(gvr, cluster.Name)
lister, informerSynced, exists := manager.GetResourceLister(gvk, cluster.Name)
g.Expect(exists).To(gomega.BeTrue())
g.Expect(lister).ToNot(gomega.BeNil())
g.Expect(informerSynced()).To(gomega.BeTrue())
Expand Down
Loading
Loading