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

Commit 4b18562

Browse files
author
Jakub Dzon
committed
API changes related to design review
Signed-off-by: Jakub Dzon <[email protected]>
1 parent d712fed commit 4b18562

File tree

8 files changed

+124
-116
lines changed

8 files changed

+124
-116
lines changed

internal/edgeapi/backend/api.go

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,44 +6,44 @@ import (
66
"github.com/project-flotta/flotta-operator/models"
77
)
88

9+
const (
10+
Registered = RegistrationStatus("registered")
11+
Unregistered = RegistrationStatus("unregistered")
12+
Unknown = RegistrationStatus("unknown")
13+
)
14+
15+
type RegistrationStatus string
16+
917
type Notification struct {
1018
DeviceID string
1119
Namespace string
1220
Heartbeat *models.Heartbeat
1321
Retry int32
1422
}
1523

16-
//go:generate mockgen -package=backend -destination=mock_heartbeat-handler.go . HeartbeatHandler
17-
type HeartbeatHandler interface {
18-
Process(ctx context.Context, notification Notification) (bool, error)
19-
}
20-
21-
// Backend represents API provided by data storage service to support edge device lifecycle.
22-
type Backend interface {
23-
// ShouldEdgeDeviceBeUnregistered responds with true, when the device identified with name and namespace should be
24-
// instructed to execute de-registration procedure
25-
ShouldEdgeDeviceBeUnregistered(ctx context.Context, name, namespace string) (bool, error)
24+
// EdgeDeviceBackend represents API provided by data storage service to support edge device lifecycle.
25+
type EdgeDeviceBackend interface {
26+
// GetRegistrationStatus responds with status of a device registration: {enrolled, registered, unregistered}
27+
GetRegistrationStatus(ctx context.Context, name, namespace string) (RegistrationStatus, error)
2628

27-
// GetDeviceConfiguration provides complete Edge Device configuration that should be applied to the device
28-
GetDeviceConfiguration(ctx context.Context, name, namespace string) (*models.DeviceConfigurationMessage, error)
29+
// GetConfiguration provides complete Edge Device configuration that should be applied to the device
30+
GetConfiguration(ctx context.Context, name, namespace string) (*models.DeviceConfigurationMessage, error)
2931

30-
// EnrolEdgeDevice records device willingness to be connected to the cluster.
31-
EnrolEdgeDevice(ctx context.Context, name string, enrolmentInfo *models.EnrolmentInfo) (bool, error)
32+
// Enrol records device willingness to be connected to the cluster.
33+
Enrol(ctx context.Context, name string, enrolmentInfo *models.EnrolmentInfo) (bool, error)
3234

33-
// InitializeEdgeDeviceRegistration is called when device sends registration request (either to issue the mTLS certificate
34-
// for the first time or renew it) and has to return information whether it is handling first registration request from the device,
35-
// and namespace the device should be created in.
36-
InitializeEdgeDeviceRegistration(ctx context.Context, name, namespace string, matchesCertificate bool) (bool, string, error)
35+
// GetTargetNamespace returns the namespace the device should belong to.
36+
GetTargetNamespace(ctx context.Context, name, namespace string, matchesCertificate bool) (string, error)
3737

38-
// FinalizeEdgeDeviceRegistration is called during device registration request handling, after mTLS certificate has
38+
// FinalizeRegistration is called during device registration request handling, after mTLS certificate has
3939
// been correctly issued.
4040
// The responsibility of the method is to potentially record information that the device is finally registered and
4141
// what hardware configuration it has.
42-
FinalizeEdgeDeviceRegistration(ctx context.Context, name, namespace string, registrationInfo *models.RegistrationInfo) error
42+
FinalizeRegistration(ctx context.Context, name, namespace string, registrationInfo *models.RegistrationInfo) error
4343

44-
// GetHeartbeatHandler provides implementation of a HeartbeatHandler that should record current state of the device sent in
44+
// UpdateStatus records current state of the device sent in a heartbeat message
4545
// (i.e. workload status, events reported by the device, OS upgrade status).
46-
GetHeartbeatHandler() HeartbeatHandler
46+
UpdateStatus(ctx context.Context, notification Notification) (bool, error)
4747
}
4848

4949
type NotApproved struct {

internal/edgeapi/backend/factory/factory.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import (
1818
"github.com/project-flotta/flotta-operator/internal/edgeapi/k8sclient"
1919
)
2020

21-
func CreateBackend(initialDeviceNamespace string, client kubeclient.Client, logger *zap.SugaredLogger, eventRecorder record.EventRecorder) backend.Backend {
21+
func CreateBackend(initialDeviceNamespace string, client kubeclient.Client, logger *zap.SugaredLogger, eventRecorder record.EventRecorder) backend.EdgeDeviceBackend {
2222
// For now just one implementation is supported
2323
k8sClient := k8sclient.NewK8sClient(client)
2424

internal/edgeapi/backend/k8s/backend.go

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -31,38 +31,38 @@ type backend struct {
3131
repository RepositoryFacade
3232
assembler *ConfigurationAssembler
3333
initialNamespace string
34-
heartbeatHandler backendapi.HeartbeatHandler
34+
heartbeatHandler *SynchronousHandler
3535
}
3636

3737
func NewBackend(repository RepositoryFacade, assembler *ConfigurationAssembler,
38-
logger *zap.SugaredLogger, initialNamespace string, recorder record.EventRecorder) backendapi.Backend {
38+
logger *zap.SugaredLogger, initialNamespace string, recorder record.EventRecorder) backendapi.EdgeDeviceBackend {
3939
return &backend{repository: repository,
4040
assembler: assembler,
4141
logger: logger,
4242
initialNamespace: initialNamespace,
4343
heartbeatHandler: NewSynchronousHandler(repository, recorder, logger)}
4444
}
4545

46-
func (b *backend) ShouldEdgeDeviceBeUnregistered(ctx context.Context, name, namespace string) (bool, error) {
46+
func (b *backend) GetRegistrationStatus(ctx context.Context, name, namespace string) (backendapi.RegistrationStatus, error) {
4747
edgeDevice, err := b.repository.GetEdgeDevice(ctx, name, namespace)
4848
if err != nil {
49-
return false, err
49+
return backendapi.Unknown, err
5050
}
5151

5252
if edgeDevice.DeletionTimestamp == nil || utils.HasFinalizer(&edgeDevice.ObjectMeta, YggdrasilWorkloadFinalizer) {
53-
return false, nil
53+
return backendapi.Registered, nil
5454
}
5555

5656
if utils.HasFinalizer(&edgeDevice.ObjectMeta, YggdrasilConnectionFinalizer) {
5757
err = b.repository.RemoveEdgeDeviceFinalizer(ctx, edgeDevice, YggdrasilConnectionFinalizer)
5858
if err != nil {
59-
return false, err
59+
return backendapi.Registered, err
6060
}
6161
}
62-
return true, nil
62+
return backendapi.Unregistered, nil
6363
}
6464

65-
func (b *backend) GetDeviceConfiguration(ctx context.Context, name, namespace string) (*models.DeviceConfigurationMessage, error) {
65+
func (b *backend) GetConfiguration(ctx context.Context, name, namespace string) (*models.DeviceConfigurationMessage, error) {
6666
logger := b.logger.With("DeviceID", name)
6767
edgeDevice, err := b.repository.GetEdgeDevice(ctx, name, namespace)
6868
if err != nil {
@@ -80,7 +80,7 @@ func (b *backend) GetDeviceConfiguration(ctx context.Context, name, namespace st
8080
return b.assembler.GetDeviceConfiguration(ctx, edgeDevice, logger)
8181
}
8282

83-
func (b *backend) EnrolEdgeDevice(ctx context.Context, name string, enrolmentInfo *models.EnrolmentInfo) (bool, error) {
83+
func (b *backend) Enrol(ctx context.Context, name string, enrolmentInfo *models.EnrolmentInfo) (bool, error) {
8484
targetNamespace := b.initialNamespace
8585
if enrolmentInfo.TargetNamespace != nil {
8686
targetNamespace = *enrolmentInfo.TargetNamespace
@@ -122,14 +122,14 @@ func (b *backend) EnrolEdgeDevice(ctx context.Context, name string, enrolmentInf
122122
return false, b.repository.CreateEdgeDeviceSignedRequest(ctx, edsr)
123123
}
124124

125-
func (b *backend) InitializeEdgeDeviceRegistration(ctx context.Context, name, identityNamespace string, matchesCertificate bool) (bool, string, error) {
125+
func (b *backend) GetTargetNamespace(ctx context.Context, name, identityNamespace string, matchesCertificate bool) (string, error) {
126126
logger := b.logger.With("DeviceID", name)
127127
namespace := identityNamespace
128128
if identityNamespace == b.initialNamespace && !matchesCertificate {
129129
// check if it's a valid device, shouldn't match
130130
esdr, err := b.repository.GetEdgeDeviceSignedRequest(ctx, name, b.initialNamespace)
131131
if err != nil {
132-
return false, "", err
132+
return "", err
133133
}
134134
if esdr.Spec.TargetNamespace != "" {
135135
namespace = esdr.Spec.TargetNamespace
@@ -138,13 +138,13 @@ func (b *backend) InitializeEdgeDeviceRegistration(ctx context.Context, name, id
138138
dvc, err := b.repository.GetEdgeDevice(ctx, name, namespace)
139139
if err != nil {
140140
if errors.IsNotFound(err) {
141-
return false, "", backendapi.NewNotApproved(err)
141+
return "", backendapi.NewNotApproved(err)
142142
}
143-
return false, "", err
143+
return "", err
144144
}
145145

146146
if dvc == nil {
147-
return false, "", fmt.Errorf("device not found")
147+
return "", fmt.Errorf("device not found")
148148
}
149149

150150
isInit := false
@@ -159,13 +159,18 @@ func (b *backend) InitializeEdgeDeviceRegistration(ctx context.Context, name, id
159159
// At this moment, the registration certificate it's no longer valid,
160160
// because the CR is already created, and need to be a device
161161
// certificate.
162-
return false, "", fmt.Errorf("forbidden")
162+
return "", fmt.Errorf("forbidden")
163163
}
164164

165-
return isInit, namespace, nil
165+
if isInit {
166+
logger.Info("EdgeDevice registered correctly for first time")
167+
} else {
168+
logger.Info("EdgeDevice renew registration correctly")
169+
}
170+
return namespace, nil
166171
}
167172

168-
func (b *backend) FinalizeEdgeDeviceRegistration(ctx context.Context, name, namespace string, registrationInfo *models.RegistrationInfo) error {
173+
func (b *backend) FinalizeRegistration(ctx context.Context, name, namespace string, registrationInfo *models.RegistrationInfo) error {
169174
logger := b.logger.With("DeviceID", name)
170175
dvc, err := b.repository.GetEdgeDevice(ctx, name, namespace)
171176
deviceCopy := dvc.DeepCopy()
@@ -187,8 +192,8 @@ func (b *backend) FinalizeEdgeDeviceRegistration(ctx context.Context, name, name
187192
return err
188193
}
189194

190-
func (b *backend) GetHeartbeatHandler() backendapi.HeartbeatHandler {
191-
return b.heartbeatHandler
195+
func (b *backend) UpdateStatus(ctx context.Context, notification backendapi.Notification) (bool, error){
196+
return b.heartbeatHandler.Process(ctx, notification)
192197
}
193198

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

internal/edgeapi/backend/mock_heartbeat-handler.go

Lines changed: 0 additions & 50 deletions
This file was deleted.

internal/edgeapi/yggdrasil/heartbeat-handler.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,16 @@ import (
77
"github.com/project-flotta/flotta-operator/internal/edgeapi/backend"
88
)
99

10+
//go:generate mockgen -package=yggdrasil -destination=mock_status-updater.go . StatusUpdater
11+
type StatusUpdater interface {
12+
UpdateStatus(ctx context.Context, notification backend.Notification) (bool, error)
13+
}
14+
1015
type RetryingDelegatingHandler struct {
11-
delegate backend.HeartbeatHandler
16+
delegate StatusUpdater
1217
}
1318

14-
func NewRetryingDelegatingHandler(delegate backend.HeartbeatHandler) *RetryingDelegatingHandler {
19+
func NewRetryingDelegatingHandler(delegate StatusUpdater) *RetryingDelegatingHandler {
1520
return &RetryingDelegatingHandler{delegate: delegate}
1621
}
1722

@@ -20,7 +25,7 @@ func (h *RetryingDelegatingHandler) Process(ctx context.Context, notification ba
2025
var err error
2126
var retry bool
2227
for i := 1; i < 5; i++ {
23-
retry, err = h.delegate.Process(ctx, notification)
28+
retry, err = h.delegate.UpdateStatus(ctx, notification)
2429
if err == nil {
2530
return nil
2631
}

internal/edgeapi/yggdrasil/heartbeat-handler_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,13 @@ var _ = Describe("Heartbeat handler", func() {
1616

1717
var (
1818
mockCtrl *gomock.Controller
19-
mockDelegate *backend.MockHeartbeatHandler
19+
mockDelegate *yggdrasil.MockStatusUpdater
2020
handler *yggdrasil.RetryingDelegatingHandler
2121
)
2222

2323
BeforeEach(func() {
2424
mockCtrl = gomock.NewController(GinkgoT())
25-
mockDelegate = backend.NewMockHeartbeatHandler(mockCtrl)
25+
mockDelegate = yggdrasil.NewMockStatusUpdater(mockCtrl)
2626

2727
handler = yggdrasil.NewRetryingDelegatingHandler(mockDelegate)
2828
})
@@ -37,7 +37,7 @@ var _ = Describe("Heartbeat handler", func() {
3737
notification := backend.Notification{DeviceID: "1234"}
3838

3939
mockDelegate.EXPECT().
40-
Process(ctx, notification).
40+
UpdateStatus(ctx, notification).
4141
Return(false, nil)
4242

4343
// when
@@ -54,10 +54,10 @@ var _ = Describe("Heartbeat handler", func() {
5454
notification := backend.Notification{DeviceID: "1234"}
5555
retryNotification := backend.Notification{DeviceID: "1234", Retry: 1}
5656
errorCall := mockDelegate.EXPECT().
57-
Process(ctx, notification).
57+
UpdateStatus(ctx, notification).
5858
Return(true, fmt.Errorf("boom"))
5959
mockDelegate.EXPECT().
60-
Process(ctx, retryNotification).
60+
UpdateStatus(ctx, retryNotification).
6161
Return(false, nil).
6262
After(errorCall)
6363

@@ -74,7 +74,7 @@ var _ = Describe("Heartbeat handler", func() {
7474
notification := backend.Notification{DeviceID: "1234"}
7575

7676
mockDelegate.EXPECT().
77-
Process(ctx, gomock.AssignableToTypeOf(notification)).
77+
UpdateStatus(ctx, gomock.AssignableToTypeOf(notification)).
7878
Return(true, fmt.Errorf("boom")).
7979
Times(4)
8080

internal/edgeapi/yggdrasil/mock_status-updater.go

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

0 commit comments

Comments
 (0)