Skip to content

Commit

Permalink
refactor: only rewrite release label
Browse files Browse the repository at this point in the history
  • Loading branch information
FabianKramm committed Aug 6, 2024
1 parent f36632b commit a970c33
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 18 deletions.
15 changes: 8 additions & 7 deletions pkg/util/translate/labels.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ import (
)

var translateLabels = map[string]bool{
// rewrite app & release
VClusterAppLabel: true,
// rewrite release
VClusterReleaseLabel: true,

// namespace, marker & controlled-by
Expand Down Expand Up @@ -58,11 +57,13 @@ func HostLabelsMap(vLabels, pLabels map[string]string, vNamespace string) map[st
}

// check if namespace or cluster-scoped
if vNamespace != "" {
newLabels[MarkerLabel] = VClusterName
newLabels[NamespaceLabel] = vNamespace
} else {
newLabels[MarkerLabel] = Default.MarkerLabelCluster()
if pLabels == nil || pLabels[MarkerLabel] != "" {
if vNamespace == "" {
newLabels[MarkerLabel] = Default.MarkerLabelCluster()
} else if Default.SingleNamespaceTarget() {
newLabels[MarkerLabel] = VClusterName
newLabels[NamespaceLabel] = vNamespace
}
}

// set controller label
Expand Down
45 changes: 35 additions & 10 deletions pkg/util/translate/translate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,31 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

func TestNotRewritingLabels(t *testing.T) {
pMap := map[string]string{
"abc": "abc",
"def": "def",
"hij": "hij",
"app": "my-app",
}

pMap = HostLabelsMap(pMap, pMap, "test")
assert.DeepEqual(t, map[string]string{
"abc": "abc",
"def": "def",
"hij": "hij",
"app": "my-app",
}, pMap)

pMap = VirtualLabelsMap(pMap, pMap)
assert.DeepEqual(t, map[string]string{
"abc": "abc",
"def": "def",
"hij": "hij",
"app": "my-app",
}, pMap)
}

func TestAnnotations(t *testing.T) {
vObj := &corev1.Secret{
ObjectMeta: metav1.ObjectMeta{
Expand Down Expand Up @@ -83,15 +108,15 @@ func TestLabelsMap(t *testing.T) {
vMap := map[string]string{
"test": "test",
"test123": "test123",
"app": "vcluster",
"vcluster.loft.sh/label-suffix-x-a172cedcae": "vcluster123",
"release": "vcluster",
"vcluster.loft.sh/label-suffix-x-a4d451ec23": "vcluster123",
}

pMap := HostLabelsMap(vMap, nil, "test")
assert.DeepEqual(t, map[string]string{
"test": "test",
"test123": "test123",
"vcluster.loft.sh/label-suffix-x-a172cedcae": "vcluster",
"vcluster.loft.sh/label-suffix-x-a4d451ec23": "vcluster",
MarkerLabel: VClusterName,
NamespaceLabel: "test",
}, pMap)
Expand All @@ -103,13 +128,13 @@ func TestLabelsMap(t *testing.T) {
"test": "test",
"test123": "test123",
"other": "other",
"app": "vcluster",
"vcluster.loft.sh/label-suffix-x-a172cedcae": "vcluster123",
"release": "vcluster",
"vcluster.loft.sh/label-suffix-x-a4d451ec23": "vcluster123",
}, vMap)

pMap = HostLabelsMap(vMap, pMap, "test")
assert.DeepEqual(t, map[string]string{
"vcluster.loft.sh/label-suffix-x-a172cedcae": "vcluster",
"vcluster.loft.sh/label-suffix-x-a4d451ec23": "vcluster",
MarkerLabel: VClusterName,
NamespaceLabel: "test",
"test": "test",
Expand All @@ -123,14 +148,14 @@ func TestLabelSelector(t *testing.T) {
MatchLabels: map[string]string{
"test": "test",
"test123": "test123",
"app": "vcluster",
"release": "vcluster",
},
})
assert.DeepEqual(t, &metav1.LabelSelector{
MatchLabels: map[string]string{
"test": "test",
"test123": "test123",
"vcluster.loft.sh/label-suffix-x-a172cedcae": "vcluster",
"vcluster.loft.sh/label-suffix-x-a4d451ec23": "vcluster",
},
}, pMap)

Expand All @@ -139,7 +164,7 @@ func TestLabelSelector(t *testing.T) {
MatchLabels: map[string]string{
"test": "test",
"test123": "test123",
"app": "vcluster",
"release": "vcluster",
},
}, vMap)

Expand All @@ -148,7 +173,7 @@ func TestLabelSelector(t *testing.T) {
MatchLabels: map[string]string{
"test": "test",
"test123": "test123",
"vcluster.loft.sh/label-suffix-x-a172cedcae": "vcluster",
"vcluster.loft.sh/label-suffix-x-a4d451ec23": "vcluster",
},
}, pMap)
}
1 change: 0 additions & 1 deletion pkg/util/translate/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ var (
)

var (
VClusterAppLabel = "app"
VClusterReleaseLabel = "release"
NamespaceLabel = "vcluster.loft.sh/namespace"
MarkerLabel = "vcluster.loft.sh/managed-by"
Expand Down

0 comments on commit a970c33

Please sign in to comment.