From 07cb0db9f00b944e87a881f4908570cb6ab92c16 Mon Sep 17 00:00:00 2001 From: Anton Bronnikov Date: Sun, 8 Sep 2024 16:32:10 +0300 Subject: [PATCH] feat: log routing updates at higher level .. and with more detail --- aws/route_table.go | 96 ++++++++++++++++++++++++++++++++++++++++------ gcp/route.go | 60 +++++++++++++++++++++++++---- job/aws.go | 2 +- job/gcp.go | 6 +-- 4 files changed, 141 insertions(+), 23 deletions(-) diff --git a/aws/route_table.go b/aws/route_table.go index 9a62352..1386ee6 100644 --- a/aws/route_table.go +++ b/aws/route_table.go @@ -99,15 +99,35 @@ func (cli *Client) FindRoute( func (cli *Client) UpdateRoute( ctx context.Context, routeTable string, + route *awstypes.Route, cidr string, networkInterfaceID string, ) error { l := logutils.LoggerFromContext(ctx) - l.Debug("Replacing route in AWS route-table...", - zap.String("destination_cidr_block", cidr), - zap.String("network_interface_id", networkInterfaceID), + l.Info("Replacing route in AWS route-table...", + // route-table zap.String("route_table_id", routeTable), + // destination + zap.String("destination_cidr_block", aws.ToString(route.DestinationCidrBlock)), + zap.String("destination_ipv6_cidr_block", aws.ToString(route.DestinationIpv6CidrBlock)), + zap.String("destination_prefix_list_id", aws.ToString(route.DestinationPrefixListId)), + // next hop + zap.String("carrier_gateway_id", aws.ToString(route.CarrierGatewayId)), + zap.String("core_network_arn", aws.ToString(route.CoreNetworkArn)), + zap.String("egress_only_internet_gateway_id", aws.ToString(route.EgressOnlyInternetGatewayId)), + zap.String("gateway_id", aws.ToString(route.GatewayId)), + zap.String("instance_id", aws.ToString(route.InstanceId)), + zap.String("instance_owner_id", aws.ToString(route.InstanceOwnerId)), + zap.String("local_gateway_id", aws.ToString(route.LocalGatewayId)), + zap.String("nat_gateway_id", aws.ToString(route.NatGatewayId)), + zap.String("network_interface_id", aws.ToString(route.NetworkInterfaceId)), + zap.String("origin", string(route.Origin)), + zap.String("state", string(route.State)), + zap.String("transit_gateway_id", aws.ToString(route.TransitGatewayId)), + zap.String("vpc_peering_connection_id", aws.ToString(route.VpcPeeringConnectionId)), + // new next hop + zap.String("new_network_interface_id", networkInterfaceID), ) _, err := cli.ec2.ReplaceRoute(ctx, &ec2.ReplaceRouteInput{ @@ -117,10 +137,28 @@ func (cli *Client) UpdateRoute( }) if err != nil { l.Error("Failed to replace route in AWS route-table", - zap.Error(err), - zap.String("cidr", cidr), - zap.String("network_interface_id", networkInterfaceID), + // route-table zap.String("route_table_id", routeTable), + // destination + zap.String("destination_cidr_block", aws.ToString(route.DestinationCidrBlock)), + zap.String("destination_ipv6_cidr_block", aws.ToString(route.DestinationIpv6CidrBlock)), + zap.String("destination_prefix_list_id", aws.ToString(route.DestinationPrefixListId)), + // next hop + zap.String("carrier_gateway_id", aws.ToString(route.CarrierGatewayId)), + zap.String("core_network_arn", aws.ToString(route.CoreNetworkArn)), + zap.String("egress_only_internet_gateway_id", aws.ToString(route.EgressOnlyInternetGatewayId)), + zap.String("gateway_id", aws.ToString(route.GatewayId)), + zap.String("instance_id", aws.ToString(route.InstanceId)), + zap.String("instance_owner_id", aws.ToString(route.InstanceOwnerId)), + zap.String("local_gateway_id", aws.ToString(route.LocalGatewayId)), + zap.String("nat_gateway_id", aws.ToString(route.NatGatewayId)), + zap.String("network_interface_id", aws.ToString(route.NetworkInterfaceId)), + zap.String("origin", string(route.Origin)), + zap.String("state", string(route.State)), + zap.String("transit_gateway_id", aws.ToString(route.TransitGatewayId)), + zap.String("vpc_peering_connection_id", aws.ToString(route.VpcPeeringConnectionId)), + // new next hop + zap.String("new_network_interface_id", networkInterfaceID), ) } return err @@ -134,10 +172,10 @@ func (cli *Client) CreateRoute( ) error { l := logutils.LoggerFromContext(ctx) - l.Debug("Creating route in AWS route-table...", + l.Info("Creating route in AWS route-table...", + zap.String("route_table_id", routeTable), zap.String("destination_cidr_block", cidr), zap.String("network_interface_id", networkInterfaceID), - zap.String("route_table_id", routeTable), ) _, err := cli.ec2.CreateRoute(ctx, &ec2.CreateRouteInput{ @@ -148,9 +186,9 @@ func (cli *Client) CreateRoute( if err != nil { l.Error("Failed to create route in AWS route-table", zap.Error(err), + zap.String("route_table_id", routeTable), zap.String("destination_cidr_block", cidr), zap.String("network_interface_id", networkInterfaceID), - zap.String("route_table_id", routeTable), ) } return err @@ -167,11 +205,28 @@ func (cli *Client) DeleteRoute( l := logutils.LoggerFromContext(ctx) - l.Debug("Deleting route in AWS route-table...", + l.Warn("Deleting route in AWS route-table...", + // route-table + zap.String("route_table_id", routeTable), + // destination zap.String("destination_cidr_block", aws.ToString(route.DestinationCidrBlock)), zap.String("destination_ipv6_cidr_block", aws.ToString(route.DestinationIpv6CidrBlock)), zap.String("destination_prefix_list_id", aws.ToString(route.DestinationPrefixListId)), - zap.String("route_table", routeTable), + // next hop + zap.String("carrier_gateway_id", aws.ToString(route.CarrierGatewayId)), + zap.String("core_network_arn", aws.ToString(route.CoreNetworkArn)), + zap.String("egress_only_internet_gateway_id", aws.ToString(route.EgressOnlyInternetGatewayId)), + zap.String("gateway_id", aws.ToString(route.GatewayId)), + zap.String("instance_id", aws.ToString(route.InstanceId)), + zap.String("instance_owner_id", aws.ToString(route.InstanceOwnerId)), + zap.String("local_gateway_id", aws.ToString(route.LocalGatewayId)), + zap.String("nat_gateway_id", aws.ToString(route.NatGatewayId)), + zap.String("network_interface_id", aws.ToString(route.NetworkInterfaceId)), + zap.String("transit_gateway_id", aws.ToString(route.TransitGatewayId)), + zap.String("vpc_peering_connection_id", aws.ToString(route.VpcPeeringConnectionId)), + // rest + zap.String("origin", string(route.Origin)), + zap.String("state", string(route.State)), ) _, err := cli.ec2.DeleteRoute(ctx, &ec2.DeleteRouteInput{ @@ -182,10 +237,27 @@ func (cli *Client) DeleteRoute( }) if err != nil { l.Error("Failed to delete route in AWS route-table", + // route-table + zap.String("route_table", routeTable), + // destination zap.String("destination_cidr_block", aws.ToString(route.DestinationCidrBlock)), zap.String("destination_ipv6_cidr_block", aws.ToString(route.DestinationIpv6CidrBlock)), zap.String("destination_prefix_list_id", aws.ToString(route.DestinationPrefixListId)), - zap.String("route_table", routeTable), + // next hop + zap.String("carrier_gateway_id", aws.ToString(route.CarrierGatewayId)), + zap.String("core_network_arn", aws.ToString(route.CoreNetworkArn)), + zap.String("egress_only_internet_gateway_id", aws.ToString(route.EgressOnlyInternetGatewayId)), + zap.String("gateway_id", aws.ToString(route.GatewayId)), + zap.String("instance_id", aws.ToString(route.InstanceId)), + zap.String("instance_owner_id", aws.ToString(route.InstanceOwnerId)), + zap.String("local_gateway_id", aws.ToString(route.LocalGatewayId)), + zap.String("nat_gateway_id", aws.ToString(route.NatGatewayId)), + zap.String("network_interface_id", aws.ToString(route.NetworkInterfaceId)), + zap.String("transit_gateway_id", aws.ToString(route.TransitGatewayId)), + zap.String("vpc_peering_connection_id", aws.ToString(route.VpcPeeringConnectionId)), + // rest + zap.String("origin", string(route.Origin)), + zap.String("state", string(route.State)), ) } return err diff --git a/gcp/route.go b/gcp/route.go index 80b9dd3..b6305bb 100644 --- a/gcp/route.go +++ b/gcp/route.go @@ -59,7 +59,7 @@ func (cli *Client) CreateRoute( ) error { l := logutils.LoggerFromContext(ctx) - l.Debug("Inserting GCP route...", + l.Info("Inserting GCP route...", zap.String("description", utils.UnwrapString(route.Description)), zap.String("dest_range", utils.UnwrapString(route.DestRange)), zap.String("name", utils.UnwrapString(route.Name)), @@ -98,19 +98,42 @@ func (cli *Client) CreateRoute( func (cli *Client) DeleteRoute( ctx context.Context, - routeName string, + route *gcepb.Route, ) error { l := logutils.LoggerFromContext(ctx) - l.Debug("Deleting GCP route...", + l.Warn("Deleting GCP route...", + // project zap.String("project", cli.projectID), - zap.String("route_name", routeName), + // network + zap.String("network", utils.UnwrapString(route.Network)), + // route + zap.String("name", utils.UnwrapString(route.Name)), + zap.String("kind", utils.UnwrapString(route.Kind)), + zap.String("route_type", utils.UnwrapString(route.RouteType)), + // destination + zap.String("dest_range", utils.UnwrapString(route.DestRange)), + // next hop + zap.String("next_hop_gateway", utils.UnwrapString(route.NextHopGateway)), + zap.String("next_hop_hub", utils.UnwrapString(route.NextHopHub)), + zap.String("next_hop_ilb", utils.UnwrapString(route.NextHopIlb)), + zap.String("next_hop_instance", utils.UnwrapString(route.NextHopInstance)), + zap.String("next_hop_ip", utils.UnwrapString(route.NextHopIp)), + zap.String("next_hop_network", utils.UnwrapString(route.NextHopNetwork)), + zap.String("next_hop_peering", utils.UnwrapString(route.NextHopPeering)), + zap.String("next_hop_vpn_tunnel", utils.UnwrapString(route.NextHopVpnTunnel)), + // priority + zap.Uint32("priority", utils.UnwrapUint32(route.Priority)), + // tags + zap.Strings("tags", route.Tags), + // rest + zap.String("route_status", utils.UnwrapString(route.RouteStatus)), ) err := func() error { op, err := cli.routes.Delete(ctx, &gcepb.DeleteRouteRequest{ Project: cli.projectID, - Route: routeName, + Route: utils.UnwrapString(route.Name), }) if err != nil { return err @@ -118,10 +141,33 @@ func (cli *Client) DeleteRoute( return op.Wait(ctx) }() if err != nil { - l.Debug("Failed to delete GCP route...", + l.Error("Failed to delete GCP route...", zap.Error(err), + // project zap.String("project", cli.projectID), - zap.String("route_name", routeName), + // network + zap.String("network", utils.UnwrapString(route.Network)), + // route + zap.String("name", utils.UnwrapString(route.Name)), + zap.String("kind", utils.UnwrapString(route.Kind)), + zap.String("route_type", utils.UnwrapString(route.RouteType)), + // destination + zap.String("dest_range", utils.UnwrapString(route.DestRange)), + // next hop + zap.String("next_hop_gateway", utils.UnwrapString(route.NextHopGateway)), + zap.String("next_hop_hub", utils.UnwrapString(route.NextHopHub)), + zap.String("next_hop_ilb", utils.UnwrapString(route.NextHopIlb)), + zap.String("next_hop_instance", utils.UnwrapString(route.NextHopInstance)), + zap.String("next_hop_ip", utils.UnwrapString(route.NextHopIp)), + zap.String("next_hop_network", utils.UnwrapString(route.NextHopNetwork)), + zap.String("next_hop_peering", utils.UnwrapString(route.NextHopPeering)), + zap.String("next_hop_vpn_tunnel", utils.UnwrapString(route.NextHopVpnTunnel)), + // priority + zap.Uint32("priority", utils.UnwrapUint32(route.Priority)), + // tags + zap.Strings("tags", route.Tags), + // rest + zap.String("route_status", utils.UnwrapString(route.RouteStatus)), ) } return err diff --git a/job/aws.go b/job/aws.go index aeb33de..bffd78a 100644 --- a/job/aws.go +++ b/job/aws.go @@ -93,7 +93,7 @@ func (j *UpdateAWSRouteTables) updateRouteTable( } // route exists but with different next hop return utils.WithTimeout(ctx, j.Timeout, func(ctx context.Context) error { - return j.aws.UpdateRoute(ctx, routeTable, cidr, networkInterfaceID) + return j.aws.UpdateRoute(ctx, routeTable, route, cidr, networkInterfaceID) }) default: diff --git a/job/gcp.go b/job/gcp.go index c4e3d0f..4da9dec 100644 --- a/job/gcp.go +++ b/job/gcp.go @@ -65,7 +65,7 @@ func (j *UpdateGCPRoute) Execute(ctx context.Context) error { } // route exists but with different config => delete, then create err := utils.WithTimeout(ctx, j.Timeout, func(ctx context.Context) error { - return gcp.DeleteRoute(ctx, utils.UnwrapString(route.Name)) + return gcp.DeleteRoute(ctx, route) }) if err != nil { return err @@ -82,7 +82,7 @@ func (j *UpdateGCPRoute) Execute(ctx context.Context) error { if foundMatch { // we already found matching rule, so let's clean up the rest err := utils.WithTimeout(ctx, j.Timeout, func(ctx context.Context) error { - return gcp.DeleteRoute(ctx, utils.UnwrapString(route.Name)) + return gcp.DeleteRoute(ctx, route) }) if err != nil { errs = append(errs, err) @@ -94,7 +94,7 @@ func (j *UpdateGCPRoute) Execute(ctx context.Context) error { continue } err := utils.WithTimeout(ctx, j.Timeout, func(ctx context.Context) error { - return gcp.DeleteRoute(ctx, utils.UnwrapString(route.Name)) + return gcp.DeleteRoute(ctx, route) }) if err != nil { errs = append(errs, err)