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

Commit fa8d856

Browse files
committed
dp delete fix and finalizer process
Signed-off-by: huiwq1990 <[email protected]>
1 parent 1eba58a commit fa8d856

File tree

2 files changed

+46
-49
lines changed

2 files changed

+46
-49
lines changed

Makefile

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
IMG ?= openyurt/yurt-device-controller:latest
44
# Produce CRDs that work back to Kubernetes 1.11 (no version conversion)
55
CRD_OPTIONS ?= "crd:trivialVersions=true,preserveUnknownFields=false"
6+
GOLANGCILINT_VERSION ?= v1.45.2
67

8+
OK = echo
79
# Get the currently used golang install path (in GOPATH/bin, unless GOBIN is set)
810
ifeq (,$(shell go env GOBIN))
911
GOBIN=$(shell go env GOPATH)/bin
@@ -128,3 +130,24 @@ GOBIN=$(PROJECT_DIR)/bin go get $(2) ;\
128130
rm -rf $$TMP_DIR ;\
129131
}
130132
endef
133+
134+
.PHONY: golangci
135+
golangci:
136+
ifneq ($(shell which golangci-lint),)
137+
@$(OK) golangci-lint is already installed
138+
GOLANGCILINT=$(shell which golangci-lint)
139+
else ifeq (, $(shell which $(GOBIN)/golangci-lint))
140+
@{ \
141+
set -e ;\
142+
echo 'installing golangci-lint-$(GOLANGCILINT_VERSION)' ;\
143+
curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(GOBIN) $(GOLANGCILINT_VERSION) ;\
144+
echo 'Successfully installed' ;\
145+
}
146+
GOLANGCILINT=$(GOBIN)/golangci-lint
147+
else
148+
@$(OK) golangci-lint is already installed
149+
GOLANGCILINT=$(GOBIN)/golangci-lint
150+
endif
151+
152+
lint: golangci
153+
$(GOLANGCILINT) run ./...

controllers/deviceprofile_controller.go

Lines changed: 23 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,14 @@ package controllers
1818

1919
import (
2020
"context"
21-
"encoding/json"
2221
"fmt"
2322

2423
apierrors "k8s.io/apimachinery/pkg/api/errors"
2524
"k8s.io/apimachinery/pkg/runtime"
26-
"k8s.io/apimachinery/pkg/types"
2725
"k8s.io/klog/v2"
2826
ctrl "sigs.k8s.io/controller-runtime"
2927
"sigs.k8s.io/controller-runtime/pkg/client"
28+
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
3029

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

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

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

6564
// 1. Handle the deviceProfile deletion event
66-
if err := r.reconcileDeleteDeviceProfile(ctx, &dp, dpActualName); err != nil {
67-
return ctrl.Result{}, client.IgnoreNotFound(err)
68-
} else if !dp.ObjectMeta.DeletionTimestamp.IsZero() {
65+
if dp.ObjectMeta.DeletionTimestamp.IsZero() {
66+
if !controllerutil.ContainsFinalizer(dp, devicev1alpha1.DeviceProfileFinalizer) {
67+
controllerutil.AddFinalizer(dp, devicev1alpha1.DeviceProfileFinalizer)
68+
if err := r.Update(ctx, dp); err != nil {
69+
return ctrl.Result{}, err
70+
}
71+
}
72+
} else {
73+
if controllerutil.ContainsFinalizer(dp, devicev1alpha1.DeviceProfileFinalizer) {
74+
// delete the deviceProfile object on edge platform
75+
if err := r.edgeClient.Delete(ctx, dpActualName, devcli.DeleteOptions{}); err != nil {
76+
return ctrl.Result{}, err
77+
}
78+
controllerutil.RemoveFinalizer(dp, devicev1alpha1.DeviceProfileFinalizer)
79+
if err := r.Update(ctx, dp); err != nil {
80+
return ctrl.Result{}, err
81+
}
82+
}
6983
return ctrl.Result{}, nil
7084
}
7185

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

99-
func (r *DeviceProfileReconciler) reconcileDeleteDeviceProfile(ctx context.Context, dp *devicev1alpha1.DeviceProfile, actualName string) error {
100-
if dp.ObjectMeta.DeletionTimestamp.IsZero() {
101-
if len(dp.GetFinalizers()) == 0 {
102-
patchString := map[string]interface{}{
103-
"metadata": map[string]interface{}{
104-
"finalizers": []string{devicev1alpha1.DeviceProfileFinalizer},
105-
},
106-
}
107-
if patchData, err := json.Marshal(patchString); err != nil {
108-
return err
109-
} else {
110-
if err = r.Patch(ctx, dp, client.RawPatch(types.MergePatchType, patchData)); err != nil {
111-
return err
112-
}
113-
}
114-
}
115-
} else {
116-
patchString := map[string]interface{}{
117-
"metadata": map[string]interface{}{
118-
"finalizers": []string{},
119-
},
120-
}
121-
// delete the deviceProfile in OpenYurt
122-
if patchData, err := json.Marshal(patchString); err != nil {
123-
return err
124-
} else {
125-
if err = r.Patch(ctx, dp, client.RawPatch(types.MergePatchType, patchData)); err != nil {
126-
return err
127-
}
128-
}
129-
130-
// delete the deviceProfile object on edge platform
131-
err := r.edgeClient.Delete(nil, actualName, devcli.DeleteOptions{})
132-
if err != nil && !clis.IsNotFoundErr(err) {
133-
return err
134-
}
135-
}
136-
return nil
137-
}
138-
139113
func (r *DeviceProfileReconciler) reconcileCreateDeviceProfile(ctx context.Context, dp *devicev1alpha1.DeviceProfile, actualName string) error {
140114
klog.V(4).Infof("Checking if deviceProfile already exist on the edge platform: %s", dp.GetName())
141115
if edgeDp, err := r.edgeClient.Get(nil, actualName, devcli.GetOptions{}); err != nil {

0 commit comments

Comments
 (0)