Skip to content

Commit

Permalink
fix: updating to use include-label-prefix
Browse files Browse the repository at this point in the history
fix: remove copy of labels

Signed-off-by: Hans Knecht <[email protected]>
  • Loading branch information
knechtionscoding committed Jun 14, 2021
1 parent 35c8957 commit e7357c4
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 12 deletions.
18 changes: 6 additions & 12 deletions pkg/router/gloo.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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{
Expand Down
19 changes: 19 additions & 0 deletions pkg/router/util.go
Original file line number Diff line number Diff line change
@@ -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 changes: 57 additions & 0 deletions pkg/router/util_test.go
Original file line number Diff line number Diff line change
@@ -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",
})
}

0 comments on commit e7357c4

Please sign in to comment.