diff --git a/pkg/router/gloo.go b/pkg/router/gloo.go index 3388f663..823a16b5 100644 --- a/pkg/router/gloo.go +++ b/pkg/router/gloo.go @@ -38,10 +38,11 @@ import ( // GlooRouter is managing Gloo route tables type GlooRouter struct { - kubeClient kubernetes.Interface - glooClient clientset.Interface - flaggerClient clientset.Interface - logger *zap.SugaredLogger + kubeClient kubernetes.Interface + glooClient clientset.Interface + flaggerClient clientset.Interface + logger *zap.SugaredLogger + includeLabelPrefix []string } // Reconcile creates or updates the Gloo Edge route table @@ -294,14 +295,7 @@ func (gr *GlooRouter) getGlooUpstreamKubeService(canary *flaggerv1.Canary, svc * Selector: svc.Spec.Selector, } - upstreamLabels := make(map[string]string) - if upstreamSpec.Labels != nil { - for k, v := range upstreamSpec.Labels { // Order not specified - if _, ok := upstreamLabels[k]; !ok { - upstreamLabels[k] = v - } - } - } + upstreamLabels := includeLabelsByPrefix(upstreamSpec.Labels, gr.includeLabelPrefix) return &gloov1.Upstream{ ObjectMeta: metav1.ObjectMeta{ diff --git a/pkg/router/util.go b/pkg/router/util.go new file mode 100644 index 00000000..e2bb04ee --- /dev/null +++ b/pkg/router/util.go @@ -0,0 +1,19 @@ +package router + +import ( + "strings" +) + +func includeLabelsByPrefix(labels map[string]string, includeLabelPrefixes []string) map[string]string { + filteredLabels := make(map[string]string) + for key, value := range labels { + for _, includeLabelPrefix := range includeLabelPrefixes { + if includeLabelPrefix == "*" || strings.HasPrefix(key, includeLabelPrefix) { + filteredLabels[key] = value + break + } + } + } + + return filteredLabels +} diff --git a/pkg/router/util_test.go b/pkg/router/util_test.go new file mode 100644 index 00000000..2069810e --- /dev/null +++ b/pkg/router/util_test.go @@ -0,0 +1,57 @@ +/* +Copyright 2020 The Flux 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 router + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestIncludeLabelsByPrefix(t *testing.T) { + labels := map[string]string{ + "foo": "foo-value", + "bar": "bar-value", + "lorem": "ipsum", + } + includeLabelPrefix := []string{"foo", "lor"} + + filteredLabels := includeLabelsByPrefix(labels, includeLabelPrefix) + + assert.Equal(t, filteredLabels, map[string]string{ + "foo": "foo-value", + "lorem": "ipsum", + // bar excluded + }) +} + +func TestIncludeLabelsByPrefixWithWildcard(t *testing.T) { + labels := map[string]string{ + "foo": "foo-value", + "bar": "bar-value", + "lorem": "ipsum", + } + includeLabelPrefix := []string{"*"} + + filteredLabels := includeLabelsByPrefix(labels, includeLabelPrefix) + + assert.Equal(t, filteredLabels, map[string]string{ + "foo": "foo-value", + "bar": "bar-value", + "lorem": "ipsum", + }) +}