Skip to content
This repository has been archived by the owner on Nov 7, 2023. It is now read-only.

Commit

Permalink
dp delete fix and finalizer process
Browse files Browse the repository at this point in the history
Signed-off-by: huiwq1990 <[email protected]>
  • Loading branch information
huiwq1990 committed Jul 11, 2022
1 parent 1eba58a commit fa8d856
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 49 deletions.
23 changes: 23 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
IMG ?= openyurt/yurt-device-controller:latest
# Produce CRDs that work back to Kubernetes 1.11 (no version conversion)
CRD_OPTIONS ?= "crd:trivialVersions=true,preserveUnknownFields=false"
GOLANGCILINT_VERSION ?= v1.45.2

OK = echo
# Get the currently used golang install path (in GOPATH/bin, unless GOBIN is set)
ifeq (,$(shell go env GOBIN))
GOBIN=$(shell go env GOPATH)/bin
Expand Down Expand Up @@ -128,3 +130,24 @@ GOBIN=$(PROJECT_DIR)/bin go get $(2) ;\
rm -rf $$TMP_DIR ;\
}
endef

.PHONY: golangci
golangci:
ifneq ($(shell which golangci-lint),)
@$(OK) golangci-lint is already installed
GOLANGCILINT=$(shell which golangci-lint)
else ifeq (, $(shell which $(GOBIN)/golangci-lint))
@{ \
set -e ;\
echo 'installing golangci-lint-$(GOLANGCILINT_VERSION)' ;\
curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(GOBIN) $(GOLANGCILINT_VERSION) ;\
echo 'Successfully installed' ;\
}
GOLANGCILINT=$(GOBIN)/golangci-lint
else
@$(OK) golangci-lint is already installed
GOLANGCILINT=$(GOBIN)/golangci-lint
endif

lint: golangci
$(GOLANGCILINT) run ./...
72 changes: 23 additions & 49 deletions controllers/deviceprofile_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,14 @@ package controllers

import (
"context"
"encoding/json"
"fmt"

apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
"k8s.io/klog/v2"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"

devicev1alpha1 "github.com/openyurtio/device-controller/api/v1alpha1"
clis "github.com/openyurtio/device-controller/clients"
Expand All @@ -50,8 +49,8 @@ type DeviceProfileReconciler struct {

// Reconcile make changes to a deviceprofile object in EdgeX based on it in Kubernetes
func (r *DeviceProfileReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
var dp devicev1alpha1.DeviceProfile
if err := r.Get(ctx, req.NamespacedName, &dp); err != nil {
var dp *devicev1alpha1.DeviceProfile
if err := r.Get(ctx, req.NamespacedName, dp); err != nil {
return ctrl.Result{}, client.IgnoreNotFound(err)
}
if dp.Spec.NodePool != r.NodePool {
Expand All @@ -60,18 +59,33 @@ func (r *DeviceProfileReconciler) Reconcile(ctx context.Context, req ctrl.Reques
klog.V(3).Infof("Reconciling the DeviceProfile: %s", dp.GetName())

// gets the actual name of deviceProfile on the edge platform from the Label of the deviceProfile
dpActualName := util.GetEdgeDeviceProfileName(&dp, EdgeXObjectName)
dpActualName := util.GetEdgeDeviceProfileName(dp, EdgeXObjectName)

// 1. Handle the deviceProfile deletion event
if err := r.reconcileDeleteDeviceProfile(ctx, &dp, dpActualName); err != nil {
return ctrl.Result{}, client.IgnoreNotFound(err)
} else if !dp.ObjectMeta.DeletionTimestamp.IsZero() {
if dp.ObjectMeta.DeletionTimestamp.IsZero() {
if !controllerutil.ContainsFinalizer(dp, devicev1alpha1.DeviceProfileFinalizer) {
controllerutil.AddFinalizer(dp, devicev1alpha1.DeviceProfileFinalizer)
if err := r.Update(ctx, dp); err != nil {
return ctrl.Result{}, err
}
}
} else {
if controllerutil.ContainsFinalizer(dp, devicev1alpha1.DeviceProfileFinalizer) {
// delete the deviceProfile object on edge platform
if err := r.edgeClient.Delete(ctx, dpActualName, devcli.DeleteOptions{}); err != nil {
return ctrl.Result{}, err
}
controllerutil.RemoveFinalizer(dp, devicev1alpha1.DeviceProfileFinalizer)
if err := r.Update(ctx, dp); err != nil {
return ctrl.Result{}, err
}
}
return ctrl.Result{}, nil
}

if dp.Status.Synced == false {
// 2. Synchronize OpenYurt deviceProfile to edge platform
if err := r.reconcileCreateDeviceProfile(ctx, &dp, dpActualName); err != nil {
if err := r.reconcileCreateDeviceProfile(ctx, dp, dpActualName); err != nil {
if apierrors.IsConflict(err) {
return ctrl.Result{Requeue: true}, nil
} else {
Expand All @@ -96,46 +110,6 @@ func (r *DeviceProfileReconciler) SetupWithManager(mgr ctrl.Manager, opts *optio
Complete(r)
}

func (r *DeviceProfileReconciler) reconcileDeleteDeviceProfile(ctx context.Context, dp *devicev1alpha1.DeviceProfile, actualName string) error {
if dp.ObjectMeta.DeletionTimestamp.IsZero() {
if len(dp.GetFinalizers()) == 0 {
patchString := map[string]interface{}{
"metadata": map[string]interface{}{
"finalizers": []string{devicev1alpha1.DeviceProfileFinalizer},
},
}
if patchData, err := json.Marshal(patchString); err != nil {
return err
} else {
if err = r.Patch(ctx, dp, client.RawPatch(types.MergePatchType, patchData)); err != nil {
return err
}
}
}
} else {
patchString := map[string]interface{}{
"metadata": map[string]interface{}{
"finalizers": []string{},
},
}
// delete the deviceProfile in OpenYurt
if patchData, err := json.Marshal(patchString); err != nil {
return err
} else {
if err = r.Patch(ctx, dp, client.RawPatch(types.MergePatchType, patchData)); err != nil {
return err
}
}

// delete the deviceProfile object on edge platform
err := r.edgeClient.Delete(nil, actualName, devcli.DeleteOptions{})
if err != nil && !clis.IsNotFoundErr(err) {
return err
}
}
return nil
}

func (r *DeviceProfileReconciler) reconcileCreateDeviceProfile(ctx context.Context, dp *devicev1alpha1.DeviceProfile, actualName string) error {
klog.V(4).Infof("Checking if deviceProfile already exist on the edge platform: %s", dp.GetName())
if edgeDp, err := r.edgeClient.Get(nil, actualName, devcli.GetOptions{}); err != nil {
Expand Down

0 comments on commit fa8d856

Please sign in to comment.