Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Removal of caching for service networks #391

Merged
merged 3 commits into from
Sep 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 12 additions & 11 deletions controllers/gateway_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ package controllers
import (
"context"
"fmt"

"github.com/aws/aws-application-networking-k8s/pkg/aws"
"github.com/aws/aws-application-networking-k8s/pkg/config"
"github.com/aws/aws-application-networking-k8s/pkg/deploy"
Expand Down Expand Up @@ -251,13 +250,19 @@ func (r *GatewayReconciler) reconcileGatewayResources(ctx context.Context, gw *g
return err
}

serviceNetworkStatus, err := r.datastore.GetServiceNetworkStatus(gw.Name, config.AccountID)
if err = r.updateGatewayStatus(ctx, &serviceNetworkStatus, gw); err != nil {
snInfo, err := r.cloud.Lattice().FindServiceNetwork(ctx, gw.Name, config.AccountID)
if err != nil {
return err
}
if snInfo == nil {
return fmt.Errorf("Service network %s for account %s not found", gw.Name, config.AccountID)
}

return nil
if err = r.updateGatewayStatus(ctx, *snInfo.SvcNetwork.Arn, gw); err != nil {
return err
}

return nil
}

func (r *GatewayReconciler) cleanupGatewayResources(ctx context.Context, gw *gateway_api.Gateway) error {
Expand All @@ -267,7 +272,7 @@ func (r *GatewayReconciler) cleanupGatewayResources(ctx context.Context, gw *gat

func (r *GatewayReconciler) updateGatewayStatus(
ctx context.Context,
serviceNetworkStatus *latticestore.ServiceNetwork,
snArn string,
gw *gateway_api.Gateway,
) error {
gwOld := gw.DeepCopy()
Expand All @@ -277,15 +282,11 @@ func (r *GatewayReconciler) updateGatewayStatus(
Status: metav1.ConditionTrue,
ObservedGeneration: gw.Generation,
Reason: string(gateway_api.GatewayReasonProgrammed),
Message: fmt.Sprintf("aws-gateway-arn: %s", serviceNetworkStatus.ARN),
Message: fmt.Sprintf("aws-gateway-arn: %s", snArn),
})

// TODO following is causing crash on some platform, see https://t.corp.amazon.com/b7c9ea6c-5168-4616-b718-c1bdf78dbdf1/communication
//gw.Annotations["gateway.networking.k8s.io/aws-gateway-id"] = serviceNetworkStatus.ID

if err := r.client.Status().Patch(ctx, gw, client.MergeFrom(gwOld)); err != nil {
return fmt.Errorf("update gw status error, gw: %s, status: %s, err: %w",
gw.Name, serviceNetworkStatus.Status, err)
return fmt.Errorf("update gw status error, gw: %s, err: %w", gw.Name, err)
}
return nil
}
Expand Down
17 changes: 7 additions & 10 deletions pkg/aws/cloud.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package aws

import (
"fmt"

"github.com/aws/aws-application-networking-k8s/pkg/aws/services"
"github.com/aws/aws-application-networking-k8s/pkg/utils/gwlog"
"github.com/aws/aws-sdk-go/aws/request"
Expand All @@ -15,8 +14,6 @@ const (
TagManagedBy = TagBase + "ManagedBy"
)

type Tags = map[string]*string

//go:generate mockgen -destination cloud_mocks.go -package aws github.com/aws/aws-application-networking-k8s/pkg/aws Cloud

type CloudConfig struct {
Expand All @@ -31,13 +28,13 @@ type Cloud interface {
Lattice() services.Lattice

// creates lattice tags with default values populated
DefaultTags() Tags
DefaultTags() services.Tags

// check if tags map has managedBy tag
ContainsManagedBy(tags services.Tags) bool

// check if managedBy tag set for lattice resource
IsArnManaged(arn string) (bool, error)

// check if tags map has managedBy tag
ContainsManagedBy(tags Tags) bool
}

// NewCloud constructs new Cloud implementation.
Expand Down Expand Up @@ -92,13 +89,13 @@ func (c *defaultCloud) Config() CloudConfig {
return c.cfg
}

func (c *defaultCloud) DefaultTags() Tags {
tags := Tags{}
func (c *defaultCloud) DefaultTags() services.Tags {
tags := services.Tags{}
tags[TagManagedBy] = &c.managedByTag
return tags
}

func (c *defaultCloud) ContainsManagedBy(tags Tags) bool {
func (c *defaultCloud) ContainsManagedBy(tags services.Tags) bool {
tag, ok := tags[TagManagedBy]
if !ok || tag == nil {
return false
Expand Down
71 changes: 71 additions & 0 deletions pkg/aws/services/vpclattice.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"os"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/arn"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/vpclattice"
"github.com/aws/aws-sdk-go/service/vpclattice/vpclatticeiface"
Expand All @@ -13,6 +14,12 @@ import (

//go:generate mockgen -destination vpclattice_mocks.go -package services github.com/aws/aws-application-networking-k8s/pkg/aws/services Lattice

type Tags = map[string]*string
mikhail-aws marked this conversation as resolved.
Show resolved Hide resolved

type ServiceNetworkInfo struct {
SvcNetwork vpclattice.ServiceNetworkSummary
Tags Tags
}
type Lattice interface {
vpclatticeiface.VPCLatticeAPI
ListServiceNetworksAsList(ctx context.Context, input *vpclattice.ListServiceNetworksInput) ([]*vpclattice.ServiceNetworkSummary, error)
Expand All @@ -21,6 +28,7 @@ type Lattice interface {
ListTargetsAsList(ctx context.Context, input *vpclattice.ListTargetsInput) ([]*vpclattice.TargetSummary, error)
ListServiceNetworkVpcAssociationsAsList(ctx context.Context, input *vpclattice.ListServiceNetworkVpcAssociationsInput) ([]*vpclattice.ServiceNetworkVpcAssociationSummary, error)
ListServiceNetworkServiceAssociationsAsList(ctx context.Context, input *vpclattice.ListServiceNetworkServiceAssociationsInput) ([]*vpclattice.ServiceNetworkServiceAssociationSummary, error)
FindServiceNetwork(ctx context.Context, name string, accountId string) (*ServiceNetworkInfo, error)
}

type defaultLattice struct {
Expand Down Expand Up @@ -169,3 +177,66 @@ func (d *defaultLattice) ListServiceNetworkServiceAssociationsAsList(ctx context

return result, nil
}

func (d *defaultLattice) FindServiceNetwork(ctx context.Context, name string, optionalAccountId string) (*ServiceNetworkInfo, error) {
input := vpclattice.ListServiceNetworksInput{}

for {

resp, err := d.ListServiceNetworksWithContext(ctx, &input)
if err != nil {
return nil, err
}

for _, r := range resp.Items {
if aws.StringValue(r.Name) != name {
continue
}
acctIdMatches, err1 := accountIdMatches(optionalAccountId, *r.Arn)
if err1 != nil {
return nil, err1
}
if !acctIdMatches {
glog.V(6).Infoln("ServiceNetwork found but does not match account id ", name, r.Arn, optionalAccountId)
continue
}

glog.V(6).Infoln("Found ServiceNetwork ", name, r.Arn, optionalAccountId)

tagsInput := vpclattice.ListTagsForResourceInput{
ResourceArn: r.Arn,
}

tagsOutput, err2 := d.ListTagsForResourceWithContext(ctx, &tagsInput)
if err2 != nil {
return nil, err2
}

return &ServiceNetworkInfo{
SvcNetwork: *r,
Tags: tagsOutput.Tags,
}, nil
}

if resp.NextToken == nil {
break
}

input.NextToken = resp.NextToken
}

return nil, nil
erikfuller marked this conversation as resolved.
Show resolved Hide resolved
}

func accountIdMatches(accountId string, itemArn string) (bool, error) {
if accountId == "" {
return true, nil
}

parsedArn, err := arn.Parse(itemArn)
if err != nil {
return false, err
}

return accountId == parsedArn.AccountID, nil
}
15 changes: 15 additions & 0 deletions pkg/aws/services/vpclattice_mocks.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading