@@ -18,15 +18,14 @@ package controllers
1818
1919import (
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
5251func (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-
139113func (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