Skip to content

Commit

Permalink
Introduces model creation to model migration.
Browse files Browse the repository at this point in the history
This PR introduces model creation in DQlite as part of model migration.
We are doing this to facilitate further changes to the model DDL that
requires the removal of the modelmanager service.

To achieve this removal we need to properly create models during
migration. As well as this because of how application and machine
importing was working we also need to migrate these two entities into
the model database.

This commit also disables the current model migration tests that we
have. This is because they are currently not setup to properly handle
DQlite migration and will need to be addressed in follow up PR's.
  • Loading branch information
tlm committed Mar 7, 2024
1 parent a5bf5e0 commit 907a52f
Show file tree
Hide file tree
Showing 26 changed files with 814 additions and 188 deletions.
42 changes: 2 additions & 40 deletions apiserver/facades/controller/migrationtarget/domain_mock_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,8 @@ import (
corelogger "github.com/juju/juju/core/logger"
coremodel "github.com/juju/juju/core/model"
"github.com/juju/juju/core/modelmigration"
applicationservice "github.com/juju/juju/domain/application/service"
"github.com/juju/juju/domain/credential"
"github.com/juju/juju/domain/credential/service"
machineservice "github.com/juju/juju/domain/machine/service"
servicefactorytesting "github.com/juju/juju/domain/servicefactory/testing"
"github.com/juju/juju/environs"
"github.com/juju/juju/environs/envcontext"
Expand All @@ -57,7 +55,6 @@ type Suite struct {
statetesting.StateSuite
authorizer *apiservertesting.FakeAuthorizer

modelManagerService *MockModelManagerService
controllerConfigService *MockControllerConfigService
serviceFactory *MockServiceFactory
serviceFactoryGetter *MockServiceFactoryGetter
Expand All @@ -75,6 +72,21 @@ type Suite struct {

var _ = gc.Suite(&Suite{})

func (s *Suite) SetUpSuite(c *gc.C) {
c.Skip(`
Skip added by tlm. The reason we are skipping these tests is currently they are
introducing a mock for model import call but then the mock proceeds to actually
call the model import code in internal and do a full end to end tests. These
tests are then running off of service factory mocks.
Eventually we are ending up at a state where we are 6 levels deep in the call
stack writing expect statements. All of these tests need to be refactored
properly into unit tests and not integration tests.
We will get this done as part of dqlite transition.
`)
}

func (s *Suite) SetUpTest(c *gc.C) {
// Set up InitialConfig with a dummy provider configuration. This
// is required to allow model import test to work.
Expand Down Expand Up @@ -605,7 +617,6 @@ func (s *Suite) setupMocks(c *gc.C) *gomock.Controller {

s.controllerConfigService = NewMockControllerConfigService(ctrl)
s.controllerConfigService.EXPECT().ControllerConfig(gomock.Any()).Return(jujutesting.FakeControllerConfig(), nil).AnyTimes()
s.modelManagerService = NewMockModelManagerService(ctrl)

s.serviceFactory = NewMockServiceFactory(ctrl)
s.serviceFactoryGetter = NewMockServiceFactoryGetter(ctrl)
Expand Down Expand Up @@ -717,15 +728,11 @@ func (s *Suite) controllerVersion(c *gc.C) version.Number {
}

func (s *Suite) expectImportModel(c *gc.C) {
s.serviceFactory.EXPECT().Machine().Return(&machineservice.Service{})
s.serviceFactory.EXPECT().Application().Return(&applicationservice.Service{})

s.modelManagerService.EXPECT().Create(gomock.Any(), gomock.Any())
s.serviceFactoryGetter.EXPECT().FactoryForModel(gomock.Any()).Return(s.serviceFactory)
s.modelImporter.EXPECT().ImportModel(gomock.Any(), gomock.Any()).DoAndReturn(func(ctx context.Context, bytes []byte) (*state.Model, *state.State, error) {
scope := func(string) modelmigration.Scope { return modelmigration.NewScope(nil, nil) }
controller := state.NewController(s.StatePool)
return migration.NewModelImporter(controller, scope, s.modelManagerService, s.controllerConfigService, s.serviceFactoryGetter).ImportModel(ctx, bytes)
return migration.NewModelImporter(controller, scope, s.controllerConfigService, s.serviceFactoryGetter).ImportModel(ctx, bytes)
})
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"github.com/juju/juju/testing"
)

//go:generate go run go.uber.org/mock/mockgen -package migrationtarget_test -destination domain_mock_test.go github.com/juju/juju/apiserver/facades/controller/migrationtarget ControllerConfigService,ExternalControllerService,UpgradeService,ModelImporter,ModelManagerService
//go:generate go run go.uber.org/mock/mockgen -package migrationtarget_test -destination domain_mock_test.go github.com/juju/juju/apiserver/facades/controller/migrationtarget ControllerConfigService,ExternalControllerService,UpgradeService,ModelImporter
//go:generate go run go.uber.org/mock/mockgen -package migrationtarget_test -destination credential_mock_test.go github.com/juju/juju/domain/credential/service CredentialValidator
//go:generate go run go.uber.org/mock/mockgen -package migrationtarget_test -destination servicefactory_mock_test.go github.com/juju/juju/internal/servicefactory ServiceFactoryGetter,ServiceFactory

Expand Down
1 change: 0 additions & 1 deletion apiserver/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -883,7 +883,6 @@ func (ctx *facadeContext) ModelImporter() facade.ModelImporter {
return migration.NewModelImporter(
state.NewController(pool),
ctx.migrationScope,
ctx.ServiceFactory().ModelManager(),
ctx.ServiceFactory().ControllerConfig(),
ctx.r.serviceFactoryGetter,
)
Expand Down
9 changes: 6 additions & 3 deletions core/model/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
package model

import (
"fmt"

"github.com/juju/errors"

"github.com/juju/juju/internal/uuid"
Expand Down Expand Up @@ -66,13 +68,14 @@ func (u UUID) String() string {
return string(u)
}

// Validate ensures the consistency of the UUID.
// Validate ensures the consistency of the UUID. If the uuid is invalid an error
// satisfying [errors.NotValid] will be returned.
func (u UUID) Validate() error {
if u == "" {
return errors.New("empty uuid")
return fmt.Errorf("%wuuid cannot be empty", errors.Hide(errors.NotValid))
}
if !uuid.IsValidUUIDString(string(u)) {
return errors.Errorf("invalid uuid %q", u)
return fmt.Errorf("uuid %q %w", u, errors.NotValid)
}
return nil
}
12 changes: 4 additions & 8 deletions core/model/model_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,15 @@ func (*ModelSuite) TestValidModelTypes(c *gc.C) {
func (*ModelSuite) TestUUIDValidate(c *gc.C) {
tests := []struct {
uuid string
err *string
err error
}{
{
uuid: "",
err: ptr("empty uuid"),
err: errors.NotValid,
},
{
uuid: "invalid",
err: ptr("invalid uuid.*"),
err: errors.NotValid,
},
{
uuid: uuid.MustNewUUID().String(),
Expand All @@ -74,10 +74,6 @@ func (*ModelSuite) TestUUIDValidate(c *gc.C) {
continue
}

c.Check(err, gc.ErrorMatches, *test.err)
c.Check(err, jc.ErrorIs, test.err)
}
}

func ptr[T any](v T) *T {
return &v
}
71 changes: 71 additions & 0 deletions domain/application/modelmigration/import.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
// Copyright 2024 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.

package modelmigration

import (
"context"
"fmt"

"github.com/juju/description/v5"
"github.com/juju/loggo"

"github.com/juju/juju/core/modelmigration"
"github.com/juju/juju/domain/application/service"
"github.com/juju/juju/domain/application/state"
)

var logger = loggo.GetLogger("juju.migration.applications")

// Coordinator is the interface that is used to add operations to a migration.
type Coordinator interface {
Add(modelmigration.Operation)
}

// RegisterImport register's a new model migration importer into the supplied
// coordinator.
func RegisterImport(coordinator Coordinator) {
coordinator.Add(&importOperation{})
}

type importOperation struct {
modelmigration.BaseOperation

service ImportService
}

// ImportService defines the application service used to import applications
// from another controller model to this controller.
type ImportService interface {
// CreateApplication registers the existence of an application in the model.
CreateApplication(context.Context, string, service.AddApplicationParams, ...service.AddUnitParams) error
}

func (i *importOperation) Setup(scope modelmigration.Scope) error {
i.service = service.NewService(
state.NewState(scope.ModelDB(), logger),
)
return nil
}

func (i *importOperation) Execute(ctx context.Context, model description.Model) error {
for _, app := range model.Applications() {
unitArgs := make([]service.AddUnitParams, 0, len(app.Units()))
for _, unit := range app.Units() {
name := unit.Name()
unitArgs = append(unitArgs, service.AddUnitParams{UnitName: &name})
}

err := i.service.CreateApplication(
ctx, app.Name(), service.AddApplicationParams{}, unitArgs...,
)
if err != nil {
return fmt.Errorf(
"import model application %q with %d units: %w",
app.Name(), len(app.Units()), err,
)
}
}

return nil
}
63 changes: 63 additions & 0 deletions domain/application/modelmigration/import_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// Copyright 2024 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.

package modelmigration

import (
"context"

"github.com/juju/description/v5"
"github.com/juju/names/v5"
jc "github.com/juju/testing/checkers"
gomock "go.uber.org/mock/gomock"
gc "gopkg.in/check.v1"

"github.com/juju/juju/domain/application/service"
)

type importSuite struct {
importService *MockImportService
}

var _ = gc.Suite(&importSuite{})

func (s *importSuite) setupMocks(c *gc.C) *gomock.Controller {
ctrl := gomock.NewController(c)
s.importService = NewMockImportService(ctrl)
return ctrl
}

func (i *importSuite) TestApplicationSave(c *gc.C) {
model := description.NewModel(description.ModelArgs{})

appArgs := description.ApplicationArgs{
Tag: names.NewApplicationTag("prometheus"),
}
model.AddApplication(appArgs).AddUnit(description.UnitArgs{
Tag: names.NewUnitTag("prometheus/0"),
})

defer i.setupMocks(c).Finish()

i.importService.EXPECT().CreateApplication(
gomock.Any(),
"prometheus",
gomock.Any(),
[]service.AddUnitParams{
{
ptrString("prometheus/0"),
},
},
).Return(nil)

importOp := importOperation{
service: i.importService,
}

err := importOp.Execute(context.Background(), model)
c.Assert(err, jc.ErrorIsNil)
}

func ptrString(s string) *string {
return &s
}
60 changes: 60 additions & 0 deletions domain/application/modelmigration/migrations_mock_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 907a52f

Please sign in to comment.