Skip to content

Commit

Permalink
Ability to query older versions of a vmspec
Browse files Browse the repository at this point in the history
I tried to come up with a proper solution to this, but all of them had
flaws.

== Options I found and explored their possibilities

1. Add a new version argument to the Get function.

That's just ugly as hell. I like the Options pattern. Much easier to
update without breaking 300 calls in the codebase.

2. Add a new function on the Repo.

It seems odd and I think it's unnecessary because Get can handle it.

3. Create a function only on the Containerd repo implementation.

That would require checks and casting everywhere we want to use it.

== Conclusion

I picked options 1, because it causes less pain not and long term.
It does not matter what content store we are using, it HAS to be able
to manage versions somehow, even if it's an external service, the
Repository implementation has to handle versions, without versions
we are are playing with a bag of venomous snakes without any kind of
antidote, maybe fun, but not safe.

Related to liquidmetal-dev#66
  • Loading branch information
yitsushi committed Oct 29, 2021
1 parent 23bf654 commit ccc98a4
Show file tree
Hide file tree
Showing 12 changed files with 396 additions and 310 deletions.
259 changes: 130 additions & 129 deletions api/services/microvm/v1alpha1/microvms.pb.go

Large diffs are not rendered by default.

229 changes: 115 additions & 114 deletions api/types/microvm.pb.go

Large diffs are not rendered by default.

41 changes: 25 additions & 16 deletions core/application/app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,11 @@ func TestApp_CreateMicroVM(t *testing.T) {

rm.Get(
gomock.AssignableToTypeOf(context.Background()),
gomock.Eq("id1234"),
gomock.Eq(defaults.MicroVMNamespace),
).Return(
nil,
nil,
)
gomock.Eq(ports.RepositoryGetOptions{
Name: "id1234",
Namespace: defaults.MicroVMNamespace,
}),
).Return(nil, nil)

expectedCreatedSpec := createTestSpec("id1234", defaults.MicroVMNamespace)
expectedCreatedSpec.Spec.CreatedAt = frozenTime().Unix()
Expand Down Expand Up @@ -77,8 +76,10 @@ func TestApp_CreateMicroVM(t *testing.T) {
expect: func(rm *mock.MockMicroVMRepositoryMockRecorder, em *mock.MockEventServiceMockRecorder, im *mock.MockIDServiceMockRecorder, pm *mock.MockMicroVMServiceMockRecorder) {
rm.Get(
gomock.AssignableToTypeOf(context.Background()),
gomock.Eq("id1234"),
gomock.Eq("default"),
gomock.Eq(ports.RepositoryGetOptions{
Name: "id1234",
Namespace: "default",
}),
).Return(
nil,
nil,
Expand Down Expand Up @@ -113,8 +114,10 @@ func TestApp_CreateMicroVM(t *testing.T) {
expect: func(rm *mock.MockMicroVMRepositoryMockRecorder, em *mock.MockEventServiceMockRecorder, im *mock.MockIDServiceMockRecorder, pm *mock.MockMicroVMServiceMockRecorder) {
rm.Get(
gomock.AssignableToTypeOf(context.Background()),
gomock.Eq("id1234"),
gomock.Eq("default"),
gomock.Eq(ports.RepositoryGetOptions{
Name: "id1234",
Namespace: "default",
}),
).Return(
createTestSpec("id1234", "default"),
nil,
Expand Down Expand Up @@ -192,8 +195,10 @@ func TestApp_UpdateMicroVM(t *testing.T) {
expect: func(rm *mock.MockMicroVMRepositoryMockRecorder, em *mock.MockEventServiceMockRecorder, im *mock.MockIDServiceMockRecorder, pm *mock.MockMicroVMServiceMockRecorder) {
rm.Get(
gomock.AssignableToTypeOf(context.Background()),
gomock.Eq("id1234"),
gomock.Eq("default"),
gomock.Eq(ports.RepositoryGetOptions{
Name: "id1234",
Namespace: "default",
}),
).Return(
createTestSpec("id1234", "default"),
nil,
Expand Down Expand Up @@ -288,8 +293,10 @@ func TestApp_DeleteMicroVM(t *testing.T) {
expect: func(rm *mock.MockMicroVMRepositoryMockRecorder, em *mock.MockEventServiceMockRecorder, im *mock.MockIDServiceMockRecorder, pm *mock.MockMicroVMServiceMockRecorder) {
rm.Get(
gomock.AssignableToTypeOf(context.Background()),
gomock.Eq("id1234"),
gomock.Eq("default"),
gomock.Eq(ports.RepositoryGetOptions{
Name: "id1234",
Namespace: "default",
}),
).Return(
createTestSpec("id1234", "default"),
nil,
Expand Down Expand Up @@ -324,8 +331,10 @@ func TestApp_DeleteMicroVM(t *testing.T) {
expect: func(rm *mock.MockMicroVMRepositoryMockRecorder, em *mock.MockEventServiceMockRecorder, im *mock.MockIDServiceMockRecorder, pm *mock.MockMicroVMServiceMockRecorder) {
rm.Get(
gomock.AssignableToTypeOf(context.Background()),
gomock.Eq("id1234"),
gomock.Eq("default"),
gomock.Eq(ports.RepositoryGetOptions{
Name: "id1234",
Namespace: "default",
}),
).Return(
nil,
nil,
Expand Down
19 changes: 16 additions & 3 deletions core/application/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/weaveworks/flintlock/api/events"
coreerrs "github.com/weaveworks/flintlock/core/errors"
"github.com/weaveworks/flintlock/core/models"
"github.com/weaveworks/flintlock/core/ports"
"github.com/weaveworks/flintlock/pkg/defaults"
"github.com/weaveworks/flintlock/pkg/log"
)
Expand All @@ -31,12 +32,16 @@ func (a *app) CreateMicroVM(ctx context.Context, mvm *models.MicroVM) (*models.M
mvm.ID = *vmid
}

foundMvm, err := a.ports.Repo.Get(ctx, mvm.ID.Name(), mvm.ID.Namespace())
foundMvm, err := a.ports.Repo.Get(ctx, ports.RepositoryGetOptions{
Name: mvm.ID.Name(),
Namespace: mvm.ID.Namespace(),
})
if err != nil {
if !coreerrs.IsSpecNotFound(err) {
return nil, fmt.Errorf("checking to see if spec exists: %w", err)
}
}

if foundMvm != nil {
return nil, specAlreadyExistsError{
name: mvm.ID.Name(),
Expand Down Expand Up @@ -76,10 +81,14 @@ func (a *app) UpdateMicroVM(ctx context.Context, mvm *models.MicroVM) (*models.M
return nil, coreerrs.ErrVMIDRequired
}

foundMvm, err := a.ports.Repo.Get(ctx, mvm.ID.Name(), mvm.ID.Namespace())
foundMvm, err := a.ports.Repo.Get(ctx, ports.RepositoryGetOptions{
Name: mvm.ID.Name(),
Namespace: mvm.ID.Namespace(),
})
if err != nil {
return nil, fmt.Errorf("checking to see if spec exists: %w", err)
}

if foundMvm == nil {
return nil, specNotFoundError{
name: mvm.ID.Name(),
Expand Down Expand Up @@ -116,10 +125,14 @@ func (a *app) DeleteMicroVM(ctx context.Context, id, namespace string) error {
return errIDRequired
}

foundMvm, err := a.ports.Repo.Get(ctx, id, namespace)
foundMvm, err := a.ports.Repo.Get(ctx, ports.RepositoryGetOptions{
Name: id,
Namespace: namespace,
})
if err != nil {
return fmt.Errorf("checking to see if spec exists: %w", err)
}

if foundMvm == nil {
return specNotFoundError{
name: id,
Expand Down
6 changes: 5 additions & 1 deletion core/application/reconcile.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

"github.com/weaveworks/flintlock/core/models"
"github.com/weaveworks/flintlock/core/plans"
"github.com/weaveworks/flintlock/core/ports"
portsctx "github.com/weaveworks/flintlock/core/ports/context"
"github.com/weaveworks/flintlock/pkg/log"
"github.com/weaveworks/flintlock/pkg/planner"
Expand All @@ -17,7 +18,10 @@ func (a *app) ReconcileMicroVM(ctx context.Context, id, namespace string) error
logger := log.GetLogger(ctx).WithField("action", "reconcile")

logger.Debugf("Getting spec for %s/%s", namespace, id)
spec, err := a.ports.Repo.Get(ctx, id, namespace)
spec, err := a.ports.Repo.Get(ctx, ports.RepositoryGetOptions{
Name: id,
Namespace: namespace,
})
if err != nil {
return fmt.Errorf("getting microvm spec for reconcile: %w", err)
}
Expand Down
10 changes: 8 additions & 2 deletions core/errors/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,21 +89,27 @@ func (e NetworkInterfaceStatusMissingError) Error() string {
return fmt.Sprintf("status for network interface %s is not found", e.guestIface)
}

func NewSpecNotFound(name, namespace string) error {
func NewSpecNotFound(name, namespace, version string) error {
return specNotFoundError{
name: name,
namespace: namespace,
version: version,
}
}

type specNotFoundError struct {
name string
namespace string
version string
}

// Error returns the error message.
func (e specNotFoundError) Error() string {
return fmt.Sprintf("microvm spec %s/%s not found", e.namespace, e.name)
if e.version == "" {
return fmt.Sprintf("microvm spec %s/%s not found", e.namespace, e.name)
}

return fmt.Sprintf("microvm spec %s/%s not found with version %s", e.namespace, e.name, e.version)
}

// IsSpecNotFound tests an error to see if its a spec not found error.
Expand Down
8 changes: 7 additions & 1 deletion core/ports/repositories.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,20 @@ import (
"github.com/weaveworks/flintlock/core/models"
)

type RepositoryGetOptions struct {
Name string
Namespace string
Version string
}

// MicroVMRepository is the port definition for a microvm repository.
type MicroVMRepository interface {
// Save will save the supplied microvm spec.
Save(ctx context.Context, microvm *models.MicroVM) (*models.MicroVM, error)
// Delete will delete the supplied microvm.
Delete(ctx context.Context, microvm *models.MicroVM) error
// Get will get the microvm spec with the given name/namespace.
Get(ctx context.Context, name, namespace string) (*models.MicroVM, error)
Get(ctx context.Context, options RepositoryGetOptions) (*models.MicroVM, error)
// GetAll will get a list of microvm details. If namespace is an empty string all
// details of microvms will be returned.
GetAll(ctx context.Context, namespace string) ([]*models.MicroVM, error)
Expand Down
4 changes: 4 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ require (
github.com/golang/protobuf v1.5.2 // indirect
github.com/golang/snappy v0.0.3 // indirect
github.com/google/gofuzz v1.1.0 // indirect
github.com/google/subcommands v1.0.1 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/googleapis/gnostic v0.4.1 // indirect
github.com/gruntwork-io/go-commons v0.8.0 // indirect
Expand Down Expand Up @@ -130,13 +131,16 @@ require (
go.mongodb.org/mongo-driver v1.3.4 // indirect
go.opencensus.io v0.23.0 // indirect
golang.org/x/crypto v0.0.0-20210817164053-32db794688a5 // indirect
golang.org/x/mod v0.4.2 // indirect
golang.org/x/net v0.0.0-20210614182718-04defd469f4e // indirect
golang.org/x/oauth2 v0.0.0-20210819190943-2bc19b11175f // indirect
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c // indirect
golang.org/x/sys v0.0.0-20210823070655-63515b42dcdf // indirect
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1 // indirect
golang.org/x/text v0.3.6 // indirect
golang.org/x/time v0.0.0-20200630173020-3af7569d3a1e // indirect
golang.org/x/tools v0.1.5 // indirect
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/genproto v0.0.0-20211021150943-2b146023228c // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
Expand Down
3 changes: 3 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -572,6 +572,7 @@ github.com/google/pprof v0.0.0-20210601050228-01bbb1931b22/go.mod h1:kpwsk12EmLe
github.com/google/pprof v0.0.0-20210609004039-a478d1d731e9/go.mod h1:kpwsk12EmLew5upagYY7GY0pfYCcupk39gWOCRROcvE=
github.com/google/pprof v0.0.0-20210720184732-4bb14d4b1be1/go.mod h1:kpwsk12EmLew5upagYY7GY0pfYCcupk39gWOCRROcvE=
github.com/google/renameio v0.1.0/go.mod h1:KWCgfxg9yswjAJkECMjeO8J8rahYeXnNhOm40UhjYkI=
github.com/google/subcommands v1.0.1 h1:/eqq+otEXm5vhfBrbREPCSVQbvofip6kIz+mX5TUH7k=
github.com/google/subcommands v1.0.1/go.mod h1:ZjhPrFU+Olkh9WazFPsl27BQ4UPiG37m3yTrtFlrHVk=
github.com/google/uuid v1.0.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/google/uuid v1.1.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
Expand Down Expand Up @@ -1119,6 +1120,7 @@ golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.4.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.4.1/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.4.2 h1:Gz96sIWK3OalVv/I/qNygP42zyoKp3xptRVCWRFEBvo=
golang.org/x/mod v0.4.2/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20180811021610-c39426892332/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
Expand Down Expand Up @@ -1394,6 +1396,7 @@ golang.org/x/tools v0.1.1/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk=
golang.org/x/tools v0.1.2/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk=
golang.org/x/tools v0.1.3/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk=
golang.org/x/tools v0.1.4/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk=
golang.org/x/tools v0.1.5 h1:ouewzE6p+/VEB31YYnTbEJdi8pFqKp4P4n85vwo3DHA=
golang.org/x/tools v0.1.5/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
Expand Down
Loading

0 comments on commit ccc98a4

Please sign in to comment.