
* images/kube-webhook-certgen/rootfs/pkg/k8s: return err from functions Initially only from some to preserve existing behavior. Signed-off-by: Mateusz Gozdek <mgozdek@microsoft.com> * images/kube-webhook-certgen/rootfs: make patching return error So we don't call log.Fatal in so many places, which makes code testable. Signed-off-by: Mateusz Gozdek <mgozdek@microsoft.com> * images/kube-webhook-certgen/rootfs/pkg/k8s: require context So initialize top-level contexts in tests and CLI, then pass them around all the way down, so there is an ability e.g. to add timeouts to patch operations, if needed and to follow general conventions. Signed-off-by: Mateusz Gozdek <mgozdek@microsoft.com> * images/kube-webhook-certgen/rootfs/pkg/k8s: support patching APIService APIService object is very similar to MutatingWebhookConfiguration and ValidatingWebhookConfiguration objects, so support for patching it shouldn't be too much of a burden. Signed-off-by: Mateusz Gozdek <mgozdek@microsoft.com> * images/kube-webhook-certgen/rootfs/cmd: use new patch API So old function PatchWebhookConfigurations can be unexported and CLI can be extended to also support patching APIService. Signed-off-by: Mateusz Gozdek <mgozdek@microsoft.com> * images/kube-webhook-certgen/rootfs/pkg/k8s: unexport old patch function PatchObjects should be now used instead. Signed-off-by: Mateusz Gozdek <mgozdek@microsoft.com> * images/kube-webhook-certgen/rootfs: add .gitignore To ignore manually built binaries during development process. Signed-off-by: Mateusz Gozdek <mgozdek@microsoft.com> * images/kube-webhook-certgen/rootfs/cmd: test patching By adding a PatchConfig and Patch function, it is now possible to test logic of flag validation, which was previously tied to CLI options. This commit adds nice set of tests covering existing logic. Signed-off-by: Mateusz Gozdek <mgozdek@microsoft.com> * images/kube-webhook-certgen/rootfs/cmd: improve formatting Those strings will be changed anyway in future commits, so at first we can properly capitalize used names. Signed-off-by: Mateusz Gozdek <mgozdek@microsoft.com> * images/kube-webhook-certgen/rootfs/cmd: support patching APIService As logic for creating a CA certificate and patching an object is almost the same for both webhook configuration and API services, this commit adds support to kube-webhook-certgen CLI to also patch APIService objects, so they can be served over TLS as well. Signed-off-by: Mateusz Gozdek <mgozdek@microsoft.com> * images/kube-webhook-certgen/rootfs: pass failure policy by value k8s.k8s.patchWebhookConfigurations() always dereferences it and we do not do a nil check, so the code may panic in some conditions, so it's safer to just pass it by value, as it's just a wrapped string. Signed-off-by: Mateusz Gozdek <mgozdek@microsoft.com>
103 lines
2.7 KiB
Go
103 lines
2.7 KiB
Go
package cmd
|
|
|
|
import (
|
|
"os"
|
|
|
|
"github.com/onrik/logrus/filename"
|
|
log "github.com/sirupsen/logrus"
|
|
"github.com/spf13/cobra"
|
|
"k8s.io/client-go/kubernetes"
|
|
"k8s.io/client-go/tools/clientcmd"
|
|
"k8s.io/kube-aggregator/pkg/client/clientset_generated/clientset"
|
|
)
|
|
|
|
var (
|
|
rootCmd = &cobra.Command{
|
|
Use: "kube-webhook-certgen",
|
|
Short: "Create certificates and patch them to admission hooks",
|
|
Long: `Use this to create a ca and signed certificates and patch admission webhooks to allow for quick
|
|
installation and configuration of validating and admission webhooks.`,
|
|
PreRun: configureLogging,
|
|
Run: rootCommand,
|
|
}
|
|
|
|
cfg = struct {
|
|
logLevel string
|
|
logfmt string
|
|
secretName string
|
|
namespace string
|
|
certName string
|
|
keyName string
|
|
host string
|
|
apiServiceName string
|
|
webhookName string
|
|
patchValidating bool
|
|
patchMutating bool
|
|
patchFailurePolicy string
|
|
kubeconfig string
|
|
}{}
|
|
)
|
|
|
|
// Execute is the main entry point for the program
|
|
func Execute() {
|
|
if err := rootCmd.Execute(); err != nil {
|
|
os.Exit(1)
|
|
}
|
|
}
|
|
|
|
func init() {
|
|
filenameHook := filename.NewHook()
|
|
filenameHook.Field = "source"
|
|
log.AddHook(filenameHook)
|
|
log.SetOutput(os.Stdout)
|
|
log.SetLevel(log.TraceLevel)
|
|
rootCmd.Flags()
|
|
rootCmd.PersistentFlags().StringVar(&cfg.logLevel, "log-level", "info", "Log level: panic|fatal|error|warn|info|debug|trace")
|
|
rootCmd.PersistentFlags().StringVar(&cfg.logfmt, "log-format", "json", "Log format: text|json")
|
|
rootCmd.PersistentFlags().StringVar(&cfg.kubeconfig, "kubeconfig", "", "Path to kubeconfig file: e.g. ~/.kube/kind-config-kind")
|
|
}
|
|
|
|
func configureLogging(_ *cobra.Command, _ []string) {
|
|
l, err := log.ParseLevel(cfg.logLevel)
|
|
if err != nil {
|
|
log.WithField("err", err).Fatal("Invalid error level")
|
|
}
|
|
log.SetLevel(l)
|
|
log.SetFormatter(getFormatter(cfg.logfmt))
|
|
}
|
|
|
|
func rootCommand(cmd *cobra.Command, _ []string) {
|
|
cmd.Help()
|
|
os.Exit(1)
|
|
}
|
|
|
|
func getFormatter(logfmt string) log.Formatter {
|
|
switch logfmt {
|
|
case "json":
|
|
return &log.JSONFormatter{}
|
|
case "text":
|
|
return &log.TextFormatter{}
|
|
}
|
|
|
|
log.Fatalf("invalid log format '%s'", logfmt)
|
|
return nil
|
|
}
|
|
|
|
func newKubernetesClients(kubeconfig string) (kubernetes.Interface, clientset.Interface) {
|
|
config, err := clientcmd.BuildConfigFromFlags("", kubeconfig)
|
|
if err != nil {
|
|
log.WithError(err).Fatal("error building kubernetes config")
|
|
}
|
|
|
|
c, err := kubernetes.NewForConfig(config)
|
|
if err != nil {
|
|
log.WithError(err).Fatal("error creating kubernetes client")
|
|
}
|
|
|
|
aggregatorClientset, err := clientset.NewForConfig(config)
|
|
if err != nil {
|
|
log.WithError(err).Fatal("error creating kubernetes aggregator client")
|
|
}
|
|
|
|
return c, aggregatorClientset
|
|
}
|