Skip to content
This repository has been archived by the owner on Jun 29, 2022. It is now read-only.

Commit

Permalink
Merge pull request #646 from kinvolk/imran/add-labels-to-namespace
Browse files Browse the repository at this point in the history
Add labels to kube-system and components namespaces
  • Loading branch information
ipochi authored Aug 11, 2020
2 parents 919924e + a51b143 commit aed5228
Show file tree
Hide file tree
Showing 223 changed files with 17,817 additions and 85 deletions.
36 changes: 36 additions & 0 deletions cli/cmd/cluster-apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"

"github.com/kinvolk/lokomotive/internal"
"github.com/kinvolk/lokomotive/pkg/install"
"github.com/kinvolk/lokomotive/pkg/k8sutil"
"github.com/kinvolk/lokomotive/pkg/lokomotive"
Expand Down Expand Up @@ -88,6 +89,12 @@ func runClusterApply(cmd *cobra.Command, args []string) {
ctxLogger.Fatalf("Verify cluster: %v", err)
}

// Update all the pre installed namespaces with lokomotive specific label.
// `lokomotive.kinvolk.io/name: <namespace_name>`.
if err := updateInstalledNamespaces(kubeconfig); err != nil {
ctxLogger.Fatalf("Updating installed namespace: %v", err)
}

// Do controlplane upgrades only if cluster already exists and it is not a managed platform.
if exists && !p.Meta().Managed {
fmt.Printf("\nEnsuring that cluster controlplane is up to date.\n")
Expand Down Expand Up @@ -141,3 +148,32 @@ func verifyCluster(kubeconfig []byte, expectedNodes int) error {

return install.Verify(cluster)
}

func updateInstalledNamespaces(kubeconfig []byte) error {
cs, err := k8sutil.NewClientset(kubeconfig)
if err != nil {
return fmt.Errorf("create clientset: %v", err)
}

nsclient := cs.CoreV1().Namespaces()

namespaces, err := k8sutil.ListNamespaces(nsclient)
if err != nil {
return fmt.Errorf("getting list of namespaces: %v", err)
}

for _, ns := range namespaces.Items {
ns := k8sutil.Namespace{
Name: ns.ObjectMeta.Name,
Labels: map[string]string{
internal.NamespaceLabelKey: ns.ObjectMeta.Name,
},
}

if err := k8sutil.CreateOrUpdateNamespace(ns, nsclient); err != nil {
return fmt.Errorf("namespace %q with labels: %v", ns, err)
}
}

return nil
}
59 changes: 59 additions & 0 deletions internal/util.go
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
}
100 changes: 100 additions & 0 deletions internal/util_test.go
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)
}
}
9 changes: 6 additions & 3 deletions pkg/components/aws-ebs-csi-driver/component.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"github.com/kinvolk/lokomotive/internal/template"
"github.com/kinvolk/lokomotive/pkg/components"
"github.com/kinvolk/lokomotive/pkg/components/util"
"github.com/kinvolk/lokomotive/pkg/k8sutil"
)

const name = "aws-ebs-csi-driver"
Expand Down Expand Up @@ -67,7 +68,7 @@ func (c *component) RenderManifests() (map[string]string, error) {
return nil, fmt.Errorf("rendering chart values template failed: %w", err)
}

renderedFiles, err := util.RenderChart(helmChart, name, c.Metadata().Namespace, values)
renderedFiles, err := util.RenderChart(helmChart, name, c.Metadata().Namespace.Name, values)
if err != nil {
return nil, fmt.Errorf("rendering chart failed: %w", err)
}
Expand All @@ -77,7 +78,9 @@ func (c *component) RenderManifests() (map[string]string, error) {

func (c *component) Metadata() components.Metadata {
return components.Metadata{
Name: name,
Namespace: "kube-system",
Name: name,
Namespace: k8sutil.Namespace{
Name: "kube-system",
},
}
}
10 changes: 8 additions & 2 deletions pkg/components/cert-manager/component.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"github.com/kinvolk/lokomotive/internal/template"
"github.com/kinvolk/lokomotive/pkg/components"
"github.com/kinvolk/lokomotive/pkg/components/util"
"github.com/kinvolk/lokomotive/pkg/k8sutil"
)

const name = "cert-manager"
Expand Down Expand Up @@ -95,8 +96,13 @@ func (c *component) RenderManifests() (map[string]string, error) {

func (c *component) Metadata() components.Metadata {
return components.Metadata{
Name: name,
Namespace: c.Namespace,
Name: name,
Namespace: k8sutil.Namespace{
Name: c.Namespace,
Labels: map[string]string{
"certmanager.k8s.io/disable-validation": "true",
},
},
Helm: components.HelmMetadata{
// Cert-manager registers admission webhooks, so we should wait for the webhook to
// become ready before proceeding with installing other components, as it may fail.
Expand Down
7 changes: 5 additions & 2 deletions pkg/components/cluster-autoscaler/component.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"github.com/kinvolk/lokomotive/internal/template"
"github.com/kinvolk/lokomotive/pkg/components"
"github.com/kinvolk/lokomotive/pkg/components/util"
"github.com/kinvolk/lokomotive/pkg/k8sutil"
)

const name = "cluster-autoscaler"
Expand Down Expand Up @@ -382,7 +383,9 @@ func (c *component) RenderManifests() (map[string]string, error) {

func (c *component) Metadata() components.Metadata {
return components.Metadata{
Name: name,
Namespace: c.Namespace,
Name: name,
Namespace: k8sutil.Namespace{
Name: c.Namespace,
},
}
}
9 changes: 6 additions & 3 deletions pkg/components/contour/component.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
internaltemplate "github.com/kinvolk/lokomotive/internal/template"
"github.com/kinvolk/lokomotive/pkg/components"
"github.com/kinvolk/lokomotive/pkg/components/util"
"github.com/kinvolk/lokomotive/pkg/k8sutil"
)

const (
Expand Down Expand Up @@ -99,7 +100,7 @@ func (c *component) RenderManifests() (map[string]string, error) {
}

// Generate YAML for the Contour deployment.
renderedFiles, err := util.RenderChart(helmChart, name, c.Metadata().Namespace, values)
renderedFiles, err := util.RenderChart(helmChart, name, c.Metadata().Namespace.Name, values)
if err != nil {
return nil, fmt.Errorf("rendering chart failed: %w", err)
}
Expand All @@ -109,7 +110,9 @@ func (c *component) RenderManifests() (map[string]string, error) {

func (c *component) Metadata() components.Metadata {
return components.Metadata{
Name: name,
Namespace: "projectcontour",
Name: name,
Namespace: k8sutil.Namespace{
Name: "projectcontour",
},
}
}
7 changes: 5 additions & 2 deletions pkg/components/dex/component.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (

internaltemplate "github.com/kinvolk/lokomotive/internal/template"
"github.com/kinvolk/lokomotive/pkg/components"
"github.com/kinvolk/lokomotive/pkg/k8sutil"
)

const name = "dex"
Expand Down Expand Up @@ -387,7 +388,9 @@ func createSecretManifest(path string) (string, error) {

func (c *component) Metadata() components.Metadata {
return components.Metadata{
Name: name,
Namespace: name,
Name: name,
Namespace: k8sutil.Namespace{
Name: name,
},
}
}
7 changes: 5 additions & 2 deletions pkg/components/external-dns/component.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"github.com/kinvolk/lokomotive/internal/template"
"github.com/kinvolk/lokomotive/pkg/components"
"github.com/kinvolk/lokomotive/pkg/components/util"
"github.com/kinvolk/lokomotive/pkg/k8sutil"
)

const name = "external-dns"
Expand Down Expand Up @@ -145,7 +146,9 @@ func (c *component) RenderManifests() (map[string]string, error) {

func (c *component) Metadata() components.Metadata {
return components.Metadata{
Name: name,
Namespace: c.Namespace,
Name: name,
Namespace: k8sutil.Namespace{
Name: c.Namespace,
},
}
}
7 changes: 5 additions & 2 deletions pkg/components/flatcar-linux-update-operator/component.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (

"github.com/kinvolk/lokomotive/pkg/assets"
"github.com/kinvolk/lokomotive/pkg/components"
"github.com/kinvolk/lokomotive/pkg/k8sutil"
"github.com/kinvolk/lokomotive/pkg/util/walkers"
)

Expand Down Expand Up @@ -54,7 +55,9 @@ func (c *component) RenderManifests() (map[string]string, error) {

func (c *component) Metadata() components.Metadata {
return components.Metadata{
Name: componentName,
Namespace: "reboot-coordinator",
Name: componentName,
Namespace: k8sutil.Namespace{
Name: "reboot-coordinator",
},
}
}
7 changes: 5 additions & 2 deletions pkg/components/gangway/component.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"github.com/pkg/errors"

"github.com/kinvolk/lokomotive/pkg/components"
"github.com/kinvolk/lokomotive/pkg/k8sutil"
)

const name = "gangway"
Expand Down Expand Up @@ -347,7 +348,9 @@ func (c *component) RenderManifests() (map[string]string, error) {

func (c *component) Metadata() components.Metadata {
return components.Metadata{
Name: name,
Namespace: name,
Name: name,
Namespace: k8sutil.Namespace{
Name: name,
},
}
}
Loading

0 comments on commit aed5228

Please sign in to comment.