Skip to content
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

Add generic client support #126

Merged
merged 1 commit into from
Jan 16, 2021
Merged
Show file tree
Hide file tree
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
16 changes: 2 additions & 14 deletions cmd/controller-manager/app/controllermanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,36 +6,24 @@ import (
"os"

"github.com/spf13/cobra"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/dynamic"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/kubernetes/scheme"
"k8s.io/component-base/logs"
"k8s.io/klog/v2"
"sigs.k8s.io/controller-runtime"

"github.com/karmada-io/karmada/cmd/controller-manager/app/options"
memberclusterv1alpha1 "github.com/karmada-io/karmada/pkg/apis/membercluster/v1alpha1"
propagationv1alpha1 "github.com/karmada-io/karmada/pkg/apis/propagationstrategy/v1alpha1"
"github.com/karmada-io/karmada/pkg/controllers/binding"
"github.com/karmada-io/karmada/pkg/controllers/execution"
"github.com/karmada-io/karmada/pkg/controllers/membercluster"
"github.com/karmada-io/karmada/pkg/controllers/policy"
"github.com/karmada-io/karmada/pkg/controllers/status"
karmadaclientset "github.com/karmada-io/karmada/pkg/generated/clientset/versioned"
"github.com/karmada-io/karmada/pkg/util/gclient"
"github.com/karmada-io/karmada/pkg/util/informermanager"
"github.com/karmada-io/karmada/pkg/util/objectwatcher"
)

// aggregatedScheme aggregates all Kubernetes and extended schemes used by controllers.
var aggregatedScheme = runtime.NewScheme()

func init() {
var _ = scheme.AddToScheme(aggregatedScheme) // add Kubernetes schemes
var _ = propagationv1alpha1.AddToScheme(aggregatedScheme) // add propagation schemes
var _ = memberclusterv1alpha1.AddToScheme(aggregatedScheme) // add membercluster schemes
}

// NewControllerManagerCommand creates a *cobra.Command object with default parameters
func NewControllerManagerCommand(stopChan <-chan struct{}) *cobra.Command {
opts := options.NewOptions()
Expand Down Expand Up @@ -66,7 +54,7 @@ func Run(opts *options.Options, stopChan <-chan struct{}) error {
panic(err)
}
controllerManager, err := controllerruntime.NewManager(config, controllerruntime.Options{
Scheme: aggregatedScheme,
Scheme: gclient.NewSchema(),
LeaderElection: true, // TODO(RainbowMango): Add a flag '--enable-leader-election' for this option.
LeaderElectionID: "41db11fa.karmada.io",
})
Expand Down
42 changes: 42 additions & 0 deletions pkg/util/gclient/gclient.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package gclient

import (
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/kubernetes/scheme"
"k8s.io/client-go/rest"
"sigs.k8s.io/controller-runtime/pkg/client"

memberclusterv1alpha1 "github.com/karmada-io/karmada/pkg/apis/membercluster/v1alpha1"
propagationv1alpha1 "github.com/karmada-io/karmada/pkg/apis/propagationstrategy/v1alpha1"
)

// aggregatedScheme aggregates Kubernetes and extended schemes.
var aggregatedScheme = runtime.NewScheme()

func init() {
var _ = scheme.AddToScheme(aggregatedScheme) // add Kubernetes schemes
var _ = propagationv1alpha1.AddToScheme(aggregatedScheme) // add propagation schemes
var _ = memberclusterv1alpha1.AddToScheme(aggregatedScheme) // add membercluster schemes
}

// NewSchema returns a singleton schema set which aggregated Kubernetes's schemes and extended schemes.
func NewSchema() *runtime.Scheme {
return aggregatedScheme
}

// NewForConfig creates a new client for the given config.
func NewForConfig(config *rest.Config) (client.Client, error) {
return client.New(config, client.Options{
Scheme: aggregatedScheme,
})
}

// NewForConfigOrDie creates a new client for the given config and
// panics if there is an error in the config.
func NewForConfigOrDie(config *rest.Config) client.Client {
c, err := NewForConfig(config)
if err != nil {
panic(err)
}
return c
}