🌱 Refactor code to fix lint warning (#218)

* Refactor code to fix lint warning

Signed-off-by: Jian Qiu <jqiu@redhat.com>

* enable lint for testing files

Signed-off-by: Jian Qiu <jqiu@redhat.com>

---------

Signed-off-by: Jian Qiu <jqiu@redhat.com>
This commit is contained in:
Jian Qiu
2023-07-25 13:12:34 +08:00
committed by GitHub
parent e22faa4545
commit e810520961
145 changed files with 1218 additions and 935 deletions

View File

@@ -55,7 +55,7 @@ func NewBootstrapController(
secretInformers: secretInformers,
}
return factory.New().WithSync(controller.sync).
WithInformersQueueKeyFunc(bootstrapSecretQueueKeyFunc(controller.klusterletLister),
WithInformersQueueKeysFunc(bootstrapSecretQueueKeyFunc(controller.klusterletLister),
secretInformers[helpers.HubKubeConfig].Informer(),
secretInformers[helpers.BootstrapHubKubeConfig].Informer(),
secretInformers[helpers.ExternalManagedKubeConfig].Informer()).
@@ -110,6 +110,7 @@ func (k *bootstrapController) sync(ctx context.Context, controllerContext factor
return nil
}
// #nosec G101
hubKubeconfigSecret, err := k.secretInformers[helpers.HubKubeConfig].Lister().Secrets(agentNamespace).Get(helpers.HubKubeConfig)
switch {
case errors.IsNotFound(err):
@@ -203,28 +204,28 @@ func (k *bootstrapController) loadKubeConfig(secret *corev1.Secret) (*clientcmda
return cluster, nil
}
func bootstrapSecretQueueKeyFunc(klusterletLister operatorlister.KlusterletLister) factory.ObjectQueueKeyFunc {
return func(obj runtime.Object) string {
func bootstrapSecretQueueKeyFunc(klusterletLister operatorlister.KlusterletLister) factory.ObjectQueueKeysFunc {
return func(obj runtime.Object) []string {
accessor, err := meta.Accessor(obj)
if err != nil {
return ""
return []string{}
}
name := accessor.GetName()
if name != helpers.BootstrapHubKubeConfig {
return ""
return []string{}
}
namespace := accessor.GetNamespace()
klusterlets, err := klusterletLister.List(labels.Everything())
if err != nil {
return ""
return []string{}
}
if klusterlet := helpers.FindKlusterletByNamespace(klusterlets, namespace); klusterlet != nil {
return namespace + "/" + klusterlet.Name
return []string{namespace + "/" + klusterlet.Name}
}
return ""
return []string{}
}
}

View File

@@ -13,6 +13,7 @@ import (
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/equality"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/fields"
"k8s.io/apimachinery/pkg/runtime"
@@ -171,25 +172,25 @@ func TestBootstrapSecretQueueKeyFunc(t *testing.T) {
name string
object runtime.Object
klusterlet *operatorapiv1.Klusterlet
expectedKey string
expectedKey []string
}{
{
name: "key by bootstrap secret",
object: newSecret("bootstrap-hub-kubeconfig", "test", []byte{}),
klusterlet: newKlusterlet("testklusterlet", "test"),
expectedKey: "test/testklusterlet",
expectedKey: []string{"test/testklusterlet"},
},
{
name: "key by wrong secret",
object: newSecret("dummy", "test", []byte{}),
klusterlet: newKlusterlet("testklusterlet", "test"),
expectedKey: "",
expectedKey: []string{},
},
{
name: "key by klusterlet with empty namespace",
object: newSecret("bootstrap-hub-kubeconfig", "open-cluster-management-agent", []byte{}),
klusterlet: newKlusterlet("testklusterlet", ""),
expectedKey: "open-cluster-management-agent/testklusterlet",
expectedKey: []string{"open-cluster-management-agent/testklusterlet"},
},
}
@@ -203,7 +204,7 @@ func TestBootstrapSecretQueueKeyFunc(t *testing.T) {
}
keyFunc := bootstrapSecretQueueKeyFunc(operatorInformers.Operator().V1().Klusterlets().Lister())
actualKey := keyFunc(c.object)
if actualKey != c.expectedKey {
if !equality.Semantic.DeepEqual(actualKey, c.expectedKey) {
t.Errorf("Queued key is not correct: actual %s, expected %s", actualKey, c.expectedKey)
}
})