Skip to content
This repository was archived by the owner on Dec 1, 2024. It is now read-only.

Commit 7068af9

Browse files
author
Jakub Dzon
committed
API unification
Signed-off-by: Jakub Dzon <[email protected]>
1 parent 4896578 commit 7068af9

File tree

7 files changed

+42
-45
lines changed

7 files changed

+42
-45
lines changed

internal/edgeapi/backend/api.go

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,24 +8,22 @@ import (
88

99
const (
1010
// Registered describes edge device that is fully authorized to communicate with the control plane
11-
Registered = RegistrationStatus("registered")
11+
Registered = RegistrationStatus("registered")
1212
// Unregistered describes edge device that is not authorized to communicate with the control plane and will be
1313
// instructed to execute unregistration logic
1414
Unregistered = RegistrationStatus("unregistered")
1515
// Unknown signals that the status of the device can't be established (for example due to data retrieval errors)
16-
Unknown = RegistrationStatus("unknown")
16+
Unknown = RegistrationStatus("unknown")
1717
)
1818

1919
type RegistrationStatus string
2020

2121
// Notification carries device heartbeat information
2222
type Notification struct {
23-
DeviceID string
24-
Namespace string
2523
// Heartbeat contains information sent by the device as part of the heartbeat
2624
Heartbeat *models.Heartbeat
2725
// Retry carries information about the retry number for the same heartbeat
28-
Retry int32
26+
Retry int32
2927
}
3028

3129
// EdgeDeviceBackend represents API provided by data storage service to support edge device lifecycle.
@@ -37,7 +35,7 @@ type EdgeDeviceBackend interface {
3735
GetConfiguration(ctx context.Context, name, namespace string) (*models.DeviceConfigurationMessage, error)
3836

3937
// Enrol records device willingness to be connected to the cluster.
40-
Enrol(ctx context.Context, name string, enrolmentInfo *models.EnrolmentInfo) (bool, error)
38+
Enrol(ctx context.Context, name, namespace string, enrolmentInfo *models.EnrolmentInfo) (bool, error)
4139

4240
// GetTargetNamespace returns the namespace the device should belong to.
4341
GetTargetNamespace(ctx context.Context, name, namespace string, matchesCertificate bool) (string, error)
@@ -50,7 +48,7 @@ type EdgeDeviceBackend interface {
5048

5149
// UpdateStatus records current state of the device sent in a heartbeat message
5250
// (i.e. workload status, events reported by the device, OS upgrade status).
53-
UpdateStatus(ctx context.Context, notification Notification) (bool, error)
51+
UpdateStatus(ctx context.Context, name, namespace string, notification Notification) (bool, error)
5452
}
5553

5654
type NotApproved struct {

internal/edgeapi/backend/k8s/backend.go

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -80,12 +80,8 @@ func (b *backend) GetConfiguration(ctx context.Context, name, namespace string)
8080
return b.assembler.GetDeviceConfiguration(ctx, edgeDevice, logger)
8181
}
8282

83-
func (b *backend) Enrol(ctx context.Context, name string, enrolmentInfo *models.EnrolmentInfo) (bool, error) {
84-
targetNamespace := b.initialNamespace
85-
if enrolmentInfo.TargetNamespace != nil {
86-
targetNamespace = *enrolmentInfo.TargetNamespace
87-
}
88-
_, err := b.repository.GetEdgeDevice(ctx, name, targetNamespace)
83+
func (b *backend) Enrol(ctx context.Context, name, namespace string, enrolmentInfo *models.EnrolmentInfo) (bool, error) {
84+
_, err := b.repository.GetEdgeDevice(ctx, name, namespace)
8985
if err == nil {
9086
// Device is already created.
9187
return true, nil
@@ -94,7 +90,7 @@ func (b *backend) Enrol(ctx context.Context, name string, enrolmentInfo *models.
9490
edsr, err := b.repository.GetEdgeDeviceSignedRequest(ctx, name, b.initialNamespace)
9591
if err == nil {
9692
// Is already created, but not approved
97-
if edsr.Spec.TargetNamespace != targetNamespace {
93+
if edsr.Spec.TargetNamespace != namespace {
9894
_, err = b.repository.GetEdgeDevice(ctx, name, edsr.Spec.TargetNamespace)
9995
if err == nil {
10096
// Device is already created.
@@ -111,7 +107,7 @@ func (b *backend) Enrol(ctx context.Context, name string, enrolmentInfo *models.
111107
Namespace: b.initialNamespace,
112108
},
113109
Spec: v1alpha1.EdgeDeviceSignedRequestSpec{
114-
TargetNamespace: targetNamespace,
110+
TargetNamespace: namespace,
115111
Approved: false,
116112
Features: &v1alpha1.Features{
117113
Hardware: hardware.MapHardware(enrolmentInfo.Features.Hardware),
@@ -192,8 +188,8 @@ func (b *backend) FinalizeRegistration(ctx context.Context, name, namespace stri
192188
return err
193189
}
194190

195-
func (b *backend) UpdateStatus(ctx context.Context, notification backendapi.Notification) (bool, error) {
196-
return b.heartbeatHandler.Process(ctx, notification)
191+
func (b *backend) UpdateStatus(ctx context.Context, name, namespace string, notification backendapi.Notification) (bool, error) {
192+
return b.heartbeatHandler.Process(ctx, name, namespace, notification)
197193
}
198194

199195
func (b *backend) updateDeviceStatus(ctx context.Context, device *v1alpha1.EdgeDevice, updateFunc func(d *v1alpha1.EdgeDevice)) error {

internal/edgeapi/backend/k8s/heartbeat-handler.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,11 @@ func NewSynchronousHandler(repository RepositoryFacade, recorder record.EventRec
2828
}
2929
}
3030

31-
func (h *SynchronousHandler) Process(ctx context.Context, notification backendapi.Notification) (bool, error) {
32-
logger := h.logger.With("DeviceID", notification.DeviceID, "Namespace", notification.Namespace)
31+
func (h *SynchronousHandler) Process(ctx context.Context, name, namespace string, notification backendapi.Notification) (bool, error) {
32+
logger := h.logger.With("DeviceID", name, "Namespace", namespace)
3333
hb := notification.Heartbeat
3434
logger.Debug("processing heartbeat", "content", hb, "retry", notification.Retry)
35-
edgeDevice, err := h.repository.GetEdgeDevice(ctx, notification.DeviceID, notification.Namespace)
35+
edgeDevice, err := h.repository.GetEdgeDevice(ctx, name, namespace)
3636
if err != nil {
3737
if errors.IsNotFound(err) {
3838
return false, err

internal/edgeapi/yggdrasil/heartbeat-handler.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99

1010
//go:generate mockgen -package=yggdrasil -destination=mock_status-updater.go . StatusUpdater
1111
type StatusUpdater interface {
12-
UpdateStatus(ctx context.Context, notification backend.Notification) (bool, error)
12+
UpdateStatus(ctx context.Context, name, namespace string, notification backend.Notification) (bool, error)
1313
}
1414

1515
type RetryingDelegatingHandler struct {
@@ -20,12 +20,12 @@ func NewRetryingDelegatingHandler(delegate StatusUpdater) *RetryingDelegatingHan
2020
return &RetryingDelegatingHandler{delegate: delegate}
2121
}
2222

23-
func (h *RetryingDelegatingHandler) Process(ctx context.Context, notification backend.Notification) error {
23+
func (h *RetryingDelegatingHandler) Process(ctx context.Context, name, namespace string, notification backend.Notification) error {
2424
// retry patching the edge device status
2525
var err error
2626
var retry bool
2727
for i := 1; i < 5; i++ {
28-
retry, err = h.delegate.UpdateStatus(ctx, notification)
28+
retry, err = h.delegate.UpdateStatus(ctx, name, namespace, notification)
2929
if err == nil {
3030
return nil
3131
}

internal/edgeapi/yggdrasil/heartbeat-handler_test.go

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ import (
1414

1515
var _ = Describe("Heartbeat handler", func() {
1616

17+
const (
18+
deviceID = "dev-ns"
19+
deviceNamespace = "dev-ns"
20+
)
1721
var (
1822
mockCtrl *gomock.Controller
1923
mockDelegate *yggdrasil.MockStatusUpdater
@@ -34,14 +38,14 @@ var _ = Describe("Heartbeat handler", func() {
3438
It("should call delegate", func() {
3539
// given
3640
ctx := context.TODO()
37-
notification := backend.Notification{DeviceID: "1234"}
41+
notification := backend.Notification{}
3842

3943
mockDelegate.EXPECT().
40-
UpdateStatus(ctx, notification).
44+
UpdateStatus(ctx, deviceID, deviceNamespace, notification).
4145
Return(false, nil)
4246

4347
// when
44-
err := handler.Process(ctx, notification)
48+
err := handler.Process(ctx, deviceID, deviceNamespace, notification)
4549

4650
// then
4751
Expect(err).ToNot(HaveOccurred())
@@ -51,18 +55,18 @@ var _ = Describe("Heartbeat handler", func() {
5155
It("should retry calling delegate on error and succeed", func() {
5256
// given
5357
ctx := context.TODO()
54-
notification := backend.Notification{DeviceID: "1234"}
55-
retryNotification := backend.Notification{DeviceID: "1234", Retry: 1}
58+
notification := backend.Notification{}
59+
retryNotification := backend.Notification{Retry: 1}
5660
errorCall := mockDelegate.EXPECT().
57-
UpdateStatus(ctx, notification).
61+
UpdateStatus(ctx, deviceID, deviceNamespace, notification).
5862
Return(true, fmt.Errorf("boom"))
5963
mockDelegate.EXPECT().
60-
UpdateStatus(ctx, retryNotification).
64+
UpdateStatus(ctx, deviceID, deviceNamespace, retryNotification).
6165
Return(false, nil).
6266
After(errorCall)
6367

6468
// when
65-
err := handler.Process(ctx, notification)
69+
err := handler.Process(ctx, deviceID, deviceNamespace, notification)
6670

6771
// then
6872
Expect(err).ToNot(HaveOccurred())
@@ -71,15 +75,15 @@ var _ = Describe("Heartbeat handler", func() {
7175
It("should retry calling delegate on error and eventually fail", func() {
7276
// given
7377
ctx := context.TODO()
74-
notification := backend.Notification{DeviceID: "1234"}
78+
notification := backend.Notification{}
7579

7680
mockDelegate.EXPECT().
77-
UpdateStatus(ctx, gomock.AssignableToTypeOf(notification)).
81+
UpdateStatus(ctx, deviceID, deviceNamespace, gomock.AssignableToTypeOf(notification)).
7882
Return(true, fmt.Errorf("boom")).
7983
Times(4)
8084

8185
// when
82-
err := handler.Process(ctx, notification)
86+
err := handler.Process(ctx, deviceID, deviceNamespace, notification)
8387

8488
// then
8589
Expect(err).To(HaveOccurred())

internal/edgeapi/yggdrasil/mock_status-updater.go

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/edgeapi/yggdrasil/yggdrasil.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -186,11 +186,7 @@ func (h *Handler) PostDataMessageForDevice(ctx context.Context, params yggdrasil
186186
if err != nil {
187187
return operations.NewPostDataMessageForDeviceBadRequest()
188188
}
189-
err = h.heartbeatHandler.Process(ctx, backendapi.Notification{
190-
DeviceID: deviceID,
191-
Namespace: h.getNamespace(ctx),
192-
Heartbeat: &hb,
193-
})
189+
err = h.heartbeatHandler.Process(ctx, deviceID, h.getNamespace(ctx), backendapi.Notification{Heartbeat: &hb})
194190
if err != nil {
195191
if errors.IsNotFound(err) {
196192
logger.Debug("Device not found")
@@ -208,8 +204,11 @@ func (h *Handler) PostDataMessageForDevice(ctx context.Context, params yggdrasil
208204
return operations.NewPostDataMessageForDeviceBadRequest()
209205
}
210206
logger.Debug("received enrolment info", "content", enrolmentInfo)
211-
212-
alreadyCreated, err := h.backend.Enrol(ctx, deviceID, &enrolmentInfo)
207+
targetNamespace := h.initialNamespace
208+
if enrolmentInfo.TargetNamespace != nil {
209+
targetNamespace = *enrolmentInfo.TargetNamespace
210+
}
211+
alreadyCreated, err := h.backend.Enrol(ctx, deviceID, targetNamespace, &enrolmentInfo)
213212
if err != nil {
214213
return operations.NewPostDataMessageForDeviceBadRequest()
215214
}

0 commit comments

Comments
 (0)