This repository has been archived by the owner on Jun 29, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #646 from kinvolk/imran/add-labels-to-namespace
Add labels to kube-system and components namespaces
- Loading branch information
Showing
223 changed files
with
17,817 additions
and
85 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
// Copyright 2020 The Lokomotive Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
// Package internal contains the utility functions used across the codebase. | ||
package internal | ||
|
||
const ( | ||
// NamespaceLabelKey acts a placeholder for the generic key name | ||
// `lokomotive.kinvolk.io/name`. | ||
// NOTE: In the subsequent versions if this value changes, it's very possible | ||
// that the change might not be backwards compatible. | ||
// In such a case we need to avoid updating this value and introduce | ||
// another key to ensure backwards compatibility. | ||
NamespaceLabelKey = "lokomotive.kinvolk.io/name" | ||
) | ||
|
||
// AppendNamespaceLabel appends the release namespace as value to the | ||
// key `lokomotive.kinvolk.io/name`. | ||
func AppendNamespaceLabel(namespace string, labels map[string]string) map[string]string { | ||
final := labels | ||
|
||
if final == nil { | ||
final = make(map[string]string) | ||
} | ||
|
||
if final[NamespaceLabelKey] == "" { | ||
final[NamespaceLabelKey] = namespace | ||
} | ||
|
||
return final | ||
} | ||
|
||
// MergeMaps merges two maps[string]string, with the values in first map | ||
// overriding the same keys in the second map. | ||
func MergeMaps(m1, m2 map[string]string) map[string]string { | ||
final := map[string]string{} | ||
|
||
for k, v := range m2 { | ||
final[k] = v | ||
} | ||
|
||
// m1 is merged last so as to not override any values from m2 | ||
for k, v := range m1 { | ||
final[k] = v | ||
} | ||
|
||
return final | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
// Copyright 2020 The Lokomotive Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
package internal_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/kinvolk/lokomotive/internal" | ||
) | ||
|
||
const namespace = "test" | ||
|
||
func TestApppendNamespaceLabelSuccess(t *testing.T) { | ||
m := map[string]string{ | ||
"key": "value", | ||
} | ||
|
||
final := internal.AppendNamespaceLabel(namespace, m) | ||
|
||
if !(final[internal.NamespaceLabelKey] == namespace) { | ||
t.Errorf("expected %s key to have the value %s", internal.NamespaceLabelKey, namespace) | ||
} | ||
} | ||
|
||
func TestApppendNamespaceLabelDontAllowOverride(t *testing.T) { | ||
value := "old_value" | ||
m := map[string]string{ | ||
internal.NamespaceLabelKey: value, | ||
} | ||
|
||
final := internal.AppendNamespaceLabel(namespace, m) | ||
|
||
if final[internal.NamespaceLabelKey] == namespace { | ||
t.Errorf("expected %s key to have the value %s", internal.NamespaceLabelKey, value) | ||
} | ||
} | ||
|
||
func TestApppendNamespaceLabelNilMap(t *testing.T) { | ||
var m map[string]string | ||
|
||
final := internal.AppendNamespaceLabel(namespace, m) | ||
|
||
if !(final[internal.NamespaceLabelKey] == namespace) { | ||
t.Errorf("expected %s key to have the value %s", internal.NamespaceLabelKey, namespace) | ||
} | ||
} | ||
|
||
func TestApppendNamespaceLabelEmptyMap(t *testing.T) { | ||
m := map[string]string{} | ||
|
||
final := internal.AppendNamespaceLabel(namespace, m) | ||
|
||
if !(final[internal.NamespaceLabelKey] == namespace) { | ||
t.Errorf("expected %s key to have the value %s", internal.NamespaceLabelKey, namespace) | ||
} | ||
} | ||
|
||
func TestMergeMapsSuccess(t *testing.T) { | ||
m1 := map[string]string{ | ||
"test": "good", | ||
"one": "two", | ||
} | ||
|
||
m2 := map[string]string{ | ||
"test": "bad", | ||
} | ||
|
||
final := internal.MergeMaps(m1, m2) | ||
|
||
if len(final) != 2 { | ||
t.Errorf("expected length of map after merging to be %d, got: %d", len(m1), len(final)) | ||
} | ||
|
||
if final["test"] != "good" { | ||
t.Errorf("expected value of key `test` as %s, got: %s", m1["test"], final["test"]) | ||
} | ||
} | ||
|
||
func TestMergeMapsNil(t *testing.T) { | ||
var m1 map[string]string | ||
|
||
var m2 map[string]string | ||
|
||
final := internal.MergeMaps(m1, m2) | ||
|
||
if final == nil { | ||
t.Errorf("expected map to be empty but not nil, got: %v", final) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.