Skip to content

Commit

Permalink
offer withdrawal
Browse files Browse the repository at this point in the history
  • Loading branch information
aleoli committed Jul 5, 2021
1 parent ee6f4cb commit c9f4198
Show file tree
Hide file tree
Showing 8 changed files with 34 additions and 41 deletions.
8 changes: 2 additions & 6 deletions apis/sharing/v1alpha1/resourceoffer_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ package v1alpha1
import (
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

"github.com/liqotech/liqo/pkg/consts"
)

// ResourceOfferSpec defines the desired state of ResourceOffer.
Expand All @@ -25,10 +23,8 @@ type ResourceOfferSpec struct {
// TimeToLive is the time instant until this ResourceOffer will be valid.
// If not refreshed, an ResourceOffer will expire after 30 minutes.
TimeToLive metav1.Time `json:"timeToLive"`
// VirtualNodePhase indicate the desired state for the virtual node
// +kubebuilder:validation:Enum="Create";"Delete"
// +kubebuilder:default="Create"
VirtualNodePhase consts.ResourceDesiredPhase `json:"virtualKubeletPhase,omitempty"`
// WithdrawalTimestamp is set when a graceful deletion is requested by the user.
WithdrawalTimestamp *metav1.Time `json:"withdrawalTimestamp,omitempty"`
}

// OfferPhase describes the phase of the ResourceOffer.
Expand Down
4 changes: 4 additions & 0 deletions apis/sharing/v1alpha1/zz_generated.deepcopy.go

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

11 changes: 4 additions & 7 deletions deployments/liqo/crds/sharing.liqo.io_resourceoffers.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -166,13 +166,10 @@ spec:
was created.
format: date-time
type: string
virtualKubeletPhase:
default: Create
description: VirtualNodePhase indicate the desired state for the virtual
node
enum:
- Create
- Delete
withdrawalTimestamp:
description: WithdrawalTimestamp is set when a graceful deletion is
requested by the user.
format: date-time
type: string
required:
- clusterId
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -280,12 +280,12 @@ var _ = Describe("ResourceRequest Operator", func() {
Expect(k8sClient.Status().Update(ctx, createdResourceOffer)).ToNot(HaveOccurred())

var resourceOffer sharingv1alpha1.ResourceOffer
Eventually(func() consts.ResourceDesiredPhase {
Eventually(func() bool {
if err := k8sClient.Get(ctx, offerName, &resourceOffer); err != nil {
return ""
return false
}
return resourceOffer.Spec.VirtualNodePhase
}, timeout, interval).Should(Equal(consts.ResourceDesiredPhaseDelete))
return !resourceOffer.Spec.WithdrawalTimestamp.IsZero()
}, timeout, interval).Should(BeTrue())

By("Checking ResourceOffer deletion")
// the ResourceOffer should be deleted when the remote VirtualKubelet will be down
Expand Down
16 changes: 9 additions & 7 deletions internal/resource-request-operator/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,9 @@ func (r *ResourceRequestReconciler) generateResourceOffer(ctx context.Context, r
ResourceQuota: corev1.ResourceQuotaSpec{
Hard: resources,
},
Labels: r.Broadcaster.clusterConfig.Spec.DiscoveryConfig.ClusterLabels,
Timestamp: creationTime,
TimeToLive: metav1.NewTime(creationTime.Add(timeToLive)),
VirtualNodePhase: consts.ResourceDesiredPhaseCreate,
Labels: r.Broadcaster.clusterConfig.Spec.DiscoveryConfig.ClusterLabels,
Timestamp: creationTime,
TimeToLive: metav1.NewTime(creationTime.Add(timeToLive)),
}
offer.Spec = spec
return controllerutil.SetControllerReference(request, offer, r.Scheme)
Expand All @@ -57,7 +56,7 @@ func (r *ResourceRequestReconciler) generateResourceOffer(ctx context.Context, r
return err
}
request.Status.OfferPhase = consts.ResourceObservedPhaseCreated
klog.Infof("%s -> %s Offer: %s/%s (phase: %v)", r.ClusterID, op, offer.Namespace, offer.Name, offer.Spec.VirtualNodePhase)
klog.Infof("%s -> %s Offer: %s/%s", r.ClusterID, op, offer.Namespace, offer.Name)
return nil
}

Expand Down Expand Up @@ -149,13 +148,16 @@ func (r *ResourceRequestReconciler) invalidateResourceOffer(ctx context.Context,

switch offer.Status.VirtualKubeletStatus {
case sharingv1alpha1.VirtualKubeletStatusDeleting, sharingv1alpha1.VirtualKubeletStatusCreated:
offer.Spec.VirtualNodePhase = consts.ResourceDesiredPhaseDelete
if offer.Spec.WithdrawalTimestamp.IsZero() {
now := metav1.Now()
offer.Spec.WithdrawalTimestamp = &now
}
err = client.IgnoreNotFound(r.Client.Update(ctx, &offer))
if err != nil {
klog.Error(err)
return err
}
klog.Infof("%s -> Offer: %s/%s (phase: %v)", r.ClusterID, offer.Namespace, offer.Name, offer.Spec.VirtualNodePhase)
klog.Infof("%s -> Offer: %s/%s", r.ClusterID, offer.Namespace, offer.Name)
return nil
case sharingv1alpha1.VirtualKubeletStatusNone:
err = client.IgnoreNotFound(r.Client.Delete(ctx, &offer))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ const (
func getDeleteVirtualKubeletPhase(resourceOffer *sharingv1alpha1.ResourceOffer) kubeletDeletePhase {
notAccepted := !isAccepted(resourceOffer)
deleting := !resourceOffer.DeletionTimestamp.IsZero()
desiredDelete := resourceOffer.Spec.VirtualNodePhase == consts.ResourceDesiredPhaseDelete
desiredDelete := !resourceOffer.Spec.WithdrawalTimestamp.IsZero()
nodeDrained := !controllerutil.ContainsFinalizer(resourceOffer, consts.NodeFinalizer)
if notAccepted || deleting || desiredDelete {
if nodeDrained {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ var (
controller *ResourceOfferReconciler
ctx context.Context
cancel context.CancelFunc

now = metav1.Now()
)

func TestIdentityManager(t *testing.T) {
Expand Down Expand Up @@ -330,7 +332,7 @@ var _ = Describe("ResourceOffer Operator util functions", func() {
Finalizers: []string{},
},
Spec: sharingv1alpha1.ResourceOfferSpec{
VirtualNodePhase: consts.ResourceDesiredPhaseDelete,
WithdrawalTimestamp: &now,
},
Status: sharingv1alpha1.ResourceOfferStatus{
Phase: sharingv1alpha1.ResourceOfferRefused,
Expand All @@ -344,9 +346,7 @@ var _ = Describe("ResourceOffer Operator util functions", func() {
ObjectMeta: metav1.ObjectMeta{
Finalizers: []string{},
},
Spec: sharingv1alpha1.ResourceOfferSpec{
VirtualNodePhase: consts.ResourceDesiredPhaseCreate,
},
Spec: sharingv1alpha1.ResourceOfferSpec{},
Status: sharingv1alpha1.ResourceOfferStatus{
Phase: sharingv1alpha1.ResourceOfferAccepted,
},
Expand All @@ -362,9 +362,7 @@ var _ = Describe("ResourceOffer Operator util functions", func() {
},
Finalizers: []string{},
},
Spec: sharingv1alpha1.ResourceOfferSpec{
VirtualNodePhase: consts.ResourceDesiredPhaseCreate,
},
Spec: sharingv1alpha1.ResourceOfferSpec{},
Status: sharingv1alpha1.ResourceOfferStatus{
Phase: sharingv1alpha1.ResourceOfferAccepted,
},
Expand All @@ -379,9 +377,7 @@ var _ = Describe("ResourceOffer Operator util functions", func() {
consts.NodeFinalizer,
},
},
Spec: sharingv1alpha1.ResourceOfferSpec{
VirtualNodePhase: consts.ResourceDesiredPhaseCreate,
},
Spec: sharingv1alpha1.ResourceOfferSpec{},
Status: sharingv1alpha1.ResourceOfferStatus{
Phase: sharingv1alpha1.ResourceOfferRefused,
},
Expand All @@ -399,9 +395,7 @@ var _ = Describe("ResourceOffer Operator util functions", func() {
consts.NodeFinalizer,
},
},
Spec: sharingv1alpha1.ResourceOfferSpec{
VirtualNodePhase: consts.ResourceDesiredPhaseCreate,
},
Spec: sharingv1alpha1.ResourceOfferSpec{},
Status: sharingv1alpha1.ResourceOfferStatus{
Phase: sharingv1alpha1.ResourceOfferAccepted,
},
Expand All @@ -417,7 +411,7 @@ var _ = Describe("ResourceOffer Operator util functions", func() {
},
},
Spec: sharingv1alpha1.ResourceOfferSpec{
VirtualNodePhase: consts.ResourceDesiredPhaseDelete,
WithdrawalTimestamp: &now,
},
Status: sharingv1alpha1.ResourceOfferStatus{
Phase: sharingv1alpha1.ResourceOfferAccepted,
Expand All @@ -432,7 +426,7 @@ var _ = Describe("ResourceOffer Operator util functions", func() {
Finalizers: []string{},
},
Spec: sharingv1alpha1.ResourceOfferSpec{
VirtualNodePhase: consts.ResourceDesiredPhaseDelete,
WithdrawalTimestamp: &now,
},
Status: sharingv1alpha1.ResourceOfferStatus{
Phase: sharingv1alpha1.ResourceOfferAccepted,
Expand Down
2 changes: 1 addition & 1 deletion pkg/virtualKubelet/liqoNodeProvider/reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import (

func isResourceOfferTerminating(resourceOffer *sharingv1alpha1.ResourceOffer) bool {
hasTimestamp := !resourceOffer.DeletionTimestamp.IsZero()
desiredDelete := resourceOffer.Spec.VirtualNodePhase == consts.ResourceDesiredPhaseDelete
desiredDelete := !resourceOffer.Spec.WithdrawalTimestamp.IsZero()
return hasTimestamp || desiredDelete
}

Expand Down

0 comments on commit c9f4198

Please sign in to comment.