Skip to content

Commit

Permalink
Address cheina97 comments
Browse files Browse the repository at this point in the history
  • Loading branch information
claudiolor committed Oct 24, 2024
1 parent 8b0a88f commit c16a934
Showing 1 changed file with 103 additions and 72 deletions.
175 changes: 103 additions & 72 deletions pkg/liqo-controller-manager/core/foreigncluster-controller/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ import (
"github.com/liqotech/liqo/pkg/utils/pod"
)

type statusException struct {
liqov1beta1.ConditionStatusType
Reason string
Message string
}

func (r *ForeignClusterReconciler) clearStatusExceptConditions(foreignCluster *liqov1beta1.ForeignCluster) {
foreignCluster.Status = liqov1beta1.ForeignClusterStatus{
Role: liqov1beta1.UnknownRole,
Expand All @@ -55,54 +61,66 @@ func (r *ForeignClusterReconciler) clearStatusExceptConditions(foreignCluster *l
}
}

func (r *ForeignClusterReconciler) handleNetworkingModuleStatus(ctx context.Context,
fc *liqov1beta1.ForeignCluster, moduleEnabled bool) error {
if !moduleEnabled {
clearModule(&fc.Status.Modules.Networking)
return nil
}

func (r *ForeignClusterReconciler) handleConnectionStatus(ctx context.Context,
fc *liqov1beta1.ForeignCluster, statusExceptions map[liqov1beta1.ConditionType]statusException) error {
clusterID := fc.Spec.ClusterID
type StatusException struct {
liqov1beta1.ConditionStatusType
Reason string
Message string
}
statusExceptions := map[liqov1beta1.ConditionType]StatusException{}

_, err := getters.GetConfigurationByClusterID(ctx, r.Client, clusterID)
connection, err := getters.GetConnectionByClusterID(ctx, r.Client, string(clusterID))
switch {
case errors.IsNotFound(err):
klog.V(6).Infof("Configuration resource not found for ForeignCluster %q", clusterID)
fcutils.DeleteModuleCondition(&fc.Status.Modules.Networking, liqov1beta1.NetworkConfigurationStatusCondition)
statusExceptions[liqov1beta1.NetworkConfigurationStatusCondition] = StatusException{
klog.V(6).Infof("Connection resource not found for ForeignCluster %q", clusterID)
fcutils.DeleteModuleCondition(&fc.Status.Modules.Networking, liqov1beta1.NetworkConnectionStatusCondition)
statusExceptions[liqov1beta1.NetworkConnectionStatusCondition] = statusException{
ConditionStatusType: liqov1beta1.ConditionStatusNotReady,
Reason: networkConfigurationMissingReason,
Message: networkConfigurationMissingMessage,
Reason: connectionMissingReason,
Message: connectionMissingMessage,
}
case err != nil:
klog.Errorf("an error occurred while getting the Configuration resource for the ForeignCluster %q: %s", clusterID, err)
klog.Errorf("an error occurred while getting the Connection resource for the ForeignCluster %q: %s", clusterID, err)
return err
default:
fcutils.EnableModuleNetworking(fc)
fcutils.EnsureModuleCondition(&fc.Status.Modules.Networking,
liqov1beta1.NetworkConfigurationStatusCondition, liqov1beta1.ConditionStatusReady,
networkConfigurationStatusReason, networkConfigurationStatusMessage)
switch connection.Status.Value {
case networkingv1beta1.Connected:
fcutils.EnsureModuleCondition(&fc.Status.Modules.Networking,
liqov1beta1.NetworkConnectionStatusCondition, liqov1beta1.ConditionStatusEstablished,
connectionEstablishedReason, connectionEstablishedMessage)
case networkingv1beta1.Connecting:
fcutils.EnsureModuleCondition(&fc.Status.Modules.Networking,
liqov1beta1.NetworkConnectionStatusCondition, liqov1beta1.ConditionStatusPending,
connectionPendingReason, connectionPendingMessage)
default:
fcutils.EnsureModuleCondition(&fc.Status.Modules.Networking,
liqov1beta1.NetworkConnectionStatusCondition, liqov1beta1.ConditionStatusError,
connectionErrorReason, connectionErrorMessage)
}
}
return nil
}

gwServer, err := getters.GetGatewayServerByClusterID(ctx, r.Client, clusterID)
switch {
case errors.IsNotFound(err):
klog.V(6).Infof("GatewayServer resource not found for ForeignCluster %q", clusterID)
fcutils.DeleteModuleCondition(&fc.Status.Modules.Networking, liqov1beta1.NetworkGatewayServerStatusCondition)
statusExceptions[liqov1beta1.NetworkGatewayServerStatusCondition] = StatusException{
func (r *ForeignClusterReconciler) handleGatewaysStatus(ctx context.Context,
fc *liqov1beta1.ForeignCluster, statusExceptions map[liqov1beta1.ConditionType]statusException) error {
clusterID := fc.Spec.ClusterID

gwServer, errServer := getters.GetGatewayServerByClusterID(ctx, r.Client, clusterID)
gwClient, errClient := getters.GetGatewayClientByClusterID(ctx, r.Client, clusterID)

if errors.IsNotFound(errServer) && errors.IsNotFound(errClient) {
klog.V(6).Infof("Both GatewayServer and GatewayClient resources not found for ForeignCluster %q", clusterID)
statusExceptions[liqov1beta1.NetworkGatewayClientStatusCondition] = statusException{
ConditionStatusType: liqov1beta1.ConditionStatusNotReady,
Reason: gatewayMissingReason,
Message: gatewayMissingMessage,
}
case err != nil:
klog.Errorf("an error occurred while getting the GatewayServer resource for the ForeignCluster %q: %s", clusterID, err)
return err
}

switch {
case errors.IsNotFound(errServer):
klog.V(6).Infof("GatewayServer resource not found for ForeignCluster %q", clusterID)
fcutils.DeleteModuleCondition(&fc.Status.Modules.Networking, liqov1beta1.NetworkGatewayServerStatusCondition)
case errServer != nil:
klog.Errorf("an error occurred while getting the GatewayServer resource for the ForeignCluster %q: %s", clusterID, errServer)
return errServer
default:
fcutils.EnableModuleNetworking(fc)
gwDeployment := &appsv1.Deployment{
Expand Down Expand Up @@ -131,50 +149,14 @@ func (r *ForeignClusterReconciler) handleNetworkingModuleStatus(ctx context.Cont
}
}

connection, err := getters.GetConnectionByClusterID(ctx, r.Client, string(clusterID))
switch {
case errors.IsNotFound(err):
klog.V(6).Infof("Connection resource not found for ForeignCluster %q", clusterID)
fcutils.DeleteModuleCondition(&fc.Status.Modules.Networking, liqov1beta1.NetworkConnectionStatusCondition)
statusExceptions[liqov1beta1.NetworkConnectionStatusCondition] = StatusException{
ConditionStatusType: liqov1beta1.ConditionStatusNotReady,
Reason: connectionMissingReason,
Message: connectionMissingMessage,
}
case err != nil:
klog.Errorf("an error occurred while getting the Connection resource for the ForeignCluster %q: %s", clusterID, err)
return err
default:
fcutils.EnableModuleNetworking(fc)
switch connection.Status.Value {
case networkingv1beta1.Connected:
fcutils.EnsureModuleCondition(&fc.Status.Modules.Networking,
liqov1beta1.NetworkConnectionStatusCondition, liqov1beta1.ConditionStatusEstablished,
connectionEstablishedReason, connectionEstablishedMessage)
case networkingv1beta1.Connecting:
fcutils.EnsureModuleCondition(&fc.Status.Modules.Networking,
liqov1beta1.NetworkConnectionStatusCondition, liqov1beta1.ConditionStatusPending,
connectionPendingReason, connectionPendingMessage)
default:
fcutils.EnsureModuleCondition(&fc.Status.Modules.Networking,
liqov1beta1.NetworkConnectionStatusCondition, liqov1beta1.ConditionStatusError,
connectionErrorReason, connectionErrorMessage)
}
}

gwClient, err := getters.GetGatewayClientByClusterID(ctx, r.Client, clusterID)
switch {
case errors.IsNotFound(err):
case errors.IsNotFound(errClient):
klog.V(6).Infof("GatewayClient resource not found for ForeignCluster %q", clusterID)
fcutils.DeleteModuleCondition(&fc.Status.Modules.Networking, liqov1beta1.NetworkGatewayClientStatusCondition)
// Do not write any exception, as if both gateways are missing we write a single condition telling that a gateway is missing.
// If instead, only the gateway client is missing, then this cluster acts as server.
case err != nil:
klog.Errorf("an error occurred while getting the GatewayClient resource for the ForeignCluster %q: %s", clusterID, err)
return err
case errClient != nil:
klog.Errorf("an error occurred while getting the GatewayClient resource for the ForeignCluster %q: %s", clusterID, errClient)
return errClient
default:
// Delete the exceptions with the Gateway, if any, if this cluster has the gateway client
delete(statusExceptions, liqov1beta1.NetworkGatewayServerStatusCondition)
fcutils.EnableModuleNetworking(fc)
gwDeployment := &appsv1.Deployment{
ObjectMeta: metav1.ObjectMeta{
Expand Down Expand Up @@ -202,6 +184,55 @@ func (r *ForeignClusterReconciler) handleNetworkingModuleStatus(ctx context.Cont
}
}

return nil
}

func (r *ForeignClusterReconciler) handleNetworkConfigurationStatus(ctx context.Context,
fc *liqov1beta1.ForeignCluster, statusExceptions map[liqov1beta1.ConditionType]statusException) error {
clusterID := fc.Spec.ClusterID
_, err := getters.GetConfigurationByClusterID(ctx, r.Client, clusterID)
switch {
case errors.IsNotFound(err):
klog.V(6).Infof("Configuration resource not found for ForeignCluster %q", clusterID)
fcutils.DeleteModuleCondition(&fc.Status.Modules.Networking, liqov1beta1.NetworkConfigurationStatusCondition)
statusExceptions[liqov1beta1.NetworkConfigurationStatusCondition] = statusException{
ConditionStatusType: liqov1beta1.ConditionStatusNotReady,
Reason: networkConfigurationMissingReason,
Message: networkConfigurationMissingMessage,
}
case err != nil:
klog.Errorf("an error occurred while getting the Configuration resource for the ForeignCluster %q: %s", clusterID, err)
return err
default:
fcutils.EnableModuleNetworking(fc)
fcutils.EnsureModuleCondition(&fc.Status.Modules.Networking,
liqov1beta1.NetworkConfigurationStatusCondition, liqov1beta1.ConditionStatusReady,
networkConfigurationStatusReason, networkConfigurationStatusMessage)
}
return nil
}

func (r *ForeignClusterReconciler) handleNetworkingModuleStatus(ctx context.Context,
fc *liqov1beta1.ForeignCluster, moduleEnabled bool) error {
if !moduleEnabled {
clearModule(&fc.Status.Modules.Networking)
return nil
}

statusExceptions := map[liqov1beta1.ConditionType]statusException{}

if err := r.handleNetworkConfigurationStatus(ctx, fc, statusExceptions); err != nil {
return err
}

if err := r.handleGatewaysStatus(ctx, fc, statusExceptions); err != nil {
return err
}

if err := r.handleConnectionStatus(ctx, fc, statusExceptions); err != nil {
return err
}

// Write the exception in the status if the module is enabled
if fc.Status.Modules.Networking.Enabled {
for condition, condDescription := range statusExceptions {
Expand Down

0 comments on commit c16a934

Please sign in to comment.