Skip to content

Commit e376581

Browse files
authored
fix: create lattice resources when no endpoints in K8s services (#386) (#406)
1 parent 5204483 commit e376581

File tree

5 files changed

+74
-1
lines changed

5 files changed

+74
-1
lines changed

pkg/deploy/lattice/targets_manager.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package lattice
33
import (
44
"context"
55
"errors"
6+
67
"github.com/golang/glog"
78

89
"github.com/aws/aws-sdk-go/aws"
@@ -96,6 +97,11 @@ func (s *defaultTargetsManager) Create(ctx context.Context, targets *latticemode
9697
targetList = append(targetList, &t)
9798
}
9899

100+
// No targets to register
101+
if len(targetList) == 0 {
102+
return nil
103+
}
104+
99105
registerRouteInput := vpclattice.RegisterTargetsInput{
100106
TargetGroupIdentifier: &tg.ID,
101107
Targets: targetList,

pkg/deploy/lattice/targets_manager_test.go

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,12 +120,19 @@ func Test_RegisterTargets_Registerfailed(t *testing.T) {
120120
Name: "test",
121121
Namespace: "",
122122
TargetGroupID: "123456789",
123-
TargetIPList: nil,
123+
TargetIPList: []latticemodel.Target{
124+
{
125+
TargetIP: "123.456.7.891",
126+
Port: sPort,
127+
},
128+
},
124129
}
130+
125131
planToRegister := latticemodel.Targets{
126132
ResourceMeta: core.ResourceMeta{},
127133
Spec: targetsSpec,
128134
}
135+
129136
registerTargetsOutput := &vpclattice.RegisterTargetsOutput{}
130137

131138
latticeDataStore := latticestore.NewLatticeDataStore()
@@ -233,3 +240,39 @@ func Test_RegisterTargets_RegisterUnsuccessfully(t *testing.T) {
233240
assert.NotNil(t, err)
234241
assert.Equal(t, err, errors.New(LATTICE_RETRY))
235242
}
243+
244+
func Test_RegisterTargets_NoTargets_NoCallRegisterTargets(t *testing.T) {
245+
planToRegister := latticemodel.Targets{
246+
ResourceMeta: core.ResourceMeta{},
247+
Spec: latticemodel.TargetsSpec{
248+
Name: "test",
249+
Namespace: "",
250+
TargetGroupID: "123456789",
251+
TargetIPList: []latticemodel.Target{},
252+
},
253+
}
254+
255+
latticeDataStore := latticestore.NewLatticeDataStore()
256+
tgName := latticestore.TargetGroupName("test", "")
257+
258+
listTargetOutput := []*vpclattice.TargetSummary{}
259+
260+
// routename
261+
latticeDataStore.AddTargetGroup(tgName, "vpc-123456789", "123456789", "123456789", false, "")
262+
263+
c := gomock.NewController(t)
264+
defer c.Finish()
265+
ctx := context.TODO()
266+
mockCloud := mocks_aws.NewMockCloud(c)
267+
mockVpcLatticeSess := mocks.NewMockLattice(c)
268+
269+
mockVpcLatticeSess.EXPECT().ListTargetsAsList(ctx, gomock.Any()).Return(listTargetOutput, nil)
270+
// Expect not to call RegisterTargets
271+
mockVpcLatticeSess.EXPECT().RegisterTargetsWithContext(ctx, gomock.Any()).MaxTimes(0)
272+
mockCloud.EXPECT().Lattice().Return(mockVpcLatticeSess).AnyTimes()
273+
274+
targetsManager := NewTargetsManager(mockCloud, latticeDataStore)
275+
err := targetsManager.Create(ctx, &planToRegister)
276+
277+
assert.Nil(t, err)
278+
}

pkg/deploy/lattice/targets_synthesizer.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"errors"
66
"fmt"
7+
78
"github.com/golang/glog"
89

910
lattice_aws "github.com/aws/aws-application-networking-k8s/pkg/aws"
@@ -49,6 +50,7 @@ func (t *targetsSynthesizer) SynthesizeTargets(ctx context.Context, resTargets [
4950
return errors.New(errmsg)
5051

5152
}
53+
5254
tgName := latticestore.TargetGroupName(targets.Spec.Name, targets.Spec.Namespace)
5355

5456
var targetList []latticestore.Target

pkg/gateway/model_build_targets.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ func (t *latticeTargetsModelBuildTask) buildLatticeTargets(ctx context.Context)
109109
}
110110

111111
definedPorts := make(map[int32]struct{})
112+
112113
if tg.ByServiceExport {
113114
serviceExport := &mcs_api.ServiceExport{}
114115
err = t.client.Get(ctx, namespacedName, serviceExport)

test/suites/integration/httproute_creation_test.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212

1313
"github.com/aws/aws-application-networking-k8s/pkg/model/core"
1414
"github.com/aws/aws-application-networking-k8s/test/pkg/test"
15+
"github.com/aws/aws-sdk-go/aws"
1516
)
1617

1718
var _ = Describe("HTTPRoute Creation", func() {
@@ -82,6 +83,26 @@ var _ = Describe("HTTPRoute Creation", func() {
8283
})
8384
})
8485

86+
Context("Order #4: Service, HttpRoute", func() {
87+
It("Create lattice resources successfully when no Targets available", func() {
88+
httpRoute = testFramework.NewHttpRoute(testGateway, service, "Service")
89+
90+
// Override port match to make the BackendRef not be able to find
91+
// any port causing no available targets to register
92+
httpRoute.Spec.Rules[0].BackendRefs[0].BackendRef.Port = (*v1beta1.PortNumber)(aws.Int32(100))
93+
94+
testFramework.ExpectCreated(
95+
ctx,
96+
httpRoute,
97+
deployment,
98+
service,
99+
)
100+
101+
testFramework.GetTargetGroup(ctx, service)
102+
testFramework.GetVpcLatticeService(ctx, core.NewHTTPRoute(*httpRoute))
103+
})
104+
})
105+
85106
AfterEach(func() {
86107
testFramework.ExpectDeleted(ctx, httpRoute)
87108
testFramework.SleepForRouteDeletion()

0 commit comments

Comments
 (0)