Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: move validation/defaults to app #514

Merged
merged 2 commits into from
Aug 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions core/application/app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -629,12 +629,13 @@ func createTestSpecWithMetadata(name, ns, uid string, metadata map[string]string
{
AllowMetadataRequests: true,
GuestMAC: "AA:FF:00:00:00:01",
GuestDeviceName: "eth0",
GuestDeviceName: "mmds",
Type: models.IfaceTypeTap,
},
{
AllowMetadataRequests: false,
GuestDeviceName: "eth1",
// TODO:
Type: models.IfaceTypeMacvtap,
},
},
RootVolume: models.Volume{
Expand Down
35 changes: 34 additions & 1 deletion core/application/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,28 @@ import (
"github.com/weaveworks-liquidmetal/flintlock/core/ports"
"github.com/weaveworks-liquidmetal/flintlock/pkg/defaults"
"github.com/weaveworks-liquidmetal/flintlock/pkg/log"
"github.com/weaveworks-liquidmetal/flintlock/pkg/validation"
"sigs.k8s.io/yaml"
)

const (
MetadataInterfaceName = "mmds"
)

func (a *app) CreateMicroVM(ctx context.Context, mvm *models.MicroVM) (*models.MicroVM, error) {
logger := log.GetLogger(ctx).WithField("component", "app")
logger.Trace("creating microvm")
logger.Debug("creating microvm")

if mvm == nil {
return nil, coreerrs.ErrSpecRequired
}

logger.Trace("validating model")
validator := validation.NewValidator()
if validErr := validator.ValidateStruct(mvm); validErr != nil {
return nil, fmt.Errorf("an error occurred when attempting to validate microvm spec: %w", validErr)
}

if mvm.ID.IsEmpty() {
name, err := a.ports.IdentifierService.GenerateRandom()
if err != nil {
Expand Down Expand Up @@ -70,6 +81,7 @@ func (a *app) CreateMicroVM(ctx context.Context, mvm *models.MicroVM) (*models.M
if err != nil {
return nil, fmt.Errorf("adding instance data: %w", err)
}
a.addMetadataInterface(mvm)

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

return nil
}

func (a *app) addMetadataInterface(mvm *models.MicroVM) {
for i := range mvm.Spec.NetworkInterfaces {
netInt := mvm.Spec.NetworkInterfaces[i]
if netInt.GuestDeviceName == MetadataInterfaceName {
return
}
}

mvm.Spec.NetworkInterfaces = append(mvm.Spec.NetworkInterfaces, models.NetworkInterface{
GuestDeviceName: MetadataInterfaceName,
Type: models.IfaceTypeTap,
AllowMetadataRequests: true,
GuestMAC: "AA:FF:00:00:00:01",
StaticAddress: &models.StaticAddress{
Address: "169.254.0.1/16",
},
})

return
Copy link
Member

@Callisto13 Callisto13 Aug 22, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this last return is likely redundant

}
4 changes: 0 additions & 4 deletions infrastructure/grpc/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,6 @@ func convertMicroVMToModel(spec *types.MicroVMSpec) (*models.MicroVM, error) {
convertedModel.Spec.NetworkInterfaces = append(convertedModel.Spec.NetworkInterfaces, *convertedNetInt)
}

ifaces := []models.NetworkInterface{*newMetadataInterface()}
ifaces = append(ifaces, convertedModel.Spec.NetworkInterfaces...)
convertedModel.Spec.NetworkInterfaces = ifaces

convertedModel.Spec.Metadata = map[string]string{}
for metadataKey, metadataValue := range spec.Metadata {
convertedModel.Spec.Metadata[metadataKey] = metadataValue
Expand Down
17 changes: 0 additions & 17 deletions infrastructure/grpc/defaults.go

This file was deleted.

21 changes: 0 additions & 21 deletions infrastructure/grpc/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,13 @@ package grpc

import (
"context"
"errors"
"fmt"

"github.com/go-playground/validator/v10"
mvmv1 "github.com/weaveworks-liquidmetal/flintlock/api/services/microvm/v1alpha1"
"github.com/weaveworks-liquidmetal/flintlock/api/types"
"github.com/weaveworks-liquidmetal/flintlock/core/models"
"github.com/weaveworks-liquidmetal/flintlock/core/ports"
"github.com/weaveworks-liquidmetal/flintlock/pkg/log"
"github.com/weaveworks-liquidmetal/flintlock/pkg/validation"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"google.golang.org/protobuf/types/known/emptypb"
Expand All @@ -22,14 +19,12 @@ func NewServer(commandUC ports.MicroVMCommandUseCases, queryUC ports.MicroVMQuer
return &server{
commandUC: commandUC,
queryUC: queryUC,
validator: validation.NewValidator(),
}
}

type server struct {
commandUC ports.MicroVMCommandUseCases
queryUC ports.MicroVMQueryUseCases
validator validation.Validator
}

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

logger.Trace("validating model")

var valErrors validator.ValidationErrors

if err = s.validator.ValidateStruct(modelSpec); err != nil {
if errors.As(err, &valErrors) {
return nil, status.Errorf(
codes.InvalidArgument,
"an error occurred when attempting to validate the request: %v",
err,
)
}

return nil, status.Errorf(codes.Internal, "an error occurred: %v", err)
}

logger.Infof("creating microvm %s", modelSpec.ID)

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