From 5acd3683ad6068c6f46898feb62fd8fd15e1b3ec Mon Sep 17 00:00:00 2001 From: okozachenko Date: Mon, 6 Apr 2020 08:44:10 -0700 Subject: [PATCH] Merge labels with CR Change-Id: Ia92c7a75cfdd24321b8e27939c1de2fe05bf1426 --- .zuul.yaml | 3 +- Makefile | 9 +- builders/pod_monitor.go | 5 ++ builders/prometheus_rule.go | 5 ++ .../infrastructure_v1alpha1_mcrouter.yaml | 2 + .../infrastructure_v1alpha1_memcached.yaml | 4 +- controllers/mcrouter_controller.go | 27 +++--- controllers/memcached_controller.go | 30 ++++--- controllers/rabbitmq_controller.go | 24 +++--- controllers/suite_test.go | 86 ------------------- go.mod | 3 +- go.sum | 3 + utils/baseutils/map.go | 26 ++++++ utils/baseutils/map_test.go | 55 ++++++++++++ utils/{ => k8sutils}/kubernetes.go | 2 +- 15 files changed, 157 insertions(+), 127 deletions(-) delete mode 100755 controllers/suite_test.go create mode 100644 utils/baseutils/map.go create mode 100644 utils/baseutils/map_test.go rename utils/{ => k8sutils}/kubernetes.go (98%) diff --git a/.zuul.yaml b/.zuul.yaml index 6370d625..a4d236f2 100644 --- a/.zuul.yaml +++ b/.zuul.yaml @@ -52,6 +52,7 @@ check: jobs: - golangci-lint + - golang-go-test - openstack-operator:linters:chart - openstack-operator:images:build - openstack-operator:functional: @@ -67,4 +68,4 @@ - openstack-operator:images:upload promote: jobs: - - openstack-operator:images:promote \ No newline at end of file + - openstack-operator:images:promote diff --git a/Makefile b/Makefile index a0460600..6f2d30c6 100644 --- a/Makefile +++ b/Makefile @@ -14,15 +14,15 @@ endif all: manager # Run tests -test: generate fmt vet manifests +test: generate fmt vet manifests lint go test ./... -coverprofile cover.out # Build manager binary -manager: generate fmt vet +manager: generate fmt vet lint go build -o bin/manager main.go # Run against the configured Kubernetes cluster in ~/.kube/config -run: generate fmt vet manifests +run: generate fmt vet manifests lint go run ./main.go # Install CRDs into a cluster @@ -48,6 +48,9 @@ manifests: controller-gen fmt: go fmt ./... +# Run golangci-lint code +lint: + golangci-lint run # Run go vet against code vet: go vet ./... diff --git a/builders/pod_monitor.go b/builders/pod_monitor.go index 064971bd..a471b674 100755 --- a/builders/pod_monitor.go +++ b/builders/pod_monitor.go @@ -25,6 +25,11 @@ func PodMonitor(existing *monitoringv1.PodMonitor, owner metav1.Object, scheme * } } +func (pm *PodMonitorBuilder) Labels(labels map[string]string) *PodMonitorBuilder { + pm.obj.Labels = labels + return pm +} + func (pm *PodMonitorBuilder) Selector(matchLabels map[string]string) *PodMonitorBuilder { pm.obj.Spec.Selector = metav1.LabelSelector{ MatchLabels: matchLabels, diff --git a/builders/prometheus_rule.go b/builders/prometheus_rule.go index d3cc4283..01f444f6 100755 --- a/builders/prometheus_rule.go +++ b/builders/prometheus_rule.go @@ -24,6 +24,11 @@ func PrometheusRule(existing *monitoringv1.PrometheusRule, owner metav1.Object, } } +func (pm *PrometheusRuleBuilder) Labels(labels map[string]string) *PrometheusRuleBuilder { + pm.obj.Labels = labels + return pm +} + // RuleGroups returns the ruleGroups func (pm *PrometheusRuleBuilder) RuleGroups(ruleGroups ...*RuleGroupBuilder) *PrometheusRuleBuilder { pm.ruleGroups = ruleGroups diff --git a/config/samples/infrastructure_v1alpha1_mcrouter.yaml b/config/samples/infrastructure_v1alpha1_mcrouter.yaml index aee8b44c..5f961300 100644 --- a/config/samples/infrastructure_v1alpha1_mcrouter.yaml +++ b/config/samples/infrastructure_v1alpha1_mcrouter.yaml @@ -2,6 +2,8 @@ apiVersion: infrastructure.vexxhost.cloud/v1alpha1 kind: Mcrouter metadata: name: sample + labels: + prometheus: helm spec: route: PoolRoute|default pools: diff --git a/config/samples/infrastructure_v1alpha1_memcached.yaml b/config/samples/infrastructure_v1alpha1_memcached.yaml index fa1c31d2..086a6f1d 100644 --- a/config/samples/infrastructure_v1alpha1_memcached.yaml +++ b/config/samples/infrastructure_v1alpha1_memcached.yaml @@ -2,5 +2,7 @@ apiVersion: infrastructure.vexxhost.cloud/v1alpha1 kind: Memcached metadata: name: sample + labels: + monitoring: haha spec: - megabytes: 128 \ No newline at end of file + megabytes: 128 diff --git a/controllers/mcrouter_controller.go b/controllers/mcrouter_controller.go index ab5892b8..d6cdf28c 100755 --- a/controllers/mcrouter_controller.go +++ b/controllers/mcrouter_controller.go @@ -16,7 +16,8 @@ import ( monitoringv1 "opendev.org/vexxhost/openstack-operator/api/monitoring/v1" infrastructurev1alpha1 "opendev.org/vexxhost/openstack-operator/api/v1alpha1" "opendev.org/vexxhost/openstack-operator/builders" - "opendev.org/vexxhost/openstack-operator/utils" + "opendev.org/vexxhost/openstack-operator/utils/baseutils" + "opendev.org/vexxhost/openstack-operator/utils/k8sutils" ) // McrouterReconciler reconciles a Mcrouter object @@ -44,10 +45,15 @@ func (r *McrouterReconciler) Reconcile(req ctrl.Request) (ctrl.Result, error) { } // Labels + typeLabels := baseutils.MergeMapsWithoutOverwrite(map[string]string{ + "app.kubernetes.io/name": "mcrouter", + "app.kubernetes.io/managed-by": "openstack-operator", + }, mcrouter.Labels) + labels := map[string]string{ "app.kubernetes.io/name": "mcrouter", - "app.kubernetes.io/instance": req.Name, "app.kubernetes.io/managed-by": "openstack-operator", + "app.kubernetes.io/instance": req.Name, } // ConfigMap @@ -57,7 +63,7 @@ func (r *McrouterReconciler) Reconcile(req ctrl.Request) (ctrl.Result, error) { Name: fmt.Sprintf("mcrouter-%s", req.Name), }, } - op, err := utils.CreateOrUpdate(ctx, r, configMap, func() error { + op, err := k8sutils.CreateOrUpdate(ctx, r, configMap, func() error { b, err := json.Marshal(mcrouter.Spec) if err != nil { @@ -80,7 +86,7 @@ func (r *McrouterReconciler) Reconcile(req ctrl.Request) (ctrl.Result, error) { Name: fmt.Sprintf("mcrouter-%s", req.Name), }, } - op, err = utils.CreateOrUpdate(ctx, r, deployment, func() error { + op, err = k8sutils.CreateOrUpdate(ctx, r, deployment, func() error { return builders.Deployment(deployment, &mcrouter, r.Scheme). Labels(labels). Replicas(2). @@ -131,15 +137,12 @@ func (r *McrouterReconciler) Reconcile(req ctrl.Request) (ctrl.Result, error) { ObjectMeta: metav1.ObjectMeta{ Namespace: req.Namespace, Name: "mcrouter-podmonitor", - Labels: map[string]string{ - "app.kubernetes.io/name": "mcrouter", - "app.kubernetes.io/managed-by": "openstack-operator", - }, }, } - op, err = utils.CreateOrUpdate(ctx, r, podMonitor, func() error { + op, err = k8sutils.CreateOrUpdate(ctx, r, podMonitor, func() error { return builders.PodMonitor(podMonitor, &mcrouter, r.Scheme). + Labels(typeLabels). Selector(map[string]string{ "app.kubernetes.io/name": "mcrouter", }). @@ -163,13 +166,13 @@ func (r *McrouterReconciler) Reconcile(req ctrl.Request) (ctrl.Result, error) { Name: "mcrouter-alertrule", }, } - op, err = utils.CreateOrUpdate(ctx, r, alertRule, func() error { + op, err = k8sutils.CreateOrUpdate(ctx, r, alertRule, func() error { return builders.PrometheusRule(alertRule, &mcrouter, r.Scheme). + Labels(typeLabels). RuleGroups(builders.RuleGroup(). Name("mcrouter-rule"). Rules( - builders.Rule(). Alert("McrouterBackendDown"). Message("Backend Memcached servers are down."). @@ -196,7 +199,7 @@ func (r *McrouterReconciler) Reconcile(req ctrl.Request) (ctrl.Result, error) { Name: fmt.Sprintf("mcrouter-%s", req.Name), }, } - op, err = utils.CreateOrUpdate(ctx, r, service, func() error { + op, err = k8sutils.CreateOrUpdate(ctx, r, service, func() error { return builders.Service(service, &mcrouter, r.Scheme). Port("mcrouter", 11211). Selector(labels). diff --git a/controllers/memcached_controller.go b/controllers/memcached_controller.go index 9d34b448..45ffa007 100755 --- a/controllers/memcached_controller.go +++ b/controllers/memcached_controller.go @@ -35,7 +35,8 @@ import ( monitoringv1 "opendev.org/vexxhost/openstack-operator/api/monitoring/v1" "opendev.org/vexxhost/openstack-operator/builders" - "opendev.org/vexxhost/openstack-operator/utils" + "opendev.org/vexxhost/openstack-operator/utils/baseutils" + "opendev.org/vexxhost/openstack-operator/utils/k8sutils" ) // MemcachedReconciler reconciles a Memcached object @@ -66,11 +67,21 @@ func (r *MemcachedReconciler) Reconcile(req ctrl.Request) (ctrl.Result, error) { size := memcached.Spec.Megabytes / 2 // Labels + typeLabels := baseutils.MergeMapsWithoutOverwrite(map[string]string{ + "app.kubernetes.io/name": "memcached", + "app.kubernetes.io/managed-by": "openstack-operator", + }, memcached.Labels) + labels := map[string]string{ "app.kubernetes.io/name": "memcached", - "app.kubernetes.io/instance": req.Name, "app.kubernetes.io/managed-by": "openstack-operator", + "app.kubernetes.io/instance": req.Name, } + mcrouterLabels := baseutils.MergeMapsWithoutOverwrite(map[string]string{ + "app.kubernetes.io/name": "memcached", + "app.kubernetes.io/managed-by": "openstack-operator", + "app.kubernetes.io/instance": req.Name, + }, memcached.Labels) // Deployment deployment := &appsv1.Deployment{ @@ -81,7 +92,7 @@ func (r *MemcachedReconciler) Reconcile(req ctrl.Request) (ctrl.Result, error) { }, } - op, err := utils.CreateOrUpdate(ctx, r, deployment, func() error { + op, err := k8sutils.CreateOrUpdate(ctx, r, deployment, func() error { return builders.Deployment(deployment, &memcached, r.Scheme). Labels(labels). Replicas(2). @@ -119,7 +130,6 @@ func (r *MemcachedReconciler) Reconcile(req ctrl.Request) (ctrl.Result, error) { log.WithValues("resource", "Deployment").WithValues("op", op).Info("Reconciled") // PodMonitor - podMonitor := &monitoringv1.PodMonitor{ TypeMeta: metav1.TypeMeta{ APIVersion: "monitoring.coreos.com/v1", @@ -128,15 +138,12 @@ func (r *MemcachedReconciler) Reconcile(req ctrl.Request) (ctrl.Result, error) { ObjectMeta: metav1.ObjectMeta{ Namespace: req.Namespace, Name: "memcached-podmonitor", - Labels: map[string]string{ - "app.kubernetes.io/name": "memcached", - "app.kubernetes.io/managed-by": "openstack-operator", - }, }, } - op, err = utils.CreateOrUpdate(ctx, r, podMonitor, func() error { + op, err = k8sutils.CreateOrUpdate(ctx, r, podMonitor, func() error { return builders.PodMonitor(podMonitor, &memcached, r.Scheme). + Labels(typeLabels). Selector(map[string]string{ "app.kubernetes.io/name": "memcached", }). @@ -184,9 +191,10 @@ func (r *MemcachedReconciler) Reconcile(req ctrl.Request) (ctrl.Result, error) { Name: "memcached-alertrule", }, } - op, err = utils.CreateOrUpdate(ctx, r, alertRule, func() error { + op, err = k8sutils.CreateOrUpdate(ctx, r, alertRule, func() error { return builders.PrometheusRule(alertRule, &memcached, r.Scheme). + Labels(typeLabels). RuleGroups(builders.RuleGroup(). Name("memcached-rule"). Rules( @@ -213,7 +221,7 @@ func (r *MemcachedReconciler) Reconcile(req ctrl.Request) (ctrl.Result, error) { ObjectMeta: metav1.ObjectMeta{ Namespace: req.Namespace, Name: fmt.Sprintf("memcached-%s", req.Name), - Labels: labels, + Labels: mcrouterLabels, }, } op, err = controllerutil.CreateOrUpdate(ctx, r, mcrouter, func() error { diff --git a/controllers/rabbitmq_controller.go b/controllers/rabbitmq_controller.go index 034de62c..444e7504 100644 --- a/controllers/rabbitmq_controller.go +++ b/controllers/rabbitmq_controller.go @@ -15,7 +15,8 @@ import ( monitoringv1 "opendev.org/vexxhost/openstack-operator/api/monitoring/v1" infrastructurev1alpha1 "opendev.org/vexxhost/openstack-operator/api/v1alpha1" "opendev.org/vexxhost/openstack-operator/builders" - "opendev.org/vexxhost/openstack-operator/utils" + "opendev.org/vexxhost/openstack-operator/utils/baseutils" + "opendev.org/vexxhost/openstack-operator/utils/k8sutils" ) // RabbitmqReconciler reconciles a Rabbitmq object @@ -52,10 +53,15 @@ func (r *RabbitmqReconciler) Reconcile(req ctrl.Request) (ctrl.Result, error) { } // Labels + typeLabels := baseutils.MergeMapsWithoutOverwrite(map[string]string{ + "app.kubernetes.io/name": "rabbitmq", + "app.kubernetes.io/managed-by": "openstack-operator", + }, Rabbitmq.Labels) + labels := map[string]string{ "app.kubernetes.io/name": "rabbitmq", - "app.kubernetes.io/instance": req.Name, "app.kubernetes.io/managed-by": "openstack-operator", + "app.kubernetes.io/instance": req.Name, } // Deployment @@ -65,7 +71,7 @@ func (r *RabbitmqReconciler) Reconcile(req ctrl.Request) (ctrl.Result, error) { Name: fmt.Sprintf("rabbitmq-%s", req.Name), }, } - op, err := utils.CreateOrUpdate(ctx, r, deployment, func() error { + op, err := k8sutils.CreateOrUpdate(ctx, r, deployment, func() error { return builders.Deployment(deployment, &Rabbitmq, r.Scheme). Labels(labels). Replicas(1). @@ -107,15 +113,12 @@ func (r *RabbitmqReconciler) Reconcile(req ctrl.Request) (ctrl.Result, error) { ObjectMeta: metav1.ObjectMeta{ Namespace: req.Namespace, Name: "rabbitmq-podmonitor", - Labels: map[string]string{ - "app.kubernetes.io/name": "rabbitmq", - "app.kubernetes.io/managed-by": "openstack-operator", - }, }, } - op, err = utils.CreateOrUpdate(ctx, r, podMonitor, func() error { + op, err = k8sutils.CreateOrUpdate(ctx, r, podMonitor, func() error { return builders.PodMonitor(podMonitor, &Rabbitmq, r.Scheme). + Labels(typeLabels). Selector(map[string]string{ "app.kubernetes.io/name": "rabbitmq", }). @@ -139,8 +142,9 @@ func (r *RabbitmqReconciler) Reconcile(req ctrl.Request) (ctrl.Result, error) { Name: "rabbitmq-alertrule", }, } - op, err = utils.CreateOrUpdate(ctx, r, alertRule, func() error { + op, err = k8sutils.CreateOrUpdate(ctx, r, alertRule, func() error { return builders.PrometheusRule(alertRule, &Rabbitmq, r.Scheme). + Labels(typeLabels). RuleGroups(builders.RuleGroup(). Name("rabbitmq-rule"). Rules( @@ -180,7 +184,7 @@ func (r *RabbitmqReconciler) Reconcile(req ctrl.Request) (ctrl.Result, error) { Name: fmt.Sprintf("rabbitmq-%s", req.Name), }, } - op, err = utils.CreateOrUpdate(ctx, r, service, func() error { + op, err = k8sutils.CreateOrUpdate(ctx, r, service, func() error { return builders.Service(service, &Rabbitmq, r.Scheme). Port("epmd", 4369). Port("amqp", 5671). diff --git a/controllers/suite_test.go b/controllers/suite_test.go deleted file mode 100755 index 0d049b95..00000000 --- a/controllers/suite_test.go +++ /dev/null @@ -1,86 +0,0 @@ -/* - - -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 controllers - -import ( - "path/filepath" - "testing" - - . "github.com/onsi/ginkgo" - . "github.com/onsi/gomega" - "k8s.io/client-go/kubernetes/scheme" - "k8s.io/client-go/rest" - "sigs.k8s.io/controller-runtime/pkg/client" - "sigs.k8s.io/controller-runtime/pkg/envtest" - "sigs.k8s.io/controller-runtime/pkg/envtest/printer" - logf "sigs.k8s.io/controller-runtime/pkg/log" - "sigs.k8s.io/controller-runtime/pkg/log/zap" - - infrastructurev1alpha1 "opendev.org/vexxhost/openstack-operator/api/v1alpha1" - // +kubebuilder:scaffold:imports -) - -// These tests use Ginkgo (BDD-style Go testing framework). Refer to -// http://onsi.github.io/ginkgo/ to learn more about Ginkgo. - -var cfg *rest.Config -var k8sClient client.Client -var testEnv *envtest.Environment - -func TestAPIs(t *testing.T) { - RegisterFailHandler(Fail) - - RunSpecsWithDefaultAndCustomReporters(t, - "Controller Suite", - []Reporter{printer.NewlineReporter{}}) -} - -var _ = BeforeSuite(func(done Done) { - logf.SetLogger(zap.LoggerTo(GinkgoWriter, true)) - - By("bootstrapping test environment") - testEnv = &envtest.Environment{ - CRDDirectoryPaths: []string{filepath.Join("..", "config", "crd", "bases")}, - } - - var err error - cfg, err = testEnv.Start() - Expect(err).ToNot(HaveOccurred()) - Expect(cfg).ToNot(BeNil()) - - err = infrastructurev1alpha1.AddToScheme(scheme.Scheme) - Expect(err).NotTo(HaveOccurred()) - - err = infrastructurev1alpha1.AddToScheme(scheme.Scheme) - Expect(err).NotTo(HaveOccurred()) - - err = infrastructurev1alpha1.AddToScheme(scheme.Scheme) - Expect(err).NotTo(HaveOccurred()) - - // +kubebuilder:scaffold:scheme - k8sClient, err = client.New(cfg, client.Options{Scheme: scheme.Scheme}) - Expect(err).ToNot(HaveOccurred()) - Expect(k8sClient).ToNot(BeNil()) - - close(done) -}, 60) - -var _ = AfterSuite(func() { - By("tearing down the test environment") - err := testEnv.Stop() - Expect(err).ToNot(HaveOccurred()) -}) diff --git a/go.mod b/go.mod index cde54e39..8d6e0c1d 100755 --- a/go.mod +++ b/go.mod @@ -6,8 +6,7 @@ require ( github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf github.com/go-logr/logr v0.1.0 github.com/google/go-cmp v0.3.0 - github.com/onsi/ginkgo v1.11.0 - github.com/onsi/gomega v1.8.1 + github.com/stretchr/testify v1.5.1 k8s.io/api v0.17.2 k8s.io/apimachinery v0.17.2 k8s.io/client-go v0.17.2 diff --git a/go.sum b/go.sum index cbb2d11a..003c6bb4 100755 --- a/go.sum +++ b/go.sum @@ -264,12 +264,15 @@ github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An github.com/spf13/viper v1.3.2/go.mod h1:ZiWeW+zYFKm7srdB9IoDzzZXaJaI5eL9QjNiN/DMA2s= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/objx v0.2.0 h1:Hbg2NidpLE8veEBkEZTL3CvlkUIVzuU9jDplZO54c48= github.com/stretchr/objx v0.2.0/go.mod h1:qt09Ya8vawLte6SNmTgCsAVtYtaKzEcn8ATUoHMkEqE= github.com/stretchr/testify v0.0.0-20151208002404-e3a8ff8ce365/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk= github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= +github.com/stretchr/testify v1.5.1 h1:nOGnQDM7FYENwehXlg/kFVnos3rEvtKTjRvOWSzb6H4= +github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA= github.com/tidwall/pretty v1.0.0/go.mod h1:XNkn88O1ChpSDQmQeStsy+sBenx6DDtFZJxhVysOjyk= github.com/tmc/grpc-websocket-proxy v0.0.0-20170815181823-89b8d40f7ca8/go.mod h1:ncp9v5uamzpCO7NfCPTXjqaC+bZgJeR0sMTm6dMHP7U= github.com/ugorji/go/codec v0.0.0-20181204163529-d75b2dcb6bc8/go.mod h1:VFNgLljTbGfSG7qAOspJ7OScBnGdDN/yBr0sguwnwf0= diff --git a/utils/baseutils/map.go b/utils/baseutils/map.go new file mode 100644 index 00000000..0c5accc7 --- /dev/null +++ b/utils/baseutils/map.go @@ -0,0 +1,26 @@ +package baseutils + +// MergeMaps merges all maps in the list +func MergeMaps(MapList ...map[string]string) map[string]string { + var baseMap = make(map[string]string) + for _, imap := range MapList { + for k, v := range imap { + baseMap[k] = v + } + } + return baseMap +} + +// MergeMapsWithoutOverwrite merges all maps in the list without overwriting. The priority is the same as the sequence of the list. +func MergeMapsWithoutOverwrite(MapList ...map[string]string) map[string]string { + var baseMap = make(map[string]string) + for _, imap := range MapList { + for k, v := range imap { + if _, ok := baseMap[k]; !ok { + baseMap[k] = v + } + + } + } + return baseMap +} diff --git a/utils/baseutils/map_test.go b/utils/baseutils/map_test.go new file mode 100644 index 00000000..a0919c28 --- /dev/null +++ b/utils/baseutils/map_test.go @@ -0,0 +1,55 @@ +package baseutils + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func assertMergeMaps(t *testing.T, cr, instance, expected map[string]string) { + merged := MergeMapsWithoutOverwrite(cr, instance) + assert.Equal(t, expected, merged) +} + +func TestMergeMapsWithNoInstanceLabels(t *testing.T) { + cr := map[string]string{ + "foo": "bar", + } + instance := map[string]string{} + expected := map[string]string{ + "foo": "bar", + } + + assertMergeMaps(t, cr, instance, expected) +} + +func TestMergeMapsWithDifferentInstanceLabels(t *testing.T) { + cr := map[string]string{ + "foo": "bar", + } + instance := map[string]string{ + "more": "options", + } + expected := map[string]string{ + "foo": "bar", + "more": "options", + } + + assertMergeMaps(t, cr, instance, expected) +} + +func TestMergeMapsWithCustomResourceLabelOverride(t *testing.T) { + cr := map[string]string{ + "foo": "bar", + } + instance := map[string]string{ + "foo": "bar2", + "more": "options", + } + expected := map[string]string{ + "foo": "bar", + "more": "options", + } + + assertMergeMaps(t, cr, instance, expected) +} diff --git a/utils/kubernetes.go b/utils/k8sutils/kubernetes.go similarity index 98% rename from utils/kubernetes.go rename to utils/k8sutils/kubernetes.go index e778a6ae..9d02a3e8 100644 --- a/utils/kubernetes.go +++ b/utils/k8sutils/kubernetes.go @@ -1,4 +1,4 @@ -package utils +package k8sutils import ( "context"