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

Commit

Permalink
Merge pull request #5 from qclc/0.1
Browse files Browse the repository at this point in the history
Get the actual edge object name from the label
  • Loading branch information
rambohe-ch authored Sep 23, 2021
2 parents 5b9f1d9 + 3c5a90d commit a9b7e1b
Show file tree
Hide file tree
Showing 8 changed files with 74 additions and 45 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -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"

Expand Down
12 changes: 7 additions & 5 deletions controllers/device_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
}
}()
Expand Down Expand Up @@ -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{}{
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
12 changes: 4 additions & 8 deletions controllers/device_syncer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -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
Expand Down Expand Up @@ -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]
}
25 changes: 11 additions & 14 deletions controllers/deviceprofile_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"context"
"fmt"
"reflect"
"strings"

apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/runtime"
Expand All @@ -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"
Expand Down Expand Up @@ -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)
Expand Down
10 changes: 6 additions & 4 deletions controllers/deviceprofile_syncer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}
Expand Down
13 changes: 9 additions & 4 deletions controllers/deviceservice_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
}
}()

Expand Down Expand Up @@ -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{}{
Expand Down Expand Up @@ -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
Expand Down
12 changes: 7 additions & 5 deletions controllers/deviceservice_syncer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -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
Expand Down
33 changes: 29 additions & 4 deletions controllers/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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
}

0 comments on commit a9b7e1b

Please sign in to comment.