Skip to content

Commit

Permalink
fixup peerings status
Browse files Browse the repository at this point in the history
  • Loading branch information
josvazg committed Feb 11, 2025
1 parent 0213d22 commit e94a672
Show file tree
Hide file tree
Showing 8 changed files with 184 additions and 159 deletions.
35 changes: 8 additions & 27 deletions api/v1/status/atlasnetworkpeering.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,40 +14,21 @@ type AtlasNetworkPeeringStatus struct {
Status string `json:"status,omitempty"`

// AWSStatus contains AWS only related status information
AWSStatus *AWSStatus `json:"awsStatus,omitempty"`
AWSStatus *AWSPeeringStatus `json:"awsStatus,omitempty"`

// AzureStatus contains Azure only related status information
AzureStatus *AzureStatus `json:"azureStatus,omitempty"`
AzureStatus *AzureContainerStatus `json:"azureStatus,omitempty"`

// GoogleStatus contains Google only related status information
GoogleStatus *GoogleStatus `json:"googleStatus,omitempty"`
// GCPStatus contains GCP only related status information
GCPStatus *GCPContainerStatus `json:"gcpStatus,omitempty"`
}

// AWSStatus contains AWS only related status for network peering & container
type AWSStatus struct {
// AWSPeeringStatus contains AWS only related status for network peering & container
type AWSPeeringStatus struct {
AWSContainerStatus `json:",inline"`

// ConnectionID is the AWS VPC peering connection ID
ConnectionID string `json:"connectionId,omitempty"`

// ContainerVPCId is the AWS Container VPC ID on the Atlas side
ContainerVpcID string `json:"containerVpcId,omitempty"`
}

// AzureStatus contains Azure only related status about the network container
type AzureStatus struct {
// AzureSubscriptionID is the Azure subcription id on the Atlas container
AzureSubscriptionID string `json:"azureSubscriptionId,omitempty"`

// VnetName is the Azure network name on the Atlas container
VnetName string `json:"vnetName,omitempty"`
}

// GoogleStatus contains Google only related status about the network container
type GoogleStatus struct {
// GCPProjectID is the Google Cloud Platform project id on the Atlas container
GCPProjectID string `json:"gcpProjectId,omitempty"`

// NetworkName is the Google network name on the Atlas container
NetworkName string `json:"networkName,omitempty"`
}

// +kubebuilder:object:generate=false
Expand Down
49 changes: 10 additions & 39 deletions api/v1/status/zz_generated.deepcopy.go

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

27 changes: 12 additions & 15 deletions config/crd/bases/atlas.mongodb.com_atlasnetworkpeerings.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -197,20 +197,19 @@ spec:
connectionId:
description: ConnectionID is the AWS VPC peering connection ID
type: string
containerVpcId:
description: ContainerVPCId is the AWS Container VPC ID on the
Atlas side
vpcId:
description: VpcID is AWS VPC id on the Atlas side
type: string
type: object
azureStatus:
description: AzureStatus contains Azure only related status information
properties:
azureSubscriptionId:
description: AzureSubscriptionID is the Azure subcription id on
the Atlas container
azureSubscriptionIDpcId:
description: AzureSubscriptionID is Azure Subscription id on the
Atlas side
type: string
vnetName:
description: VnetName is the Azure network name on the Atlas container
vNetName:
description: VnetName is Azure network on the Atlas side
type: string
type: object
conditions:
Expand Down Expand Up @@ -243,16 +242,14 @@ spec:
- type
type: object
type: array
googleStatus:
description: GoogleStatus contains Google only related status information
gcpStatus:
description: GCPStatus contains GCP only related status information
properties:
gcpProjectId:
description: GCPProjectID is the Google Cloud Platform project
id on the Atlas container
gcpProjectID:
description: GCPProjectID is GCP project on the Atlas side
type: string
networkName:
description: NetworkName is the Google network name on the Atlas
container
description: NetworkName is GCP network on the Atlas side
type: string
type: object
id:
Expand Down
2 changes: 1 addition & 1 deletion internal/controller/atlasnetworkpeering/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func (r *AtlasNetworkPeeringReconciler) handle(workflowCtx *workflow.Context, re
case !deleted && inAtlas:
return r.sync(workflowCtx, req, atlasPeer, container)
case deleted && inAtlas:
return r.delete(workflowCtx, req, atlasPeer)
return r.delete(workflowCtx, req, atlasPeer, container)
default: // deleted && !inAtlas
return r.unmanage(workflowCtx, req)
}
Expand Down
67 changes: 47 additions & 20 deletions internal/controller/atlasnetworkpeering/transition.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package atlasnetworkpeering

import (
"fmt"
"reflect"

ctrl "sigs.k8s.io/controller-runtime"

Expand All @@ -25,7 +24,10 @@ func (r *AtlasNetworkPeeringReconciler) create(workflowCtx *workflow.Context, re
if err != nil {
return r.terminate(workflowCtx, req.networkPeering, workflow.Internal, err)
}
workflowCtx.EnsureStatusOption(updatePeeringStatusOption(newPeer))
if err := customresource.ManageFinalizer(workflowCtx.Context, r.Client, req.networkPeering, customresource.SetFinalizer); err != nil {
return r.terminate(workflowCtx, req.networkPeering, workflow.AtlasFinalizerNotSet, err)
}
workflowCtx.EnsureStatusOption(updatePeeringStatusOption(newPeer, container))
return workflow.InProgress(
workflow.NetworkPeeringConnectionCreating,
fmt.Sprintf("Network Peering Connection %s is %s",
Expand All @@ -39,23 +41,24 @@ func (r *AtlasNetworkPeeringReconciler) sync(workflowCtx *workflow.Context, req
err := fmt.Errorf("peer connection failed: %s", atlasPeer.ErrorMessage)
return r.terminate(workflowCtx, req.networkPeering, workflow.Internal, err)
case !atlasPeer.Available():
return r.inProgress(workflowCtx, atlasPeer)
return r.inProgress(workflowCtx, atlasPeer, container)
}
if !reflect.DeepEqual(atlasPeer.AtlasNetworkPeeringConfig, &req.networkPeering.Spec.AtlasNetworkPeeringConfig) {
specPeer := networkpeering.NewNetworkPeer(atlasPeer.ID, &req.networkPeering.Spec.AtlasNetworkPeeringConfig)
if !networkpeering.CompareConfigs(atlasPeer, specPeer) {
return r.update(workflowCtx, req, container)
}
return r.ready(workflowCtx, req, atlasPeer)
return r.ready(workflowCtx, req, atlasPeer, container)
}

func (r *AtlasNetworkPeeringReconciler) update(workflowCtx *workflow.Context, req *reconcileRequest, container *networkcontainer.NetworkContainer) ctrl.Result {
updatedPeer, err := req.service.Update(workflowCtx.Context, req.projectID, req.networkPeering.Status.ID, container.ID, &req.networkPeering.Spec.AtlasNetworkPeeringConfig)
if err != nil {
return r.terminate(workflowCtx, req.networkPeering, workflow.Internal, err)
}
return r.inProgress(workflowCtx, updatedPeer)
return r.inProgress(workflowCtx, updatedPeer, container)
}

func (r *AtlasNetworkPeeringReconciler) delete(workflowCtx *workflow.Context, req *reconcileRequest, atlasPeer *networkpeering.NetworkPeer) ctrl.Result {
func (r *AtlasNetworkPeeringReconciler) delete(workflowCtx *workflow.Context, req *reconcileRequest, atlasPeer *networkpeering.NetworkPeer, container *networkcontainer.NetworkContainer) ctrl.Result {
id := req.networkPeering.Status.ID
peer := atlasPeer
if id != "" && !atlasPeer.Closing() {
Expand All @@ -70,7 +73,7 @@ func (r *AtlasNetworkPeeringReconciler) delete(workflowCtx *workflow.Context, re
}
peer = closingPeer
}
return r.inProgress(workflowCtx, peer)
return r.inProgress(workflowCtx, peer, container)
}

func (r *AtlasNetworkPeeringReconciler) unmanage(workflowCtx *workflow.Context, req *reconcileRequest) ctrl.Result {
Expand All @@ -82,21 +85,21 @@ func (r *AtlasNetworkPeeringReconciler) unmanage(workflowCtx *workflow.Context,
return workflow.Deleted().ReconcileResult()
}

func (r *AtlasNetworkPeeringReconciler) inProgress(workflowCtx *workflow.Context, peer *networkpeering.NetworkPeer) ctrl.Result {
workflowCtx.EnsureStatusOption(updatePeeringStatusOption(peer))
func (r *AtlasNetworkPeeringReconciler) inProgress(workflowCtx *workflow.Context, peer *networkpeering.NetworkPeer, container *networkcontainer.NetworkContainer) ctrl.Result {
workflowCtx.EnsureStatusOption(updatePeeringStatusOption(peer, container))

return workflow.InProgress(
workflow.NetworkPeeringConnectionPending,
fmt.Sprintf("Network Peering Connection %s is %s", peer.ID, peer.Status),
).ReconcileResult()
}

func (r *AtlasNetworkPeeringReconciler) ready(workflowCtx *workflow.Context, req *reconcileRequest, peer *networkpeering.NetworkPeer) ctrl.Result {
func (r *AtlasNetworkPeeringReconciler) ready(workflowCtx *workflow.Context, req *reconcileRequest, peer *networkpeering.NetworkPeer, container *networkcontainer.NetworkContainer) ctrl.Result {
if err := customresource.ManageFinalizer(workflowCtx.Context, r.Client, req.networkPeering, customresource.SetFinalizer); err != nil {
return r.terminate(workflowCtx, req.networkPeering, workflow.AtlasFinalizerNotSet, err)
}

workflowCtx.EnsureStatusOption(updatePeeringStatusOption(peer))
workflowCtx.EnsureStatusOption(updatePeeringStatusOption(peer, container))
workflowCtx.SetConditionTrue(api.ReadyType)

if req.networkPeering.Spec.ExternalProjectRef != nil {
Expand All @@ -121,21 +124,45 @@ func (r *AtlasNetworkPeeringReconciler) terminate(
return result.ReconcileResult()
}

func updatePeeringStatusOption(peer *networkpeering.NetworkPeer) status.AtlasNetworkPeeringStatusOption {
func updatePeeringStatusOption(peer *networkpeering.NetworkPeer, container *networkcontainer.NetworkContainer) status.AtlasNetworkPeeringStatusOption {
return func(peeringStatus *status.AtlasNetworkPeeringStatus) {
applyPeeringStatus(peeringStatus, peer)
applyPeeringStatus(peeringStatus, peer, container)
}
}

func applyPeeringStatus(peeringStatus *status.AtlasNetworkPeeringStatus, peer *networkpeering.NetworkPeer) {
func applyPeeringStatus(peeringStatus *status.AtlasNetworkPeeringStatus, peer *networkpeering.NetworkPeer, container *networkcontainer.NetworkContainer) {
peeringStatus.ID = peer.ID
peeringStatus.Status = peer.Status
providerName := provider.ProviderName(peer.Provider)
if providerName == provider.ProviderAWS && peer.AWSStatus != nil {
if peeringStatus.AWSStatus == nil {
peeringStatus.AWSStatus = &status.AWSStatus{}
switch provider.ProviderName(peer.Provider) {
case provider.ProviderAWS:
if peer.AWSStatus != nil {
if peeringStatus.AWSStatus == nil {
peeringStatus.AWSStatus = &status.AWSPeeringStatus{}
}
peeringStatus.AWSStatus.ConnectionID = peer.AWSStatus.ConnectionID
}
if container.AWSStatus != nil {
if peeringStatus.AWSStatus == nil {
peeringStatus.AWSStatus = &status.AWSPeeringStatus{}
}
peeringStatus.AWSStatus.VpcID = container.AWSStatus.VpcID
}
case provider.ProviderAzure:
if container.AzureStatus != nil {
if peeringStatus.AzureStatus == nil {
peeringStatus.AzureStatus = &status.AzureContainerStatus{}
}
peeringStatus.AzureStatus.AzureSubscriptionID = container.AzureStatus.AzureSubscriptionID
peeringStatus.AzureStatus.VnetName = container.AzureStatus.VnetName
}
case provider.ProviderGCP:
if container.GCPStatus != nil {
if peeringStatus.GCPStatus == nil {
peeringStatus.GCPStatus = &status.GCPContainerStatus{}
}
peeringStatus.GCPStatus.GCPProjectID = container.GCPStatus.GCPProjectID
peeringStatus.GCPStatus.NetworkName = container.GCPStatus.NetworkName
}
peeringStatus.AWSStatus.ConnectionID = peer.AWSStatus.ConnectionID
}
}

Expand Down
Loading

0 comments on commit e94a672

Please sign in to comment.