From 6b74cd1d6b3bacce64d2908a50f8ad1438d62210 Mon Sep 17 00:00:00 2001 From: Jose Diaz-Gonzalez Date: Wed, 6 Mar 2024 00:33:45 -0500 Subject: [PATCH] feat: implement autoscaling-auth:report --- docs/deployment/schedulers/k3s.md | 18 +++++----- plugins/scheduler-k3s/Makefile | 2 +- plugins/scheduler-k3s/report.go | 34 +++++++++++++++++++ .../src/subcommands/subcommands.go | 8 +++++ plugins/scheduler-k3s/subcommands.go | 23 +++++++++++++ 5 files changed, 76 insertions(+), 9 deletions(-) diff --git a/docs/deployment/schedulers/k3s.md b/docs/deployment/schedulers/k3s.md index 6e0d33519..62b694dbc 100644 --- a/docs/deployment/schedulers/k3s.md +++ b/docs/deployment/schedulers/k3s.md @@ -6,6 +6,7 @@ ``` scheduler-k3s:annotations:set () [--process-type PROCESS_TYPE] <--resource-type RESOURCE_TYPE>, Set or clear an annotation for a given app/process-type/resource-type combination scheduler-k3s:autoscaling-auth:set [<--metadata key=value>...], Set or clear a scheduler-k3s autoscaling keda trigger authentication resource for an app +scheduler-k3s:autoscaling-auth:report [--format stdout|json] [--include-metadata] # Displays a scheduler-k3s autoscaling auth report for an app scheduler-k3s:cluster-add [ssh://user@host:port] # Adds a server node to a Dokku-managed cluster scheduler-k3s:cluster-list # Lists all nodes in a Dokku-managed cluster scheduler-k3s:cluster-remove [node-id] # Removes client node to a Dokku-managed cluster @@ -433,7 +434,7 @@ If the `--global` flag is specified instead of an app name, a custom helm chart ##### Removing Authentication Resources -To remove a configured authenticatin resource, run the `scheduler-k3s:autoscaling-auth:set` command with no parameters specified. Subsequent deploys will not include these resources. +To remove a configured authenticatin resource, run the `scheduler-k3s:autoscaling-auth:set` command with no metadata specified. Subsequent deploys will not include these resources. ```shell dokku scheduler-k3s:autoscaling-auth:set $APP $TRIGGER_TYPE @@ -444,7 +445,7 @@ dokku scheduler-k3s:autoscaling-auth:set $APP $TRIGGER_TYPE To see a list of authentication resources managed by Dokku, run the `scheduler-k3s:autoscaling-auth:report` command. ```shell -dokku scheduler-k3s:autoscaling-auth:report $APP +dokku scheduler-k3s:autoscaling-auth:report node-js-app ``` ``` @@ -452,17 +453,18 @@ dokku scheduler-k3s:autoscaling-auth:report $APP datadog: configured ``` -By default, the report will not display configured parameters - making it safe to include in Dokku report output. To include parameters, add the `--include-parameters` flag: +By default, the report will not display configured metadata - making it safe to include in Dokku report output. To include metadata and their values, add the `--include-metadata` flag: ```shell -dokku scheduler-k3s:autoscaling-auth:report $APP --include-parameters +dokku scheduler-k3s:autoscaling-auth:report node-js-app --include-metadata ``` ``` -====> $APP autoscaling-auth report - datadog: configured - datadog-apiKey: some-api-key - datadog-appKey: some-app-key +====> node-js-app autoscaling-auth report + Datadog: configured + Datadog apiKey: 1234567890 + Datadog appKey: asdfghjkl + Datadog datadogSite: us5.datadoghq.com ``` ### Using kubectl remotely diff --git a/plugins/scheduler-k3s/Makefile b/plugins/scheduler-k3s/Makefile index 0fcac0b5a..0efc49c33 100644 --- a/plugins/scheduler-k3s/Makefile +++ b/plugins/scheduler-k3s/Makefile @@ -1,4 +1,4 @@ -SUBCOMMANDS = subcommands/annotations:set subcommands/autoscaling-auth:set subcommands/cluster-add subcommands/cluster-list subcommands/cluster-remove subcommands/initialize subcommands/labels:set subcommands/report subcommands/set subcommands/show-kubeconfig subcommands/uninstall +SUBCOMMANDS = subcommands/annotations:set subcommands/autoscaling-auth:set subcommands/autoscaling-auth:report subcommands/cluster-add subcommands/cluster-list subcommands/cluster-remove subcommands/initialize subcommands/labels:set subcommands/report subcommands/set subcommands/show-kubeconfig subcommands/uninstall TRIGGERS = triggers/install triggers/post-app-clone-setup triggers/post-app-rename-setup triggers/post-delete triggers/report triggers/scheduler-deploy triggers/scheduler-enter triggers/scheduler-logs triggers/scheduler-post-delete triggers/scheduler-run triggers/scheduler-run-list triggers/scheduler-stop BUILD = commands subcommands triggers PLUGIN_NAME = scheduler-k3s diff --git a/plugins/scheduler-k3s/report.go b/plugins/scheduler-k3s/report.go index 2e25445df..fd388c067 100644 --- a/plugins/scheduler-k3s/report.go +++ b/plugins/scheduler-k3s/report.go @@ -1,6 +1,9 @@ package scheduler_k3s import ( + "fmt" + "strings" + "github.com/dokku/dokku/plugins/common" ) @@ -45,6 +48,37 @@ func ReportSingleApp(appName string, format string, infoFlag string) error { return common.ReportSingleApp("scheduler-k3s", appName, infoFlag, infoFlags, flagKeys, format, trimPrefix, uppercaseFirstCharacter) } +// ReportAutoscalingAuthSingleApp is an internal function that displays the scheduler-k3s autoscaling-auth report for one app +func ReportAutoscalingAuthSingleApp(appName string, format string, includeMetadata bool) error { + properties, err := common.PropertyGetAllByPrefix("scheduler-k3s", appName, TriggerAuthPropertyPrefix) + if err != nil { + return fmt.Errorf("Unable to get property list: %w", err) + } + + infoFlags := map[string]string{} + for key, value := range properties { + metadataKey := strings.TrimPrefix(key, TriggerAuthPropertyPrefix) + authType := strings.SplitN(metadataKey, ".", 2)[0] + if authType == "" { + continue + } + + infoFlags[authType] = "configured" + if includeMetadata { + infoFlags[strings.ReplaceAll(metadataKey, ".", "-")] = value + } + } + + flagKeys := []string{} + for flagKey := range infoFlags { + flagKeys = append(flagKeys, flagKey) + } + + trimPrefix := false + uppercaseFirstCharacter := true + return common.ReportSingleApp("scheduler-k3s", appName, "", infoFlags, flagKeys, format, trimPrefix, uppercaseFirstCharacter) +} + func reportComputedDeployTimeout(appName string) string { return getComputedDeployTimeout(appName) } diff --git a/plugins/scheduler-k3s/src/subcommands/subcommands.go b/plugins/scheduler-k3s/src/subcommands/subcommands.go index 5aaa8b0c6..883dc4a56 100644 --- a/plugins/scheduler-k3s/src/subcommands/subcommands.go +++ b/plugins/scheduler-k3s/src/subcommands/subcommands.go @@ -46,6 +46,14 @@ func main() { trigger = args.Arg(0) } err = scheduler_k3s.CommandAutoscalingAuthSet(appName, trigger, *metadata, *global) + case "autoscaling-auth:report": + args := flag.NewFlagSet("scheduler-k3s:autoscaling-auth:report", flag.ExitOnError) + global := args.Bool("global", false, "--global: show a global report") + includeMetadata := args.Bool("include-metadata", false, "--include-metadata: include metadata in the report") + format := args.String("format", "stdout", "format: [ stdout | json ]") + args.Parse(os.Args[2:]) + appName := args.Arg(0) + err = scheduler_k3s.CommandAutoscalingAuthReport(appName, *format, *global, *includeMetadata) case "cluster-add": args := flag.NewFlagSet("scheduler-k3s:cluster-add", flag.ExitOnError) allowUknownHosts := args.Bool("insecure-allow-unknown-hosts", false, "insecure-allow-unknown-hosts: allow unknown hosts") diff --git a/plugins/scheduler-k3s/subcommands.go b/plugins/scheduler-k3s/subcommands.go index 315c70bc9..2fbd83624 100644 --- a/plugins/scheduler-k3s/subcommands.go +++ b/plugins/scheduler-k3s/subcommands.go @@ -86,6 +86,29 @@ func CommandAutoscalingAuthSet(appName string, trigger string, metadata map[stri return nil } +// CommandAutoscalingAuthReport displays a scheduler-k3s autoscaling keda trigger authentication report for one or more apps +func CommandAutoscalingAuthReport(appName string, format string, global bool, includeMetadata bool) error { + if len(appName) == 0 && !global { + return fmt.Errorf("Missing required app name or --global flag") + } + + if len(appName) > 0 && global { + return fmt.Errorf("Cannot specify both app name and --global flag") + } + + if !global { + if err := common.VerifyAppName(appName); err != nil { + return err + } + } + + if len(appName) > 0 { + return ReportAutoscalingAuthSingleApp(appName, format, includeMetadata) + } + + return ReportAutoscalingAuthSingleApp("--global", format, includeMetadata) +} + // CommandInitialize initializes a k3s cluster on the local server func CommandInitialize(ingressClass string, serverIP string, taintScheduling bool) error { if ingressClass != "nginx" && ingressClass != "traefik" {