Skip to content

Commit

Permalink
Merge pull request #2084 from FabianKramm/main
Browse files Browse the repository at this point in the history
feat: add labels patch
  • Loading branch information
FabianKramm authored Aug 20, 2024
2 parents 3bb7084 + 0071b92 commit 9970784
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 4 deletions.
17 changes: 16 additions & 1 deletion chart/values.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -3066,6 +3066,12 @@
"reference"
],
"title": "reference"
},
{
"required": [
"labels"
],
"title": "labels"
}
],
"properties": {
Expand All @@ -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,
Expand Down Expand Up @@ -3116,6 +3126,11 @@
"additionalProperties": false,
"type": "object"
},
"TranslatePatchLabels": {
"properties": {},
"additionalProperties": false,
"type": "object"
},
"TranslatePatchReference": {
"properties": {
"apiVersion": {
Expand Down
9 changes: 8 additions & 1 deletion config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down
12 changes: 10 additions & 2 deletions pkg/util/translate/labels.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down
1 change: 1 addition & 0 deletions pkg/util/translate/translate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit 9970784

Please sign in to comment.