Skip to content

Commit

Permalink
NamespaceMap refactor and some util functions
Browse files Browse the repository at this point in the history
- MappingPhase type now can be exported.
- Add Enum validation for Phase field, in NamespaceMap status.
- Add two util functions to get and update remote namespaces' conditions.
  • Loading branch information
Andreagit97 committed May 20, 2021
1 parent cc05ac0 commit 716e0e9
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 7 deletions.
14 changes: 9 additions & 5 deletions apis/virtualKubelet/v1alpha1/namespacemap_types.go
Expand Up @@ -20,21 +20,25 @@ 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"
MappingAccepted MappingPhase = "Accepted"
// MappingRefused indicates that at the moment is impossible to create a remote namespace.
MappingRefused mappingPhase = "Refused"
MappingRefused MappingPhase = "Refused"
)

// 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";"Refused"
Phase MappingPhase `json:"phase,omitempty"`
// Message gives some information about remote Namespace creation (e.g. why the creation has failed)
Message string `json:"message,omitempty"`
}

// NamespaceMapSpec defines the desired state of NamespaceMap.
Expand Down
10 changes: 8 additions & 2 deletions deployments/liqo/crds/virtualkubelet.liqo.io_namespacemaps.yaml
Expand Up @@ -54,13 +54,19 @@ spec:
description: RemoteNamespaceStatus contains some information about
remote namespace status.
properties:
message:
description: Message gives some information about remote Namespace
creation (e.g. why the creation has failed)
type: string
phase:
description: Phase is the remote Namespace's actual status (Accepted,Refused).
enum:
- Accepted
- Refused
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,
Expand Down
49 changes: 49 additions & 0 deletions pkg/utils/namespaceOffloading_conditions_update.go
@@ -0,0 +1,49 @@
package utils

import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

offv1alpha1 "github.com/liqotech/liqo/apis/offloading/v1alpha1"
)

// UpdateNamespaceOffloadingCondition -> if present update condition of that type, otherwise create a new one.
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 condition of a certain type if it is present.
func GetNamespaceOffloadingCondition(conditions []offv1alpha1.RemoteNamespaceCondition,
conditionType offv1alpha1.RemoteNamespaceConditionType) *offv1alpha1.RemoteNamespaceCondition {
for i := range conditions {
if conditions[i].Type == conditionType {
return &(conditions[i])
}
}
return nil
}

0 comments on commit 716e0e9

Please sign in to comment.