Skip to content

Commit

Permalink
Ability to query older versions of a vmspec (#184)
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 #66
  • Loading branch information
yitsushi authored Nov 3, 2021
1 parent 3e85f2c commit 7f6db21
Show file tree
Hide file tree
Showing 11 changed files with 165 additions and 74 deletions.
59 changes: 37 additions & 22 deletions core/application/app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,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 @@ -78,8 +77,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 @@ -114,8 +115,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 @@ -193,8 +196,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 @@ -289,8 +294,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 @@ -325,8 +332,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 Expand Up @@ -409,8 +418,10 @@ func TestApp_GetMicroVM(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: defaults.MicroVMNamespace,
}),
).Return(
nil,
nil,
Expand All @@ -425,8 +436,10 @@ func TestApp_GetMicroVM(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: defaults.MicroVMNamespace,
}),
).Return(
nil,
errors.New("an random error occurred"),
Expand All @@ -441,8 +454,10 @@ func TestApp_GetMicroVM(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: defaults.MicroVMNamespace,
}),
).Return(
createTestSpec("id1234", "default"),
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
3 changes: 2 additions & 1 deletion core/application/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"

"github.com/weaveworks/flintlock/core/models"
"github.com/weaveworks/flintlock/core/ports"
"github.com/weaveworks/flintlock/pkg/log"
)

Expand All @@ -20,7 +21,7 @@ func (a *app) GetMicroVM(ctx context.Context, id, namespace string) (*models.Mic
return nil, errNamespaceRequired
}

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 nil, fmt.Errorf("error attempting to locate microvm with id: %s, in namespace: %s: %w", id, namespace, err)
}
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 @@ -74,6 +74,7 @@ require (
github.com/golang/groupcache v0.0.0-20200121045136-8c9f03a8e57e // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/golang/snappy v0.0.3 // indirect
github.com/google/subcommands v1.0.1 // indirect
github.com/hashicorp/errwrap v1.0.0 // indirect
github.com/hashicorp/go-multierror v1.1.0 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
Expand Down Expand Up @@ -102,9 +103,12 @@ 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/sync v0.0.0-20210220032951-036812b2e83c // indirect
golang.org/x/sys v0.0.0-20210823070655-63515b42dcdf // indirect
golang.org/x/text v0.3.6 // indirect
golang.org/x/tools v0.1.5 // indirect
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect
google.golang.org/genproto v0.0.0-20211021150943-2b146023228c // indirect
gopkg.in/ini.v1 v1.63.2 // indirect
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 // indirect
Expand Down
3 changes: 3 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -522,6 +522,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 @@ -1011,6 +1012,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-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
Expand Down Expand Up @@ -1279,6 +1281,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 7f6db21

Please sign in to comment.