Skip to content

Commit

Permalink
node cleanup
Browse files Browse the repository at this point in the history
This commit adds the logic to clean up the node when the peering is tearing down.

This is composed of 4 steps:

* node cordoning
* eviction of pods using the Eviction API
* waiting for pod deletion
* delete the node
  • Loading branch information
aleoli authored and palexster committed Jul 1, 2021
1 parent 9bd3daa commit 5ddb319
Show file tree
Hide file tree
Showing 16 changed files with 423 additions and 64 deletions.
8 changes: 8 additions & 0 deletions cmd/virtual-kubelet/root/root.go
Expand Up @@ -189,6 +189,14 @@ func runRootCommand(ctx context.Context, s *provider.Store, c *Opts) error {
newNode.SetOwnerReferences(refs)
}

if liqoNodeProvider, ok := nodeProviderModule.(*liqonodeprovider.LiqoNodeProvider); ok {
if liqoNodeProvider.IsTerminating() {
// this avoids the re-creation of terminated nodes
klog.V(4).Info("skipping: node is in terminating phase")
return nil
}
}

oldNode, newErr := client.Client().CoreV1().Nodes().Get(context.TODO(), newNode.Name, metav1.GetOptions{})
if newErr != nil {
if !k8serrors.IsNotFound(newErr) {
Expand Down
Expand Up @@ -50,6 +50,12 @@ rules:
- list
- patch
- watch
- apiGroups:
- ""
resources:
- pods/eviction
verbs:
- create
- apiGroups:
- apps
resources:
Expand Down Expand Up @@ -97,10 +103,12 @@ rules:
- sharing.liqo.io
resources:
- advertisements
- resourceoffers
verbs:
- delete
- get
- list
- patch
- update
- watch
- apiGroups:
Expand Down
14 changes: 14 additions & 0 deletions go.sum
Expand Up @@ -92,7 +92,9 @@ github.com/NYTimes/gziphandler v0.0.0-20170623195520-56545f4a5d46/go.mod h1:3wb0
github.com/NYTimes/gziphandler v1.1.1/go.mod h1:n/CVRwUEOgIxrgPvAQhUUr9oeUtvrhMomdKFjzJNB0c=
github.com/OneOfOne/xxhash v1.2.2/go.mod h1:HSdplMjZKSmBqAxg5vPj2TmRDmfkzw+cTzAElWljhcU=
github.com/PuerkitoBio/purell v1.1.0/go.mod h1:c11w/QuzBsJSee3cPx9rAFu61PvFxuPbtSwDGJws/X0=
github.com/PuerkitoBio/purell v1.1.1 h1:WEQqlqaGbrPkxLJWfBwQmfEAE1Z7ONdDLqrN38tNFfI=
github.com/PuerkitoBio/purell v1.1.1/go.mod h1:c11w/QuzBsJSee3cPx9rAFu61PvFxuPbtSwDGJws/X0=
github.com/PuerkitoBio/urlesc v0.0.0-20170810143723-de5bf2ad4578 h1:d+Bc7a5rLufV/sSk/8dngufqelfh6jnri85riMAaF/M=
github.com/PuerkitoBio/urlesc v0.0.0-20170810143723-de5bf2ad4578/go.mod h1:uGdkoq3SwY9Y+13GIhn11/XLaGBb4BfwItxLd5jeuXE=
github.com/Shopify/logrus-bugsnag v0.0.0-20171204204709-577dee27f20d/go.mod h1:HI8ITrYtUY+O+ZhtlqUnD8+KwNPOyugEhfP9fdUIaEQ=
github.com/Shopify/sarama v1.19.0/go.mod h1:FVkBWblsNy7DGZRfXLU0O9RCGt5g3g3yEuWXgklEdEo=
Expand Down Expand Up @@ -366,10 +368,12 @@ github.com/go-openapi/errors v0.19.2/go.mod h1:qX0BLWsyaKfvhluLejVpVNwNRdXZhEbTA
github.com/go-openapi/jsonpointer v0.17.0/go.mod h1:cOnomiV+CVVwFLk0A/MExoFMjwdsUdVpsRhURCKh+3M=
github.com/go-openapi/jsonpointer v0.18.0/go.mod h1:cOnomiV+CVVwFLk0A/MExoFMjwdsUdVpsRhURCKh+3M=
github.com/go-openapi/jsonpointer v0.19.2/go.mod h1:3akKfEdA7DF1sugOqz1dVQHBcuDBPKZGEoHC/NkiQRg=
github.com/go-openapi/jsonpointer v0.19.3 h1:gihV7YNZK1iK6Tgwwsxo2rJbD1GTbdm72325Bq8FI3w=
github.com/go-openapi/jsonpointer v0.19.3/go.mod h1:Pl9vOtqEWErmShwVjC8pYs9cog34VGT37dQOVbmoatg=
github.com/go-openapi/jsonreference v0.17.0/go.mod h1:g4xxGn04lDIRh0GJb5QlpE3HfopLOL6uZrK/VgnsK9I=
github.com/go-openapi/jsonreference v0.18.0/go.mod h1:g4xxGn04lDIRh0GJb5QlpE3HfopLOL6uZrK/VgnsK9I=
github.com/go-openapi/jsonreference v0.19.2/go.mod h1:jMjeRr2HHw6nAVajTXJ4eiUwohSTlpa0o73RUL1owJc=
github.com/go-openapi/jsonreference v0.19.3 h1:5cxNfTy0UVC3X8JL5ymxzyoUZmo8iZb+jeTWn7tUa8o=
github.com/go-openapi/jsonreference v0.19.3/go.mod h1:rjx6GuL8TTa9VaixXglHmQmIL98+wF9xc8zWvFonSJ8=
github.com/go-openapi/loads v0.17.0/go.mod h1:72tmFy5wsWx89uEVddd0RjRWPZm92WRLhf7AC+0+OOU=
github.com/go-openapi/loads v0.18.0/go.mod h1:72tmFy5wsWx89uEVddd0RjRWPZm92WRLhf7AC+0+OOU=
Expand All @@ -383,6 +387,7 @@ github.com/go-openapi/spec v0.17.0/go.mod h1:XkF/MOi14NmjsfZ8VtAKf8pIlbZzyoTvZsd
github.com/go-openapi/spec v0.18.0/go.mod h1:XkF/MOi14NmjsfZ8VtAKf8pIlbZzyoTvZsdfssdxcBI=
github.com/go-openapi/spec v0.19.2/go.mod h1:sCxk3jxKgioEJikev4fgkNmwS+3kuYdJtcsZsD5zxMY=
github.com/go-openapi/spec v0.19.3/go.mod h1:FpwSN1ksY1eteniUU7X0N/BgJ7a4WvBFVA8Lj9mJglo=
github.com/go-openapi/spec v0.19.5 h1:Xm0Ao53uqnk9QE/LlYV5DEU09UAgpliA85QoT9LzqPw=
github.com/go-openapi/spec v0.19.5/go.mod h1:Hm2Jr4jv8G1ciIAo+frC/Ft+rR2kQDh8JHKHb3gWUSk=
github.com/go-openapi/strfmt v0.17.0/go.mod h1:P82hnJI0CXkErkXi8IKjPbNBM6lV6+5pLP5l494TcyU=
github.com/go-openapi/strfmt v0.18.0/go.mod h1:P82hnJI0CXkErkXi8IKjPbNBM6lV6+5pLP5l494TcyU=
Expand All @@ -392,6 +397,7 @@ github.com/go-openapi/strfmt v0.19.5/go.mod h1:eftuHTlB/dI8Uq8JJOyRlieZf+WkkxUuk
github.com/go-openapi/swag v0.17.0/go.mod h1:AByQ+nYG6gQg71GINrmuDXCPWdL640yX49/kXLo40Tg=
github.com/go-openapi/swag v0.18.0/go.mod h1:AByQ+nYG6gQg71GINrmuDXCPWdL640yX49/kXLo40Tg=
github.com/go-openapi/swag v0.19.2/go.mod h1:POnQmlKehdgb5mhVOsnJFsivZCEZ/vjK9gh66Z9tfKk=
github.com/go-openapi/swag v0.19.5 h1:lTz6Ys4CmqqCQmZPBlbQENR1/GucA2bzYTE12Pw4tFY=
github.com/go-openapi/swag v0.19.5/go.mod h1:POnQmlKehdgb5mhVOsnJFsivZCEZ/vjK9gh66Z9tfKk=
github.com/go-openapi/validate v0.18.0/go.mod h1:Uh4HdOzKt19xGIGm1qHf/ofbX1YQ4Y+MYsct2VUrAJ4=
github.com/go-openapi/validate v0.19.2/go.mod h1:1tRCw7m3jtI8eNWEEliiAqUIcBztB2KDnRCRMUi7GTA=
Expand Down Expand Up @@ -479,6 +485,7 @@ github.com/google/pprof v0.0.0-20191218002539-d4f498aebedc/go.mod h1:ZgVRPoUq/hf
github.com/google/pprof v0.0.0-20200212024743-f11f1df84d12/go.mod h1:ZgVRPoUq/hfqzAqh7sHMqb3I9Rq5C59dIz2SbBwJ4eM=
github.com/google/pprof v0.0.0-20200229191704-1ebb73c60ed3/go.mod h1:ZgVRPoUq/hfqzAqh7sHMqb3I9Rq5C59dIz2SbBwJ4eM=
github.com/google/renameio v0.1.0/go.mod h1:KWCgfxg9yswjAJkECMjeO8J8rahYeXnNhOm40UhjYkI=
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 h1:El6M4kTTCOh6aBiKaUGG7oYTSPP8MxqL4YI3kZKwcP4=
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510/go.mod h1:pupxD2MaaD3pAXIBCelhxNneeOaAeabZDe5s4K6zSpQ=
github.com/google/uuid v1.0.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/google/uuid v1.1.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
Expand Down Expand Up @@ -632,6 +639,7 @@ github.com/mailru/easyjson v0.0.0-20180823135443-60711f1a8329/go.mod h1:C1wdFJiN
github.com/mailru/easyjson v0.0.0-20190312143242-1de009706dbe/go.mod h1:C1wdFJiN94OJF2b5HbByQZoLdCWB1Yqtg26g4irojpc=
github.com/mailru/easyjson v0.0.0-20190614124828-94de47d64c63/go.mod h1:C1wdFJiN94OJF2b5HbByQZoLdCWB1Yqtg26g4irojpc=
github.com/mailru/easyjson v0.0.0-20190626092158-b2ccc519800e/go.mod h1:C1wdFJiN94OJF2b5HbByQZoLdCWB1Yqtg26g4irojpc=
github.com/mailru/easyjson v0.7.0 h1:aizVhC/NAAcKWb+5QsU1iNOZb4Yws5UO2I+aIprQITM=
github.com/mailru/easyjson v0.7.0/go.mod h1:KAzv3t3aY1NaHWoQz1+4F1ccyAH66Jk7yos7ldAVICs=
github.com/markbates/pkger v0.17.1/go.mod h1:0JoVlrol20BSywW79rN3kdFFsE5xYM+rSCQDXbLhiuI=
github.com/marstr/guid v1.1.0/go.mod h1:74gB1z2wpxxInTG6yaqA7KrtM0NZ+RbrcqDvYHefzho=
Expand Down Expand Up @@ -699,6 +707,7 @@ github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lN
github.com/modern-go/reflect2 v1.0.1 h1:9f412s+6RmYXLWZSEzVVgPGK7C2PphHj5RJrvfx9AWI=
github.com/modern-go/reflect2 v1.0.1/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
github.com/mohae/deepcopy v0.0.0-20170603005431-491d3605edfb/go.mod h1:TaXosZuwdSHYgviHp1DAtfrULt5eUgsSMsZf+YrPgl8=
github.com/monochromegane/go-gitignore v0.0.0-20200626010858-205db1a8cc00 h1:n6/2gBQ3RWajuToeY6ZtZTIKv2v7ThUy5KKusIT0yc0=
github.com/monochromegane/go-gitignore v0.0.0-20200626010858-205db1a8cc00/go.mod h1:Pm3mSP3c5uWn86xMLZ5Sa7JB9GsEZySvHYXCTK4E9q4=
github.com/morikuni/aec v0.0.0-20170113033406-39771216ff4c/go.mod h1:BbKIizmSmc5MMPqRYbxO4ZU0S0+P200+tUnFx7PXmsc=
github.com/morikuni/aec v1.0.0 h1:nP9CBfwrvYnBRgY6qfDQkygYDmYwOilePFkwzv4dU8A=
Expand Down Expand Up @@ -962,6 +971,7 @@ github.com/xeipuuv/gojsonpointer v0.0.0-20180127040702-4e3ac2762d5f/go.mod h1:N2
github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415/go.mod h1:GwrjFmJcFw6At/Gs6z4yjiIwzuJ1/+UwLxMQDVQXShQ=
github.com/xeipuuv/gojsonschema v0.0.0-20180618132009-1d523034197f/go.mod h1:5yf86TLmAcydyeJq5YvxkGPE2fm/u4myDekKRoLuqhs=
github.com/xiang90/probing v0.0.0-20190116061207-43a291ad63a2/go.mod h1:UETIi67q53MR2AWcXfiuqkDkRtnGDLqkBTpCHuJHxtU=
github.com/xlab/treeprint v0.0.0-20181112141820-a009c3971eca h1:1CFlNzQhALwjS9mBAUkycX616GzgsuYUOCHA5+HSlXI=
github.com/xlab/treeprint v0.0.0-20181112141820-a009c3971eca/go.mod h1:ce1O1j6UtZfjr22oyGxGLbauSBp2YVXpARAosm7dHBg=
github.com/xordataexchange/crypt v0.0.3-0.20170626215501-b2862e3d0a77/go.mod h1:aYKd//L2LvnjZzWKhF00oedf4jCCReLcmhLdhm1A27Q=
github.com/yuin/goldmark v1.1.25/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
Expand All @@ -986,6 +996,7 @@ go.opencensus.io v0.22.2/go.mod h1:yxeiOL68Rb0Xd1ddK5vPZ/oVn4vY4Ynel7k9FzqtOIw=
go.opencensus.io v0.22.3/go.mod h1:yxeiOL68Rb0Xd1ddK5vPZ/oVn4vY4Ynel7k9FzqtOIw=
go.opencensus.io v0.23.0 h1:gqCw0LfLxScz8irSi8exQc7fyQ0fKQU/qnC/X8+V/1M=
go.opencensus.io v0.23.0/go.mod h1:XItmlyltB5F7CS4xOC1DcqMoFqwtC6OG2xF7mCv7P7E=
go.starlark.net v0.0.0-20200306205701-8dd3e2ee1dd5 h1:+FNtrFTmVw0YZGpBGX56XDee331t6JAXeK2bcyhLOOc=
go.starlark.net v0.0.0-20200306205701-8dd3e2ee1dd5/go.mod h1:nmDLcffg48OtT/PSW0Hg7FvpRQsQh5OSqIylirxKC7o=
go.uber.org/atomic v1.3.2/go.mod h1:gD2HeocX3+yG+ygLZcrzQJaqmWj9AIm7n08wl/qW/PE=
go.uber.org/atomic v1.4.0/go.mod h1:gD2HeocX3+yG+ygLZcrzQJaqmWj9AIm7n08wl/qW/PE=
Expand Down Expand Up @@ -1459,6 +1470,7 @@ k8s.io/apimachinery v0.21.0 h1:3Fx+41if+IRavNcKOz09FwEXDBG6ORh6iMsTSelhkMA=
k8s.io/apimachinery v0.21.0/go.mod h1:jbreFvJo3ov9rj7eWT7+sYiRx+qZuCYXwWT1bcDswPY=
k8s.io/apiserver v0.21.0 h1:1hWMfsz+cXxB77k6/y0XxWxwl6l9OF26PC9QneUVn1Q=
k8s.io/apiserver v0.21.0/go.mod h1:w2YSn4/WIwYuxG5zJmcqtRdtqgW/J2JRgFAqps3bBpg=
k8s.io/cli-runtime v0.21.0 h1:/V2Kkxtf6x5NI2z+Sd/mIrq4FQyQ8jzZAUD6N5RnN7Y=
k8s.io/cli-runtime v0.21.0/go.mod h1:XoaHP93mGPF37MkLbjGVYqg3S1MnsFdKtiA/RZzzxOo=
k8s.io/client-go v0.21.0 h1:n0zzzJsAQmJngpC0IhgFcApZyoGXPrDIAD601HD09ag=
k8s.io/client-go v0.21.0/go.mod h1:nNBytTF9qPFDEhoqgEPaarobC8QPae13bElIVHzIglA=
Expand Down Expand Up @@ -1518,9 +1530,11 @@ sigs.k8s.io/apiserver-network-proxy/konnectivity-client v0.0.15/go.mod h1:LEScyz
sigs.k8s.io/controller-runtime v0.8.3/go.mod h1:U/l+DUopBc1ecfRZ5aviA9JDmGFQKvLf5YkZNx2e0sU=
sigs.k8s.io/controller-runtime v0.9.0-beta.2 h1:T2sG4AGBWKRsUJyEeMRsIpAdn/1Tqk+3J7KSJB4pWPo=
sigs.k8s.io/controller-runtime v0.9.0-beta.2/go.mod h1:ufPDuvefw2Y1KnBgHQrLdOjueYlj+XJV2AszbT+WTxs=
sigs.k8s.io/kustomize/api v0.8.5 h1:bfCXGXDAbFbb/Jv5AhMj2BB8a5VAJuuQ5/KU69WtDjQ=
sigs.k8s.io/kustomize/api v0.8.5/go.mod h1:M377apnKT5ZHJS++6H4rQoCHmWtt6qTpp3mbe7p6OLY=
sigs.k8s.io/kustomize/cmd/config v0.9.7/go.mod h1:MvXCpHs77cfyxRmCNUQjIqCmZyYsbn5PyQpWiq44nW0=
sigs.k8s.io/kustomize/kustomize/v4 v4.0.5/go.mod h1:C7rYla7sI8EnxHE/xEhRBSHMNfcL91fx0uKmUlUhrBk=
sigs.k8s.io/kustomize/kyaml v0.10.15 h1:dSLgG78KyaxN4HylPXdK+7zB3k7sW6q3IcCmcfKA+aI=
sigs.k8s.io/kustomize/kyaml v0.10.15/go.mod h1:mlQFagmkm1P+W4lZJbJ/yaxMd8PqMRSC4cPcfUVt5Hg=
sigs.k8s.io/structured-merge-diff/v4 v4.0.2/go.mod h1:bJZC9H9iH24zzfZ/41RGcq60oK1F7G282QMXDPYydCw=
sigs.k8s.io/structured-merge-diff/v4 v4.1.0 h1:C4r9BgJ98vrKnnVCjwCSXcWjWe0NKcUQkmzDXZXGwH8=
Expand Down
4 changes: 4 additions & 0 deletions pkg/consts/virtualNode.go
@@ -0,0 +1,4 @@
package consts

// NodeFinalizer is the finalizer added on a ResourceOffer when the related VirtualNode is up.
const NodeFinalizer = "liqo.io/node"
Expand Up @@ -18,26 +18,36 @@ package resourceoffercontroller

import (
"context"
"fmt"
"sync"
"time"

v1 "k8s.io/api/apps/v1"
kerrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/tools/record"
"k8s.io/client-go/util/workqueue"
"k8s.io/klog/v2"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/builder"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/event"
"sigs.k8s.io/controller-runtime/pkg/handler"
"sigs.k8s.io/controller-runtime/pkg/predicate"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
"sigs.k8s.io/controller-runtime/pkg/source"

configv1alpha1 "github.com/liqotech/liqo/apis/config/v1alpha1"
sharingv1alpha1 "github.com/liqotech/liqo/apis/sharing/v1alpha1"
crdreplicator "github.com/liqotech/liqo/internal/crdReplicator"
"github.com/liqotech/liqo/pkg/clusterid"
"github.com/liqotech/liqo/pkg/vkMachinery"
)

const resourceOfferAnnotation = "liqo.io/resourceoffer"

// ResourceOfferReconciler reconciles a ResourceOffer object.
type ResourceOfferReconciler struct {
client.Client
Expand Down Expand Up @@ -119,7 +129,7 @@ func (r *ResourceOfferReconciler) Reconcile(ctx context.Context, req ctrl.Reques
}
}

if !isAccepted(&resourceOffer) || !resourceOffer.DeletionTimestamp.IsZero() {
if canDeleteVirtualKubeletDeployment(&resourceOffer) {
// delete virtual kubelet deployment
if err = r.deleteVirtualKubeletDeployment(ctx, &resourceOffer); err != nil {
klog.Error(err)
Expand All @@ -139,14 +149,74 @@ func (r *ResourceOfferReconciler) Reconcile(ctx context.Context, req ctrl.Reques

// SetupWithManager sets up the controller with the Manager.
func (r *ResourceOfferReconciler) SetupWithManager(mgr ctrl.Manager) error {
// select replicated resources only
p, err := predicate.LabelSelectorPredicate(crdreplicator.ReplicatedResourcesLabelSelector)
if err != nil {
klog.Error(err)
return err
}

// select virtual kubelet deployments only
deployPredicate, err := predicate.LabelSelectorPredicate(metav1.LabelSelector{
MatchLabels: vkMachinery.KubeletBaseLabels,
})
if err != nil {
klog.Error(err)
return err
}

return ctrl.NewControllerManagedBy(mgr).
For(&sharingv1alpha1.ResourceOffer{}, builder.WithPredicates(p)).
Owns(&v1.Deployment{}).
Watches(&source.Kind{Type: &v1.Deployment{}},
getVirtualKubeletEventHandler(), builder.WithPredicates(deployPredicate)).
Complete(r)
}

// getVirtualKubeletEventHandler creates and returns an event handle with the same behavior of the
// owner reference event handler, but using an annotation. This allows us to have a graceful deletion
// of the owned object, impossible using a standard owner reference, keeping the possibility to be
// triggered on children updates and to enforce their status.
func getVirtualKubeletEventHandler() handler.EventHandler {
return &handler.Funcs{
CreateFunc: func(ce event.CreateEvent, rli workqueue.RateLimitingInterface) {
if req, err := getReconcileRequestFromObject(ce.Object); err == nil {
rli.Add(req)
}
},
UpdateFunc: func(ue event.UpdateEvent, rli workqueue.RateLimitingInterface) {
if req, err := getReconcileRequestFromObject(ue.ObjectOld); err == nil {
rli.Add(req)
}
if req, err := getReconcileRequestFromObject(ue.ObjectNew); err == nil {
rli.Add(req)
}
},
DeleteFunc: func(de event.DeleteEvent, rli workqueue.RateLimitingInterface) {
if req, err := getReconcileRequestFromObject(de.Object); err == nil {
rli.Add(req)
}
},
GenericFunc: func(ge event.GenericEvent, rli workqueue.RateLimitingInterface) {
if req, err := getReconcileRequestFromObject(ge.Object); err == nil {
rli.Add(req)
}
},
}
}

// getReconcileRequestFromObject reads an annotation in the object and returns the reconcile request
// to be enqueued for the reconciliation.
func getReconcileRequestFromObject(obj client.Object) (reconcile.Request, error) {
resourceOfferName, ok := obj.GetAnnotations()[resourceOfferAnnotation]
if !ok {
return reconcile.Request{}, fmt.Errorf("%v annotation not found in object %v/%v",
resourceOfferAnnotation, obj.GetNamespace(), obj.GetName())
}

return reconcile.Request{
NamespacedName: types.NamespacedName{
Name: resourceOfferName,
Namespace: obj.GetNamespace(),
},
}, nil
}
Expand Up @@ -14,6 +14,7 @@ import (

configv1alpha1 "github.com/liqotech/liqo/apis/config/v1alpha1"
sharingv1alpha1 "github.com/liqotech/liqo/apis/sharing/v1alpha1"
"github.com/liqotech/liqo/pkg/consts"
crdclient "github.com/liqotech/liqo/pkg/crdClient"
"github.com/liqotech/liqo/pkg/utils"
foreigncluster "github.com/liqotech/liqo/pkg/utils/foreignCluster"
Expand Down Expand Up @@ -117,7 +118,7 @@ func (r *ResourceOfferReconciler) createVirtualKubeletDeployment(
// create the base resources
vkServiceAccount := forge.VirtualKubeletServiceAccount(name, namespace)
op, err := controllerutil.CreateOrUpdate(ctx, r.Client, vkServiceAccount, func() error {
return controllerutil.SetOwnerReference(resourceOffer, vkServiceAccount, r.Scheme)
return nil
})
if err != nil {
klog.Error(err)
Expand Down Expand Up @@ -145,7 +146,9 @@ func (r *ResourceOfferReconciler) createVirtualKubeletDeployment(
}

op, err = controllerutil.CreateOrUpdate(context.TODO(), r.Client, vkDeployment, func() error {
return controllerutil.SetControllerReference(resourceOffer, vkDeployment, r.Scheme)
// set the "owner" object name in the annotation to be able to reconcile deployment changes
vkDeployment.Annotations[resourceOfferAnnotation] = resourceOffer.GetName()
return nil
})
if err != nil {
klog.Error(err)
Expand Down Expand Up @@ -222,6 +225,13 @@ func (r *ResourceOfferReconciler) getVirtualKubeletDeployment(
return &deployList.Items[0], nil
}

func canDeleteVirtualKubeletDeployment(resourceOffer *sharingv1alpha1.ResourceOffer) bool {
notAccepted := !isAccepted(resourceOffer)
deleting := !resourceOffer.DeletionTimestamp.IsZero()
nodeDrained := !controllerutil.ContainsFinalizer(resourceOffer, consts.NodeFinalizer)
return (notAccepted || deleting) && nodeDrained
}

// isAccepted checks if a ResourceOffer is in Accepted phase.
func isAccepted(resourceOffer *sharingv1alpha1.ResourceOffer) bool {
return resourceOffer.Status.Phase == sharingv1alpha1.ResourceOfferAccepted
Expand Down
Expand Up @@ -15,7 +15,6 @@ import (
rbacv1 "k8s.io/api/rbac/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/utils/pointer"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/manager"

Expand Down Expand Up @@ -228,25 +227,14 @@ var _ = Describe("ResourceOffer Controller", func() {
return reflect.DeepEqual(deploymentList.Items[0], *vkDeploy)
}, timeout, interval).Should(BeTrue())

// check tht the deployment has the controller reference
Eventually(func() []metav1.OwnerReference {
// check tht the deployment has the controller reference annotation
Eventually(func() string {
vkDeploy, err := controller.getVirtualKubeletDeployment(ctx, resourceOffer)
if err != nil || vkDeploy == nil {
return nil
return ""
}
return vkDeploy.OwnerReferences
}, timeout, interval).Should(Equal(
[]metav1.OwnerReference{
{
APIVersion: resourceOffer.GroupVersionKind().GroupVersion().String(),
Kind: resourceOffer.GroupVersionKind().Kind,
Name: resourceOffer.GetName(),
UID: resourceOffer.GetUID(),
BlockOwnerDeletion: pointer.BoolPtr(true),
Controller: pointer.BoolPtr(true),
},
},
))
return vkDeploy.Annotations[resourceOfferAnnotation]
}, timeout, interval).Should(Equal(resourceOffer.Name))

// check the existence of the ClusterRoleBinding
Eventually(func() int {
Expand Down
21 changes: 21 additions & 0 deletions pkg/virtualKubelet/liqoNodeProvider/cordon.go
@@ -0,0 +1,21 @@
package liqonodeprovider

import (
"context"

v1 "k8s.io/api/core/v1"
"k8s.io/klog/v2"
)

// cordonNode cordons the controlled node setting it in the unschedulable state.
func (p *LiqoNodeProvider) cordonNode(ctx context.Context) error {
if err := p.patchNode(func(node *v1.Node) error {
node.Spec.Unschedulable = true
return nil
}); err != nil {
klog.Error(err)
return err
}

return nil
}

0 comments on commit 5ddb319

Please sign in to comment.