Skip to content
This repository has been archived by the owner on Feb 5, 2021. It is now read-only.

Commit

Permalink
apply source labels correctly for routing vs
Browse files Browse the repository at this point in the history
  • Loading branch information
isurulucky authored and Mirage20 committed Oct 4, 2019
1 parent a52d206 commit 948d54c
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 34 deletions.
13 changes: 11 additions & 2 deletions pkg/controller/cell/resources/virtual_svc.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ func buildInterCellRoutingInfo(cell *v1alpha2.Cell, cellLister listers.CellListe
if len(depCell.Spec.Gateway.Spec.Ingress.HTTPRoutes) > 0 {
hostNames = append(hostNames, routing.BuildHostNameForCellDependency(dependencyInst))
// build http routes
intercellHttpRoutes = append(intercellHttpRoutes, routing.BuildHttpRoutesForCellDependency(cell.Name, dependencyInst, isWebCell)...)
intercellHttpRoutes = append(intercellHttpRoutes, routing.BuildHttpRoutesForCellDependency(cell.Name, dependencyInst, isWebCell, CellSrcLabelBulder{})...)
}
} else if dependencyKind == routing.CompositeKind {
depComposite, err := compositeLister.Composites(cell.Namespace).Get(dependencyInst)
Expand All @@ -101,7 +101,7 @@ func buildInterCellRoutingInfo(cell *v1alpha2.Cell, cellLister listers.CellListe
}
if len(depComposite.Spec.Components) > 0 {
hostNames = append(hostNames, routing.BuildHostNamesForCompositeDependency(dependencyInst, depComposite.Spec.Components)...)
intercellHttpRoutes = append(intercellHttpRoutes, routing.BuildHttpRoutesForCompositeDependency(cell.Name, dependencyInst, depComposite.Spec.Components, isWebCell)...)
intercellHttpRoutes = append(intercellHttpRoutes, routing.BuildHttpRoutesForCompositeDependency(cell.Name, dependencyInst, depComposite.Spec.Components, isWebCell, CellSrcLabelBulder{})...)
}
} else {
// unknown dependency kind
Expand Down Expand Up @@ -277,3 +277,12 @@ func Annotate(vs *v1alpha3.VirtualService, name string, value string) {
}
vs.Annotations = annotations
}

type CellSrcLabelBulder struct{}

func (labelBuilder CellSrcLabelBulder) Get(instance string) map[string]string {
return map[string]string{
meta.CellLabelKeySource: instance,
meta.ComponentLabelKeySource: "true",
}
}
15 changes: 13 additions & 2 deletions pkg/controller/composite/resources/virtual_svc.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ package resources
import (
"fmt"

"github.com/cellery-io/mesh-controller/pkg/meta"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

"github.com/cellery-io/mesh-controller/pkg/apis/istio/networking/v1alpha3"
Expand Down Expand Up @@ -86,7 +88,7 @@ func buildInterCellRoutingInfo(composite *v1alpha2.Composite, compositeLister li
if len(depCell.Spec.Gateway.Spec.Ingress.HTTPRoutes) > 0 {
hostNames = append(hostNames, routing.BuildHostNameForCellDependency(dependencyInst))
// build http routes
intercellHttpRoutes = append(intercellHttpRoutes, routing.BuildHttpRoutesForCellDependency(composite.Name, dependencyInst, false)...)
intercellHttpRoutes = append(intercellHttpRoutes, routing.BuildHttpRoutesForCellDependency(composite.Name, dependencyInst, false, CompositeSrcLabelBulder{})...)
}
} else if dependencyKind == routing.CompositeKind {
// retrieve the cell using the cell instance name
Expand All @@ -96,7 +98,7 @@ func buildInterCellRoutingInfo(composite *v1alpha2.Composite, compositeLister li
}
if len(depComposite.Spec.Components) > 0 {
hostNames = append(hostNames, routing.BuildHostNamesForCompositeDependency(dependencyInst, depComposite.Spec.Components)...)
intercellHttpRoutes = append(intercellHttpRoutes, routing.BuildHttpRoutesForCompositeDependency(composite.Name, dependencyInst, depComposite.Spec.Components, false)...)
intercellHttpRoutes = append(intercellHttpRoutes, routing.BuildHttpRoutesForCompositeDependency(composite.Name, dependencyInst, depComposite.Spec.Components, false, CompositeSrcLabelBulder{})...)
}
} else {
// unknown dependency kind
Expand All @@ -120,3 +122,12 @@ func CopyRoutingVs(source, destination *v1alpha3.VirtualService) {
func StatusFromRoutingVs(composite *v1alpha2.Composite, vs *v1alpha3.VirtualService) {
composite.Status.RoutingVsGeneration = vs.Generation
}

type CompositeSrcLabelBulder struct{}

func (labelBuilder CompositeSrcLabelBulder) Get(instance string) map[string]string {
return map[string]string{
meta.CompositeLabelKeySource: instance,
meta.ComponentLabelKeySource: "true",
}
}
34 changes: 8 additions & 26 deletions pkg/controller/routing/advanced_routing.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func BuildHostNamesForCompositeDependency(dependencyInst string, components []v1
return svcNames
}

func BuildHttpRoutesForCellDependency(name string, dependencyInst string, isInstanceIdBasedRulesRequired bool) []*v1alpha3.HTTPRoute {
func BuildHttpRoutesForCellDependency(name string, dependencyInst string, isInstanceIdBasedRulesRequired bool, builder SrcLabelBulder) []*v1alpha3.HTTPRoute {
var routes []*v1alpha3.HTTPRoute
if isInstanceIdBasedRulesRequired {
routes = append(routes, &v1alpha3.HTTPRoute{
Expand All @@ -61,10 +61,7 @@ func BuildHttpRoutesForCellDependency(name string, dependencyInst string, isInst
Exact: "1",
},
},
SourceLabels: map[string]string{
meta.CellLabelKeySource: name,
meta.ComponentLabelKeySource: "true",
},
SourceLabels: builder.Get(name),
},
},
Route: []*v1alpha3.DestinationWeight{
Expand All @@ -86,10 +83,7 @@ func BuildHttpRoutesForCellDependency(name string, dependencyInst string, isInst
Exact: "2",
},
},
SourceLabels: map[string]string{
meta.CellLabelKeySource: name,
meta.ComponentLabelKeySource: "true",
},
SourceLabels: builder.Get(name),
},
},
Route: []*v1alpha3.DestinationWeight{
Expand All @@ -107,10 +101,7 @@ func BuildHttpRoutesForCellDependency(name string, dependencyInst string, isInst
Authority: &v1alpha3.StringMatch{
Regex: fmt.Sprintf("^(%s)(--gateway-service)(\\S*)$", dependencyInst),
},
SourceLabels: map[string]string{
meta.CellLabelKeySource: name,
meta.ComponentLabelKeySource: "true",
},
SourceLabels: builder.Get(name),
},
},
Route: []*v1alpha3.DestinationWeight{
Expand All @@ -124,7 +115,7 @@ func BuildHttpRoutesForCellDependency(name string, dependencyInst string, isInst
return routes
}

func BuildHttpRoutesForCompositeDependency(name string, dependencyInst string, components []v1alpha2.Component, isInstanceIdBasedRulesRequired bool) []*v1alpha3.HTTPRoute {
func BuildHttpRoutesForCompositeDependency(name string, dependencyInst string, components []v1alpha2.Component, isInstanceIdBasedRulesRequired bool, builder SrcLabelBulder) []*v1alpha3.HTTPRoute {
// three virtual services for each
// TODO: create upon request from SDK side?
var routes []*v1alpha3.HTTPRoute
Expand All @@ -141,10 +132,7 @@ func BuildHttpRoutesForCompositeDependency(name string, dependencyInst string, c
Exact: "1",
},
},
SourceLabels: map[string]string{
meta.CellLabelKeySource: name,
meta.ComponentLabelKeySource: "true",
},
SourceLabels: builder.Get(name),
},
},
Route: []*v1alpha3.DestinationWeight{
Expand All @@ -166,10 +154,7 @@ func BuildHttpRoutesForCompositeDependency(name string, dependencyInst string, c
Exact: "2",
},
},
SourceLabels: map[string]string{
meta.CellLabelKeySource: name,
meta.ComponentLabelKeySource: "true",
},
SourceLabels: builder.Get(name),
},
},
Route: []*v1alpha3.DestinationWeight{
Expand All @@ -187,10 +172,7 @@ func BuildHttpRoutesForCompositeDependency(name string, dependencyInst string, c
Authority: &v1alpha3.StringMatch{
Regex: fmt.Sprintf("^(%s)(--%s)(\\S*)$", dependencyInst, component.Name),
},
SourceLabels: map[string]string{
meta.CellLabelKeySource: name,
meta.ComponentLabelKeySource: "true",
},
SourceLabels: builder.Get(name),
},
},
Route: []*v1alpha3.DestinationWeight{
Expand Down
10 changes: 6 additions & 4 deletions pkg/controller/routing/advanced_routing_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ import (
"fmt"
"testing"

"github.com/cellery-io/mesh-controller/pkg/controller/composite/resources"

"github.com/cellery-io/mesh-controller/pkg/apis/mesh/v1alpha2"
"github.com/cellery-io/mesh-controller/pkg/meta"

Expand Down Expand Up @@ -89,7 +91,7 @@ func TestBuildHttpRoutesForCellDependency(t *testing.T) {
},
},
}
actual := BuildHttpRoutesForCellDependency(instName, dependencyInst, false)
actual := BuildHttpRoutesForCellDependency(instName, dependencyInst, false, resources.CompositeSrcLabelBulder{})
if diff := cmp.Diff(expected, actual); diff != "" {
t.Errorf("BuildHttpRoutesForCellDependency (-expected, +actual)\n%v", diff)
}
Expand Down Expand Up @@ -170,7 +172,7 @@ func TestBuildHttpRoutesForCellDependencyWithInstanceIdBasedRules(t *testing.T)
},
},
}
actual := BuildHttpRoutesForCellDependency(instName, dependencyInst, true)
actual := BuildHttpRoutesForCellDependency(instName, dependencyInst, true, resources.CompositeSrcLabelBulder{})
if diff := cmp.Diff(expected, actual); diff != "" {
t.Errorf("BuildHttpRoutesForCellDependency (-expected, +actual)\n%v", diff)
}
Expand Down Expand Up @@ -206,7 +208,7 @@ func TestBuildHttpRoutesForCompositeDependency(t *testing.T) {
},
},
}
actual := BuildHttpRoutesForCompositeDependency(instName, dependencyInst, []v1alpha2.Component{component}, false)
actual := BuildHttpRoutesForCompositeDependency(instName, dependencyInst, []v1alpha2.Component{component}, false, resources.CompositeSrcLabelBulder{})
if diff := cmp.Diff(expected, actual); diff != "" {
t.Errorf("BuildHttpRoutesForCompositeDependency (-expected, +actual)\n%v", diff)
}
Expand Down Expand Up @@ -292,7 +294,7 @@ func TestBuildHttpRoutesForCompositeDependencyWithInstanceBasedRules(t *testing.
},
},
}
actual := BuildHttpRoutesForCompositeDependency(instName, dependencyInst, []v1alpha2.Component{svcTemplate}, true)
actual := BuildHttpRoutesForCompositeDependency(instName, dependencyInst, []v1alpha2.Component{svcTemplate}, true, resources.CompositeSrcLabelBulder{})
if diff := cmp.Diff(expected, actual); diff != "" {
t.Errorf("BuildHttpRoutesForCompositeDependency (-expected, +actual)\n%v", diff)
}
Expand Down
23 changes: 23 additions & 0 deletions pkg/controller/routing/source_label.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* Copyright (c) 2019 WSO2 Inc. (http:www.wso2.org) All Rights Reserved.
*
* WSO2 Inc. licenses this file to you 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 commons

type SrcLabelBulder interface {
Get(instance string) map[string]string
}

0 comments on commit 948d54c

Please sign in to comment.