Skip to content

Commit

Permalink
support yurthub component work in specified namespace (#1366)
Browse files Browse the repository at this point in the history
  • Loading branch information
huweihuang authored Apr 13, 2023
1 parent 228240a commit 83e8526
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 20 deletions.
26 changes: 14 additions & 12 deletions cmd/yurthub/app/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ type YurtHubConfiguration struct {
YurtHubDummyProxyServerServing *apiserver.DeprecatedInsecureServingInfo
YurtHubSecureProxyServerServing *apiserver.SecureServingInfo
YurtHubProxyServerAddr string
YurtHubNamespace string
ProxiedClient kubernetes.Interface
DiskCachePath string
CoordinatorPKIDir string
Expand Down Expand Up @@ -137,7 +138,7 @@ func Complete(options *options.YurtHubOptions) (*YurtHubConfiguration, error) {
return nil, err
}
tenantNs := util.ParseTenantNsFromOrgs(options.YurtHubCertOrganizations)
registerInformers(sharedFactory, yurtSharedFactory, workingMode, serviceTopologyFilterEnabled(options), options.NodePoolName, options.NodeName, tenantNs)
registerInformers(options, sharedFactory, yurtSharedFactory, workingMode, tenantNs)
filterManager, err := manager.NewFilterManager(options, sharedFactory, yurtSharedFactory, serializerManager, storageWrapper, us[0].Host)
if err != nil {
klog.Errorf("could not create filter manager, %v", err)
Expand Down Expand Up @@ -166,6 +167,7 @@ func Complete(options *options.YurtHubOptions) (*YurtHubConfiguration, error) {
MinRequestTimeout: options.MinRequestTimeout,
TenantNs: tenantNs,
YurtHubProxyServerAddr: fmt.Sprintf("%s:%d", options.YurtHubProxyHost, options.YurtHubProxyPort),
YurtHubNamespace: options.YurtHubNamespace,
ProxiedClient: proxiedClient,
DiskCachePath: options.DiskCachePath,
CoordinatorPKIDir: filepath.Join(options.RootDir, "poolcoordinator"),
Expand Down Expand Up @@ -260,28 +262,28 @@ func createClientAndSharedInformers(proxyAddr string, enableNodePool bool) (kube
}

// registerInformers reconstruct node/nodePool/configmap informers
func registerInformers(informerFactory informers.SharedInformerFactory,
func registerInformers(options *options.YurtHubOptions,
informerFactory informers.SharedInformerFactory,
yurtInformerFactory yurtinformers.SharedInformerFactory,
workingMode util.WorkingMode,
serviceTopologyFilterEnabled bool,
nodePoolName, nodeName string,
tenantNs string) {
// skip construct node/nodePool informers if service topology filter disabled
serviceTopologyFilterEnabled := isServiceTopologyFilterEnabled(options)
if serviceTopologyFilterEnabled {
if workingMode == util.WorkingModeCloud {
newNodeInformer := func(client kubernetes.Interface, resyncPeriod time.Duration) cache.SharedIndexInformer {
tweakListOptions := func(options *metav1.ListOptions) {
options.FieldSelector = fields.Set{"metadata.name": nodeName}.String()
tweakListOptions := func(ops *metav1.ListOptions) {
ops.FieldSelector = fields.Set{"metadata.name": options.NodeName}.String()
}
return coreinformers.NewFilteredNodeInformer(client, resyncPeriod, nil, tweakListOptions)
}
informerFactory.InformerFor(&corev1.Node{}, newNodeInformer)
}

if len(nodePoolName) != 0 {
if len(options.NodePoolName) != 0 {
newNodePoolInformer := func(client yurtclientset.Interface, resyncPeriod time.Duration) cache.SharedIndexInformer {
tweakListOptions := func(options *metav1.ListOptions) {
options.FieldSelector = fields.Set{"metadata.name": nodePoolName}.String()
tweakListOptions := func(ops *metav1.ListOptions) {
ops.FieldSelector = fields.Set{"metadata.name": options.NodePoolName}.String()
}
return yurtv1alpha1.NewFilteredNodePoolInformer(client, resyncPeriod, nil, tweakListOptions)
}
Expand All @@ -294,7 +296,7 @@ func registerInformers(informerFactory informers.SharedInformerFactory,
tweakListOptions := func(options *metav1.ListOptions) {
options.FieldSelector = fields.Set{"metadata.name": util.YurthubConfigMapName}.String()
}
return coreinformers.NewFilteredConfigMapInformer(client, util.YurtHubNamespace, resyncPeriod, nil, tweakListOptions)
return coreinformers.NewFilteredConfigMapInformer(client, options.YurtHubNamespace, resyncPeriod, nil, tweakListOptions)
}
informerFactory.InformerFor(&corev1.ConfigMap{}, newConfigmapInformer)

Expand All @@ -308,8 +310,8 @@ func registerInformers(informerFactory informers.SharedInformerFactory,

}

// serviceTopologyFilterEnabled is used to verify the service topology filter should be enabled or not.
func serviceTopologyFilterEnabled(options *options.YurtHubOptions) bool {
// isServiceTopologyFilterEnabled is used to verify the service topology filter should be enabled or not.
func isServiceTopologyFilterEnabled(options *options.YurtHubOptions) bool {
if !options.EnableResourceFilter {
return false
}
Expand Down
3 changes: 3 additions & 0 deletions cmd/yurthub/app/options/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ type YurtHubOptions struct {
YurtHubPort int
YurtHubProxyPort int
YurtHubProxySecurePort int
YurtHubNamespace string
GCFrequency int
YurtHubCertOrganizations []string
NodeName string
Expand Down Expand Up @@ -95,6 +96,7 @@ func NewYurtHubOptions() *YurtHubOptions {
YurtHubProxyPort: util.YurtHubProxyPort,
YurtHubPort: util.YurtHubPort,
YurtHubProxySecurePort: util.YurtHubProxySecurePort,
YurtHubNamespace: util.YurtHubNamespace,
GCFrequency: 120,
YurtHubCertOrganizations: make([]string, 0),
LBMode: "rr",
Expand Down Expand Up @@ -174,6 +176,7 @@ func (o *YurtHubOptions) AddFlags(fs *pflag.FlagSet) {
fs.StringVar(&o.YurtHubProxyHost, "bind-proxy-address", o.YurtHubProxyHost, "the IP address of YurtHub Proxy Server")
fs.IntVar(&o.YurtHubProxyPort, "proxy-port", o.YurtHubProxyPort, "the port on which to proxy HTTP requests to kube-apiserver")
fs.IntVar(&o.YurtHubProxySecurePort, "proxy-secure-port", o.YurtHubProxySecurePort, "the port on which to proxy HTTPS requests to kube-apiserver")
fs.StringVar(&o.YurtHubNamespace, "namespace", o.YurtHubNamespace, "the namespace of YurtHub Server")
fs.StringVar(&o.ServerAddr, "server-addr", o.ServerAddr, "the address of Kubernetes kube-apiserver,the format is: \"server1,server2,...\"")
fs.StringSliceVar(&o.YurtHubCertOrganizations, "hub-cert-organizations", o.YurtHubCertOrganizations, "Organizations that will be added into hub's apiserver client certificate, the format is: certOrg1,certOrg2,...")
fs.IntVar(&o.GCFrequency, "gc-frequency", o.GCFrequency, "the frequency to gc cache in storage(unit: minute).")
Expand Down
1 change: 1 addition & 0 deletions cmd/yurthub/app/options/options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ func TestNewYurtHubOptions(t *testing.T) {
YurtHubProxyPort: util.YurtHubProxyPort,
YurtHubPort: util.YurtHubPort,
YurtHubProxySecurePort: util.YurtHubProxySecurePort,
YurtHubNamespace: util.YurtHubNamespace,
GCFrequency: 120,
YurtHubCertOrganizations: make([]string, 0),
LBMode: "rr",
Expand Down
2 changes: 1 addition & 1 deletion cmd/yurthub/app/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ func coordinatorRun(ctx context.Context,
var coordinatorServiceUrl *url.URL

go func() {
coorCertManager, err := coordinatorcertmgr.NewCertManager(cfg.CoordinatorPKIDir, cfg.ProxiedClient, cfg.SharedFactory)
coorCertManager, err := coordinatorcertmgr.NewCertManager(cfg.CoordinatorPKIDir, cfg.YurtHubNamespace, cfg.ProxiedClient, cfg.SharedFactory)
close(coordinatorInformerRegistryChan) // notify the coordinator secret informer registry event
if err != nil {
klog.Errorf("coordinator failed to create coordinator cert manager, %v", err)
Expand Down
4 changes: 2 additions & 2 deletions pkg/yurthub/poolcoordinator/certmanager/certmanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ var certFileNames = map[CertFileType]string{
NodeLeaseProxyClientKey: "node-lease-proxy-client.key",
}

func NewCertManager(pkiDir string, yurtClient kubernetes.Interface, informerFactory informers.SharedInformerFactory) (*CertManager, error) {
func NewCertManager(pkiDir, yurtHubNs string, yurtClient kubernetes.Interface, informerFactory informers.SharedInformerFactory) (*CertManager, error) {
store := fs.FileSystemOperator{}
if err := store.CreateDir(pkiDir); err != nil && err != fs.ErrExists {
return nil, fmt.Errorf("failed to create dir %s, %v", pkiDir, err)
Expand All @@ -69,7 +69,7 @@ func NewCertManager(pkiDir string, yurtClient kubernetes.Interface, informerFact
tweakListOptions := func(options *metav1.ListOptions) {
options.FieldSelector = fields.Set{"metadata.name": constants.PoolCoordinatorClientSecretName}.String()
}
return coreinformers.NewFilteredSecretInformer(yurtClient, constants.PoolCoordinatorClientSecretNamespace, 0, nil, tweakListOptions)
return coreinformers.NewFilteredSecretInformer(yurtClient, yurtHubNs, 0, nil, tweakListOptions)
}
secretInformer := informerFactory.InformerFor(&corev1.Secret{}, secretInformerFunc)
secretInformer.AddEventHandler(cache.ResourceEventHandlerFuncs{
Expand Down
5 changes: 3 additions & 2 deletions pkg/yurthub/poolcoordinator/certmanager/certmanager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import (
"k8s.io/client-go/kubernetes/fake"

"github.com/openyurtio/openyurt/pkg/yurthub/poolcoordinator/constants"
"github.com/openyurtio/openyurt/pkg/yurthub/util"
"github.com/openyurtio/openyurt/pkg/yurthub/util/fs"
)

Expand Down Expand Up @@ -192,7 +193,7 @@ var (
poolCoordinatorSecret = &corev1.Secret{
ObjectMeta: v1.ObjectMeta{
Name: constants.PoolCoordinatorClientSecretName,
Namespace: constants.PoolCoordinatorClientSecretNamespace,
Namespace: util.YurtHubNamespace,
},
TypeMeta: v1.TypeMeta{
Kind: "Secret",
Expand Down Expand Up @@ -623,7 +624,7 @@ func TestCreateOrUpdateFile(t *testing.T) {
func initFakeClientAndCertManager() (*fake.Clientset, *CertManager, func(), error) {
fakeClientSet := fake.NewSimpleClientset()
fakeInformerFactory := informers.NewSharedInformerFactory(fakeClientSet, 0)
certMgr, err := NewCertManager(testPKIDir, fakeClientSet, fakeInformerFactory)
certMgr, err := NewCertManager(testPKIDir, util.YurtHubNamespace, fakeClientSet, fakeInformerFactory)
if err != nil {
return nil, nil, nil, fmt.Errorf("failed to create cert manager, %v", err)
}
Expand Down
5 changes: 2 additions & 3 deletions pkg/yurthub/poolcoordinator/constants/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ var (
)

const (
DefaultPoolScopedUserAgent = "leader-yurthub"
PoolCoordinatorClientSecretName = "pool-coordinator-yurthub-certs"
PoolCoordinatorClientSecretNamespace = "kube-system"
DefaultPoolScopedUserAgent = "leader-yurthub"
PoolCoordinatorClientSecretName = "pool-coordinator-yurthub-certs"
)

0 comments on commit 83e8526

Please sign in to comment.