From 732eebb67b1486067626fcdcb4e69ddfb10a507d Mon Sep 17 00:00:00 2001 From: neo502721 Date: Mon, 6 Sep 2021 14:12:43 +0800 Subject: [PATCH] feature: improve list/watch node pool resource in serviceTopology filter (#421) --- cmd/yurthub/app/config/config.go | 15 ++++++++++++--- cmd/yurthub/app/options/options.go | 2 ++ 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/cmd/yurthub/app/config/config.go b/cmd/yurthub/app/config/config.go index 70eb9829241..b4d85c39c43 100644 --- a/cmd/yurthub/app/config/config.go +++ b/cmd/yurthub/app/config/config.go @@ -36,6 +36,8 @@ import ( yurtclientset "github.com/openyurtio/yurt-app-manager-api/pkg/yurtappmanager/client/clientset/versioned" yurtinformers "github.com/openyurtio/yurt-app-manager-api/pkg/yurtappmanager/client/informers/externalversions" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/apimachinery/pkg/fields" "k8s.io/client-go/informers" "k8s.io/client-go/kubernetes" "k8s.io/client-go/rest" @@ -98,7 +100,7 @@ func Complete(options *options.YurtHubOptions) (*YurtHubConfiguration, error) { proxySecureServerAddr := net.JoinHostPort(options.YurtHubHost, options.YurtHubProxySecurePort) proxyServerDummyAddr := net.JoinHostPort(options.HubAgentDummyIfIP, options.YurtHubProxyPort) proxySecureServerDummyAddr := net.JoinHostPort(options.HubAgentDummyIfIP, options.YurtHubProxySecurePort) - sharedFactory, yurtSharedFactory, err := createSharedInformers(fmt.Sprintf("http://%s", proxyServerAddr)) + sharedFactory, yurtSharedFactory, err := createSharedInformers(fmt.Sprintf("http://%s", proxyServerAddr), options.NodePoolName) if err != nil { return nil, err } @@ -182,7 +184,7 @@ 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, nodePoolName string) (informers.SharedInformerFactory, yurtinformers.SharedInformerFactory, error) { var kubeConfig *rest.Config var err error kubeConfig, err = clientcmd.BuildConfigFromFlags(proxyAddr, "") @@ -200,7 +202,14 @@ func createSharedInformers(proxyAddr string) (informers.SharedInformerFactory, y return nil, nil, err } - return informers.NewSharedInformerFactory(client, 24*time.Hour), yurtinformers.NewSharedInformerFactory(yurtClient, 24*time.Hour), nil + if len(nodePoolName) == 0 { + return informers.NewSharedInformerFactory(client, 24*time.Hour), yurtinformers.NewSharedInformerFactory(yurtClient, 24*time.Hour), nil + } + yurtSharedInformerFactory := yurtinformers.NewSharedInformerFactoryWithOptions(yurtClient, 24*time.Hour, + yurtinformers.WithTweakListOptions(func(options *metav1.ListOptions) { + options.FieldSelector = fields.Set{"metadata.name": nodePoolName}.String() + })) + return informers.NewSharedInformerFactory(client, 24*time.Hour), yurtSharedInformerFactory, nil } // registerAllFilters by order, the front registered filter will be diff --git a/cmd/yurthub/app/options/options.go b/cmd/yurthub/app/options/options.go index 9e086aba1a3..a87450e509a 100644 --- a/cmd/yurthub/app/options/options.go +++ b/cmd/yurthub/app/options/options.go @@ -44,6 +44,7 @@ type YurtHubOptions struct { KubeletRootCAFilePath string KubeletPairFilePath string NodeName string + NodePoolName string LBMode string HeartbeatFailedRetry int HeartbeatHealthyThreshold int @@ -147,6 +148,7 @@ func (o *YurtHubOptions) AddFlags(fs *pflag.FlagSet) { fs.BoolVar(&o.AccessServerThroughHub, "access-server-through-hub", o.AccessServerThroughHub, "enable pods access kube-apiserver through yurthub or not") fs.BoolVar(&o.EnableResourceFilter, "enable-resource-filter", o.EnableResourceFilter, "enable to filter response that comes back from reverse proxy") fs.StringSliceVar(&o.DisabledResourceFilters, "disabled-resource-filters", o.DisabledResourceFilters, "disable resource filters to handle response") + fs.StringVar(&o.NodePoolName, "nodepool-name", o.NodePoolName, "the name of node pool that runs hub agent") } // verifyDummyIP verify the specified ip is valid or not