Skip to content

Make MapperProvider multi-cluster by default #12

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 1, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 21 additions & 3 deletions pkg/kcp/wrappers.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,21 @@ package kcp

import (
"net/http"
"strings"

"k8s.io/apimachinery/pkg/api/meta"
"k8s.io/client-go/rest"
k8scache "k8s.io/client-go/tools/cache"

kcpcache "github.com/kcp-dev/apimachinery/pkg/cache"
kcpclient "github.com/kcp-dev/apimachinery/pkg/client"

ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/cache"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/client/apiutil"
"sigs.k8s.io/controller-runtime/pkg/cluster"
"sigs.k8s.io/controller-runtime/pkg/manager"

kcpcache "github.com/kcp-dev/apimachinery/pkg/cache"
kcpclient "github.com/kcp-dev/apimachinery/pkg/client"
)

// NewClusterAwareManager returns a kcp-aware manager with appropriate defaults for cache and
Expand All @@ -38,10 +41,15 @@ func NewClusterAwareManager(cfg *rest.Config, options ctrl.Options) (manager.Man
if options.NewCache == nil {
options.NewCache = NewClusterAwareCache
}

if options.NewClient == nil {
options.NewClient = NewClusterAwareClient
}

if options.MapperProvider == nil {
options.MapperProvider = NewClusterAwareMapperProvider
}

return ctrl.NewManager(cfg, options)
}

Expand Down Expand Up @@ -89,3 +97,13 @@ func ClusterAwareHTTPClient(config *rest.Config) (*http.Client, error) {
httpClient.Transport = kcpclient.NewClusterRoundTripper(httpClient.Transport)
return httpClient, nil
}

// NewClusterAwareMapperProvider is a MapperProvider that returns a logical cluster aware meta.RESTMapper.
func NewClusterAwareMapperProvider(c *rest.Config) (meta.RESTMapper, error) {
mapperCfg := rest.CopyConfig(c)
if !strings.HasSuffix(mapperCfg.Host, "/clusters/*") {
mapperCfg.Host += "/clusters/*"
}

return apiutil.NewDynamicRESTMapper(mapperCfg)
}