diff --git a/apis/virtualKubelet/v1alpha1/namespacemap_types.go b/apis/virtualKubelet/v1alpha1/namespacemap_types.go index 2a357a81bf..ea36d68257 100644 --- a/apis/virtualKubelet/v1alpha1/namespacemap_types.go +++ b/apis/virtualKubelet/v1alpha1/namespacemap_types.go @@ -20,21 +20,23 @@ import ( metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" ) -type mappingPhase string +// MappingPhase indicates the status of the remote namespace. +type MappingPhase string const ( // MappingAccepted indicates that a remote namespace is successfully created. - MappingAccepted mappingPhase = "Accepted" - // MappingRefused indicates that at the moment is impossible to create a remote namespace. - MappingRefused mappingPhase = "Refused" + MappingAccepted MappingPhase = "Accepted" + // MappingCreationLoopBackOff indicates that at the moment is impossible to create a remote namespace. + MappingCreationLoopBackOff MappingPhase = "CreationLoopBackOff" ) // RemoteNamespaceStatus contains some information about remote namespace status. type RemoteNamespaceStatus struct { - // RemoteNamespace is the name chosen by the user at creation time (when he puts mapping label on his local namespace). + // RemoteNamespace is the name chosen by the user at creation time according to NamespaceMappingStrategy RemoteNamespace string `json:"remoteNamespace,omitempty"` // Phase is the remote Namespace's actual status (Accepted,Refused). - Phase mappingPhase `json:"phase,omitempty"` + // +kubebuilder:validation:Enum="Accepted";"CreationLoopBackOff" + Phase MappingPhase `json:"phase,omitempty"` } // NamespaceMapSpec defines the desired state of NamespaceMap. diff --git a/deployments/liqo/crds/virtualkubelet.liqo.io_namespacemaps.yaml b/deployments/liqo/crds/virtualkubelet.liqo.io_namespacemaps.yaml index 35c6ed6b1c..2cf6d84b0c 100644 --- a/deployments/liqo/crds/virtualkubelet.liqo.io_namespacemaps.yaml +++ b/deployments/liqo/crds/virtualkubelet.liqo.io_namespacemaps.yaml @@ -56,11 +56,13 @@ spec: properties: phase: description: Phase is the remote Namespace's actual status (Accepted,Refused). + enum: + - Accepted + - CreationLoopBackOff type: string remoteNamespace: description: RemoteNamespace is the name chosen by the user - at creation time (when he puts mapping label on his local - namespace). + at creation time according to NamespaceMappingStrategy type: string type: object description: CurrentMapping is filled by NamespaceMap Controller, diff --git a/pkg/utils/namespaceOffloading_conditions_update.go b/pkg/utils/namespaceOffloading_conditions_update.go new file mode 100644 index 0000000000..cf4f25eed3 --- /dev/null +++ b/pkg/utils/namespaceOffloading_conditions_update.go @@ -0,0 +1,48 @@ +package utils + +import ( + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + + offv1alpha1 "github.com/liqotech/liqo/apis/offloading/v1alpha1" +) + +// UpdateNamespaceOffloadingCondition updates the content of the given condition for the selected Cluster ID. +func UpdateNamespaceOffloadingCondition(status *offv1alpha1.NamespaceOffloadingStatus, + newCondition *offv1alpha1.RemoteNamespaceCondition, clusterID string) bool { + if status.RemoteNamespacesConditions == nil { + status.RemoteNamespacesConditions = map[string]offv1alpha1.RemoteNamespaceConditions{} + } + + oldCondition := GetNamespaceOffloadingCondition(status.RemoteNamespacesConditions[clusterID], newCondition.Type) + + if oldCondition == nil { + newCondition.LastTransitionTime = metav1.Now() + status.RemoteNamespacesConditions[clusterID] = append(status.RemoteNamespacesConditions[clusterID], + *newCondition) + return true + } + + if oldCondition.Status != newCondition.Status || + oldCondition.Message != newCondition.Message || oldCondition.Reason != newCondition.Reason { + if oldCondition.Status != newCondition.Status { + oldCondition.LastTransitionTime = metav1.Now() + } + oldCondition.Status = newCondition.Status + oldCondition.Reason = newCondition.Reason + oldCondition.Message = newCondition.Message + return true + } + + return false +} + +// GetNamespaceOffloadingCondition get the content of the specific condition. +func GetNamespaceOffloadingCondition(conditions []offv1alpha1.RemoteNamespaceCondition, + conditionType offv1alpha1.RemoteNamespaceConditionType) *offv1alpha1.RemoteNamespaceCondition { + for i := range conditions { + if conditions[i].Type == conditionType { + return &(conditions[i]) + } + } + return nil +}