Skip to content

Commit

Permalink
fix: create lattice resources when no endpoints in K8s services (#386) (
Browse files Browse the repository at this point in the history
  • Loading branch information
scottlaiaws authored Sep 20, 2023
1 parent 5204483 commit e376581
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 1 deletion.
6 changes: 6 additions & 0 deletions pkg/deploy/lattice/targets_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package lattice
import (
"context"
"errors"

"github.com/golang/glog"

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

// No targets to register
if len(targetList) == 0 {
return nil
}

registerRouteInput := vpclattice.RegisterTargetsInput{
TargetGroupIdentifier: &tg.ID,
Targets: targetList,
Expand Down
45 changes: 44 additions & 1 deletion pkg/deploy/lattice/targets_manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,12 +120,19 @@ func Test_RegisterTargets_Registerfailed(t *testing.T) {
Name: "test",
Namespace: "",
TargetGroupID: "123456789",
TargetIPList: nil,
TargetIPList: []latticemodel.Target{
{
TargetIP: "123.456.7.891",
Port: sPort,
},
},
}

planToRegister := latticemodel.Targets{
ResourceMeta: core.ResourceMeta{},
Spec: targetsSpec,
}

registerTargetsOutput := &vpclattice.RegisterTargetsOutput{}

latticeDataStore := latticestore.NewLatticeDataStore()
Expand Down Expand Up @@ -233,3 +240,39 @@ func Test_RegisterTargets_RegisterUnsuccessfully(t *testing.T) {
assert.NotNil(t, err)
assert.Equal(t, err, errors.New(LATTICE_RETRY))
}

func Test_RegisterTargets_NoTargets_NoCallRegisterTargets(t *testing.T) {
planToRegister := latticemodel.Targets{
ResourceMeta: core.ResourceMeta{},
Spec: latticemodel.TargetsSpec{
Name: "test",
Namespace: "",
TargetGroupID: "123456789",
TargetIPList: []latticemodel.Target{},
},
}

latticeDataStore := latticestore.NewLatticeDataStore()
tgName := latticestore.TargetGroupName("test", "")

listTargetOutput := []*vpclattice.TargetSummary{}

// routename
latticeDataStore.AddTargetGroup(tgName, "vpc-123456789", "123456789", "123456789", false, "")

c := gomock.NewController(t)
defer c.Finish()
ctx := context.TODO()
mockCloud := mocks_aws.NewMockCloud(c)
mockVpcLatticeSess := mocks.NewMockLattice(c)

mockVpcLatticeSess.EXPECT().ListTargetsAsList(ctx, gomock.Any()).Return(listTargetOutput, nil)
// Expect not to call RegisterTargets
mockVpcLatticeSess.EXPECT().RegisterTargetsWithContext(ctx, gomock.Any()).MaxTimes(0)
mockCloud.EXPECT().Lattice().Return(mockVpcLatticeSess).AnyTimes()

targetsManager := NewTargetsManager(mockCloud, latticeDataStore)
err := targetsManager.Create(ctx, &planToRegister)

assert.Nil(t, err)
}
2 changes: 2 additions & 0 deletions pkg/deploy/lattice/targets_synthesizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"errors"
"fmt"

"github.com/golang/glog"

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

}

tgName := latticestore.TargetGroupName(targets.Spec.Name, targets.Spec.Namespace)

var targetList []latticestore.Target
Expand Down
1 change: 1 addition & 0 deletions pkg/gateway/model_build_targets.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ func (t *latticeTargetsModelBuildTask) buildLatticeTargets(ctx context.Context)
}

definedPorts := make(map[int32]struct{})

if tg.ByServiceExport {
serviceExport := &mcs_api.ServiceExport{}
err = t.client.Get(ctx, namespacedName, serviceExport)
Expand Down
21 changes: 21 additions & 0 deletions test/suites/integration/httproute_creation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (

"github.com/aws/aws-application-networking-k8s/pkg/model/core"
"github.com/aws/aws-application-networking-k8s/test/pkg/test"
"github.com/aws/aws-sdk-go/aws"
)

var _ = Describe("HTTPRoute Creation", func() {
Expand Down Expand Up @@ -82,6 +83,26 @@ var _ = Describe("HTTPRoute Creation", func() {
})
})

Context("Order #4: Service, HttpRoute", func() {
It("Create lattice resources successfully when no Targets available", func() {
httpRoute = testFramework.NewHttpRoute(testGateway, service, "Service")

// Override port match to make the BackendRef not be able to find
// any port causing no available targets to register
httpRoute.Spec.Rules[0].BackendRefs[0].BackendRef.Port = (*v1beta1.PortNumber)(aws.Int32(100))

testFramework.ExpectCreated(
ctx,
httpRoute,
deployment,
service,
)

testFramework.GetTargetGroup(ctx, service)
testFramework.GetVpcLatticeService(ctx, core.NewHTTPRoute(*httpRoute))
})
})

AfterEach(func() {
testFramework.ExpectDeleted(ctx, httpRoute)
testFramework.SleepForRouteDeletion()
Expand Down

0 comments on commit e376581

Please sign in to comment.