Skip to content

Commit 40bd434

Browse files
authored
refactor: move validation/defaults to app (#514)
This change moves the adding of the mmds network interface and validation of the microvm spec to the app and out of the gRPC infrastructure. Signed-off-by: Richard Case <richard.case@outlook.com>
1 parent e6b0d58 commit 40bd434

File tree

5 files changed

+37
-45
lines changed

5 files changed

+37
-45
lines changed

core/application/app_test.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -629,12 +629,13 @@ func createTestSpecWithMetadata(name, ns, uid string, metadata map[string]string
629629
{
630630
AllowMetadataRequests: true,
631631
GuestMAC: "AA:FF:00:00:00:01",
632-
GuestDeviceName: "eth0",
632+
GuestDeviceName: "mmds",
633+
Type: models.IfaceTypeTap,
633634
},
634635
{
635636
AllowMetadataRequests: false,
636637
GuestDeviceName: "eth1",
637-
// TODO:
638+
Type: models.IfaceTypeMacvtap,
638639
},
639640
},
640641
RootVolume: models.Volume{

core/application/commands.go

+34-1
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,28 @@ import (
1414
"github.com/weaveworks-liquidmetal/flintlock/core/ports"
1515
"github.com/weaveworks-liquidmetal/flintlock/pkg/defaults"
1616
"github.com/weaveworks-liquidmetal/flintlock/pkg/log"
17+
"github.com/weaveworks-liquidmetal/flintlock/pkg/validation"
1718
"sigs.k8s.io/yaml"
1819
)
1920

21+
const (
22+
MetadataInterfaceName = "mmds"
23+
)
24+
2025
func (a *app) CreateMicroVM(ctx context.Context, mvm *models.MicroVM) (*models.MicroVM, error) {
2126
logger := log.GetLogger(ctx).WithField("component", "app")
22-
logger.Trace("creating microvm")
27+
logger.Debug("creating microvm")
2328

2429
if mvm == nil {
2530
return nil, coreerrs.ErrSpecRequired
2631
}
2732

33+
logger.Trace("validating model")
34+
validator := validation.NewValidator()
35+
if validErr := validator.ValidateStruct(mvm); validErr != nil {
36+
return nil, fmt.Errorf("an error occurred when attempting to validate microvm spec: %w", validErr)
37+
}
38+
2839
if mvm.ID.IsEmpty() {
2940
name, err := a.ports.IdentifierService.GenerateRandom()
3041
if err != nil {
@@ -70,6 +81,7 @@ func (a *app) CreateMicroVM(ctx context.Context, mvm *models.MicroVM) (*models.M
7081
if err != nil {
7182
return nil, fmt.Errorf("adding instance data: %w", err)
7283
}
84+
a.addMetadataInterface(mvm)
7385

7486
// Set the timestamp when the VMspec was created.
7587
mvm.Spec.CreatedAt = a.ports.Clock().Unix()
@@ -171,3 +183,24 @@ func (a *app) addInstanceData(vm *models.MicroVM, logger *logrus.Entry) error {
171183

172184
return nil
173185
}
186+
187+
func (a *app) addMetadataInterface(mvm *models.MicroVM) {
188+
for i := range mvm.Spec.NetworkInterfaces {
189+
netInt := mvm.Spec.NetworkInterfaces[i]
190+
if netInt.GuestDeviceName == MetadataInterfaceName {
191+
return
192+
}
193+
}
194+
195+
mvm.Spec.NetworkInterfaces = append(mvm.Spec.NetworkInterfaces, models.NetworkInterface{
196+
GuestDeviceName: MetadataInterfaceName,
197+
Type: models.IfaceTypeTap,
198+
AllowMetadataRequests: true,
199+
GuestMAC: "AA:FF:00:00:00:01",
200+
StaticAddress: &models.StaticAddress{
201+
Address: "169.254.0.1/16",
202+
},
203+
})
204+
205+
return
206+
}

infrastructure/grpc/convert.go

-4
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,6 @@ func convertMicroVMToModel(spec *types.MicroVMSpec) (*models.MicroVM, error) {
7272
convertedModel.Spec.NetworkInterfaces = append(convertedModel.Spec.NetworkInterfaces, *convertedNetInt)
7373
}
7474

75-
ifaces := []models.NetworkInterface{*newMetadataInterface()}
76-
ifaces = append(ifaces, convertedModel.Spec.NetworkInterfaces...)
77-
convertedModel.Spec.NetworkInterfaces = ifaces
78-
7975
convertedModel.Spec.Metadata = map[string]string{}
8076
for metadataKey, metadataValue := range spec.Metadata {
8177
convertedModel.Spec.Metadata[metadataKey] = metadataValue

infrastructure/grpc/defaults.go

-17
This file was deleted.

infrastructure/grpc/server.go

-21
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,13 @@ package grpc
22

33
import (
44
"context"
5-
"errors"
65
"fmt"
76

8-
"github.com/go-playground/validator/v10"
97
mvmv1 "github.com/weaveworks-liquidmetal/flintlock/api/services/microvm/v1alpha1"
108
"github.com/weaveworks-liquidmetal/flintlock/api/types"
119
"github.com/weaveworks-liquidmetal/flintlock/core/models"
1210
"github.com/weaveworks-liquidmetal/flintlock/core/ports"
1311
"github.com/weaveworks-liquidmetal/flintlock/pkg/log"
14-
"github.com/weaveworks-liquidmetal/flintlock/pkg/validation"
1512
"google.golang.org/grpc/codes"
1613
"google.golang.org/grpc/status"
1714
"google.golang.org/protobuf/types/known/emptypb"
@@ -22,14 +19,12 @@ func NewServer(commandUC ports.MicroVMCommandUseCases, queryUC ports.MicroVMQuer
2219
return &server{
2320
commandUC: commandUC,
2421
queryUC: queryUC,
25-
validator: validation.NewValidator(),
2622
}
2723
}
2824

2925
type server struct {
3026
commandUC ports.MicroVMCommandUseCases
3127
queryUC ports.MicroVMQueryUseCases
32-
validator validation.Validator
3328
}
3429

3530
func (s *server) CreateMicroVM(
@@ -51,22 +46,6 @@ func (s *server) CreateMicroVM(
5146
return nil, fmt.Errorf("converting request: %w", err)
5247
}
5348

54-
logger.Trace("validating model")
55-
56-
var valErrors validator.ValidationErrors
57-
58-
if err = s.validator.ValidateStruct(modelSpec); err != nil {
59-
if errors.As(err, &valErrors) {
60-
return nil, status.Errorf(
61-
codes.InvalidArgument,
62-
"an error occurred when attempting to validate the request: %v",
63-
err,
64-
)
65-
}
66-
67-
return nil, status.Errorf(codes.Internal, "an error occurred: %v", err)
68-
}
69-
7049
logger.Infof("creating microvm %s", modelSpec.ID)
7150

7251
createdModel, err := s.commandUC.CreateMicroVM(ctx, modelSpec)

0 commit comments

Comments
 (0)