feat: add a whole-set form for scheduler-k3s metadata
The `scheduler-k3s` metadata commands were all per-key, so matching an app's stored annotations, labels or node sysctls to a declared set meant reading the report, computing the difference in both directions and issuing one command per added, changed and removed key, with a failure partway through leaving a mixture of the two sets. A `--replace` flag on `annotations:set`, `labels:set`, `node-sysctls:set` and `autoscaling-auth:set` now writes the declared map in a single call, taking trailing `key=value` pairs everywhere except `autoscaling-auth:set`, which keeps its repeatable `--metadata` flag. Every pair is parsed before anything is written, so a rejected pair leaves the stored map untouched, and a pair ending in `=` stores an empty value, which the per-key form cannot express. An empty pair list is rejected rather than treated as a request to remove everything, so a generated list that expands to nothing cannot silently drop an app's metadata; `annotations:clear`, `labels:clear` and `node-sysctls:clear` handle that case, taking the same optional filters as their report commands. The `--resource-type` flag and the app name are now validated on the set commands as well, where a typo previously exited zero and wrote a property that no report and no deploy ever read.
This commit is contained in:
12 files changed
+1271
-195
No files matched your search
@@ -396,101 +396,34 @@ func ReportLabelsSingleApp(appName string, format string, processType string, re
|
||||
// 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
|
||||
return collectMetadataEntries(annotationsField, appName, processType, resourceType, getAnnotation)
|
||||
}
|
||||
|
||||
// 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)
|
||||
}
|
||||
return collectMetadataEntries(labelsField, appName, processType, resourceType, getLabel)
|
||||
}
|
||||
|
||||
knownResourceTypes := map[string]bool{}
|
||||
for _, rt := range LabelResourceTypes {
|
||||
knownResourceTypes[rt] = true
|
||||
// collectMetadataEntries flattens every stored scope a field owns on appName into
|
||||
// report entries, reading the scopes through the same scan the clear commands use.
|
||||
func collectMetadataEntries(field metadataField, appName string, processType string, resourceType string, read func(string, string, string) (map[string]string, error)) ([]annotationsLabelsReportEntry, error) {
|
||||
properties, err := matchingMetadataProperties(field, appName, processType, resourceType)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
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)
|
||||
for _, property := range properties {
|
||||
values, err := read(appName, property.ProcessType, property.ResourceType)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("Unable to read label %s: %w", propertyName, err)
|
||||
return nil, fmt.Errorf("Unable to read %s %s: %w", field.Singular, property.PropertyName, err)
|
||||
}
|
||||
|
||||
for key, value := range labelMap {
|
||||
for key, value := range values {
|
||||
entries = append(entries, annotationsLabelsReportEntry{
|
||||
ProcessType: propProcessType,
|
||||
ResourceType: propResourceType,
|
||||
ProcessType: property.ProcessType,
|
||||
ResourceType: property.ResourceType,
|
||||
Key: key,
|
||||
Value: value,
|
||||
})
|
||||
|
||||
Reference in new issue
Block a user