Skip to content

Commit

Permalink
request withdrawal
Browse files Browse the repository at this point in the history
  • Loading branch information
aleoli committed Jul 5, 2021
1 parent c9f4198 commit eced38e
Show file tree
Hide file tree
Showing 10 changed files with 49 additions and 86 deletions.
13 changes: 4 additions & 9 deletions apis/discovery/v1alpha1/resourcerequest_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"k8s.io/client-go/kubernetes/scheme"

advtypes "github.com/liqotech/liqo/apis/sharing/v1alpha1"
"github.com/liqotech/liqo/pkg/consts"
crdclient "github.com/liqotech/liqo/pkg/crdClient"
object_references "github.com/liqotech/liqo/pkg/object-references"
)
Expand All @@ -23,20 +22,16 @@ type ResourceRequestSpec struct {
ClusterIdentity ClusterIdentity `json:"clusterIdentity"`
// Local auth service address
AuthURL string `json:"authUrl"`
// OfferPhase indicate the desired state for the ResourceOffer
// +kubebuilder:validation:Enum="Create";"Delete"
// +kubebuilder:default="Create"
OfferPhase consts.ResourceDesiredPhase `json:"offerPhase,omitempty"`
// WithdrawalTimestamp is set when a graceful deletion is requested by the user.
WithdrawalTimestamp *metav1.Time `json:"withdrawalTimestamp,omitempty"`
}

// ResourceRequestStatus defines the observed state of ResourceRequest.
type ResourceRequestStatus struct {
BroadcasterRef *object_references.DeploymentReference `json:"broadcasterRef,omitempty"`
AdvertisementStatus advtypes.AdvPhase `json:"advertisementStatus,omitempty"`
// OfferPhase indicate the observed state for the ResourceOffer
// +kubebuilder:validation:Enum="Created";"Deleted"
// +kubebuilder:default="Deleted"
OfferPhase consts.ResourceObservedPhase `json:"offerPhase,omitempty"`
// OfferWithdrawalTimestamp is the withdrawal timestamp of the child ResourceOffer resource.
OfferWithdrawalTimestamp *metav1.Time `json:"offerWithdrawalTimestamp,omitempty"`
}

// +kubebuilder:object:root=true
Expand Down
10 changes: 9 additions & 1 deletion apis/discovery/v1alpha1/zz_generated.deepcopy.go

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

20 changes: 8 additions & 12 deletions deployments/liqo/crds/discovery.liqo.io_resourcerequests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,10 @@ spec:
required:
- clusterID
type: object
offerPhase:
default: Create
description: OfferPhase indicate the desired state for the ResourceOffer
enum:
- Create
- Delete
withdrawalTimestamp:
description: WithdrawalTimestamp is set when a graceful deletion is
requested by the user.
format: date-time
type: string
required:
- authUrl
Expand All @@ -82,12 +80,10 @@ spec:
name must be unique.
type: string
type: object
offerPhase:
default: Deleted
description: OfferPhase indicate the observed state for the ResourceOffer
enum:
- Created
- Deleted
offerWithdrawalTimestamp:
description: OfferWithdrawalTimestamp is the withdrawal timestamp
of the child ResourceOffer resource.
format: date-time
type: string
type: object
type: object
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -356,26 +356,24 @@ func (r *ForeignClusterReconciler) unpeerNamespaced(ctx context.Context,
return err
}

switch resourceRequest.Status.OfferPhase {
case liqoconst.ResourceObservedPhaseCreated:
resourceRequest.Spec.OfferPhase = liqoconst.ResourceDesiredPhaseDelete
if resourceRequest.Status.OfferWithdrawalTimestamp.IsZero() {
if resourceRequest.Spec.WithdrawalTimestamp.IsZero() {
now := metav1.Now()
resourceRequest.Spec.WithdrawalTimestamp = &now
}
err = r.Client.Update(ctx, &resourceRequest)
if err != nil && !errors.IsNotFound(err) {
klog.Error(err)
return err
}
foreignCluster.Status.Outgoing.PeeringPhase = discoveryv1alpha1.PeeringPhaseDisconnecting
case liqoconst.ResourceObservedPhaseDeleted:
} else {
err = r.deleteResourceRequest(ctx, foreignCluster)
if err != nil && !errors.IsNotFound(err) {
klog.Error(err)
return err
}
foreignCluster.Status.Outgoing.PeeringPhase = discoveryv1alpha1.PeeringPhaseNone
default:
err := fmt.Errorf("unknown phase %v", resourceRequest.Status.OfferPhase)
klog.Error(err)
return err
}
return nil
}
Expand Down Expand Up @@ -433,11 +431,9 @@ func getPeeringPhase(resourceRequestList *discoveryv1alpha1.ResourceRequestList)
return discoveryv1alpha1.PeeringPhaseNone, nil
case 1:
resourceRequest := &resourceRequestList.Items[0]
desired := resourceRequest.Spec.OfferPhase
observed := resourceRequest.Status.OfferPhase
if desired == liqoconst.ResourceDesiredPhaseCreate && observed == liqoconst.ResourceObservedPhaseDeleted {
return discoveryv1alpha1.PeeringPhasePending, nil
} else if desired == liqoconst.ResourceDesiredPhaseDelete && observed == liqoconst.ResourceObservedPhaseCreated {
desiredDelete := !resourceRequest.Spec.WithdrawalTimestamp.IsZero()
deleted := !resourceRequest.Status.OfferWithdrawalTimestamp.IsZero()
if desiredDelete && !deleted {
return discoveryv1alpha1.PeeringPhaseDisconnecting, nil
}
return discoveryv1alpha1.PeeringPhaseEstablished, nil
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import (
discoveryv1alpha1 "github.com/liqotech/liqo/apis/discovery/v1alpha1"
crdreplicator "github.com/liqotech/liqo/internal/crdReplicator"
"github.com/liqotech/liqo/pkg/clusterid/test"
"github.com/liqotech/liqo/pkg/consts"
"github.com/liqotech/liqo/pkg/discovery"
identitymanager "github.com/liqotech/liqo/pkg/identityManager"
tenantcontrolnamespace "github.com/liqotech/liqo/pkg/tenantControlNamespace"
Expand Down Expand Up @@ -59,6 +58,8 @@ var _ = Describe("ForeignClusterOperator", func() {
mgr manager.Manager
ctx context.Context
cancel context.CancelFunc

now = metav1.Now()
)

BeforeEach(func() {
Expand Down Expand Up @@ -423,7 +424,6 @@ var _ = Describe("ForeignClusterOperator", func() {
// populate the resourcerequest CR
c.rr.Name = controller.clusterID.GetClusterID()
c.rr.Spec.ClusterIdentity.ClusterID = c.fc.Spec.ClusterIdentity.ClusterID
c.rr.Spec.OfferPhase = consts.ResourceDesiredPhaseCreate
c.rr.Labels = resourceRequestLabels(c.fc.Spec.ClusterIdentity.ClusterID)

// create the foreigncluster CR
Expand Down Expand Up @@ -454,7 +454,6 @@ var _ = Describe("ForeignClusterOperator", func() {
Expect(rr).NotTo(BeNil())

// set the ResourceRequest status to created
rr.Status.OfferPhase = consts.ResourceObservedPhaseCreated
obj, err = controller.crdClient.Resource("resourcerequests").Namespace(tenantNamespace.Name).UpdateStatus(rr.Name, rr, &metav1.UpdateOptions{})
Expect(err).To(BeNil())
Expect(obj).NotTo(BeNil())
Expand Down Expand Up @@ -484,12 +483,12 @@ var _ = Describe("ForeignClusterOperator", func() {
// and the resource request has been set for deletion in the correct namespace
Expect(len(rrs.Items)).To(c.expectedPeeringLength)
if len(rrs.Items) > 0 {
Expect(rrs.Items[0].Spec.OfferPhase).To(Equal(consts.ResourceDesiredPhaseDelete))
Expect(rrs.Items[0].Spec.WithdrawalTimestamp.IsZero()).To(BeFalse())
rr = &rrs.Items[0]
}

// set the ResourceRequest status to deleted
rr.Status.OfferPhase = consts.ResourceObservedPhaseDeleted
rr.Status.OfferWithdrawalTimestamp = &now
obj, err = controller.crdClient.Resource("resourcerequests").Namespace(tenantNamespace.Name).UpdateStatus(rr.Name, rr, &metav1.UpdateOptions{})
Expect(err).To(BeNil())
Expect(obj).NotTo(BeNil())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (

discoveryv1alpha1 "github.com/liqotech/liqo/apis/discovery/v1alpha1"
crdreplicator "github.com/liqotech/liqo/internal/crdReplicator"
"github.com/liqotech/liqo/pkg/consts"
)

// createResourceRequest creates a resource request to be sent to the specified ForeignCluster.
Expand Down Expand Up @@ -56,8 +55,7 @@ func (r *ForeignClusterReconciler) createResourceRequest(ctx context.Context,
ClusterID: localClusterID,
ClusterName: r.ConfigProvider.GetConfig().ClusterName,
},
AuthURL: authURL,
OfferPhase: consts.ResourceDesiredPhaseCreate,
AuthURL: authURL,
}

return controllerutil.SetControllerReference(foreignCluster, resourceRequest, r.Scheme)
Expand All @@ -66,8 +64,8 @@ func (r *ForeignClusterReconciler) createResourceRequest(ctx context.Context,
klog.Error(err)
return controllerutil.OperationResultNone, err
}
klog.Infof("[%v] ensured the existence of ResourceRequest with phase %v (with %v operation)",
remoteClusterID, result, resourceRequest.Spec.OfferPhase)
klog.Infof("[%v] ensured the existence of ResourceRequest (with %v operation)",
remoteClusterID, result)

return result, nil
}
Expand Down
11 changes: 2 additions & 9 deletions internal/resource-request-operator/resourceRequest_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package resourcerequestoperator

import (
"context"
"fmt"
"time"

"k8s.io/apimachinery/pkg/runtime"
Expand All @@ -15,7 +14,6 @@ import (
discoveryv1alpha1 "github.com/liqotech/liqo/apis/discovery/v1alpha1"
sharingv1alpha1 "github.com/liqotech/liqo/apis/sharing/v1alpha1"
crdreplicator "github.com/liqotech/liqo/internal/crdReplicator"
"github.com/liqotech/liqo/pkg/consts"
)

// ResourceRequestReconciler reconciles a ResourceRequest object.
Expand Down Expand Up @@ -90,23 +88,18 @@ func (r *ResourceRequestReconciler) Reconcile(ctx context.Context, req ctrl.Requ
}
}()

switch resourceRequest.Spec.OfferPhase {
case consts.ResourceDesiredPhaseCreate:
if resourceRequest.Spec.WithdrawalTimestamp.IsZero() {
err = r.generateResourceOffer(ctx, &resourceRequest)
if err != nil {
klog.Errorf("%s -> Error generating resourceOffer: %s", remoteClusterID, err)
return ctrl.Result{}, err
}
case consts.ResourceDesiredPhaseDelete:
} else {
err = r.invalidateResourceOffer(ctx, &resourceRequest)
if err != nil {
klog.Errorf("%s -> Error invalidating resourceOffer: %s", remoteClusterID, err)
return ctrl.Result{}, err
}
default:
err = fmt.Errorf("unknown phase %v", resourceRequest.Spec.OfferPhase)
klog.Error(err)
return ctrl.Result{}, err
}

return ctrl.Result{}, nil
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
discoveryv1alpha1 "github.com/liqotech/liqo/apis/discovery/v1alpha1"
sharingv1alpha1 "github.com/liqotech/liqo/apis/sharing/v1alpha1"
crdreplicator "github.com/liqotech/liqo/internal/crdReplicator"
"github.com/liqotech/liqo/pkg/consts"
"github.com/liqotech/liqo/pkg/discovery"
"github.com/liqotech/liqo/pkg/virtualKubelet/forge"
)
Expand All @@ -34,6 +33,10 @@ const (
homeClusterID = "2468825c-0f62-44d7-bed1-9a7bc331c0b0"
)

var (
now = metav1.Now()
)

func createTestNodes() (*corev1.Node, *corev1.Node) {
resources := corev1.ResourceList{}
resources[corev1.ResourceCPU] = *resource.NewQuantity(2, resource.DecimalSI)
Expand Down Expand Up @@ -162,7 +165,6 @@ func createResourceRequest() *discoveryv1alpha1.ResourceRequest {
ClusterIdentity: discoveryv1alpha1.ClusterIdentity{
ClusterID: homeClusterID,
},
OfferPhase: consts.ResourceDesiredPhaseCreate,
},
}
Expect(k8sClient.Create(ctx, resourceRequest)).Should(Succeed())
Expand Down Expand Up @@ -213,7 +215,7 @@ var _ = Describe("ResourceRequest Operator", func() {
return resourceRequest.Finalizers
}, timeout, interval).Should(ContainElement(tenantFinalizer))

Expect(resourceRequest.Status.OfferPhase).To(Equal(consts.ResourceObservedPhaseCreated))
Expect(resourceRequest.Status.OfferWithdrawalTimestamp.IsZero()).To(BeTrue())

By("Checking Tenant creation")
var tenant capsulev1alpha1.Tenant
Expand Down Expand Up @@ -271,7 +273,7 @@ var _ = Describe("ResourceRequest Operator", func() {
}

By("Checking ResourceOffer invalidation on request set deleting phase")
resourceRequest.Spec.OfferPhase = consts.ResourceDesiredPhaseDelete
resourceRequest.Spec.WithdrawalTimestamp = &now
Expect(k8sClient.Update(ctx, &resourceRequest)).ToNot(HaveOccurred())

// set the vk status in the ResourceOffer to created
Expand Down
7 changes: 4 additions & 3 deletions internal/resource-request-operator/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import (
discoveryv1alpha1 "github.com/liqotech/liqo/apis/discovery/v1alpha1"
sharingv1alpha1 "github.com/liqotech/liqo/apis/sharing/v1alpha1"
crdreplicator "github.com/liqotech/liqo/internal/crdReplicator"
"github.com/liqotech/liqo/pkg/consts"
"github.com/liqotech/liqo/pkg/discovery"
)

Expand Down Expand Up @@ -55,7 +54,6 @@ func (r *ResourceRequestReconciler) generateResourceOffer(ctx context.Context, r
klog.Error(err)
return err
}
request.Status.OfferPhase = consts.ResourceObservedPhaseCreated
klog.Infof("%s -> %s Offer: %s/%s", r.ClusterID, op, offer.Namespace, offer.Name)
return nil
}
Expand Down Expand Up @@ -165,7 +163,10 @@ func (r *ResourceRequestReconciler) invalidateResourceOffer(ctx context.Context,
klog.Error(err)
return err
}
request.Status.OfferPhase = consts.ResourceObservedPhaseDeleted
if request.Status.OfferWithdrawalTimestamp.IsZero() {
now := metav1.Now()
request.Status.OfferWithdrawalTimestamp = &now
}
klog.Infof("%s -> Deleted Offer: %s/%s", r.ClusterID, offer.Namespace, offer.Name)
return nil
default:
Expand Down
25 changes: 0 additions & 25 deletions pkg/consts/resourcePhase.go

This file was deleted.

0 comments on commit eced38e

Please sign in to comment.