Refactor to use common queue/filter funcs (#197)

Signed-off-by: Jian Qiu <jqiu@redhat.com>
This commit is contained in:
Jian Qiu
2023-06-28 15:59:19 +02:00
committed by GitHub
parent 9450d14759
commit e4792e4b83
47 changed files with 411 additions and 395 deletions
@@ -7,7 +7,6 @@ import (
"github.com/openshift/library-go/pkg/operator/events"
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/runtime"
utilerrors "k8s.io/apimachinery/pkg/util/errors"
"k8s.io/client-go/tools/cache"
"k8s.io/klog/v2"
@@ -22,6 +21,7 @@ import (
clusterv1beta1 "open-cluster-management.io/api/cluster/v1beta1"
"open-cluster-management.io/ocm/pkg/common/patcher"
"open-cluster-management.io/ocm/pkg/common/queue"
)
// addonConfigurationController is a controller to update configuration of mca with the following order
@@ -64,6 +64,8 @@ func NewAddonConfigurationController(
addonClient: addonClient,
clusterManagementAddonLister: clusterManagementAddonInformers.Lister(),
managedClusterAddonIndexer: addonInformers.Informer().GetIndexer(),
placementLister: placementInformer.Lister(),
placementDecisionLister: placementDecisionInformer.Lister(),
addonFilterFunc: addonFilterFunc,
}
@@ -79,27 +81,14 @@ func NewAddonConfigurationController(
}
controllerFactory := factory.New().WithFilteredEventsInformersQueueKeysFunc(
func(obj runtime.Object) []string {
key, _ := cache.DeletionHandlingMetaNamespaceKeyFunc(obj)
return []string{key}
},
queue.QueueKeyByMetaNamespaceName,
c.addonFilterFunc,
clusterManagementAddonInformers.Informer()).WithInformersQueueKeysFunc(
func(obj runtime.Object) []string {
key, _ := cache.DeletionHandlingMetaNamespaceKeyFunc(obj)
return []string{key}
},
addonInformers.Informer())
// This is to handle the case the self managed addon-manager does not have placementInformer/placementDecisionInformer.
// we will not consider installStrategy related placement for self managed addon-manager.
if placementInformer != nil && placementDecisionInformer != nil {
controllerFactory = controllerFactory.WithInformersQueueKeysFunc(
clusterManagementAddonInformers.Informer()).
WithInformersQueueKeysFunc(queue.QueueKeyByMetaNamespaceName, addonInformers.Informer()).
WithInformersQueueKeysFunc(
index.ClusterManagementAddonByPlacementDecisionQueueKey(clusterManagementAddonInformers), placementDecisionInformer.Informer()).
WithInformersQueueKeysFunc(index.ClusterManagementAddonByPlacementQueueKey(clusterManagementAddonInformers), placementInformer.Informer())
c.placementLister = placementInformer.Lister()
c.placementDecisionLister = placementDecisionInformer.Lister()
}
WithInformersQueueKeysFunc(
index.ClusterManagementAddonByPlacementQueueKey(clusterManagementAddonInformers), placementInformer.Informer())
return controllerFactory.WithSync(c.sync).ToController("addon-configuration-controller", recorder)
}
@@ -6,7 +6,6 @@ import (
"github.com/openshift/library-go/pkg/controller/factory"
"github.com/openshift/library-go/pkg/operator/events"
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/runtime"
utilerrors "k8s.io/apimachinery/pkg/util/errors"
"k8s.io/client-go/tools/cache"
"k8s.io/klog/v2"
@@ -17,6 +16,8 @@ import (
addoninformerv1alpha1 "open-cluster-management.io/api/client/addon/informers/externalversions/addon/v1alpha1"
addonlisterv1alpha1 "open-cluster-management.io/api/client/addon/listers/addon/v1alpha1"
clusterinformersv1beta1 "open-cluster-management.io/api/client/cluster/informers/externalversions/cluster/v1beta1"
"open-cluster-management.io/ocm/pkg/common/queue"
)
type addonManagementController struct {
@@ -65,10 +66,7 @@ func NewAddonManagementController(
}
return factory.New().WithInformersQueueKeysFunc(
func(obj runtime.Object) []string {
key, _ := cache.DeletionHandlingMetaNamespaceKeyFunc(obj)
return []string{key}
},
queue.QueueKeyByMetaNamespaceName,
addonInformers.Informer(), clusterManagementAddonInformers.Informer()).
WithInformersQueueKeysFunc(
index.ClusterManagementAddonByPlacementDecisionQueueKey(
@@ -7,7 +7,6 @@ import (
"github.com/openshift/library-go/pkg/operator/events"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/tools/cache"
"k8s.io/klog/v2"
@@ -16,6 +15,8 @@ import (
addonv1alpha1client "open-cluster-management.io/api/client/addon/clientset/versioned"
addoninformerv1alpha1 "open-cluster-management.io/api/client/addon/informers/externalversions/addon/v1alpha1"
addonlisterv1alpha1 "open-cluster-management.io/api/client/addon/listers/addon/v1alpha1"
"open-cluster-management.io/ocm/pkg/common/queue"
)
const UnsupportedConfigurationType = "UnsupportedConfiguration"
@@ -45,16 +46,10 @@ func NewAddonOwnerController(
return factory.New().
WithFilteredEventsInformersQueueKeysFunc(
func(obj runtime.Object) []string {
key, _ := cache.DeletionHandlingMetaNamespaceKeyFunc(obj)
return []string{key}
},
queue.QueueKeyByMetaNamespaceName,
c.addonFilterFunc, clusterManagementAddonInformers.Informer()).
WithInformersQueueKeysFunc(
func(obj runtime.Object) []string {
key, _ := cache.DeletionHandlingMetaNamespaceKeyFunc(obj)
return []string{key}
},
queue.QueueKeyByMetaNamespaceName,
addonInformers.Informer()).
WithSync(c.sync).
ToController("addon-owner-controller", recorder)
@@ -29,6 +29,7 @@ import (
workapiv1 "open-cluster-management.io/api/work/v1"
"open-cluster-management.io/ocm/pkg/common/patcher"
"open-cluster-management.io/ocm/pkg/common/queue"
)
const (
@@ -64,10 +65,7 @@ func NewAddonProgressingController(
}
return factory.New().WithInformersQueueKeysFunc(
func(obj runtime.Object) []string {
key, _ := cache.DeletionHandlingMetaNamespaceKeyFunc(obj)
return []string{key}
},
queue.QueueKeyByMetaNamespaceName,
addonInformers.Informer(), clusterManagementAddonInformers.Informer()).
// TODO: consider hosted manifestwork
WithInformersQueueKeysFunc(
@@ -8,7 +8,6 @@ import (
"github.com/openshift/library-go/pkg/operator/events"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
utilrand "k8s.io/apimachinery/pkg/util/rand"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
"k8s.io/client-go/dynamic/dynamicinformer"
@@ -28,6 +27,7 @@ import (
workv1informers "open-cluster-management.io/api/client/work/informers/externalversions"
"open-cluster-management.io/ocm/pkg/addon/templateagent"
"open-cluster-management.io/ocm/pkg/common/queue"
)
// addonTemplateController monitors ManagedClusterAddOns on hub to get all the in-used addon templates,
@@ -81,10 +81,7 @@ func NewAddonTemplateController(
c.runControllerFunc = c.runController
}
return factory.New().WithInformersQueueKeysFunc(
func(obj runtime.Object) []string {
key, _ := cache.DeletionHandlingMetaNamespaceKeyFunc(obj)
return []string{key}
},
queue.QueueKeyByMetaNamespaceName,
addonInformers.Addon().V1alpha1().ClusterManagementAddOns().Informer()).
WithSync(c.sync).
ToController("addon-template-controller", recorder)
@@ -6,8 +6,6 @@ import (
"github.com/openshift/library-go/pkg/controller/factory"
"github.com/openshift/library-go/pkg/operator/events"
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/meta"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/klog/v2"
addonv1alpha1 "open-cluster-management.io/api/addon/v1alpha1"
@@ -16,6 +14,7 @@ import (
addonlisterv1alpha1 "open-cluster-management.io/api/client/addon/listers/addon/v1alpha1"
"open-cluster-management.io/ocm/pkg/common/patcher"
"open-cluster-management.io/ocm/pkg/common/queue"
)
// managementAddonInstallProgressionController reconciles instances of clustermanagementaddon the hub
@@ -45,10 +44,7 @@ func NewManagementAddonInstallProgressionController(
}
return factory.New().WithInformersQueueKeysFunc(
func(obj runtime.Object) []string {
accessor, _ := meta.Accessor(obj)
return []string{accessor.GetName()}
},
queue.QueueKeyByMetaName,
addonInformers.Informer(), clusterManagementAddonInformers.Informer()).
WithSync(c.sync).ToController("management-addon-status-controller", recorder)
+70
View File
@@ -0,0 +1,70 @@
package queue
import (
"github.com/openshift/library-go/pkg/controller/factory"
"k8s.io/apimachinery/pkg/api/meta"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/tools/cache"
)
func FileterByLabel(key string) factory.EventFilterFunc {
return func(obj interface{}) bool {
accessor, _ := meta.Accessor(obj)
return len(accessor.GetLabels()) > 0 && len(accessor.GetLabels()[key]) > 0
}
}
func FileterByLabelKeyValue(key, value string) factory.EventFilterFunc {
return func(obj interface{}) bool {
accessor, _ := meta.Accessor(obj)
return len(accessor.GetLabels()) > 0 && accessor.GetLabels()[key] == value
}
}
func FilterByNames(names ...string) factory.EventFilterFunc {
return func(obj interface{}) bool {
accessor, _ := meta.Accessor(obj)
for _, name := range names {
if accessor.GetName() == name {
return true
}
}
return false
}
}
func UnionFilter(filters ...factory.EventFilterFunc) factory.EventFilterFunc {
return func(obj interface{}) bool {
for _, filter := range filters {
if !filter(obj) {
return false
}
}
return true
}
}
func QueueKeyByLabel(key string) factory.ObjectQueueKeysFunc {
return func(obj runtime.Object) []string {
accessor, _ := meta.Accessor(obj)
if len(accessor.GetLabels()) == 0 || len(accessor.GetLabels()[key]) == 0 {
return []string{}
}
return []string{accessor.GetLabels()[key]}
}
}
func QueueKeyByMetaName(obj runtime.Object) []string {
accessor, _ := meta.Accessor(obj)
return []string{accessor.GetName()}
}
func QueueKeyByMetaNamespace(obj runtime.Object) []string {
accessor, _ := meta.Accessor(obj)
return []string{accessor.GetNamespace()}
}
func QueueKeyByMetaNamespaceName(obj runtime.Object) []string {
key, _ := cache.MetaNamespaceKeyFunc(obj)
return []string{key}
}
+128
View File
@@ -0,0 +1,128 @@
package queue
import (
"reflect"
"testing"
"github.com/openshift/library-go/pkg/controller/factory"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
)
func TestFileter(t *testing.T) {
tc := []struct {
name string
filter factory.EventFilterFunc
object runtime.Object
filtered bool
}{
{
name: "filter by label with no label",
filter: FileterByLabel("test"),
object: &corev1.Namespace{ObjectMeta: metav1.ObjectMeta{Name: "test", Labels: map[string]string{"test1": "value1"}}},
filtered: false,
},
{
name: "filter by label with label",
filter: FileterByLabel("test"),
object: &corev1.Namespace{ObjectMeta: metav1.ObjectMeta{Name: "test", Labels: map[string]string{"test": "value1"}}},
filtered: true,
},
{
name: "filter by label with incorrect labelkeyvalue",
filter: FileterByLabelKeyValue("test", "value"),
object: &corev1.Namespace{ObjectMeta: metav1.ObjectMeta{Name: "test", Labels: map[string]string{"test": "value1"}}},
filtered: false,
},
{
name: "filter by label with incorrect labelkeyvalue",
filter: FileterByLabelKeyValue("test", "value1"),
object: &corev1.Namespace{ObjectMeta: metav1.ObjectMeta{Name: "test", Labels: map[string]string{"test": "value1"}}},
filtered: true,
},
{
name: "filter by unmatched name",
filter: FilterByNames("test"),
object: &corev1.Namespace{ObjectMeta: metav1.ObjectMeta{Name: "test1"}},
filtered: false,
},
{
name: "filter by matched name",
filter: FilterByNames("test", "test1"),
object: &corev1.Namespace{ObjectMeta: metav1.ObjectMeta{Name: "test1"}},
filtered: true,
},
{
name: "uniion filter by unmatched",
filter: UnionFilter(FilterByNames("test"), FileterByLabel("test")),
object: &corev1.Namespace{ObjectMeta: metav1.ObjectMeta{Name: "test"}},
filtered: false,
},
{
name: "uniion filter by matched",
filter: UnionFilter(FilterByNames("test"), FileterByLabel("test")),
object: &corev1.Namespace{ObjectMeta: metav1.ObjectMeta{Name: "test", Labels: map[string]string{"test": "value"}}},
filtered: true,
},
}
for _, c := range tc {
t.Run(c.name, func(t *testing.T) {
actual := c.filter(c.object)
if c.filtered != actual {
t.Errorf("expect filter %v, but got %v", c.filtered, actual)
}
})
}
}
func TestQueueKey(t *testing.T) {
tc := []struct {
name string
queueKeyFunc factory.ObjectQueueKeysFunc
object runtime.Object
expecteKey []string
}{
{
name: "by name",
queueKeyFunc: QueueKeyByMetaName,
object: &corev1.Secret{ObjectMeta: metav1.ObjectMeta{Name: "test", Namespace: "testns"}},
expecteKey: []string{"test"},
},
{
name: "by namespace",
queueKeyFunc: QueueKeyByMetaNamespace,
object: &corev1.Secret{ObjectMeta: metav1.ObjectMeta{Name: "test", Namespace: "testns"}},
expecteKey: []string{"testns"},
},
{
name: "by namespace name",
queueKeyFunc: QueueKeyByMetaNamespaceName,
object: &corev1.Secret{ObjectMeta: metav1.ObjectMeta{Name: "test", Namespace: "testns"}},
expecteKey: []string{"testns/test"},
},
{
name: "by emptey label",
queueKeyFunc: QueueKeyByLabel("testlabel"),
object: &corev1.Secret{ObjectMeta: metav1.ObjectMeta{Name: "test", Namespace: "testns"}},
expecteKey: []string{},
},
{
name: "by matched label",
queueKeyFunc: QueueKeyByLabel("testlabel"),
object: &corev1.Secret{ObjectMeta: metav1.ObjectMeta{
Name: "test", Namespace: "testns", Labels: map[string]string{"testlabel": "value"}}},
expecteKey: []string{"value"},
},
}
for _, c := range tc {
t.Run(c.name, func(t *testing.T) {
actual := c.queueKeyFunc(c.object)
if !reflect.DeepEqual(c.expecteKey, actual) {
t.Errorf("expect key %v, but got %v", c.expecteKey, actual)
}
})
}
}
@@ -10,10 +10,8 @@ import (
errorhelpers "github.com/openshift/library-go/pkg/operator/v1helpers"
operatorhelpers "github.com/openshift/library-go/pkg/operator/v1helpers"
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/runtime"
corev1informers "k8s.io/client-go/informers/core/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/klog/v2"
@@ -22,6 +20,7 @@ import (
operatorlister "open-cluster-management.io/api/client/operator/listers/operator/v1"
operatorv1 "open-cluster-management.io/api/operator/v1"
"open-cluster-management.io/ocm/pkg/common/queue"
"open-cluster-management.io/ocm/pkg/operator/certrotation"
"open-cluster-management.io/ocm/pkg/operator/helpers"
)
@@ -79,10 +78,7 @@ func NewCertRotationController(
return factory.New().
ResyncEvery(ResyncInterval).
WithSync(c.sync).
WithInformersQueueKeyFunc(func(obj runtime.Object) string {
accessor, _ := meta.Accessor(obj)
return accessor.GetName()
}, clusterManagerInformer.Informer()).
WithInformersQueueKeysFunc(queue.QueueKeyByMetaName, clusterManagerInformer.Informer()).
WithInformersQueueKeyFunc(helpers.ClusterManagerQueueKeyFunc(c.clusterManagerLister),
configMapInformer.Informer(),
secretInformers[helpers.SignerSecret].Informer(),
@@ -15,7 +15,6 @@ import (
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
utilerrors "k8s.io/apimachinery/pkg/util/errors"
appsinformer "k8s.io/client-go/informers/apps/v1"
corev1informers "k8s.io/client-go/informers/core/v1"
@@ -33,6 +32,7 @@ import (
"open-cluster-management.io/ocm/manifests"
"open-cluster-management.io/ocm/pkg/common/patcher"
"open-cluster-management.io/ocm/pkg/common/queue"
"open-cluster-management.io/ocm/pkg/operator/helpers"
)
@@ -104,18 +104,9 @@ func NewClusterManagerController(
WithInformersQueueKeyFunc(helpers.ClusterManagerDeploymentQueueKeyFunc(controller.clusterManagerLister), deploymentInformer.Informer()).
WithFilteredEventsInformersQueueKeyFunc(
helpers.ClusterManagerConfigmapQueueKeyFunc(controller.clusterManagerLister),
func(obj interface{}) bool {
accessor, _ := meta.Accessor(obj)
if name := accessor.GetName(); name != helpers.CaBundleConfigmap {
return false
}
return true
},
queue.FilterByNames(helpers.CaBundleConfigmap),
configMapInformer.Informer()).
WithInformersQueueKeyFunc(func(obj runtime.Object) string {
accessor, _ := meta.Accessor(obj)
return accessor.GetName()
}, clusterManagerInformer.Informer()).
WithInformersQueueKeysFunc(queue.QueueKeyByMetaName, clusterManagerInformer.Informer()).
ToController("ClusterManagerController", recorder)
}
@@ -14,7 +14,6 @@ import (
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"k8s.io/klog/v2"
@@ -22,6 +21,7 @@ import (
operatorinformer "open-cluster-management.io/api/client/operator/informers/externalversions/operator/v1"
operatorlister "open-cluster-management.io/api/client/operator/listers/operator/v1"
"open-cluster-management.io/ocm/pkg/common/queue"
"open-cluster-management.io/ocm/pkg/operator/helpers"
"open-cluster-management.io/ocm/pkg/operator/operators/clustermanager/controllers/migrationcontroller"
)
@@ -56,10 +56,7 @@ func NewCRDStatusController(
}
return factory.New().WithSync(controller.sync).
WithInformersQueueKeyFunc(func(obj runtime.Object) string {
accessor, _ := meta.Accessor(obj)
return accessor.GetName()
}, clusterManagerInformer.Informer()).
WithInformersQueueKeysFunc(queue.QueueKeyByMetaName, clusterManagerInformer.Informer()).
ToController("CRDStatusController", recorder)
}
@@ -32,6 +32,7 @@ import (
"open-cluster-management.io/ocm/manifests"
"open-cluster-management.io/ocm/pkg/common/patcher"
"open-cluster-management.io/ocm/pkg/common/queue"
"open-cluster-management.io/ocm/pkg/operator/helpers"
)
@@ -88,10 +89,7 @@ func NewCRDMigrationController(
}
return factory.New().WithSync(controller.sync).
WithInformersQueueKeyFunc(func(obj runtime.Object) string {
accessor, _ := meta.Accessor(obj)
return accessor.GetName()
}, clusterManagerInformer.Informer()).
WithInformersQueueKeysFunc(queue.QueueKeyByMetaName, clusterManagerInformer.Informer()).
ToController("CRDMigrationController", recorder)
}
@@ -9,7 +9,6 @@ import (
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
appsinformer "k8s.io/client-go/informers/apps/v1"
appslister "k8s.io/client-go/listers/apps/v1"
"k8s.io/klog/v2"
@@ -20,6 +19,7 @@ import (
operatorapiv1 "open-cluster-management.io/api/operator/v1"
"open-cluster-management.io/ocm/pkg/common/patcher"
"open-cluster-management.io/ocm/pkg/common/queue"
"open-cluster-management.io/ocm/pkg/operator/helpers"
)
@@ -52,10 +52,7 @@ func NewClusterManagerStatusController(
return factory.New().WithSync(controller.sync).
WithInformersQueueKeyFunc(
helpers.ClusterManagerDeploymentQueueKeyFunc(controller.clusterManagerLister), deploymentInformer.Informer()).
WithInformersQueueKeyFunc(func(obj runtime.Object) string {
accessor, _ := meta.Accessor(obj)
return accessor.GetName()
}, clusterManagerInformer.Informer()).
WithInformersQueueKeysFunc(queue.QueueKeyByMetaName, clusterManagerInformer.Informer()).
ToController("ClusterManagerStatusController", recorder)
}
@@ -5,13 +5,12 @@ import (
"github.com/openshift/library-go/pkg/controller/factory"
"github.com/openshift/library-go/pkg/operator/events"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
coreinformer "k8s.io/client-go/informers/core/v1"
"k8s.io/client-go/kubernetes"
"open-cluster-management.io/ocm/pkg/common/queue"
"open-cluster-management.io/ocm/pkg/operator/helpers"
)
@@ -40,14 +39,10 @@ func NewAddonPullImageSecretController(kubeClient kubernetes.Interface, operator
kubeClient: kubeClient,
recorder: recorder,
}
return factory.New().WithFilteredEventsInformersQueueKeyFunc(func(o runtime.Object) string {
namespace := o.(*corev1.Namespace)
return namespace.GetName()
}, func(obj interface{}) bool {
// if obj has the label, return true
namespace := obj.(*corev1.Namespace)
return namespace.Labels[addonInstallNamespaceLabelKey] == "true"
}, namespaceInformer.Informer()).WithSync(ac.sync).ToController("AddonPullImageSecretController", recorder)
return factory.New().WithFilteredEventsInformersQueueKeysFunc(
queue.QueueKeyByMetaName,
queue.FileterByLabelKeyValue(addonInstallNamespaceLabelKey, "true"),
namespaceInformer.Informer()).WithSync(ac.sync).ToController("AddonPullImageSecretController", recorder)
}
func (c *addonPullImageSecretController) sync(ctx context.Context, controllerContext factory.SyncContext) error {
@@ -12,7 +12,6 @@ import (
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
utilerrors "k8s.io/apimachinery/pkg/util/errors"
"k8s.io/apimachinery/pkg/util/version"
appsinformer "k8s.io/client-go/informers/apps/v1"
@@ -28,6 +27,7 @@ import (
"open-cluster-management.io/ocm/manifests"
"open-cluster-management.io/ocm/pkg/common/patcher"
"open-cluster-management.io/ocm/pkg/common/queue"
"open-cluster-management.io/ocm/pkg/operator/helpers"
)
@@ -68,10 +68,7 @@ func NewKlusterletCleanupController(
secretInformers[helpers.BootstrapHubKubeConfig].Informer(),
secretInformers[helpers.ExternalManagedKubeConfig].Informer()).
WithInformersQueueKeyFunc(helpers.KlusterletDeploymentQueueKeyFunc(controller.klusterletLister), deploymentInformer.Informer()).
WithInformersQueueKeyFunc(func(obj runtime.Object) string {
accessor, _ := meta.Accessor(obj)
return accessor.GetName()
}, klusterletInformer.Informer()).
WithInformersQueueKeysFunc(queue.QueueKeyByMetaName, klusterletInformer.Informer()).
ToController("KlusterletCleanupController", recorder)
}
@@ -13,7 +13,6 @@ import (
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
utilerrors "k8s.io/apimachinery/pkg/util/errors"
"k8s.io/apimachinery/pkg/util/version"
appsinformer "k8s.io/client-go/informers/apps/v1"
@@ -30,6 +29,7 @@ import (
operatorapiv1 "open-cluster-management.io/api/operator/v1"
"open-cluster-management.io/ocm/pkg/common/patcher"
"open-cluster-management.io/ocm/pkg/common/queue"
"open-cluster-management.io/ocm/pkg/operator/helpers"
)
@@ -101,10 +101,7 @@ func NewKlusterletController(
secretInformers[helpers.BootstrapHubKubeConfig].Informer(),
secretInformers[helpers.ExternalManagedKubeConfig].Informer()).
WithInformersQueueKeyFunc(helpers.KlusterletDeploymentQueueKeyFunc(controller.klusterletLister), deploymentInformer.Informer()).
WithInformersQueueKeyFunc(func(obj runtime.Object) string {
accessor, _ := meta.Accessor(obj)
return accessor.GetName()
}, klusterletInformer.Informer()).
WithInformersQueueKeysFunc(queue.QueueKeyByMetaName, klusterletInformer.Informer()).
ToController("KlusterletController", recorder)
}
@@ -13,7 +13,6 @@ import (
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
coreinformer "k8s.io/client-go/informers/core/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/klog/v2"
@@ -24,6 +23,7 @@ import (
operatorapiv1 "open-cluster-management.io/api/operator/v1"
"open-cluster-management.io/ocm/pkg/common/patcher"
"open-cluster-management.io/ocm/pkg/common/queue"
"open-cluster-management.io/ocm/pkg/operator/helpers"
)
@@ -70,10 +70,7 @@ func NewKlusterletSSARController(
secretInformers[helpers.HubKubeConfig].Informer(),
secretInformers[helpers.BootstrapHubKubeConfig].Informer(),
secretInformers[helpers.ExternalManagedKubeConfig].Informer()).
WithInformersQueueKeyFunc(func(obj runtime.Object) string {
accessor, _ := meta.Accessor(obj)
return accessor.GetName()
}, klusterletInformer.Informer()).
WithInformersQueueKeysFunc(queue.QueueKeyByMetaName, klusterletInformer.Informer()).
ToController("KlusterletSSARController", recorder)
}
@@ -10,7 +10,6 @@ import (
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
appsinformer "k8s.io/client-go/informers/apps/v1"
"k8s.io/client-go/kubernetes"
appslister "k8s.io/client-go/listers/apps/v1"
@@ -22,6 +21,7 @@ import (
operatorapiv1 "open-cluster-management.io/api/operator/v1"
"open-cluster-management.io/ocm/pkg/common/patcher"
"open-cluster-management.io/ocm/pkg/common/queue"
"open-cluster-management.io/ocm/pkg/operator/helpers"
)
@@ -55,10 +55,7 @@ func NewKlusterletStatusController(
}
return factory.New().WithSync(controller.sync).
WithInformersQueueKeyFunc(helpers.KlusterletDeploymentQueueKeyFunc(controller.klusterletLister), deploymentInformer.Informer()).
WithInformersQueueKeyFunc(func(obj runtime.Object) string {
accessor, _ := meta.Accessor(obj)
return accessor.GetName()
}, klusterletInformer.Informer()).
WithInformersQueueKeysFunc(queue.QueueKeyByMetaName, klusterletInformer.Informer()).
ToController("KlusterletStatusController", recorder)
}
@@ -34,7 +34,8 @@ func TestOnClusterChange(t *testing.T) {
},
{
name: "clusterset does not exist",
obj: testinghelpers.NewManagedCluster("cluster1").WithLabel(clusterSetLabel, "clusterset1").Build(),
obj: testinghelpers.NewManagedCluster("cluster1").WithLabel(
clusterapiv1beta2.ClusterSetLabel, "clusterset1").Build(),
initObjs: []runtime.Object{
testinghelpers.NewClusterSetBinding("ns1", "clusterset1"),
testinghelpers.NewPlacement("ns1", "placement1").Build(),
@@ -43,7 +44,8 @@ func TestOnClusterChange(t *testing.T) {
},
{
name: "clusterset exists",
obj: testinghelpers.NewManagedCluster("cluster1").WithLabel(clusterSetLabel, "clusterset1").Build(),
obj: testinghelpers.NewManagedCluster("cluster1").WithLabel(
clusterapiv1beta2.ClusterSetLabel, "clusterset1").Build(),
initObjs: []runtime.Object{
testinghelpers.NewClusterSet("clusterset1").Build(),
testinghelpers.NewClusterSet("clusterset2").Build(),
@@ -61,7 +63,8 @@ func TestOnClusterChange(t *testing.T) {
},
{
name: "cluster blongs to multiple clusterset",
obj: testinghelpers.NewManagedCluster("cluster1").WithLabel("cloud", "Amazon").WithLabel(clusterSetLabel, "clusterset2").Build(),
obj: testinghelpers.NewManagedCluster("cluster1").WithLabel("cloud", "Amazon").WithLabel(
clusterapiv1beta2.ClusterSetLabel, "clusterset2").Build(),
initObjs: []runtime.Object{
testinghelpers.NewClusterSet("global").WithClusterSelector(clusterapiv1beta2.ManagedClusterSelector{
SelectorType: clusterapiv1beta2.LabelSelector,
@@ -139,9 +142,11 @@ func TestOnClusterUpdate(t *testing.T) {
},
},
{
name: "cluster blongs to multiple clusterset",
newObj: testinghelpers.NewManagedCluster("cluster1").WithLabel(clusterSetLabel, "clusterset2").WithLabel("cloud", "Google").Build(),
oldObj: testinghelpers.NewManagedCluster("cluster1").WithLabel(clusterSetLabel, "clusterset2").WithLabel("cloud", "Amazon").Build(),
name: "cluster blongs to multiple clusterset",
newObj: testinghelpers.NewManagedCluster("cluster1").WithLabel(
clusterapiv1beta2.ClusterSetLabel, "clusterset2").WithLabel("cloud", "Google").Build(),
oldObj: testinghelpers.NewManagedCluster("cluster1").WithLabel(
clusterapiv1beta2.ClusterSetLabel, "clusterset2").WithLabel("cloud", "Amazon").Build(),
initObjs: []runtime.Object{
testinghelpers.NewClusterSet("global").WithClusterSelector(clusterapiv1beta2.ManagedClusterSelector{
SelectorType: clusterapiv1beta2.LabelSelector,
@@ -172,7 +177,7 @@ func TestOnClusterUpdate(t *testing.T) {
{
name: "assign a cluster to a clusterset",
newObj: testinghelpers.NewManagedCluster("cluster1").
WithLabel(clusterSetLabel, "clusterset1").WithLabel("cloud", "Amazon").Build(),
WithLabel(clusterapiv1beta2.ClusterSetLabel, "clusterset1").WithLabel("cloud", "Amazon").Build(),
oldObj: testinghelpers.NewManagedCluster("cluster1").WithLabel("cloud", "Amazon").Build(),
initObjs: []runtime.Object{
testinghelpers.NewClusterSet("clusterset1").Build(),
@@ -186,7 +191,8 @@ func TestOnClusterUpdate(t *testing.T) {
{
name: "remove cluster from a clusterset",
newObj: testinghelpers.NewManagedCluster("cluster1").Build(),
oldObj: testinghelpers.NewManagedCluster("cluster1").WithLabel(clusterSetLabel, "clusterset2").WithLabel("cloud", "Amazon").Build(),
oldObj: testinghelpers.NewManagedCluster("cluster1").WithLabel(
clusterapiv1beta2.ClusterSetLabel, "clusterset2").WithLabel("cloud", "Amazon").Build(),
initObjs: []runtime.Object{
testinghelpers.NewClusterSet("global").WithClusterSelector(clusterapiv1beta2.ManagedClusterSelector{
SelectorType: clusterapiv1beta2.LabelSelector,
@@ -217,9 +223,9 @@ func TestOnClusterUpdate(t *testing.T) {
{
name: "label change only",
newObj: testinghelpers.NewManagedCluster("cluster1").
WithLabel(clusterSetLabel, "clusterset1").WithLabel("cloud", "Amazon").Build(),
WithLabel(clusterapiv1beta2.ClusterSetLabel, "clusterset1").WithLabel("cloud", "Amazon").Build(),
oldObj: testinghelpers.NewManagedCluster("cluster1").
WithLabel(clusterSetLabel, "clusterset1").WithLabel("cloud", "google").Build(),
WithLabel(clusterapiv1beta2.ClusterSetLabel, "clusterset1").WithLabel("cloud", "google").Build(),
initObjs: []runtime.Object{
testinghelpers.NewClusterSet("clusterset1").Build(),
testinghelpers.NewClusterSetBinding("ns1", "clusterset1"),
@@ -232,9 +238,9 @@ func TestOnClusterUpdate(t *testing.T) {
{
name: "move cluster from one clusterset to another",
newObj: testinghelpers.NewManagedCluster("cluster1").
WithLabel(clusterSetLabel, "clusterset2").WithLabel("cloud", "Amazon").Build(),
WithLabel(clusterapiv1beta2.ClusterSetLabel, "clusterset2").WithLabel("cloud", "Amazon").Build(),
oldObj: testinghelpers.NewManagedCluster("cluster1").
WithLabel(clusterSetLabel, "clusterset1").WithLabel("cloud", "Amazon").Build(),
WithLabel(clusterapiv1beta2.ClusterSetLabel, "clusterset1").WithLabel("cloud", "Amazon").Build(),
initObjs: []runtime.Object{
testinghelpers.NewClusterSet("clusterset1").Build(),
testinghelpers.NewClusterSet("clusterset2").Build(),
@@ -295,7 +301,8 @@ func TestOnClusterDelete(t *testing.T) {
},
{
name: "cluster",
obj: testinghelpers.NewManagedCluster("cluster1").WithLabel(clusterSetLabel, "clusterset2").WithLabel("cloud", "Amazon").Build(),
obj: testinghelpers.NewManagedCluster("cluster1").WithLabel(
clusterapiv1beta2.ClusterSetLabel, "clusterset2").WithLabel("cloud", "Amazon").Build(),
initObjs: []runtime.Object{
testinghelpers.NewClusterSet("global").WithClusterSelector(clusterapiv1beta2.ManagedClusterSelector{
SelectorType: clusterapiv1beta2.LabelSelector,
@@ -326,7 +333,8 @@ func TestOnClusterDelete(t *testing.T) {
{
name: "tombstone",
obj: cache.DeletedFinalStateUnknown{
Obj: testinghelpers.NewManagedCluster("cluster1").WithLabel(clusterSetLabel, "clusterset1").Build(),
Obj: testinghelpers.NewManagedCluster("cluster1").WithLabel(
clusterapiv1beta2.ClusterSetLabel, "clusterset1").Build(),
},
initObjs: []runtime.Object{
testinghelpers.NewClusterSet("clusterset1").Build(),
@@ -323,7 +323,7 @@ func TestEnqueuePlacementsByScore(t *testing.T) {
testinghelpers.NewPlacement("ns1", "placement1").WithScoreCoordinateAddOn("score1", "cpu", 1).Build(),
testinghelpers.NewPlacement("ns2", "placement2").WithScoreCoordinateAddOn("score2", "cpu", 1).Build(),
testinghelpers.NewPlacement("ns3", "placement3").WithScoreCoordinateAddOn("score1", "cpu", 1).Build(),
testinghelpers.NewManagedCluster("cluster1").WithLabel(clusterSetLabel, "clusterset1").Build(),
testinghelpers.NewManagedCluster("cluster1").WithLabel(clusterapiv1beta2.ClusterSetLabel, "clusterset1").Build(),
testinghelpers.NewClusterSet("clusterset1").Build(),
testinghelpers.NewClusterSetBinding("ns1", "clusterset1"),
testinghelpers.NewClusterSetBinding("ns3", "clusterset1"),
@@ -340,7 +340,7 @@ func TestEnqueuePlacementsByScore(t *testing.T) {
testinghelpers.NewPlacement("ns1", "placement1").WithScoreCoordinateAddOn("score1", "cpu", 1).Build(),
testinghelpers.NewPlacement("ns2", "placement2").WithScoreCoordinateAddOn("score2", "cpu", 1).Build(),
testinghelpers.NewPlacement("ns3", "placement3").WithScoreCoordinateAddOn("score1", "cpu", 1).Build(),
testinghelpers.NewManagedCluster("cluster1").WithLabel(clusterSetLabel, "clusterset1").Build(),
testinghelpers.NewManagedCluster("cluster1").WithLabel(clusterapiv1beta2.ClusterSetLabel, "clusterset1").Build(),
testinghelpers.NewClusterSet("clusterset1").Build(),
testinghelpers.NewClusterSetBinding("ns1", "clusterset2"),
testinghelpers.NewClusterSetBinding("ns3", "clusterset1"),
@@ -357,7 +357,7 @@ func TestEnqueuePlacementsByScore(t *testing.T) {
},
initObjs: []runtime.Object{
testinghelpers.NewPlacement("ns1", "placement1").WithScoreCoordinateAddOn("score1", "cpu", 1).Build(),
testinghelpers.NewManagedCluster("cluster1").WithLabel(clusterSetLabel, "clusterset1").Build(),
testinghelpers.NewManagedCluster("cluster1").WithLabel(clusterapiv1beta2.ClusterSetLabel, "clusterset1").Build(),
testinghelpers.NewClusterSet("clusterset1").Build(),
testinghelpers.NewClusterSetBinding("ns1", "clusterset1"),
},
@@ -4,6 +4,7 @@ import (
"context"
"encoding/json"
"fmt"
clusterapiv1beta2 "open-cluster-management.io/api/cluster/v1beta2"
"reflect"
"sort"
"testing"
@@ -71,7 +72,8 @@ func TestSchedule(t *testing.T) {
},
},
clusters: []*clusterapiv1.ManagedCluster{
testinghelpers.NewManagedCluster("cluster1").WithLabel(clusterSetLabel, clusterSetName).Build(),
testinghelpers.NewManagedCluster("cluster1").WithLabel(
clusterapiv1beta2.ClusterSetLabel, clusterSetName).Build(),
},
expectedUnScheduled: 0,
expectedStatus: *framework.NewStatus("", framework.Success, ""),
@@ -85,7 +87,7 @@ func TestSchedule(t *testing.T) {
},
decisions: []runtime.Object{},
clusters: []*clusterapiv1.ManagedCluster{
testinghelpers.NewManagedCluster("cluster1").WithLabel(clusterSetLabel, clusterSetName).Build(),
testinghelpers.NewManagedCluster("cluster1").WithLabel(clusterapiv1beta2.ClusterSetLabel, clusterSetName).Build(),
},
expectedDecisions: []clusterapiv1beta1.ClusterDecision{
{ClusterName: "cluster1"},
@@ -128,7 +130,7 @@ func TestSchedule(t *testing.T) {
},
decisions: []runtime.Object{},
clusters: []*clusterapiv1.ManagedCluster{
testinghelpers.NewManagedCluster("cluster1").WithLabel(clusterSetLabel, clusterSetName).Build(),
testinghelpers.NewManagedCluster("cluster1").WithLabel(clusterapiv1beta2.ClusterSetLabel, clusterSetName).Build(),
},
expectedDecisions: []clusterapiv1beta1.ClusterDecision{},
expectedFilterResult: []FilterResult{
@@ -152,18 +154,18 @@ func TestSchedule(t *testing.T) {
testinghelpers.NewClusterSet(clusterSetName).Build(),
testinghelpers.NewClusterSetBinding(placementNamespace, clusterSetName),
testinghelpers.NewPlacementDecision(placementNamespace, placementDecisionName(placementName, 1)).
WithLabel(placementLabel, placementName).
WithLabel(clusterapiv1beta1.PlacementLabel, placementName).
WithDecisions("cluster1", "cluster2").Build(),
},
decisions: []runtime.Object{
testinghelpers.NewPlacementDecision(placementNamespace, placementDecisionName(placementName, 1)).
WithLabel(placementLabel, placementName).
WithLabel(clusterapiv1beta1.PlacementLabel, placementName).
WithDecisions("cluster1", "cluster2").Build(),
},
clusters: []*clusterapiv1.ManagedCluster{
testinghelpers.NewManagedCluster("cluster1").WithLabel(clusterSetLabel, clusterSetName).Build(),
testinghelpers.NewManagedCluster("cluster2").WithLabel(clusterSetLabel, clusterSetName).Build(),
testinghelpers.NewManagedCluster("cluster3").WithLabel(clusterSetLabel, clusterSetName).Build(),
testinghelpers.NewManagedCluster("cluster1").WithLabel(clusterapiv1beta2.ClusterSetLabel, clusterSetName).Build(),
testinghelpers.NewManagedCluster("cluster2").WithLabel(clusterapiv1beta2.ClusterSetLabel, clusterSetName).Build(),
testinghelpers.NewManagedCluster("cluster3").WithLabel(clusterapiv1beta2.ClusterSetLabel, clusterSetName).Build(),
},
expectedDecisions: []clusterapiv1beta1.ClusterDecision{
{ClusterName: "cluster1"},
@@ -228,7 +230,7 @@ func TestSchedule(t *testing.T) {
},
},
clusters: []*clusterapiv1.ManagedCluster{
testinghelpers.NewManagedCluster("cluster1").WithLabel(clusterSetLabel, clusterSetName).Build(),
testinghelpers.NewManagedCluster("cluster1").WithLabel(clusterapiv1beta2.ClusterSetLabel, clusterSetName).Build(),
},
expectedUnScheduled: 0,
expectedStatus: *framework.NewStatus("", framework.Success, ""),
@@ -249,21 +251,21 @@ func TestSchedule(t *testing.T) {
testinghelpers.NewAddOnPlacementScore("cluster3", "demo").WithScore("demo", 50).Build(),
},
clusters: []*clusterapiv1.ManagedCluster{
testinghelpers.NewManagedCluster("cluster1").WithLabel(clusterSetLabel, clusterSetName).WithTaint(
testinghelpers.NewManagedCluster("cluster1").WithLabel(clusterapiv1beta2.ClusterSetLabel, clusterSetName).WithTaint(
&clusterapiv1.Taint{
Key: "key1",
Value: "value1",
Effect: clusterapiv1.TaintEffectNoSelect,
TimeAdded: metav1.Time{},
}).Build(),
testinghelpers.NewManagedCluster("cluster2").WithLabel(clusterSetLabel, clusterSetName).WithTaint(
testinghelpers.NewManagedCluster("cluster2").WithLabel(clusterapiv1beta2.ClusterSetLabel, clusterSetName).WithTaint(
&clusterapiv1.Taint{
Key: "key2",
Value: "value2",
Effect: clusterapiv1.TaintEffectNoSelect,
TimeAdded: metav1.Time{},
}).Build(),
testinghelpers.NewManagedCluster("cluster3").WithLabel(clusterSetLabel, clusterSetName).Build(),
testinghelpers.NewManagedCluster("cluster3").WithLabel(clusterapiv1beta2.ClusterSetLabel, clusterSetName).Build(),
},
decisions: []runtime.Object{},
expectedDecisions: []clusterapiv1beta1.ClusterDecision{
@@ -306,9 +308,9 @@ func TestSchedule(t *testing.T) {
testinghelpers.NewAddOnPlacementScore("cluster3", "demo").WithScore("demo", 50).Build(),
},
clusters: []*clusterapiv1.ManagedCluster{
testinghelpers.NewManagedCluster("cluster1").WithLabel(clusterSetLabel, clusterSetName).WithResource(clusterapiv1.ResourceMemory, "100", "100").Build(),
testinghelpers.NewManagedCluster("cluster2").WithLabel(clusterSetLabel, clusterSetName).WithResource(clusterapiv1.ResourceMemory, "50", "100").Build(),
testinghelpers.NewManagedCluster("cluster3").WithLabel(clusterSetLabel, clusterSetName).WithResource(clusterapiv1.ResourceMemory, "0", "100").Build(),
testinghelpers.NewManagedCluster("cluster1").WithLabel(clusterapiv1beta2.ClusterSetLabel, clusterSetName).WithResource(clusterapiv1.ResourceMemory, "100", "100").Build(),
testinghelpers.NewManagedCluster("cluster2").WithLabel(clusterapiv1beta2.ClusterSetLabel, clusterSetName).WithResource(clusterapiv1.ResourceMemory, "50", "100").Build(),
testinghelpers.NewManagedCluster("cluster3").WithLabel(clusterapiv1beta2.ClusterSetLabel, clusterSetName).WithResource(clusterapiv1.ResourceMemory, "0", "100").Build(),
},
decisions: []runtime.Object{},
expectedDecisions: []clusterapiv1beta1.ClusterDecision{
@@ -358,9 +360,9 @@ func TestSchedule(t *testing.T) {
testinghelpers.NewClusterSetBinding(placementNamespace, clusterSetName),
},
clusters: []*clusterapiv1.ManagedCluster{
testinghelpers.NewManagedCluster("cluster1").WithLabel(clusterSetLabel, clusterSetName).WithResource(clusterapiv1.ResourceMemory, "100", "100").Build(),
testinghelpers.NewManagedCluster("cluster2").WithLabel(clusterSetLabel, clusterSetName).WithResource(clusterapiv1.ResourceMemory, "50", "100").Build(),
testinghelpers.NewManagedCluster("cluster3").WithLabel(clusterSetLabel, clusterSetName).WithResource(clusterapiv1.ResourceMemory, "0", "100").Build(),
testinghelpers.NewManagedCluster("cluster1").WithLabel(clusterapiv1beta2.ClusterSetLabel, clusterSetName).WithResource(clusterapiv1.ResourceMemory, "100", "100").Build(),
testinghelpers.NewManagedCluster("cluster2").WithLabel(clusterapiv1beta2.ClusterSetLabel, clusterSetName).WithResource(clusterapiv1.ResourceMemory, "50", "100").Build(),
testinghelpers.NewManagedCluster("cluster3").WithLabel(clusterapiv1beta2.ClusterSetLabel, clusterSetName).WithResource(clusterapiv1.ResourceMemory, "0", "100").Build(),
},
decisions: []runtime.Object{},
expectedDecisions: []clusterapiv1beta1.ClusterDecision{
@@ -399,16 +401,16 @@ func TestSchedule(t *testing.T) {
testinghelpers.NewClusterSet(clusterSetName).Build(),
testinghelpers.NewClusterSetBinding(placementNamespace, clusterSetName),
testinghelpers.NewPlacementDecision(placementNamespace, placementDecisionName(placementName, 1)).
WithLabel(placementLabel, placementName).
WithLabel(clusterapiv1beta1.PlacementLabel, placementName).
WithDecisions("cluster1").Build(),
},
clusters: []*clusterapiv1.ManagedCluster{
testinghelpers.NewManagedCluster("cluster1").WithLabel(clusterSetLabel, clusterSetName).Build(),
testinghelpers.NewManagedCluster("cluster2").WithLabel(clusterSetLabel, clusterSetName).Build(),
testinghelpers.NewManagedCluster("cluster1").WithLabel(clusterapiv1beta2.ClusterSetLabel, clusterSetName).Build(),
testinghelpers.NewManagedCluster("cluster2").WithLabel(clusterapiv1beta2.ClusterSetLabel, clusterSetName).Build(),
},
decisions: []runtime.Object{
testinghelpers.NewPlacementDecision(placementNamespace, placementDecisionName(placementName, 1)).
WithLabel(placementLabel, placementName).
WithLabel(clusterapiv1beta1.PlacementLabel, placementName).
WithDecisions("cluster1").Build(),
},
expectedDecisions: []clusterapiv1beta1.ClusterDecision{
@@ -454,9 +456,9 @@ func TestSchedule(t *testing.T) {
WithDecisions("cluster1", "cluster2").Build(),
},
clusters: []*clusterapiv1.ManagedCluster{
testinghelpers.NewManagedCluster("cluster1").WithLabel(clusterSetLabel, clusterSetName).Build(),
testinghelpers.NewManagedCluster("cluster2").WithLabel(clusterSetLabel, clusterSetName).Build(),
testinghelpers.NewManagedCluster("cluster3").WithLabel(clusterSetLabel, clusterSetName).Build(),
testinghelpers.NewManagedCluster("cluster1").WithLabel(clusterapiv1beta2.ClusterSetLabel, clusterSetName).Build(),
testinghelpers.NewManagedCluster("cluster2").WithLabel(clusterapiv1beta2.ClusterSetLabel, clusterSetName).Build(),
testinghelpers.NewManagedCluster("cluster3").WithLabel(clusterapiv1beta2.ClusterSetLabel, clusterSetName).Build(),
},
expectedDecisions: []clusterapiv1beta1.ClusterDecision{
{ClusterName: "cluster3"},
@@ -497,7 +499,7 @@ func TestSchedule(t *testing.T) {
testinghelpers.NewPlacementDecision(placementNamespace, placementDecisionName("others", 2)).
WithDecisions("cluster1", "cluster2").Build(),
testinghelpers.NewPlacementDecision(placementNamespace, placementDecisionName(placementName, 1)).
WithLabel(placementLabel, placementName).
WithLabel(clusterapiv1beta1.PlacementLabel, placementName).
WithDecisions("cluster3").Build(),
},
decisions: []runtime.Object{
@@ -506,13 +508,13 @@ func TestSchedule(t *testing.T) {
testinghelpers.NewPlacementDecision(placementNamespace, placementDecisionName("others", 2)).
WithDecisions("cluster1", "cluster2").Build(),
testinghelpers.NewPlacementDecision(placementNamespace, placementDecisionName(placementName, 1)).
WithLabel(placementLabel, placementName).
WithLabel(clusterapiv1beta1.PlacementLabel, placementName).
WithDecisions("cluster3").Build(),
},
clusters: []*clusterapiv1.ManagedCluster{
testinghelpers.NewManagedCluster("cluster1").WithLabel(clusterSetLabel, clusterSetName).Build(),
testinghelpers.NewManagedCluster("cluster2").WithLabel(clusterSetLabel, clusterSetName).Build(),
testinghelpers.NewManagedCluster("cluster3").WithLabel(clusterSetLabel, clusterSetName).Build(),
testinghelpers.NewManagedCluster("cluster1").WithLabel(clusterapiv1beta2.ClusterSetLabel, clusterSetName).Build(),
testinghelpers.NewManagedCluster("cluster2").WithLabel(clusterapiv1beta2.ClusterSetLabel, clusterSetName).Build(),
testinghelpers.NewManagedCluster("cluster3").WithLabel(clusterapiv1beta2.ClusterSetLabel, clusterSetName).Build(),
},
expectedDecisions: []clusterapiv1beta1.ClusterDecision{
{ClusterName: "cluster3"},
@@ -37,16 +37,14 @@ import (
clusterapiv1beta1 "open-cluster-management.io/api/cluster/v1beta1"
clusterapiv1beta2 "open-cluster-management.io/api/cluster/v1beta2"
"open-cluster-management.io/ocm/pkg/common/queue"
"open-cluster-management.io/ocm/pkg/placement/controllers/framework"
)
const (
clusterSetLabel = "cluster.open-cluster-management.io/clusterset"
placementLabel = "cluster.open-cluster-management.io/placement"
schedulingControllerName = "SchedulingController"
schedulingControllerResyncName = "SchedulingControllerResync"
maxNumOfClusterDecisions = 100
maxEventMessageLength = 1000 //the event message can have at most 1024 characters, use 1000 as limitation here to keep some buffer
schedulingControllerName = "SchedulingController"
maxNumOfClusterDecisions = 100
maxEventMessageLength = 1000 //the event message can have at most 1024 characters, use 1000 as limitation here to keep some buffer
)
var ResyncInterval = time.Minute * 5
@@ -152,26 +150,17 @@ func NewSchedulingController(
return factory.New().
WithSyncContext(syncCtx).
WithInformersQueueKeyFunc(func(obj runtime.Object) string {
key, _ := cache.MetaNamespaceKeyFunc(obj)
return key
}, placementInformer.Informer()).
WithInformersQueueKeysFunc(
queue.QueueKeyByMetaNamespaceName,
placementInformer.Informer()).
WithFilteredEventsInformersQueueKeyFunc(func(obj runtime.Object) string {
accessor, _ := meta.Accessor(obj)
labels := accessor.GetLabels()
placementName := labels[placementLabel]
placementName := labels[clusterapiv1beta1.PlacementLabel]
return fmt.Sprintf("%s/%s", accessor.GetNamespace(), placementName)
}, func(obj interface{}) bool {
accessor, err := meta.Accessor(obj)
if err != nil {
return false
}
labels := accessor.GetLabels()
if _, ok := labels[placementLabel]; ok {
return true
}
return false
}, placementDecisionInformer.Informer()).
},
queue.FileterByLabel(clusterapiv1beta1.PlacementLabel),
placementDecisionInformer.Informer()).
WithBareInformers(clusterInformer.Informer(), clusterSetInformer.Informer(), clusterSetBindingInformer.Informer(), placementScoreInformer.Informer()).
WithSync(c.sync).
ToController(schedulingControllerName, recorder)
@@ -493,7 +482,7 @@ func (c *schedulingController) bind(
}
// query all placementdecisions of the placement
requirement, err := labels.NewRequirement(placementLabel, selection.Equals, []string{placement.Name})
requirement, err := labels.NewRequirement(clusterapiv1beta1.PlacementLabel, selection.Equals, []string{placement.Name})
if err != nil {
return err
}
@@ -549,7 +538,7 @@ func (c *schedulingController) createOrUpdatePlacementDecision(
Name: placementDecisionName,
Namespace: placement.Namespace,
Labels: map[string]string{
placementLabel: placement.Name,
clusterapiv1beta1.PlacementLabel: placement.Name,
},
OwnerReferences: []metav1.OwnerReference{*owner},
},
@@ -90,7 +90,8 @@ func TestSchedulingController_sync(t *testing.T) {
initObjs: []runtime.Object{
testinghelpers.NewClusterSet("clusterset1").Build(),
testinghelpers.NewClusterSetBinding(placementNamespace, "clusterset1"),
testinghelpers.NewManagedCluster("cluster1").WithLabel(clusterSetLabel, "clusterset1").Build(),
testinghelpers.NewManagedCluster("cluster1").WithLabel(
clusterapiv1beta2.ClusterSetLabel, "clusterset1").Build(),
},
scheduleResult: &scheduleResult{
feasibleClusters: []*clusterapiv1.ManagedCluster{
@@ -214,7 +215,7 @@ func TestSchedulingController_sync(t *testing.T) {
initObjs: []runtime.Object{
testinghelpers.NewClusterSet("clusterset1").Build(),
testinghelpers.NewClusterSetBinding(placementNamespace, "clusterset1"),
testinghelpers.NewManagedCluster("cluster1").WithLabel(clusterSetLabel, "clusterset1").Build(),
testinghelpers.NewManagedCluster("cluster1").WithLabel(clusterapiv1beta2.ClusterSetLabel, "clusterset1").Build(),
},
scheduleResult: &scheduleResult{
feasibleClusters: []*clusterapiv1.ManagedCluster{
@@ -261,9 +262,9 @@ func TestSchedulingController_sync(t *testing.T) {
initObjs: []runtime.Object{
testinghelpers.NewClusterSet("clusterset1").Build(),
testinghelpers.NewClusterSetBinding(placementNamespace, "clusterset1"),
testinghelpers.NewManagedCluster("cluster1").WithLabel(clusterSetLabel, "clusterset1").Build(),
testinghelpers.NewManagedCluster("cluster1").WithLabel(clusterapiv1beta2.ClusterSetLabel, "clusterset1").Build(),
testinghelpers.NewPlacementDecision(placementNamespace, placementDecisionName(placementName, 1)).
WithLabel(placementLabel, placementName).
WithLabel(clusterapiv1beta1.PlacementLabel, placementName).
WithDecisions("cluster1", "cluster2", "cluster3").Build(),
},
scheduleResult: &scheduleResult{
@@ -475,8 +476,8 @@ func TestGetAvailableClusters(t *testing.T) {
initObjs: []runtime.Object{
testinghelpers.NewClusterSet("clusterset1").Build(),
testinghelpers.NewClusterSet("clusterset2").Build(),
testinghelpers.NewManagedCluster("cluster1").WithLabel(clusterSetLabel, "clusterset1").Build(),
testinghelpers.NewManagedCluster("cluster2").WithLabel(clusterSetLabel, "clusterset2").Build(),
testinghelpers.NewManagedCluster("cluster1").WithLabel(clusterapiv1beta2.ClusterSetLabel, "clusterset1").Build(),
testinghelpers.NewManagedCluster("cluster2").WithLabel(clusterapiv1beta2.ClusterSetLabel, "clusterset2").Build(),
},
expectedClusterNames: []string{"cluster1", "cluster2"},
},
@@ -485,7 +486,7 @@ func TestGetAvailableClusters(t *testing.T) {
clusterSetNames: []string{"clusterset1"},
initObjs: []runtime.Object{
testinghelpers.NewClusterSet("clusterset1").Build(),
testinghelpers.NewManagedCluster("cluster1").WithLabel(clusterSetLabel, "clusterset1").Build(),
testinghelpers.NewManagedCluster("cluster1").WithLabel(clusterapiv1beta2.ClusterSetLabel, "clusterset1").Build(),
},
expectedClusterNames: []string{"cluster1"},
},
@@ -494,7 +495,7 @@ func TestGetAvailableClusters(t *testing.T) {
clusterSetNames: []string{"clusterset1"},
initObjs: []runtime.Object{
testinghelpers.NewClusterSet("clusterset1").WithClusterSelector(clusterapiv1beta2.ManagedClusterSelector{}).Build(),
testinghelpers.NewManagedCluster("cluster1").WithLabel(clusterSetLabel, "clusterset1").Build(),
testinghelpers.NewManagedCluster("cluster1").WithLabel(clusterapiv1beta2.ClusterSetLabel, "clusterset1").Build(),
},
expectedClusterNames: []string{"cluster1"},
},
@@ -505,7 +506,7 @@ func TestGetAvailableClusters(t *testing.T) {
testinghelpers.NewClusterSet("clusterset1").WithClusterSelector(clusterapiv1beta2.ManagedClusterSelector{
SelectorType: clusterapiv1beta2.ExclusiveClusterSetLabel,
}).Build(),
testinghelpers.NewManagedCluster("cluster1").WithLabel(clusterSetLabel, "clusterset1").Build(),
testinghelpers.NewManagedCluster("cluster1").WithLabel(clusterapiv1beta2.ClusterSetLabel, "clusterset1").Build(),
},
expectedClusterNames: []string{"cluster1"},
},
@@ -521,7 +522,7 @@ func TestGetAvailableClusters(t *testing.T) {
},
},
}).Build(),
testinghelpers.NewManagedCluster("cluster1").WithLabel(clusterSetLabel, "clusterset1").Build(),
testinghelpers.NewManagedCluster("cluster1").WithLabel(clusterapiv1beta2.ClusterSetLabel, "clusterset1").Build(),
testinghelpers.NewManagedCluster("cluster2").WithLabel("vendor", "openShift").Build(),
},
expectedClusterNames: []string{"cluster2"},
@@ -534,7 +535,7 @@ func TestGetAvailableClusters(t *testing.T) {
SelectorType: clusterapiv1beta2.LabelSelector,
LabelSelector: &metav1.LabelSelector{},
}).Build(),
testinghelpers.NewManagedCluster("cluster1").WithLabel(clusterSetLabel, "clusterset1").Build(),
testinghelpers.NewManagedCluster("cluster1").WithLabel(clusterapiv1beta2.ClusterSetLabel, "clusterset1").Build(),
testinghelpers.NewManagedCluster("cluster2").Build(),
},
expectedClusterNames: []string{"cluster1", "cluster2"},
@@ -548,7 +549,7 @@ func TestGetAvailableClusters(t *testing.T) {
SelectorType: "errorType",
},
).Build(),
testinghelpers.NewManagedCluster("cluster1").WithLabel(clusterSetLabel, "clusterset1").Build(),
testinghelpers.NewManagedCluster("cluster1").WithLabel(clusterapiv1beta2.ClusterSetLabel, "clusterset1").Build(),
},
expectedClusterNames: []string{},
},
@@ -790,10 +791,10 @@ func TestBind(t *testing.T) {
clusterDecisions: newClusterDecisions(128),
initObjs: []runtime.Object{
testinghelpers.NewPlacementDecision(placementNamespace, placementDecisionName(placementName, 1)).
WithLabel(placementLabel, placementName).
WithLabel(clusterapiv1beta1.PlacementLabel, placementName).
WithDecisions(newSelectedClusters(128)[:100]...).Build(),
testinghelpers.NewPlacementDecision(placementNamespace, placementDecisionName(placementName, 2)).
WithLabel(placementLabel, placementName).
WithLabel(clusterapiv1beta1.PlacementLabel, placementName).
WithDecisions(newSelectedClusters(128)[100:]...).Build(),
},
validateActions: testingcommon.AssertNoActions,
@@ -803,7 +804,7 @@ func TestBind(t *testing.T) {
clusterDecisions: newClusterDecisions(128),
initObjs: []runtime.Object{
testinghelpers.NewPlacementDecision(placementNamespace, placementDecisionName(placementName, 1)).
WithLabel(placementLabel, placementName).
WithLabel(clusterapiv1beta1.PlacementLabel, placementName).
WithDecisions(newSelectedClusters(128)[:100]...).Build(),
},
validateActions: func(t *testing.T, actions []clienttesting.Action) {
@@ -822,10 +823,10 @@ func TestBind(t *testing.T) {
clusterDecisions: newClusterDecisions(10),
initObjs: []runtime.Object{
testinghelpers.NewPlacementDecision(placementNamespace, placementDecisionName(placementName, 1)).
WithLabel(placementLabel, placementName).
WithLabel(clusterapiv1beta1.PlacementLabel, placementName).
WithDecisions(newSelectedClusters(128)[:100]...).Build(),
testinghelpers.NewPlacementDecision(placementNamespace, placementDecisionName(placementName, 2)).
WithLabel(placementLabel, placementName).
WithLabel(clusterapiv1beta1.PlacementLabel, placementName).
WithDecisions(newSelectedClusters(128)[100:]...).Build(),
},
validateActions: func(t *testing.T, actions []clienttesting.Action) {
@@ -843,10 +844,10 @@ func TestBind(t *testing.T) {
clusterDecisions: newClusterDecisions(0),
initObjs: []runtime.Object{
testinghelpers.NewPlacementDecision(placementNamespace, placementDecisionName(placementName, 1)).
WithLabel(placementLabel, placementName).
WithLabel(clusterapiv1beta1.PlacementLabel, placementName).
WithDecisions(newSelectedClusters(128)[:100]...).Build(),
testinghelpers.NewPlacementDecision(placementNamespace, placementDecisionName(placementName, 2)).
WithLabel(placementLabel, placementName).
WithLabel(clusterapiv1beta1.PlacementLabel, placementName).
WithDecisions(newSelectedClusters(128)[100:]...).Build(),
},
validateActions: func(t *testing.T, actions []clienttesting.Action) {
@@ -12,7 +12,6 @@ import (
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/runtime"
addonv1alpha1 "open-cluster-management.io/api/addon/v1alpha1"
addoninformerv1alpha1 "open-cluster-management.io/api/client/addon/informers/externalversions/addon/v1alpha1"
@@ -23,6 +22,7 @@ import (
clusterv1 "open-cluster-management.io/api/cluster/v1"
"open-cluster-management.io/ocm/pkg/common/patcher"
"open-cluster-management.io/ocm/pkg/common/queue"
)
const (
@@ -58,17 +58,11 @@ func NewAddOnFeatureDiscoveryController(
}
return factory.New().
WithInformersQueueKeyFunc(
func(obj runtime.Object) string {
accessor, _ := meta.Accessor(obj)
return accessor.GetName()
},
WithInformersQueueKeysFunc(
queue.QueueKeyByMetaName,
clusterInformer.Informer()).
WithInformersQueueKeyFunc(
func(obj runtime.Object) string {
accessor, _ := meta.Accessor(obj)
return accessor.GetNamespace()
},
WithInformersQueueKeysFunc(
queue.QueueKeyByMetaNamespace,
addOnInformers.Informer()).
WithSync(c.sync).
ToController("AddOnFeatureDiscoveryController", recorder)
@@ -10,7 +10,6 @@ import (
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/runtime"
addonv1alpha1 "open-cluster-management.io/api/addon/v1alpha1"
addonclient "open-cluster-management.io/api/client/addon/clientset/versioned"
@@ -21,6 +20,7 @@ import (
clusterv1 "open-cluster-management.io/api/cluster/v1"
patcher "open-cluster-management.io/ocm/pkg/common/patcher"
"open-cluster-management.io/ocm/pkg/common/queue"
)
// managedClusterAddonHealthCheckController udpates managed cluster addons status through watching the managed cluster status on
@@ -43,10 +43,7 @@ func NewManagedClusterAddOnHealthCheckController(addOnClient addonclient.Interfa
}
return factory.New().
WithInformersQueueKeyFunc(func(obj runtime.Object) string {
accessor, _ := meta.Accessor(obj)
return accessor.GetName()
}, clusterInformer.Informer()).
WithInformersQueueKeysFunc(queue.QueueKeyByMetaName, clusterInformer.Informer()).
WithSync(c.sync).
ToController("ManagedClusterAddonHealthCheckController", recorder)
}
@@ -9,9 +9,7 @@ import (
"github.com/openshift/library-go/pkg/operator/events"
"github.com/openshift/library-go/pkg/operator/resource/resourceapply"
operatorhelpers "github.com/openshift/library-go/pkg/operator/v1helpers"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/util/sets"
rbacv1informers "k8s.io/client-go/informers/rbac/v1"
"k8s.io/client-go/kubernetes"
@@ -19,6 +17,7 @@ import (
clusterv1listers "open-cluster-management.io/api/client/cluster/listers/cluster/v1"
"open-cluster-management.io/ocm/pkg/common/apply"
"open-cluster-management.io/ocm/pkg/common/queue"
)
const (
@@ -64,11 +63,8 @@ func NewManagedClusterClusterroleController(
}
return factory.New().
WithFilteredEventsInformers(
func(obj interface{}) bool {
clusterRoles := sets.NewString(registrationClusterRole, workClusterRole)
metaObj := obj.(metav1.Object)
return clusterRoles.Has(metaObj.GetName())
}, clusterRoleInformer.Informer()).
queue.FilterByNames(registrationClusterRole, workClusterRole),
clusterRoleInformer.Informer()).
WithInformers(clusterInformer.Informer()).
WithSync(c.sync).
ToController("ManagedClusterClusterRoleController", recorder)
+2 -6
View File
@@ -9,13 +9,12 @@ import (
certificatesv1beta1 "k8s.io/api/certificates/v1beta1"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/cache"
"k8s.io/klog/v2"
"open-cluster-management.io/ocm/pkg/common/queue"
"open-cluster-management.io/ocm/pkg/registration/helpers"
)
@@ -53,10 +52,7 @@ func NewCSRApprovingController[T CSR](
}
return factory.New().
WithInformersQueueKeyFunc(func(obj runtime.Object) string {
accessor, _ := meta.Accessor(obj)
return accessor.GetName()
}, csrInformer).
WithInformersQueueKeysFunc(queue.QueueKeyByMetaName, csrInformer).
WithSync(c.sync).
ToController("CSRApprovingController", recorder)
}
+5 -25
View File
@@ -10,7 +10,6 @@ import (
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
coordinformers "k8s.io/client-go/informers/coordination/v1"
"k8s.io/client-go/kubernetes"
coordlisters "k8s.io/client-go/listers/coordination/v1"
@@ -22,6 +21,7 @@ import (
clusterv1 "open-cluster-management.io/api/cluster/v1"
"open-cluster-management.io/ocm/pkg/common/patcher"
"open-cluster-management.io/ocm/pkg/common/queue"
)
const leaseDurationTimes = 5
@@ -58,32 +58,12 @@ func NewClusterLeaseController(
eventRecorder: recorder.WithComponentSuffix("managed-cluster-lease-controller"),
}
return factory.New().
WithFilteredEventsInformersQueueKeyFunc(
func(obj runtime.Object) string {
accessor, _ := meta.Accessor(obj)
return accessor.GetLabels()[clusterv1.ClusterNameLabelKey]
},
func(obj interface{}) bool {
metaObj, ok := obj.(metav1.ObjectMetaAccessor)
if !ok {
return false
}
// only handle the managed cluster lease
// TODO instead of this by adding label filter in the SharedInformerFactory
// see https://github.com/open-cluster-management-io/registration/issues/225
if _, ok := metaObj.GetObjectMeta().GetLabels()[clusterv1.ClusterNameLabelKey]; !ok {
return false
}
return metaObj.GetObjectMeta().GetName() == leaseName
},
WithFilteredEventsInformersQueueKeysFunc(
queue.QueueKeyByLabel(clusterv1.ClusterNameLabelKey),
queue.UnionFilter(queue.FileterByLabel(clusterv1.ClusterNameLabelKey), queue.FilterByNames(leaseName)),
leaseInformer.Informer(),
).
WithInformersQueueKeyFunc(func(obj runtime.Object) string {
accessor, _ := meta.Accessor(obj)
return accessor.GetName()
}, clusterInformer.Informer()).
WithInformersQueueKeysFunc(queue.QueueKeyByMetaName, clusterInformer.Informer()).
WithSync(c.sync).
ToController("ManagedClusterLeaseController", recorder)
}
@@ -13,7 +13,6 @@ import (
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
rbacv1informers "k8s.io/client-go/informers/rbac/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/klog/v2"
@@ -25,6 +24,7 @@ import (
"open-cluster-management.io/ocm/pkg/common/apply"
"open-cluster-management.io/ocm/pkg/common/patcher"
"open-cluster-management.io/ocm/pkg/common/queue"
"open-cluster-management.io/ocm/pkg/registration/helpers"
)
@@ -77,13 +77,10 @@ func NewManagedClusterController(
eventRecorder: recorder.WithComponentSuffix("managed-cluster-controller"),
}
return factory.New().
WithInformersQueueKeyFunc(func(obj runtime.Object) string {
accessor, _ := meta.Accessor(obj)
return accessor.GetName()
}, clusterInformer.Informer()).
WithFilteredEventsInformersQueueKeyFunc(
permissionQueueKey,
permissionFilterKey,
WithInformersQueueKeysFunc(queue.QueueKeyByMetaName, clusterInformer.Informer()).
WithFilteredEventsInformersQueueKeysFunc(
queue.QueueKeyByLabel(v1.ClusterNameLabelKey),
queue.FileterByLabel(v1.ClusterNameLabelKey),
roleInformer.Informer(),
rolebindingInformer.Informer(),
clusterRoleInformer.Informer(),
@@ -92,19 +89,6 @@ func NewManagedClusterController(
ToController("ManagedClusterController", recorder)
}
func permissionFilterKey(obj interface{}) bool {
accessor, _ := meta.Accessor(obj)
return len(accessor.GetLabels()) > 0 && len(accessor.GetLabels()[v1.ClusterNameLabelKey]) > 0
}
func permissionQueueKey(obj runtime.Object) string {
accessor, _ := meta.Accessor(obj)
if len(accessor.GetLabels()) == 0 {
return ""
}
return accessor.GetLabels()[v1.ClusterNameLabelKey]
}
func (c *managedClusterController) sync(ctx context.Context, syncCtx factory.SyncContext) error {
managedClusterName := syncCtx.QueueKey()
klog.V(4).Infof("Reconciling ManagedCluster %s", managedClusterName)
@@ -10,7 +10,6 @@ import (
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
"k8s.io/apimachinery/pkg/util/sets"
"k8s.io/client-go/tools/cache"
@@ -26,6 +25,7 @@ import (
clusterv1beta2 "open-cluster-management.io/api/cluster/v1beta2"
"open-cluster-management.io/ocm/pkg/common/patcher"
"open-cluster-management.io/ocm/pkg/common/queue"
)
// managedClusterSetController reconciles instances of ManagedClusterSet on the hub.
@@ -110,10 +110,7 @@ func NewManagedClusterSetController(
return factory.New().
WithSyncContext(syncCtx).
WithInformersQueueKeyFunc(func(obj runtime.Object) string {
accessor, _ := meta.Accessor(obj)
return accessor.GetName()
}, clusterSetInformer.Informer()).
WithInformersQueueKeysFunc(queue.QueueKeyByMetaName, clusterSetInformer.Informer()).
WithBareInformers(clusterInformer.Informer()).
WithSync(c.sync).
ToController("ManagedClusterSetController", recorder)
@@ -9,15 +9,15 @@ import (
"github.com/openshift/library-go/pkg/operator/events"
"k8s.io/apimachinery/pkg/api/equality"
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/klog/v2"
clustersetv1beta2 "open-cluster-management.io/api/client/cluster/clientset/versioned/typed/cluster/v1beta2"
clusterinformerv1beta2 "open-cluster-management.io/api/client/cluster/informers/externalversions/cluster/v1beta2"
clusterlisterv1beta2 "open-cluster-management.io/api/client/cluster/listers/cluster/v1beta2"
clusterv1beta2 "open-cluster-management.io/api/cluster/v1beta2"
"open-cluster-management.io/ocm/pkg/common/queue"
)
const (
@@ -54,19 +54,9 @@ func NewDefaultManagedClusterSetController(
}
return factory.New().
WithFilteredEventsInformersQueueKeyFunc(
func(obj runtime.Object) string {
accessor, _ := meta.Accessor(obj)
return accessor.GetName()
},
func(obj interface{}) bool {
metaObj, ok := obj.(metav1.ObjectMetaAccessor)
if !ok {
return false
}
// filter clustersets except defaultManagedClusterSet.
return DefaultManagedClusterSetName != metaObj.GetObjectMeta().GetName()
},
WithFilteredEventsInformersQueueKeysFunc(
queue.QueueKeyByMetaName,
queue.FilterByNames(DefaultManagedClusterSetName),
clusterSetInformer.Informer(),
).
WithSync(c.sync).
@@ -9,15 +9,15 @@ import (
"github.com/openshift/library-go/pkg/operator/events"
"k8s.io/apimachinery/pkg/api/equality"
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/klog/v2"
clustersetv1beta2 "open-cluster-management.io/api/client/cluster/clientset/versioned/typed/cluster/v1beta2"
clusterinformerv1beta2 "open-cluster-management.io/api/client/cluster/informers/externalversions/cluster/v1beta2"
clusterlisterv1beta2 "open-cluster-management.io/api/client/cluster/listers/cluster/v1beta2"
clusterv1beta2 "open-cluster-management.io/api/cluster/v1beta2"
"open-cluster-management.io/ocm/pkg/common/queue"
)
const (
@@ -54,19 +54,9 @@ func NewGlobalManagedClusterSetController(
}
return factory.New().
WithFilteredEventsInformersQueueKeyFunc(
func(obj runtime.Object) string {
accessor, _ := meta.Accessor(obj)
return accessor.GetName()
},
func(obj interface{}) bool {
metaObj, ok := obj.(metav1.ObjectMetaAccessor)
if !ok {
return false
}
// filter clustersets except globalManagedClusterSet.
return GlobalManagedClusterSetName != metaObj.GetObjectMeta().GetName()
},
WithFilteredEventsInformersQueueKeysFunc(
queue.QueueKeyByMetaName,
queue.FilterByNames(GlobalManagedClusterSetName),
clusterSetInformer.Informer(),
).
WithSync(c.sync).
@@ -9,7 +9,6 @@ import (
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
"k8s.io/client-go/tools/cache"
"k8s.io/client-go/util/workqueue"
@@ -21,6 +20,7 @@ import (
clusterv1beta2 "open-cluster-management.io/api/cluster/v1beta2"
"open-cluster-management.io/ocm/pkg/common/patcher"
"open-cluster-management.io/ocm/pkg/common/queue"
)
const (
@@ -78,10 +78,7 @@ func NewManagedClusterSetBindingController(
return factory.New().
WithSyncContext(syncCtx).
WithInformersQueueKeyFunc(func(obj runtime.Object) string {
key, _ := cache.MetaNamespaceKeyFunc(obj)
return key
}, clusterSetBindingInformer.Informer()).
WithInformersQueueKeysFunc(queue.QueueKeyByMetaNamespaceName, clusterSetBindingInformer.Informer()).
WithBareInformers(clusterSetInformer.Informer()).
WithSync(c.sync).
ToController("ManagedClusterSetController", recorder)
@@ -24,6 +24,8 @@ import (
clusterv1listers "open-cluster-management.io/api/client/cluster/listers/cluster/v1"
worklister "open-cluster-management.io/api/client/work/listers/work/v1"
clusterv1 "open-cluster-management.io/api/cluster/v1"
"open-cluster-management.io/ocm/pkg/common/queue"
)
const (
@@ -63,10 +65,7 @@ func NewFinalizeController(
}
return factory.New().
WithInformersQueueKeyFunc(func(obj runtime.Object) string {
key, _ := cache.MetaNamespaceKeyFunc(obj)
return key
}, roleInformer.Informer(), roleBindingInformer.Informer()).
WithInformersQueueKeysFunc(queue.QueueKeyByMetaNamespaceName, roleInformer.Informer(), roleBindingInformer.Informer()).
WithSync(controller.sync).ToController("FinalizeController", eventRecorder)
}
+2 -5
View File
@@ -8,7 +8,6 @@ import (
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/klog/v2"
clientset "open-cluster-management.io/api/client/cluster/clientset/versioned"
@@ -17,6 +16,7 @@ import (
v1 "open-cluster-management.io/api/cluster/v1"
"open-cluster-management.io/ocm/pkg/common/patcher"
"open-cluster-management.io/ocm/pkg/common/queue"
"open-cluster-management.io/ocm/pkg/registration/helpers"
)
@@ -52,10 +52,7 @@ func NewTaintController(
eventRecorder: recorder.WithComponentSuffix("taint-controller"),
}
return factory.New().
WithInformersQueueKeyFunc(func(obj runtime.Object) string {
accessor, _ := meta.Accessor(obj)
return accessor.GetName()
}, clusterInformer.Informer()).
WithInformersQueueKeysFunc(queue.QueueKeyByMetaName, clusterInformer.Informer()).
WithSync(c.sync).
ToController("taintController", recorder)
}
@@ -12,7 +12,6 @@ import (
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
"k8s.io/client-go/informers"
"k8s.io/client-go/kubernetes"
@@ -26,6 +25,7 @@ import (
clusterv1 "open-cluster-management.io/api/cluster/v1"
"open-cluster-management.io/ocm/pkg/common/patcher"
"open-cluster-management.io/ocm/pkg/common/queue"
"open-cluster-management.io/ocm/pkg/registration/clientcert"
)
@@ -98,11 +98,8 @@ func NewAddOnRegistrationController(
c.startRegistrationFunc = c.startRegistration
return factory.New().
WithInformersQueueKeyFunc(
func(obj runtime.Object) string {
accessor, _ := meta.Accessor(obj)
return accessor.GetName()
},
WithInformersQueueKeysFunc(
queue.QueueKeyByMetaName,
hubAddOnInformers.Informer()).
WithSync(c.sync).
ResyncEvery(10*time.Minute).
@@ -15,10 +15,11 @@ import (
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
corev1informers "k8s.io/client-go/informers/core/v1"
corev1client "k8s.io/client-go/kubernetes/typed/core/v1"
"k8s.io/klog/v2"
"open-cluster-management.io/ocm/pkg/common/queue"
)
// hubKubeconfigSecretController watches the HubKubeconfig secret, if the secret is changed, this controller creates/updates the
@@ -44,11 +45,8 @@ func NewHubKubeconfigSecretController(
}
return factory.New().
WithFilteredEventsInformersQueueKeyFunc(
func(obj runtime.Object) string {
accessor, _ := meta.Accessor(obj)
return accessor.GetName()
},
WithFilteredEventsInformersQueueKeysFunc(
queue.QueueKeyByMetaName,
func(obj interface{}) bool {
accessor, err := meta.Accessor(obj)
if err != nil {
@@ -28,6 +28,7 @@ import (
workapiv1alpha1 "open-cluster-management.io/api/work/v1alpha1"
"open-cluster-management.io/ocm/pkg/common/patcher"
"open-cluster-management.io/ocm/pkg/common/queue"
)
const (
@@ -82,14 +83,7 @@ func NewManifestWorkReplicaSetController(
}
return factory.New().
WithInformersQueueKeyFunc(func(obj runtime.Object) string {
key, err := cache.DeletionHandlingMetaNamespaceKeyFunc(obj)
if err != nil {
utilruntime.HandleError(err)
return ""
}
return key
}, manifestWorkReplicaSetInformer.Informer()).
WithInformersQueueKeysFunc(queue.QueueKeyByMetaNamespaceName, manifestWorkReplicaSetInformer.Informer()).
WithFilteredEventsInformersQueueKeyFunc(func(obj runtime.Object) string {
accessor, _ := meta.Accessor(obj)
labelValue, ok := accessor.GetLabels()[ManifestWorkReplicaSetControllerNameLabelKey]
@@ -101,16 +95,9 @@ func NewManifestWorkReplicaSetController(
return ""
}
return fmt.Sprintf("%s/%s", keys[0], keys[1])
}, func(obj interface{}) bool {
accessor, err := meta.Accessor(obj)
if err != nil {
return false
}
if _, ok := accessor.GetLabels()[ManifestWorkReplicaSetControllerNameLabelKey]; ok {
return true
}
return false
}, manifestWorkInformer.Informer()).
},
queue.FileterByLabel(ManifestWorkReplicaSetControllerNameLabelKey),
manifestWorkInformer.Informer()).
WithInformersQueueKeysFunc(controller.placementDecisionQueueKeysFunc, placeDecisionInformer.Informer()).
WithInformersQueueKeysFunc(controller.placementQueueKeysFunc, placementInformer.Informer()).
WithSync(controller.sync).ToController("ManifestWorkReplicaSetController", recorder)
@@ -2,6 +2,7 @@ package manifestworkreplicasetcontroller
import (
"fmt"
"strings"
"k8s.io/apimachinery/pkg/api/meta"
"k8s.io/apimachinery/pkg/runtime"
@@ -71,7 +72,11 @@ func (m *ManifestWorkReplicaSetController) manifestWorkQueueKeyFunc(obj runtime.
if !ok {
return ""
}
return key
keys := strings.Split(key, ".")
if len(keys) != 2 {
return ""
}
return fmt.Sprintf("%s/%s", keys[0], keys[1])
}
func indexManifestWorkReplicaSetByPlacement(obj interface{}) ([]string, error) {
@@ -111,7 +111,7 @@ func TestPlaceMWControllerIndex(t *testing.T) {
// Check manifestWork Queue Keys
key := pmwController.manifestWorkQueueKeyFunc(mw)
if key != mwrSetTest.Namespace+"."+mwrSetTest.Name {
if key != mwrSetTest.Namespace+"/"+mwrSetTest.Name {
t.Fatal("Expected manifestwork key not match", key, " - ", mwrSetTest.Name)
}
// Check manifestWork Queue Keys not exist
@@ -10,9 +10,7 @@ import (
"github.com/openshift/library-go/pkg/controller/factory"
"github.com/openshift/library-go/pkg/operator/events"
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
utilerrors "k8s.io/apimachinery/pkg/util/errors"
"k8s.io/client-go/dynamic"
@@ -25,6 +23,7 @@ import (
workapiv1 "open-cluster-management.io/api/work/v1"
"open-cluster-management.io/ocm/pkg/common/patcher"
"open-cluster-management.io/ocm/pkg/common/queue"
"open-cluster-management.io/ocm/pkg/work/helper"
)
@@ -61,10 +60,7 @@ func NewAppliedManifestWorkController(
}
return factory.New().
WithInformersQueueKeyFunc(func(obj runtime.Object) string {
accessor, _ := meta.Accessor(obj)
return accessor.GetName()
}, manifestWorkInformer.Informer()).
WithInformersQueueKeysFunc(queue.QueueKeyByMetaName, manifestWorkInformer.Informer()).
WithFilteredEventsInformersQueueKeyFunc(
helper.AppliedManifestworkQueueKeyFunc(hubHash),
helper.AppliedManifestworkHubHashFilter(hubHash),
@@ -6,8 +6,6 @@ import (
"github.com/openshift/library-go/pkg/controller/factory"
"github.com/openshift/library-go/pkg/operator/events"
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/meta"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/klog/v2"
workv1client "open-cluster-management.io/api/client/work/clientset/versioned/typed/work/v1"
@@ -16,6 +14,7 @@ import (
workapiv1 "open-cluster-management.io/api/work/v1"
"open-cluster-management.io/ocm/pkg/common/patcher"
"open-cluster-management.io/ocm/pkg/common/queue"
"open-cluster-management.io/ocm/pkg/work/spoke/controllers"
)
@@ -41,10 +40,7 @@ func NewAddFinalizerController(
}
return factory.New().
WithInformersQueueKeyFunc(func(obj runtime.Object) string {
accessor, _ := meta.Accessor(obj)
return accessor.GetName()
}, manifestWorkInformer.Informer()).
WithInformersQueueKeysFunc(queue.QueueKeyByMetaName, manifestWorkInformer.Informer()).
WithSync(controller.sync).ToController("ManifestWorkAddFinalizerController", recorder)
}
@@ -8,8 +8,6 @@ import (
"github.com/openshift/library-go/pkg/controller/factory"
"github.com/openshift/library-go/pkg/operator/events"
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/meta"
"k8s.io/apimachinery/pkg/runtime"
utilerrors "k8s.io/apimachinery/pkg/util/errors"
"k8s.io/client-go/dynamic"
"k8s.io/client-go/util/workqueue"
@@ -21,6 +19,7 @@ import (
workapiv1 "open-cluster-management.io/api/work/v1"
"open-cluster-management.io/ocm/pkg/common/patcher"
"open-cluster-management.io/ocm/pkg/common/queue"
"open-cluster-management.io/ocm/pkg/work/helper"
"open-cluster-management.io/ocm/pkg/work/spoke/controllers"
)
@@ -52,10 +51,8 @@ func NewAppliedManifestWorkFinalizeController(
}
return factory.New().
WithFilteredEventsInformersQueueKeyFunc(func(obj runtime.Object) string {
accessor, _ := meta.Accessor(obj)
return accessor.GetName()
}, helper.AppliedManifestworkAgentIDFilter(agentID), appliedManifestWorkInformer.Informer()).
WithFilteredEventsInformersQueueKeysFunc(queue.QueueKeyByMetaName,
helper.AppliedManifestworkAgentIDFilter(agentID), appliedManifestWorkInformer.Informer()).
WithSync(controller.sync).ToController("AppliedManifestWorkFinalizer", recorder)
}
@@ -8,9 +8,7 @@ import (
"github.com/openshift/library-go/pkg/controller/factory"
"github.com/openshift/library-go/pkg/operator/events"
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/util/workqueue"
"k8s.io/klog/v2"
@@ -20,6 +18,7 @@ import (
workapiv1 "open-cluster-management.io/api/work/v1"
"open-cluster-management.io/ocm/pkg/common/patcher"
"open-cluster-management.io/ocm/pkg/common/queue"
"open-cluster-management.io/ocm/pkg/work/helper"
"open-cluster-management.io/ocm/pkg/work/spoke/controllers"
)
@@ -56,10 +55,7 @@ func NewManifestWorkFinalizeController(
}
return factory.New().
WithInformersQueueKeyFunc(func(obj runtime.Object) string {
accessor, _ := meta.Accessor(obj)
return accessor.GetName()
}, manifestWorkInformer.Informer()).
WithInformersQueueKeysFunc(queue.QueueKeyByMetaName, manifestWorkInformer.Informer()).
WithFilteredEventsInformersQueueKeyFunc(
helper.AppliedManifestworkQueueKeyFunc(hubHash),
helper.AppliedManifestworkHubHashFilter(hubHash),
@@ -21,6 +21,7 @@ import (
workapiv1 "open-cluster-management.io/api/work/v1"
"open-cluster-management.io/ocm/pkg/common/patcher"
"open-cluster-management.io/ocm/pkg/common/queue"
"open-cluster-management.io/ocm/pkg/work/helper"
)
@@ -71,11 +72,9 @@ func NewUnManagedAppliedWorkController(
accessor, _ := meta.Accessor(obj)
return fmt.Sprintf("%s-%s", hubHash, accessor.GetName())
}, manifestWorkInformer.Informer()).
WithFilteredEventsInformersQueueKeyFunc(
func(obj runtime.Object) string {
accessor, _ := meta.Accessor(obj)
return accessor.GetName()
}, helper.AppliedManifestworkAgentIDFilter(agentID), appliedManifestWorkInformer.Informer()).
WithFilteredEventsInformersQueueKeysFunc(
queue.QueueKeyByMetaName,
helper.AppliedManifestworkAgentIDFilter(agentID), appliedManifestWorkInformer.Informer()).
WithSync(controller.sync).ToController("UnManagedAppliedManifestWork", recorder)
}
@@ -27,6 +27,7 @@ import (
workapiv1 "open-cluster-management.io/api/work/v1"
"open-cluster-management.io/ocm/pkg/common/patcher"
"open-cluster-management.io/ocm/pkg/common/queue"
"open-cluster-management.io/ocm/pkg/work/helper"
"open-cluster-management.io/ocm/pkg/work/spoke/apply"
"open-cluster-management.io/ocm/pkg/work/spoke/auth"
@@ -96,10 +97,7 @@ func NewManifestWorkController(
}
return factory.New().
WithInformersQueueKeyFunc(func(obj runtime.Object) string {
accessor, _ := meta.Accessor(obj)
return accessor.GetName()
}, manifestWorkInformer.Informer()).
WithInformersQueueKeysFunc(queue.QueueKeyByMetaName, manifestWorkInformer.Informer()).
WithFilteredEventsInformersQueueKeyFunc(
helper.AppliedManifestworkQueueKeyFunc(hubHash),
helper.AppliedManifestworkHubHashFilter(hubHash),
@@ -13,7 +13,6 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
utilerrors "k8s.io/apimachinery/pkg/util/errors"
"k8s.io/client-go/dynamic"
@@ -25,6 +24,7 @@ import (
workapiv1 "open-cluster-management.io/api/work/v1"
"open-cluster-management.io/ocm/pkg/common/patcher"
"open-cluster-management.io/ocm/pkg/common/queue"
"open-cluster-management.io/ocm/pkg/work/helper"
"open-cluster-management.io/ocm/pkg/work/spoke/controllers"
"open-cluster-management.io/ocm/pkg/work/spoke/statusfeedback"
@@ -62,10 +62,7 @@ func NewAvailableStatusController(
}
return factory.New().
WithInformersQueueKeyFunc(func(obj runtime.Object) string {
accessor, _ := meta.Accessor(obj)
return accessor.GetName()
}, manifestWorkInformer.Informer()).
WithInformersQueueKeysFunc(queue.QueueKeyByMetaName, manifestWorkInformer.Informer()).
WithSync(controller.sync).ResyncEvery(syncInterval).ToController("AvailableStatusController", recorder)
}