From 5d95aaad85e8b53819e4cf796002eedc27fe8c0e Mon Sep 17 00:00:00 2001 From: rambohe Date: Tue, 10 May 2022 22:12:02 +0800 Subject: [PATCH] add enable-node-pool parameter for yurthub in order to disable nodepools list/watch in filters when testing. (#822) --- cmd/yurthub/app/config/config.go | 21 ++++++++++++++++----- cmd/yurthub/app/options/options.go | 3 +++ 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/cmd/yurthub/app/config/config.go b/cmd/yurthub/app/config/config.go index d111ac7f113..82c3a13c447 100644 --- a/cmd/yurthub/app/config/config.go +++ b/cmd/yurthub/app/config/config.go @@ -27,10 +27,12 @@ import ( corev1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/fields" + "k8s.io/apimachinery/pkg/watch" "k8s.io/client-go/informers" coreinformers "k8s.io/client-go/informers/core/v1" "k8s.io/client-go/kubernetes" "k8s.io/client-go/rest" + core "k8s.io/client-go/testing" "k8s.io/client-go/tools/cache" "k8s.io/client-go/tools/clientcmd" "k8s.io/klog/v2" @@ -50,6 +52,7 @@ import ( "github.com/openyurtio/openyurt/pkg/yurthub/util" yurtcorev1alpha1 "github.com/openyurtio/yurt-app-manager-api/pkg/yurtappmanager/apis/apps/v1alpha1" yurtclientset "github.com/openyurtio/yurt-app-manager-api/pkg/yurtappmanager/client/clientset/versioned" + "github.com/openyurtio/yurt-app-manager-api/pkg/yurtappmanager/client/clientset/versioned/fake" yurtinformers "github.com/openyurtio/yurt-app-manager-api/pkg/yurtappmanager/client/informers/externalversions" yurtv1alpha1 "github.com/openyurtio/yurt-app-manager-api/pkg/yurtappmanager/client/informers/externalversions/apps/v1alpha1" ) @@ -119,7 +122,7 @@ func Complete(options *options.YurtHubOptions) (*YurtHubConfiguration, error) { proxyServerDummyAddr := net.JoinHostPort(options.HubAgentDummyIfIP, options.YurtHubProxyPort) proxySecureServerDummyAddr := net.JoinHostPort(options.HubAgentDummyIfIP, options.YurtHubProxySecurePort) workingMode := util.WorkingMode(options.WorkingMode) - sharedFactory, yurtSharedFactory, err := createSharedInformers(fmt.Sprintf("http://%s", proxyServerAddr)) + sharedFactory, yurtSharedFactory, err := createSharedInformers(fmt.Sprintf("http://%s", proxyServerAddr), options.EnableNodePool) if err != nil { return nil, err } @@ -200,8 +203,9 @@ func parseRemoteServers(serverAddr string) ([]*url.URL, error) { } // createSharedInformers create sharedInformers from the given proxyAddr. -func createSharedInformers(proxyAddr string) (informers.SharedInformerFactory, yurtinformers.SharedInformerFactory, error) { +func createSharedInformers(proxyAddr string, enableNodePool bool) (informers.SharedInformerFactory, yurtinformers.SharedInformerFactory, error) { var kubeConfig *rest.Config + var yurtClient yurtclientset.Interface var err error kubeConfig, err = clientcmd.BuildConfigFromFlags(proxyAddr, "") if err != nil { @@ -213,9 +217,16 @@ func createSharedInformers(proxyAddr string) (informers.SharedInformerFactory, y return nil, nil, err } - yurtClient, err := yurtclientset.NewForConfig(kubeConfig) - if err != nil { - return nil, nil, err + fakeYurtClient := &fake.Clientset{} + fakeWatch := watch.NewFake() + fakeYurtClient.AddWatchReactor("nodepools", core.DefaultWatchReactor(fakeWatch, nil)) + // init yurtClient by fake client + yurtClient = fakeYurtClient + if enableNodePool { + yurtClient, err = yurtclientset.NewForConfig(kubeConfig) + if err != nil { + return nil, nil, err + } } return informers.NewSharedInformerFactory(client, 24*time.Hour), diff --git a/cmd/yurthub/app/options/options.go b/cmd/yurthub/app/options/options.go index f239d3186cb..36ccb761bbc 100644 --- a/cmd/yurthub/app/options/options.go +++ b/cmd/yurthub/app/options/options.go @@ -67,6 +67,7 @@ type YurtHubOptions struct { DisabledResourceFilters []string WorkingMode string KubeletHealthGracePeriod time.Duration + EnableNodePool bool } // NewYurtHubOptions creates a new YurtHubOptions with a default config. @@ -97,6 +98,7 @@ func NewYurtHubOptions() *YurtHubOptions { DisabledResourceFilters: make([]string, 0), WorkingMode: string(util.WorkingModeEdge), KubeletHealthGracePeriod: time.Second * 40, + EnableNodePool: true, } return o } @@ -163,6 +165,7 @@ func (o *YurtHubOptions) AddFlags(fs *pflag.FlagSet) { fs.StringVar(&o.NodePoolName, "nodepool-name", o.NodePoolName, "the name of node pool that runs hub agent") fs.StringVar(&o.WorkingMode, "working-mode", o.WorkingMode, "the working mode of yurthub(edge, cloud).") fs.DurationVar(&o.KubeletHealthGracePeriod, "kubelet-health-grace-period", o.KubeletHealthGracePeriod, "the amount of time which we allow kubelet to be unresponsive before stop renew node lease") + fs.BoolVar(&o.EnableNodePool, "enable-node-pool", o.EnableNodePool, "enable list/watch nodepools resource or not for filters(only used for testing)") } // verifyDummyIP verify the specified ip is valid or not