From 3c5a90df921ad2544ac87923f6c6c00bb4f63de2 Mon Sep 17 00:00:00 2001 From: qclc <39878907+qclc@users.noreply.github.com> Date: Thu, 23 Sep 2021 15:22:14 +0800 Subject: [PATCH] Get the actual edge object name from the label --- Makefile | 2 +- controllers/device_controller.go | 12 +++++---- controllers/device_syncer.go | 12 +++------ controllers/deviceprofile_controller.go | 25 +++++++++---------- controllers/deviceprofile_syncer.go | 10 +++++--- controllers/deviceservice_controller.go | 13 +++++++--- controllers/deviceservice_syncer.go | 12 +++++---- controllers/util/util.go | 33 ++++++++++++++++++++++--- 8 files changed, 74 insertions(+), 45 deletions(-) diff --git a/Makefile b/Makefile index 1944f55..05ec318 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,6 @@ # Image URL to use all building/pushing image targets -IMG ?= controller:latest +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" diff --git a/controllers/device_controller.go b/controllers/device_controller.go index 4f60f16..87adc3e 100644 --- a/controllers/device_controller.go +++ b/controllers/device_controller.go @@ -67,14 +67,16 @@ func (r *DeviceReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctr log.V(4).Info("Reconciling the Device object", "Device", d.GetName()) // Update the conditions for device defer func() { + if d.Spec.Managed != true { + conditions.MarkFalse(&d, devicev1alpha1.DeviceManagingCondition, "this device is not managed by openyurt", clusterv1.ConditionSeverityInfo, "") + } conditions.SetSummary(&d, conditions.WithConditions(devicev1alpha1.DeviceSyncedCondition, devicev1alpha1.DeviceManagingCondition), ) err := r.Status().Update(ctx, &d) if client.IgnoreNotFound(err) != nil { if !apierrors.IsConflict(err) { - log.Info("err", "Conditions", d.Status.Conditions) - log.Error(err, "update device conditions failed") + log.V(4).Error(err, "update device conditions failed", "device", d.GetName()) } } }() @@ -136,7 +138,7 @@ func (r *DeviceReconciler) SetupWithManager(mgr ctrl.Manager) error { func (r *DeviceReconciler) reconcileDeleteDevice(ctx context.Context, d *devicev1alpha1.Device, log logr.Logger) error { // gets the actual name of the device on the Edge platform from the Label of the device - edgeDeviceName := d.ObjectMeta.Labels[EdgeXObjectName] + edgeDeviceName := util.GetEdgeDeviceName(d, EdgeXObjectName) if d.ObjectMeta.DeletionTimestamp.IsZero() { if len(d.GetFinalizers()) == 0 { patchData, _ := json.Marshal(map[string]interface{}{ @@ -170,7 +172,7 @@ func (r *DeviceReconciler) reconcileDeleteDevice(ctx context.Context, d *devicev func (r *DeviceReconciler) reconcileCreateDevice(ctx context.Context, d *devicev1alpha1.Device, log logr.Logger) error { // get the actual name of the device on the Edge platform from the Label of the device - edgeDeviceName := d.ObjectMeta.Labels[EdgeXObjectName] + edgeDeviceName := util.GetEdgeDeviceName(d, EdgeXObjectName) newDeviceStatus := d.Status.DeepCopy() log.V(4).Info("Checking if device already exist on the edge platform", "device", d.GetName()) // Checking if device already exist on the edge platform @@ -246,7 +248,7 @@ func (r *DeviceReconciler) reconcileUpdateDevice(ctx context.Context, d *devicev } else if len(failedPropertyNames) != 0 { err = fmt.Errorf("the following device properties failed to reconcile: %v", failedPropertyNames) conditions.MarkFalse(d, devicev1alpha1.DeviceManagingCondition, err.Error(), clusterv1.ConditionSeverityInfo, "") - return err + return nil } conditions.MarkTrue(d, devicev1alpha1.DeviceManagingCondition) return nil diff --git a/controllers/device_syncer.go b/controllers/device_syncer.go index bb9a51a..8008e2e 100644 --- a/controllers/device_syncer.go +++ b/controllers/device_syncer.go @@ -141,12 +141,12 @@ func (ds *DeviceSyncer) getAllDevices() (map[string]devicev1alpha1.Device, map[s return edgeDevice, kubeDevice, err } for i := range eDevs { - deviceName := getActualName(&eDevs[i]) + deviceName := util.GetEdgeDeviceName(&eDevs[i], EdgeXObjectName) edgeDevice[deviceName] = eDevs[i] } for i := range kDevs.Items { - deviceName := getActualName(&kDevs.Items[i]) + deviceName := util.GetEdgeDeviceName(&kDevs.Items[i], EdgeXObjectName) kubeDevice[deviceName] = kDevs.Items[i] } return edgeDevice, kubeDevice, nil @@ -163,7 +163,7 @@ func (ds *DeviceSyncer) findDiffDevice( for n := range edgeDevice { tmp := edgeDevice[n] - edName := getActualName(&tmp) + edName := util.GetEdgeDeviceName(&tmp, EdgeXObjectName) if _, exists := kubeDevice[edName]; !exists { ed := edgeDevice[n] redundantEdgeDevices[edName] = ds.completeCreateContent(&ed) @@ -179,7 +179,7 @@ func (ds *DeviceSyncer) findDiffDevice( continue } tmp := kubeDevice[n] - kdName := getActualName(&tmp) + kdName := util.GetEdgeDeviceName(&tmp, EdgeXObjectName) if _, exists := edgeDevice[kdName]; !exists { kd := kubeDevice[n] redundantKubeDevices[kdName] = &kd @@ -251,7 +251,3 @@ func (ds *DeviceSyncer) completeUpdateContent(kubeDevice *devicev1alpha1.Device, updatedDevice.Status.DeviceProperties = aps return updatedDevice } - -func getActualName(d *devicev1alpha1.Device) string { - return d.Labels[EdgeXObjectName] -} diff --git a/controllers/deviceprofile_controller.go b/controllers/deviceprofile_controller.go index 0501b1c..4a50539 100644 --- a/controllers/deviceprofile_controller.go +++ b/controllers/deviceprofile_controller.go @@ -20,7 +20,6 @@ import ( "context" "fmt" "reflect" - "strings" apierrors "k8s.io/apimachinery/pkg/api/errors" "k8s.io/apimachinery/pkg/runtime" @@ -30,6 +29,7 @@ import ( "github.com/go-logr/logr" devicev1alpha1 "github.com/openyurtio/device-controller/api/v1alpha1" + clis "github.com/openyurtio/device-controller/clients" devcli "github.com/openyurtio/device-controller/clients" edgexclis "github.com/openyurtio/device-controller/clients/edgex-foundry" "github.com/openyurtio/device-controller/controllers/util" @@ -59,27 +59,24 @@ func (r *DeviceProfileReconciler) Reconcile(ctx context.Context, req ctrl.Reques return ctrl.Result{}, nil } - dpName := util.GetEdgeNameTrimNodePool(curdp.GetName(), r.NodePool) - var prevdp devicev1alpha1.DeviceProfile + dpActualName := util.GetEdgeDeviceProfileName(&curdp, EdgeXObjectName) + var prevdp *devicev1alpha1.DeviceProfile var exist bool - edps, err := r.edgeClient.List(context.Background(), devcli.ListOptions{}) - if err != nil { + prevdp, err := r.edgeClient.Get(context.Background(), dpActualName, devcli.GetOptions{}) + if err == nil { + exist = true + } else if clis.IsNotFoundErr(err) { + exist = false + } else { return ctrl.Result{}, err } - for _, edp := range edps { - if strings.ToLower(edp.Name) == dpName { - prevdp = edp - exist = true - break - } - } if !curdp.ObjectMeta.DeletionTimestamp.IsZero() { if exist { - if err := r.edgeClient.Delete(context.Background(), prevdp.Name, devcli.DeleteOptions{}); err != nil { + if err := r.edgeClient.Delete(context.Background(), dpActualName, devcli.DeleteOptions{}); err != nil { return ctrl.Result{}, fmt.Errorf("Fail to delete DeviceProfile on Edgex: %v", err) } - log.Info("Successfully delete DeviceProfile on EdgeX", "DeviceProfile", prevdp.Name) + log.Info("Successfully delete DeviceProfile on EdgeX", "DeviceProfile", dpActualName) } controllerutil.RemoveFinalizer(&curdp, "devicecontroller.openyurt.io") err := r.Update(context.TODO(), &curdp) diff --git a/controllers/deviceprofile_syncer.go b/controllers/deviceprofile_syncer.go index b476861..7742d47 100644 --- a/controllers/deviceprofile_syncer.go +++ b/controllers/deviceprofile_syncer.go @@ -128,8 +128,9 @@ func findNewUpdateDeviceProfile(edgeXDevs, kubeDevs []devicev1alpha1.DeviceProfi var addDevs, updateDevs []devicev1alpha1.DeviceProfile for _, exd := range edgeXDevs { var exist bool - for _, kd := range kubeDevs { - if strings.ToLower(exd.Name) == util.GetEdgeNameTrimNodePool(kd.Name, kd.Spec.NodePool) { + for i, kd := range kubeDevs { + dp := kubeDevs[i] + if exd.Name == strings.ToLower(util.GetEdgeDeviceProfileName(&dp, EdgeXObjectName)) { exist = true if !reflect.DeepEqual(exd.Spec, kd.Spec) { kd.Spec = exd.Spec @@ -151,8 +152,9 @@ func findDeleteDeviceProfile(edgeXDevs, kubeDevs []devicev1alpha1.DeviceProfile) var deleteDevs []devicev1alpha1.DeviceProfile for _, kd := range kubeDevs { var exist bool - for _, exd := range edgeXDevs { - if strings.ToLower(exd.Name) == util.GetEdgeNameTrimNodePool(kd.Name, kd.Spec.NodePool) { + for i, exd := range edgeXDevs { + dp := edgeXDevs[i] + if exd.Name == strings.ToLower(util.GetEdgeDeviceProfileName(&dp, EdgeXObjectName)) { exist = true break } diff --git a/controllers/deviceservice_controller.go b/controllers/deviceservice_controller.go index 5b756ea..e12724e 100644 --- a/controllers/deviceservice_controller.go +++ b/controllers/deviceservice_controller.go @@ -64,13 +64,18 @@ func (r *DeviceServiceReconciler) Reconcile(ctx context.Context, req ctrl.Reques log.V(4).Info("Reconciling the DeviceService object", "DeviceService", ds.GetName()) // Update deviceService conditions defer func() { + if ds.Spec.Managed != true { + conditions.MarkFalse(&ds, devicev1alpha1.DeviceServiceManagingCondition, "this deviceService is not managed by openyurt", clusterv1.ConditionSeverityInfo, "") + } conditions.SetSummary(&ds, conditions.WithConditions( devicev1alpha1.DeviceServiceSyncedCondition, devicev1alpha1.DeviceServiceManagingCondition), ) err := r.Status().Update(ctx, &ds) if client.IgnoreNotFound(err) != nil { - log.Error(err, "update deviceService conditions failed", "deviceService") + if !apierrors.IsConflict(err) { + log.V(4).Error(err, "update deviceService conditions failed", "deviceService", ds.GetName()) + } } }() @@ -128,7 +133,7 @@ func (r *DeviceServiceReconciler) SetupWithManager(mgr ctrl.Manager) error { func (r *DeviceServiceReconciler) reconcileDeleteDeviceService(ctx context.Context, ds *devicev1alpha1.DeviceService) error { // gets the actual name of deviceService on the edge platform from the Label of the device - edgeDeviceServiceName := ds.ObjectMeta.Labels[EdgeXObjectName] + edgeDeviceServiceName := util.GetEdgeDeviceServiceName(ds, EdgeXObjectName) if ds.ObjectMeta.DeletionTimestamp.IsZero() { if len(ds.GetFinalizers()) == 0 { patchString := map[string]interface{}{ @@ -170,10 +175,10 @@ func (r *DeviceServiceReconciler) reconcileDeleteDeviceService(ctx context.Conte func (r *DeviceServiceReconciler) reconcileCreateDeviceService(ctx context.Context, ds *devicev1alpha1.DeviceService, log logr.Logger) error { // get the actual name of deviceService on the Edge platform from the Label of the device - edgeDeviceName := ds.ObjectMeta.Labels[EdgeXObjectName] + edgeDeviceServiceName := util.GetEdgeDeviceServiceName(ds, EdgeXObjectName) log.V(4).Info("Checking if deviceService already exist on the edge platform", "deviceService", ds.GetName()) // Checking if deviceService already exist on the edge platform - if edgeDs, err := r.deviceServiceCli.Get(nil, edgeDeviceName, edgeInterface.GetOptions{}); err != nil { + if edgeDs, err := r.deviceServiceCli.Get(nil, edgeDeviceServiceName, edgeInterface.GetOptions{}); err != nil { if !clis.IsNotFoundErr(err) { log.V(4).Error(err, "fail to visit the edge platform") return nil diff --git a/controllers/deviceservice_syncer.go b/controllers/deviceservice_syncer.go index ea4106c..28f3715 100644 --- a/controllers/deviceservice_syncer.go +++ b/controllers/deviceservice_syncer.go @@ -138,12 +138,12 @@ func (ds *DeviceServiceSyncer) getAllDeviceServices() ( return edgeDeviceServices, kubeDeviceServices, err } for i := range eDevSs { - deviceServicesName := eDevSs[i].Labels[EdgeXObjectName] + deviceServicesName := util.GetEdgeDeviceServiceName(&eDevSs[i], EdgeXObjectName) edgeDeviceServices[deviceServicesName] = eDevSs[i] } for i := range kDevSs.Items { - deviceServicesName := kDevSs.Items[i].Labels[EdgeXObjectName] + deviceServicesName := util.GetEdgeDeviceServiceName(&kDevSs.Items[i], EdgeXObjectName) kubeDeviceServices[deviceServicesName] = kDevSs.Items[i] } return edgeDeviceServices, kubeDeviceServices, nil @@ -158,8 +158,9 @@ func (ds *DeviceServiceSyncer) findDiffDeviceServices( redundantKubeDeviceServices = map[string]*devicev1alpha1.DeviceService{} syncedDeviceServices = map[string]*devicev1alpha1.DeviceService{} - for n, v := range edgeDeviceService { - edName := v.Labels[EdgeXObjectName] + for n := range edgeDeviceService { + eds := edgeDeviceService[n] + edName := util.GetEdgeDeviceServiceName(&eds, EdgeXObjectName) if _, exists := kubeDeviceService[edName]; !exists { ed := edgeDeviceService[n] redundantEdgeDeviceServices[edName] = ds.completeCreateContent(&ed) @@ -174,7 +175,8 @@ func (ds *DeviceServiceSyncer) findDiffDeviceServices( if !v.Status.Synced { continue } - kdName := v.Labels[EdgeXObjectName] + kds := kubeDeviceService[k] + kdName := util.GetEdgeDeviceServiceName(&kds, EdgeXObjectName) if _, exists := edgeDeviceService[kdName]; !exists { kd := kubeDeviceService[k] redundantKubeDeviceServices[kdName] = &kd diff --git a/controllers/util/util.go b/controllers/util/util.go index 1222a14..27b127e 100644 --- a/controllers/util/util.go +++ b/controllers/util/util.go @@ -25,6 +25,8 @@ import ( metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/client-go/kubernetes" "k8s.io/client-go/rest" + + devicev1alpha1 "github.com/openyurtio/device-controller/api/v1alpha1" ) const ( @@ -66,9 +68,32 @@ func GetNodePool(cfg *rest.Config) (string, error) { return nodePool, err } -func GetEdgeNameTrimNodePool(kubeName, NodePoolName string) string { - if NodePoolName == "" { - return kubeName +func GetEdgeDeviceServiceName(ds *devicev1alpha1.DeviceService, label string) string { + var actualDSName string + if _, ok := ds.ObjectMeta.Labels[label]; ok { + actualDSName = ds.ObjectMeta.Labels[label] + } else { + actualDSName = ds.GetName() + } + return actualDSName +} + +func GetEdgeDeviceName(d *devicev1alpha1.Device, label string) string { + var actualDeviceName string + if _, ok := d.ObjectMeta.Labels[label]; ok { + actualDeviceName = d.ObjectMeta.Labels[label] + } else { + actualDeviceName = d.GetName() + } + return actualDeviceName +} + +func GetEdgeDeviceProfileName(dp *devicev1alpha1.DeviceProfile, label string) string { + var actualDPName string + if _, ok := dp.ObjectMeta.Labels[label]; ok { + actualDPName = dp.ObjectMeta.Labels[label] + } else { + actualDPName = dp.GetName() } - return strings.TrimPrefix(kubeName, fmt.Sprintf("%s-", NodePoolName)) + return actualDPName }