From e6f33bfae73571c8c76638658a18bea98da18045 Mon Sep 17 00:00:00 2001 From: izturn <44051386+izturn@users.noreply.github.com> Date: Thu, 5 Oct 2023 03:29:44 +0800 Subject: [PATCH] gateway provisioner: add flags to enable running provisioner out of cluster (#5686) Adds --incluster and --kubeconfig flags to the gateway provisioner to enable running outside of the cluster. Signed-off-by: gang.liu --- changelogs/unreleased/5686-izturn-small.md | 1 + cmd/contour/gatewayprovisioner.go | 26 +++++++++++++++++++--- 2 files changed, 24 insertions(+), 3 deletions(-) create mode 100644 changelogs/unreleased/5686-izturn-small.md diff --git a/changelogs/unreleased/5686-izturn-small.md b/changelogs/unreleased/5686-izturn-small.md new file mode 100644 index 00000000000..7e36c60111d --- /dev/null +++ b/changelogs/unreleased/5686-izturn-small.md @@ -0,0 +1 @@ +Add flags: `--incluster`, `--kubeconfig` for enable run the `gateway-provisioner` in or out of the cluster. \ No newline at end of file diff --git a/cmd/contour/gatewayprovisioner.go b/cmd/contour/gatewayprovisioner.go index bf5c1fb3a42..873f74ae948 100644 --- a/cmd/contour/gatewayprovisioner.go +++ b/cmd/contour/gatewayprovisioner.go @@ -17,11 +17,13 @@ import ( "fmt" "os" - "github.com/alecthomas/kingpin/v2" - "github.com/novln/docker-parser/distribution/reference" + "github.com/projectcontour/contour/internal/k8s" "github.com/projectcontour/contour/internal/provisioner" "github.com/projectcontour/contour/internal/provisioner/controller" "github.com/projectcontour/contour/pkg/config" + + "github.com/alecthomas/kingpin/v2" + "github.com/novln/docker-parser/distribution/reference" _ "k8s.io/client-go/plugin/pkg/client/auth/gcp" "k8s.io/client-go/rest" ctrl "sigs.k8s.io/controller-runtime" @@ -56,6 +58,13 @@ func registerGatewayProvisioner(app *kingpin.Application) (*kingpin.CmdClause, * Default(provisionerConfig.gatewayControllerName). StringVar(&provisionerConfig.gatewayControllerName) + cmd.Flag("incluster", "Use in cluster configuration."). + Default("true"). + BoolVar(&provisionerConfig.inCluster) + cmd.Flag("kubeconfig", "Path to kubeconfig (if not in running inside a cluster)."). + PlaceHolder("/path/to/file"). + StringVar(&provisionerConfig.kubeconfig) + cmd.Flag("leader-election-namespace", "The namespace in which the leader election resource will be created."). Default(config.GetenvOr("CONTOUR_PROVISIONER_NAMESPACE", "projectcontour")). StringVar(&provisionerConfig.leaderElectionNamespace) @@ -95,6 +104,10 @@ type gatewayProvisionerConfig struct { // gatewayControllerName defines the controller string that this gateway provisioner instance // will process GatewayClasses and Gateways for. gatewayControllerName string + + // Kubernetes client parameters. + inCluster bool + kubeconfig string } func runGatewayProvisioner(config *gatewayProvisionerConfig) { @@ -111,7 +124,14 @@ func runGatewayProvisioner(config *gatewayProvisionerConfig) { setupLog.Info("using contour", "image", config.contourImage) setupLog.Info("using envoy", "image", config.envoyImage) - mgr, err := createManager(ctrl.GetConfigOrDie(), config) + // Establish k8s core client connection. + restConfig, err := k8s.NewRestConfig(config.kubeconfig, config.inCluster) + if err != nil { + setupLog.Error(err, "failed to create REST config for Kubernetes clients") + os.Exit(1) + } + + mgr, err := createManager(restConfig, config) if err != nil { setupLog.Error(err, "failed to create contour gateway provisioner") os.Exit(1)