diff --git a/chart/values.schema.json b/chart/values.schema.json index 9a6b1904f..4329ce4a6 100755 --- a/chart/values.schema.json +++ b/chart/values.schema.json @@ -3066,6 +3066,12 @@ "reference" ], "title": "reference" + }, + { + "required": [ + "labels" + ], + "title": "labels" } ], "properties": { @@ -3079,7 +3085,11 @@ }, "reference": { "$ref": "#/$defs/TranslatePatchReference", - "description": "Reference rewrites the value value according to the name." + "description": "Reference treats the path value as a reference to another object and will rewrite it based on the chosen mode\nautomatically. In single-namespace mode this will translate the name to \"vxxxxxxxxx\" to avoid conflicts with\nother names, in multi-namespace mode this will not translate the name." + }, + "labels": { + "$ref": "#/$defs/TranslatePatchLabels", + "description": "Labels treats the path value as a labels selector." } }, "additionalProperties": false, @@ -3116,6 +3126,11 @@ "additionalProperties": false, "type": "object" }, + "TranslatePatchLabels": { + "properties": {}, + "additionalProperties": false, + "type": "object" + }, "TranslatePatchReference": { "properties": { "apiVersion": { diff --git a/config/config.go b/config/config.go index c51fab5e9..84f76c9d1 100644 --- a/config/config.go +++ b/config/config.go @@ -453,10 +453,17 @@ type TranslatePatch struct { // Expression transforms the value according to the given JavaScript expression. Expression *TranslatePatchExpression `json:"expression,omitempty" jsonschema:"oneof_required=expression"` - // Reference rewrites the value value according to the name. + // Reference treats the path value as a reference to another object and will rewrite it based on the chosen mode + // automatically. In single-namespace mode this will translate the name to "vxxxxxxxxx" to avoid conflicts with + // other names, in multi-namespace mode this will not translate the name. Reference *TranslatePatchReference `json:"reference,omitempty" jsonschema:"oneof_required=reference"` + + // Labels treats the path value as a labels selector. + Labels *TranslatePatchLabels `json:"labels,omitempty" jsonschema:"oneof_required=labels"` } +type TranslatePatchLabels struct{} + type TranslatePatchReference struct { // APIVersion is the apiVersion of the referenced object. APIVersion string `json:"apiVersion,omitempty" jsonschema:"required"` diff --git a/pkg/util/translate/labels.go b/pkg/util/translate/labels.go index c2c4c87f1..7ecf52f92 100644 --- a/pkg/util/translate/labels.go +++ b/pkg/util/translate/labels.go @@ -179,7 +179,11 @@ func VirtualLabels(pObj, vObj client.Object) map[string]string { if vObj != nil { vLabels = vObj.GetLabels() } - return VirtualLabelsMap(pLabels, vLabels) + retLabels := VirtualLabelsMap(pLabels, vLabels) + if len(retLabels) == 0 { + return nil + } + return retLabels } func HostLabels(vObj, pObj client.Object) map[string]string { @@ -191,7 +195,11 @@ func HostLabels(vObj, pObj client.Object) map[string]string { if pObj != nil { pLabels = pObj.GetLabels() } - return HostLabelsMap(vLabels, pLabels, vObj.GetNamespace(), true) + retLabels := HostLabelsMap(vLabels, pLabels, vObj.GetNamespace(), true) + if len(retLabels) == 0 { + return nil + } + return retLabels } func MergeLabelSelectors(elems ...*metav1.LabelSelector) *metav1.LabelSelector { diff --git a/pkg/util/translate/translate_test.go b/pkg/util/translate/translate_test.go index 964b62e48..44b803f55 100644 --- a/pkg/util/translate/translate_test.go +++ b/pkg/util/translate/translate_test.go @@ -11,6 +11,7 @@ import ( ) func TestVirtualLabels(t *testing.T) { + assert.Assert(t, VirtualLabelsMap(nil, nil) == nil) vMap := VirtualLabelsMap(map[string]string{ "test": "test", "test123": "test123",