From f6b7bc973b05441b36609cf2964ba8b7bdaac06f Mon Sep 17 00:00:00 2001 From: joshvanl Date: Sat, 31 Jul 2021 20:28:41 +0100 Subject: [PATCH] Adds /cmd/ctl/pkg/factory which registers Kube CLI flags, and provides clients/config needed Signed-off-by: joshvanl --- cmd/ctl/pkg/factory/BUILD.bazel | 31 +++++++++ cmd/ctl/pkg/factory/factory.go | 110 ++++++++++++++++++++++++++++++++ 2 files changed, 141 insertions(+) create mode 100644 cmd/ctl/pkg/factory/BUILD.bazel create mode 100644 cmd/ctl/pkg/factory/factory.go diff --git a/cmd/ctl/pkg/factory/BUILD.bazel b/cmd/ctl/pkg/factory/BUILD.bazel new file mode 100644 index 000000000..fead217dd --- /dev/null +++ b/cmd/ctl/pkg/factory/BUILD.bazel @@ -0,0 +1,31 @@ +load("@io_bazel_rules_go//go:def.bzl", "go_library") + +go_library( + name = "go_default_library", + srcs = ["factory.go"], + importpath = "github.com/jetstack/cert-manager/cmd/ctl/pkg/factory", + visibility = ["//visibility:public"], + deps = [ + "//pkg/client/clientset/versioned:go_default_library", + "@com_github_spf13_cobra//:go_default_library", + "@io_k8s_cli_runtime//pkg/genericclioptions:go_default_library", + "@io_k8s_client_go//kubernetes:go_default_library", + "@io_k8s_client_go//plugin/pkg/client/auth:go_default_library", + "@io_k8s_client_go//rest:go_default_library", + "@io_k8s_kubectl//pkg/cmd/util:go_default_library", + ], +) + +filegroup( + name = "package-srcs", + srcs = glob(["**"]), + tags = ["automanaged"], + visibility = ["//visibility:private"], +) + +filegroup( + name = "all-srcs", + srcs = [":package-srcs"], + tags = ["automanaged"], + visibility = ["//visibility:public"], +) diff --git a/cmd/ctl/pkg/factory/factory.go b/cmd/ctl/pkg/factory/factory.go new file mode 100644 index 000000000..5cb37dc97 --- /dev/null +++ b/cmd/ctl/pkg/factory/factory.go @@ -0,0 +1,110 @@ +/* +Copyright 2021 The cert-manager Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package factory + +import ( + "github.com/spf13/cobra" + "k8s.io/cli-runtime/pkg/genericclioptions" + "k8s.io/client-go/kubernetes" + "k8s.io/client-go/rest" + "k8s.io/kubectl/pkg/cmd/util" + + // Load all auth plugins + _ "k8s.io/client-go/plugin/pkg/client/auth" + + cmclient "github.com/jetstack/cert-manager/pkg/client/clientset/versioned" +) + +var ( + kubeConfigFlags = genericclioptions.NewConfigFlags(true) + factory = util.NewFactory(kubeConfigFlags) +) + +// Factory provides a set of clients and configurations to authenticate and +// access a target Kubernetes cluster. Factory will ensure that its fields are +// populated and valid during command execution. +type Factory struct { + // Namespace is the namespace that the user has requested with the + // "--namespace" / "-n" flag. Defaults to "default" if the flag was not + // provided. + Namespace string + + // EnforceNamespace will be true if the user provided the namespace flag. + EnforceNamespace bool + + // RESTConfig is a Kubernetes REST config that contains the user's + // authentication and access configuration. + RESTConfig *rest.Config + + // CMClient is a Kubernetes clientset for interacting with cert-manager APIs. + CMClient cmclient.Interface + + // KubeClient is a Kubernetes clientset for interacting with the base + // Kubernetes APIs. + KubeClient kubernetes.Interface +} + +// New returns a new Factory. The supplied command will have flags registered +// for interacting with the Kubernetes access options. Factory will be +// populated when the command is executed using the cobra PreRun. If a PreRun +// is already defined, it will be executed _after_ Factory has been populated, +// making it available. +func New(cmd *cobra.Command) *Factory { + f := new(Factory) + + kubeConfigFlags.AddFlags(cmd.Flags()) + + // Setup a PreRun to populate the Factory. Catch the existing PreRun command + // if one was defined, and execute it second. + existingPreRun := cmd.PreRun + cmd.PreRun = func(cmd *cobra.Command, args []string) { + util.CheckErr(f.complete()) + if existingPreRun != nil { + existingPreRun(cmd, args) + } + } + + return f +} + +// complete will populate the Factory with values using the shared Kubernetes +// CLI factory. +func (f *Factory) complete() error { + var err error + + f.Namespace, f.EnforceNamespace, err = factory.ToRawKubeConfigLoader().Namespace() + if err != nil { + return err + } + + f.RESTConfig, err = factory.ToRESTConfig() + if err != nil { + return err + } + + f.KubeClient, err = kubernetes.NewForConfig(f.RESTConfig) + if err != nil { + return err + } + + f.CMClient, err = cmclient.NewForConfig(f.RESTConfig) + if err != nil { + return err + } + + return nil +}