Skip to content

Commit

Permalink
feat: create client from kubeconfig if flag is set
Browse files Browse the repository at this point in the history
Signed-off-by: Carlos Salas <carlos.salas@suse.com>
  • Loading branch information
salasberryfin committed Aug 31, 2023
1 parent f4419ec commit 301ed9b
Showing 1 changed file with 44 additions and 1 deletion.
45 changes: 44 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ import (
"k8s.io/apimachinery/pkg/runtime"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
clientcmdapi "k8s.io/client-go/tools/clientcmd/api"
"k8s.io/client-go/tools/leaderelection/resourcelock"
"k8s.io/component-base/version"
"k8s.io/klog/v2"
Expand Down Expand Up @@ -57,6 +60,7 @@ var (
syncPeriod time.Duration
healthAddr string
concurrencyNumber int
rancherKubeconfig string
)

func init() {
Expand Down Expand Up @@ -97,6 +101,9 @@ func initFlags(fs *pflag.FlagSet) {

fs.IntVar(&concurrencyNumber, "concurrency", 1,
"Number of resources to process simultaneously")

fs.StringVar(&rancherKubeconfig, "rancher-kubeconfig", "",
"Path to a kubeconfig file. Only required if out-of-cluster.")
}

func main() {
Expand Down Expand Up @@ -154,12 +161,48 @@ func setupChecks(mgr ctrl.Manager) {
}
}

// setupReconcilers can either create a client for an in-cluster installation (rancher and rancher-turtles in the same cluster)
// or create a client for an out-of-cluster installation (rancher and rancher-turtles in different clusters).
func setupReconcilers(ctx context.Context, mgr ctrl.Manager) {
var c client.Client

if len(rancherKubeconfig) > 0 {
setupLog.Info("out-of-cluster installation of rancher-turtles", "using kubeconfig from path", rancherKubeconfig)

restConfig, err := loadConfigWithContext("", &clientcmd.ClientConfigLoadingRules{ExplicitPath: rancherKubeconfig}, "")
if err != nil {
setupLog.Error(err, "unable to load kubeconfig from file")
os.Exit(1)
}

c, err = client.New(restConfig, client.Options{Scheme: mgr.GetClient().Scheme()})

if err != nil {
setupLog.Error(err, "failed to create client")
os.Exit(1)
}
} else {
setupLog.Info("in-cluster installation of rancher-turtles")
c = mgr.GetClient()
}

if err := (&controllers.CAPIImportReconciler{
Client: mgr.GetClient(),
Client: c,
WatchFilterValue: watchFilterValue,
}).SetupWithManager(ctx, mgr, controller.Options{MaxConcurrentReconciles: concurrencyNumber}); err != nil {
setupLog.Error(err, "unable to create capi controller")
os.Exit(1)
}
}

// loadConfigWithContext loads a REST Config from a path using a logic similar to the one used in controller-runtime.
func loadConfigWithContext(apiServerURL string, loader clientcmd.ClientConfigLoader, context string) (*rest.Config, error) {
return clientcmd.NewNonInteractiveDeferredLoadingClientConfig(
loader,
&clientcmd.ConfigOverrides{
ClusterInfo: clientcmdapi.Cluster{
Server: apiServerURL,
},
CurrentContext: context,
}).ClientConfig()
}

0 comments on commit 301ed9b

Please sign in to comment.