feat: add scheduler-k3s annotations:report and labels:report

The scheduler-k3s plugin exposes `annotations:set` and `labels:set` but no matching report subcommands, so scripts could not inspect the configured state for idempotent management without reading the property files directly. The new `scheduler-k3s:annotations:report` and `scheduler-k3s:labels:report` surface the configured entries with stdout, JSON, single-flag query, and `--process-type` / `--resource-type` filtering, mirroring the recently added `scheduler-k3s:charts:report`. The literal `GlobalProcessType` value `--global` is rendered as `global` in report keys to avoid leading dashes. The `scheduler-k3s:autoscaling-auth:report` command is updated alongside to loop over every app when no app and no `--global` flag is provided, matching the convention used by `scheduler-k3s:report`.
This commit is contained in:
Jose Diaz-Gonzalez committed 2026-05-30 20:12:00 -04:00
1 parent 4346a18194
commit 5dbb2951bb
10 files changed
+712 -19

No files matched your search

+310
View File
@@ -1,12 +1,24 @@
package scheduler_k3s
import (
"encoding/json"
"fmt"
"sort"
"strings"
"github.com/dokku/dokku/plugins/common"
)
const (
annotationsReportType = "scheduler-k3s-annotations"
labelsReportType = "scheduler-k3s-labels"
// reportProcessTypeGlobal is the rendered form of GlobalProcessType in annotation
// and label report keys (both JSON and single-flag). Real process types cannot
// collide because they cannot contain "--".
reportProcessTypeGlobal = "global"
)
// tokenMask is shown in place of the raw token value in default stdout output.
const tokenMask = "*******"
@@ -161,6 +173,304 @@ func ReportAutoscalingAuthSingleApp(appName string, format string, includeMetada
})
}
// ExtractReportInfoFlag scans arguments for a single info flag whose name starts with
// the given prefix (e.g. "--scheduler-k3s-annotations.") and returns it separately from
// the remaining passthrough arguments. Returns an error if more than one matching flag
// is present. Matching args are removed from the passthrough list so the caller's
// flag.FlagSet sees only its own flags and positional arguments.
func ExtractReportInfoFlag(prefix string, arguments []string) ([]string, string, error) {
passthrough := []string{}
infoFlag := ""
for _, argument := range arguments {
if strings.HasPrefix(argument, prefix) {
if infoFlag != "" {
return nil, "", fmt.Errorf("only a single info flag may be specified")
}
infoFlag = argument
continue
}
passthrough = append(passthrough, argument)
}
return passthrough, infoFlag, nil
}
// annotationsLabelsReportEntry is a parsed (processType, resourceType, key) triple
// produced when scanning the property store for annotations or labels.
type annotationsLabelsReportEntry struct {
ProcessType string
ResourceType string
Key string
Value string
}
// ReportAnnotationsSingleApp displays the scheduler-k3s annotations report for one app.
func ReportAnnotationsSingleApp(appName string, format string, processType string, resourceType string, infoFlag string) error {
if appName != "--global" {
if err := common.VerifyAppName(appName); err != nil {
return err
}
}
entries, err := collectAnnotationsEntries(appName, processType, resourceType)
if err != nil {
return err
}
return renderAnnotationsLabelsReport(annotationsLabelsRenderInput{
AppName: appName,
ReportType: annotationsReportType,
Heading: "annotations",
RowLabel: "Annotation",
Entries: entries,
Format: format,
InfoFlag: infoFlag,
})
}
// ReportLabelsSingleApp displays the scheduler-k3s labels report for one app.
func ReportLabelsSingleApp(appName string, format string, processType string, resourceType string, infoFlag string) error {
if appName != "--global" {
if err := common.VerifyAppName(appName); err != nil {
return err
}
}
entries, err := collectLabelsEntries(appName, processType, resourceType)
if err != nil {
return err
}
return renderAnnotationsLabelsReport(annotationsLabelsRenderInput{
AppName: appName,
ReportType: labelsReportType,
Heading: "labels",
RowLabel: "Label",
Entries: entries,
Format: format,
InfoFlag: infoFlag,
})
}
// collectAnnotationsEntries scans the property store for annotation entries on appName,
// optionally filtered by processType/resourceType.
func collectAnnotationsEntries(appName string, processType string, resourceType string) ([]annotationsLabelsReportEntry, error) {
properties, err := common.PropertyGetAllByPrefix("scheduler-k3s", appName, "")
if err != nil {
return nil, fmt.Errorf("Unable to get property list: %w", err)
}
knownResourceTypes := map[string]bool{}
for _, rt := range AnnotationResourceTypes {
knownResourceTypes[rt] = true
}
entries := []annotationsLabelsReportEntry{}
for propertyName := range properties {
if isReservedAnnotationProperty(propertyName) {
continue
}
dot := strings.LastIndex(propertyName, ".")
if dot <= 0 || dot == len(propertyName)-1 {
continue
}
propProcessType := propertyName[:dot]
propResourceType := propertyName[dot+1:]
if !knownResourceTypes[propResourceType] {
continue
}
if processType != "" && propProcessType != processType {
continue
}
if resourceType != "" && propResourceType != resourceType {
continue
}
annotationMap, err := getAnnotation(appName, propProcessType, propResourceType)
if err != nil {
return nil, fmt.Errorf("Unable to read annotation %s: %w", propertyName, err)
}
for key, value := range annotationMap {
entries = append(entries, annotationsLabelsReportEntry{
ProcessType: propProcessType,
ResourceType: propResourceType,
Key: key,
Value: value,
})
}
}
return entries, nil
}
// collectLabelsEntries scans the property store for label entries on appName,
// optionally filtered by processType/resourceType.
func collectLabelsEntries(appName string, processType string, resourceType string) ([]annotationsLabelsReportEntry, error) {
properties, err := common.PropertyGetAllByPrefix("scheduler-k3s", appName, "labels.")
if err != nil {
return nil, fmt.Errorf("Unable to get property list: %w", err)
}
knownResourceTypes := map[string]bool{}
for _, rt := range LabelResourceTypes {
knownResourceTypes[rt] = true
}
entries := []annotationsLabelsReportEntry{}
for propertyName := range properties {
suffix := strings.TrimPrefix(propertyName, "labels.")
dot := strings.LastIndex(suffix, ".")
if dot <= 0 || dot == len(suffix)-1 {
continue
}
propProcessType := suffix[:dot]
propResourceType := suffix[dot+1:]
if !knownResourceTypes[propResourceType] {
continue
}
if processType != "" && propProcessType != processType {
continue
}
if resourceType != "" && propResourceType != resourceType {
continue
}
labelMap, err := getLabel(appName, propProcessType, propResourceType)
if err != nil {
return nil, fmt.Errorf("Unable to read label %s: %w", propertyName, err)
}
for key, value := range labelMap {
entries = append(entries, annotationsLabelsReportEntry{
ProcessType: propProcessType,
ResourceType: propResourceType,
Key: key,
Value: value,
})
}
}
return entries, nil
}
func isReservedAnnotationProperty(propertyName string) bool {
for _, prefix := range reservedAnnotationPrefixes {
if strings.HasPrefix(propertyName, prefix) {
return true
}
}
return false
}
type annotationsLabelsRenderInput struct {
AppName string
ReportType string
// Heading is the plural noun used in stdout headers (e.g. "annotations", "labels").
Heading string
// RowLabel is the singular noun used to prefix each stdout row (e.g. "Annotation").
RowLabel string
Entries []annotationsLabelsReportEntry
Format string
InfoFlag string
}
// renderAnnotationsLabelsReport dispatches to stdout/json/single-flag rendering for the
// annotations and labels reports.
func renderAnnotationsLabelsReport(input annotationsLabelsRenderInput) error {
if input.Format != "stdout" && input.Format != "json" {
return fmt.Errorf("Invalid format: %s", input.Format)
}
if input.Format == "json" && input.InfoFlag != "" {
return fmt.Errorf("--format flag cannot be specified when specifying an info flag")
}
flatValues := map[string]string{}
flagToValue := map[string]string{}
flagPrefix := "--" + input.ReportType + "."
for _, entry := range input.Entries {
composedKey := renderProcessType(entry.ProcessType) + "." + entry.ResourceType + "." + entry.Key
flatValues[composedKey] = entry.Value
flagToValue[flagPrefix+composedKey] = entry.Value
}
if input.InfoFlag != "" {
value, ok := flagToValue[input.InfoFlag]
if !ok {
validFlags := []string{}
for flag := range flagToValue {
validFlags = append(validFlags, flag)
}
sort.Strings(validFlags)
return fmt.Errorf("Invalid flag passed, valid flags: %s", strings.Join(validFlags, ", "))
}
fmt.Println(value)
return nil
}
if input.Format == "json" {
b, err := json.Marshal(flatValues)
if err != nil {
return fmt.Errorf("Unable to marshal json: %w", err)
}
fmt.Println(string(b))
return nil
}
entriesByGroup := map[string][]annotationsLabelsReportEntry{}
for _, entry := range input.Entries {
groupKey := renderProcessType(entry.ProcessType) + "/" + entry.ResourceType
entriesByGroup[groupKey] = append(entriesByGroup[groupKey], entry)
}
groupKeys := []string{}
for groupKey := range entriesByGroup {
groupKeys = append(groupKeys, groupKey)
}
sort.Strings(groupKeys)
common.LogInfo2Quiet(fmt.Sprintf("%s %s information", input.AppName, input.Heading))
if len(groupKeys) == 0 {
return nil
}
for _, groupKey := range groupKeys {
groupEntries := entriesByGroup[groupKey]
sort.Slice(groupEntries, func(i, j int) bool {
return groupEntries[i].Key < groupEntries[j].Key
})
length := 31
for _, entry := range groupEntries {
label := fmt.Sprintf("%s (%s) %s:", input.RowLabel, groupKey, entry.Key)
if len(label) > length {
length = len(label)
}
}
for _, entry := range groupEntries {
label := fmt.Sprintf("%s (%s) %s:", input.RowLabel, groupKey, entry.Key)
common.LogVerbose(fmt.Sprintf("%s%s", common.RightPad(label, length, " "), entry.Value))
}
}
return nil
}
// renderProcessType normalizes the in-storage process type for display in JSON keys,
// stdout headers, and single-flag lookups. GlobalProcessType (literal "--global") is
// rendered as "global"; real process types pass through unchanged.
func renderProcessType(processType string) string {
if processType == GlobalProcessType {
return reportProcessTypeGlobal
}
return processType
}
func reportComputedDeployTimeout(appName string) string {
return getComputedDeployTimeout(appName)
}