Skip to content

Commit

Permalink
Fix for #425. More robust target group naming and conflict resolution…
Browse files Browse the repository at this point in the history
…. Various refactoring and improvements.
  • Loading branch information
erikfuller committed Oct 27, 2023
1 parent ad00db0 commit 5be20c3
Show file tree
Hide file tree
Showing 87 changed files with 6,065 additions and 9,471 deletions.
16 changes: 5 additions & 11 deletions cmd/aws-application-networking-k8s/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ import (
anv1alpha1 "github.com/aws/aws-application-networking-k8s/pkg/apis/applicationnetworking/v1alpha1"
"github.com/aws/aws-application-networking-k8s/pkg/config"
"github.com/aws/aws-application-networking-k8s/pkg/k8s"
"github.com/aws/aws-application-networking-k8s/pkg/latticestore"
)

var (
Expand Down Expand Up @@ -112,9 +111,8 @@ func main() {
setupLog.Infow("init config",
"VpcId", config.VpcID,
"Region", config.Region,
"AccoundId", config.AccountID,
"AccountId", config.AccountID,
"DefaultServiceNetwork", config.DefaultServiceNetwork,
"UseLongTgName", config.UseLongTGName,
"ClusterName", config.ClusterName,
)

Expand All @@ -141,7 +139,6 @@ func main() {
}

finalizerManager := k8s.NewDefaultFinalizerManager(mgr.GetClient())
latticeDataStore := latticestore.NewLatticeDataStoreWithLog(log.Named("datastore"))

// parent logging scope for all controllers
ctrlLog := log.Named("controller")
Expand All @@ -151,7 +148,7 @@ func main() {
setupLog.Fatalf("pod controller setup failed: %s", err)
}

err = controllers.RegisterServiceController(ctrlLog.Named("service"), cloud, latticeDataStore, finalizerManager, mgr)
err = controllers.RegisterServiceController(ctrlLog.Named("service"), cloud, finalizerManager, mgr)
if err != nil {
setupLog.Fatalf("service controller setup failed: %s", err)
}
Expand All @@ -166,17 +163,17 @@ func main() {
setupLog.Fatalf("gateway controller setup failed: %s", err)
}

err = controllers.RegisterAllRouteControllers(ctrlLog.Named("route"), cloud, latticeDataStore, finalizerManager, mgr)
err = controllers.RegisterAllRouteControllers(ctrlLog.Named("route"), cloud, finalizerManager, mgr)
if err != nil {
setupLog.Fatalf("route controller setup failed: %s", err)
}

err = controllers.RegisterServiceImportController(ctrlLog.Named("service-import"), mgr, latticeDataStore, finalizerManager)
err = controllers.RegisterServiceImportController(ctrlLog.Named("service-import"), mgr, finalizerManager)
if err != nil {
setupLog.Fatalf("serviceimport controller setup failed: %s", err)
}

err = controllers.RegisterServiceExportController(ctrlLog.Named("service-export"), cloud, latticeDataStore, finalizerManager, mgr)
err = controllers.RegisterServiceExportController(ctrlLog.Named("service-export"), cloud, finalizerManager, mgr)
if err != nil {
setupLog.Fatalf("serviceexport controller setup failed: %s", err)
}
Expand All @@ -190,9 +187,6 @@ func main() {
if err != nil {
setupLog.Fatalf("iam auth policy controller setup failed: %s", err)
}

go latticestore.GetDefaultLatticeDataStore().ServeIntrospection()

//+kubebuilder:scaffold:builder

if err := mgr.AddHealthzCheck("healthz", healthz.Ping); err != nil {
Expand Down
4 changes: 2 additions & 2 deletions controllers/iamauthpolicy_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ func (c *IAMAuthPolicyController) deleteGatewayPolicy(ctx context.Context, k8sPo

func (c *IAMAuthPolicyController) findSnId(ctx context.Context, k8sPolicy *anv1alpha1.IAMAuthPolicy) (string, error) {
tr := k8sPolicy.Spec.TargetRef
snInfo, err := c.policyMgr.Cloud.Lattice().FindServiceNetworkByK8sName(ctx, string(tr.Name))
snInfo, err := c.policyMgr.Cloud.Lattice().FindServiceNetwork(ctx, string(tr.Name), "")
if err != nil {
return "", err
}
Expand All @@ -149,7 +149,7 @@ func (c *IAMAuthPolicyController) upsertGatewayPolicy(ctx context.Context, k8sPo
func (c *IAMAuthPolicyController) findSvcId(ctx context.Context, k8sPolicy *anv1alpha1.IAMAuthPolicy) (string, error) {
tr := k8sPolicy.Spec.TargetRef
svcName := utils.LatticeServiceName(string(tr.Name), k8sPolicy.Namespace)
svcInfo, err := c.policyMgr.Cloud.Lattice().FindServiceByK8sName(ctx, svcName)
svcInfo, err := c.policyMgr.Cloud.Lattice().FindService(ctx, svcName)
if err != nil {
return "", err
}
Expand Down
48 changes: 16 additions & 32 deletions controllers/route_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,8 @@ package controllers
import (
"context"
"fmt"

"github.com/aws/aws-application-networking-k8s/pkg/aws/services"
"github.com/aws/aws-application-networking-k8s/pkg/utils"

"github.com/aws/aws-application-networking-k8s/pkg/utils/gwlog"

"github.com/pkg/errors"
Expand All @@ -49,9 +47,7 @@ import (
"github.com/aws/aws-application-networking-k8s/pkg/deploy/lattice"
"github.com/aws/aws-application-networking-k8s/pkg/gateway"
"github.com/aws/aws-application-networking-k8s/pkg/k8s"
"github.com/aws/aws-application-networking-k8s/pkg/latticestore"
"github.com/aws/aws-application-networking-k8s/pkg/model/core"
model "github.com/aws/aws-application-networking-k8s/pkg/model/lattice"
lattice_runtime "github.com/aws/aws-application-networking-k8s/pkg/runtime"
)

Expand All @@ -69,7 +65,6 @@ type routeReconciler struct {
eventRecorder record.EventRecorder
modelBuilder gateway.LatticeServiceBuilder
stackDeployer deploy.StackDeployer
latticeDataStore *latticestore.LatticeDataStore
stackMarshaller deploy.StackMarshaller
cloud aws.Cloud
}
Expand All @@ -78,18 +73,9 @@ const (
LatticeAssignedDomainName = "application-networking.k8s.aws/lattice-assigned-domain-name"
)

type RouteLSNProvider struct {
Route core.Route
}

func (r *RouteLSNProvider) LatticeServiceName() string {
return utils.LatticeServiceName(r.Route.Name(), r.Route.Namespace())
}

func RegisterAllRouteControllers(
log gwlog.Logger,
cloud aws.Cloud,
datastore *latticestore.LatticeDataStore,
finalizerManager k8s.FinalizerManager,
mgr ctrl.Manager,
) error {
Expand All @@ -113,9 +99,8 @@ func RegisterAllRouteControllers(
scheme: mgr.GetScheme(),
finalizerManager: finalizerManager,
eventRecorder: mgr.GetEventRecorderFor(string(routeInfo.routeType) + "route"),
latticeDataStore: datastore,
modelBuilder: gateway.NewLatticeServiceBuilder(log, mgrClient, datastore, cloud),
stackDeployer: deploy.NewLatticeServiceStackDeploy(log, cloud, mgrClient, datastore),
modelBuilder: gateway.NewLatticeServiceBuilder(log, mgrClient),
stackDeployer: deploy.NewLatticeServiceStackDeploy(log, cloud, mgrClient),
stackMarshaller: deploy.NewDefaultStackMarshaller(),
cloud: cloud,
}
Expand Down Expand Up @@ -195,8 +180,8 @@ func (r *routeReconciler) reconcileDelete(ctx context.Context, req ctrl.Request,
r.eventRecorder.Event(route.K8sObject(), corev1.EventTypeNormal,
k8s.RouteEventReasonReconcile, "Deleting Reconcile")

if err := r.cleanupRouteResources(ctx, route); err != nil {
return fmt.Errorf("failed to cleanup GRPCRoute %s, %s: %w", route.Name(), route.Namespace(), err)
if _, err := r.buildAndDeployModel(ctx, route); err != nil {
return fmt.Errorf("failed to cleanup route %s, %s: %w", route.Name(), route.Namespace(), err)
}

if err := updateRouteListenerStatus(ctx, r.client, route); err != nil {
Expand Down Expand Up @@ -238,11 +223,6 @@ func updateRouteListenerStatus(ctx context.Context, k8sClient client.Client, rou
return UpdateGWListenerStatus(ctx, k8sClient, gw)
}

func (r *routeReconciler) cleanupRouteResources(ctx context.Context, route core.Route) error {
_, _, err := r.buildAndDeployModel(ctx, route)
return err
}

func (r *routeReconciler) isRouteRelevant(ctx context.Context, route core.Route) bool {
if len(route.Spec().ParentRefs()) == 0 {
r.log.Infof("Ignore Route which has no ParentRefs gateway %s ", route.Name())
Expand Down Expand Up @@ -290,8 +270,8 @@ func (r *routeReconciler) isRouteRelevant(ctx context.Context, route core.Route)
func (r *routeReconciler) buildAndDeployModel(
ctx context.Context,
route core.Route,
) (core.Stack, *model.Service, error) {
stack, latticeService, err := r.modelBuilder.Build(ctx, route)
) (core.Stack, error) {
stack, err := r.modelBuilder.Build(ctx, route)

if err != nil {
r.eventRecorder.Event(route.K8sObject(), corev1.EventTypeWarning,
Expand All @@ -300,14 +280,16 @@ func (r *routeReconciler) buildAndDeployModel(

// Build failed
// TODO continue deploy to trigger reconcile of stale Route and policy
return nil, nil, err
return nil, err
}

_, err = r.stackMarshaller.Marshal(stack)
json, err := r.stackMarshaller.Marshal(stack)
if err != nil {
r.log.Errorf("error on r.stackMarshaller.Marshal error %s", err)
}

r.log.Debugf("stack: %s", json)

if err := r.stackDeployer.Deploy(ctx, stack); err != nil {
if errors.As(err, &lattice.RetryErr) {
r.eventRecorder.Event(route.K8sObject(), corev1.EventTypeNormal,
Expand All @@ -316,10 +298,10 @@ func (r *routeReconciler) buildAndDeployModel(
r.eventRecorder.Event(route.K8sObject(), corev1.EventTypeWarning,
k8s.RouteEventReasonFailedDeployModel, fmt.Sprintf("Failed deploy model due to %s", err))
}
return nil, nil, err
return nil, err
}

return stack, latticeService, err
return stack, err
}

func (r *routeReconciler) reconcileUpsert(ctx context.Context, req ctrl.Request, route core.Route) error {
Expand Down Expand Up @@ -353,19 +335,21 @@ func (r *routeReconciler) reconcileUpsert(ctx context.Context, req ctrl.Request,
return backendRefIPFamiliesErr
}

if _, _, err := r.buildAndDeployModel(ctx, route); err != nil {
if _, err := r.buildAndDeployModel(ctx, route); err != nil {
return err
}

r.eventRecorder.Event(route.K8sObject(), corev1.EventTypeNormal,
k8s.RouteEventReasonDeploySucceed, "Adding/Updating reconcile Done!")

svc, err := r.cloud.Lattice().FindService(ctx, &RouteLSNProvider{route})
svcName := utils.LatticeServiceName(route.Name(), route.Namespace())
svc, err := r.cloud.Lattice().FindService(ctx, svcName)
if err != nil && !services.IsNotFoundError(err) {
return err
}

if svc == nil || svc.DnsEntry == nil || svc.DnsEntry.DomainName == nil {
r.log.Infof("Either service, dns entry, or domain name is not available. Will Retry")
return errors.New(lattice.LATTICE_RETRY)
}

Expand Down
Loading

0 comments on commit 5be20c3

Please sign in to comment.