fix: updating to use include-label-prefix

fix: remove copy of labels

Signed-off-by: Hans Knecht <Hans.Knecht@missionlane.com>
This commit is contained in:
Hans Knecht
2021-06-14 11:39:55 -04:00
parent 35c8957a55
commit e7357c4e07
3 changed files with 82 additions and 12 deletions

View File

@@ -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{

19
pkg/router/util.go Normal file
View File

@@ -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
}

57
pkg/router/util_test.go Normal file
View File

@@ -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",
})
}