Skip to content

Commit

Permalink
Merge pull request juju#12953 from wallyworld/provider-context-refactor
Browse files Browse the repository at this point in the history
juju#12953

We use a `environs/context.ProviderCallContext` to pass to API calls in the Environ interface. This context instance provides a callback which can be used to report invalid credentials.

This PR embeds a `context.Context` in the above context. This is immediately useful for the Azure provider where the Azure SDK takes a `context.Context` and we were having to create a separate one of those ourselves. Now, we can just use a single context instance.

Most of the churn here is mechanical - a common helper method used by tests was renamed. 
NewCloudCallContext -> NewEmptyCloudCallContext

Also, the workers which use a context got some refactoring to create the context on demand for each API call instead of creating one up front and using that same instance each time.

A followup PR will use this work to provide a context with deadline to the environ.Destroy() call to allow model destroy operations to timeout cleanly.

## QA steps

This is just a refactor. QA can be to bootstrap on a few substrates (including Azure) and deploy a few charms.
  • Loading branch information
jujubot authored May 7, 2021
2 parents ad431ba + fddd976 commit 4f057ab
Show file tree
Hide file tree
Showing 122 changed files with 608 additions and 450 deletions.
4 changes: 2 additions & 2 deletions apiserver/common/credentialcommon/modelcredential_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func (s *CheckMachinesSuite) SetUpTest(c *gc.C) {
return []instances.Instance{s.instance}, nil
},
}
s.callContext = context.NewCloudCallContext()
s.callContext = context.NewEmptyCloudCallContext()
}

func (s *CheckMachinesSuite) TestCheckMachinesSuccess(c *gc.C) {
Expand Down Expand Up @@ -231,7 +231,7 @@ type ModelCredentialSuite struct {
func (s *ModelCredentialSuite) SetUpTest(c *gc.C) {
s.IsolationSuite.SetUpTest(c)
s.backend = createModelBackend()
s.callContext = context.NewCloudCallContext()
s.callContext = context.NewEmptyCloudCallContext()
}

func (s *ModelCredentialSuite) TestValidateNewModelCredentialUnknownModelType(c *gc.C) {
Expand Down
2 changes: 1 addition & 1 deletion apiserver/facades/agent/uniter/uniter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ func (s *uniterSuiteBase) setupCAASModel(c *gc.C) (*apiuniter.State, *state.CAAS
c.Assert(err, jc.ErrorIsNil)

apiInfo, err := environs.APIInfo(
context.NewCloudCallContext(),
context.NewEmptyCloudCallContext(),
s.ControllerConfig.ControllerUUID(),
st.ModelUUID(),
coretesting.CACert,
Expand Down
6 changes: 3 additions & 3 deletions apiserver/facades/client/applicationoffers/mock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
package applicationoffers_test

import (
stdcontet "context"
stdcontext "context"
"fmt"
"strings"
"time"
Expand Down Expand Up @@ -386,7 +386,7 @@ func (m *mockState) OfferConnections(offerUUID string) ([]applicationoffers.Offe
}

func (m *mockState) GetModelCallContext() context.ProviderCallContext {
return context.NewCloudCallContext()
return context.NewEmptyCloudCallContext()
}

func (m *mockState) User(tag names.UserTag) (applicationoffers.User, error) {
Expand Down Expand Up @@ -525,7 +525,7 @@ type mockBakeryService struct {
caveats map[string][]checkers.Caveat
}

func (s *mockBakeryService) NewMacaroon(ctx stdcontet.Context, version bakery.Version, caveats []checkers.Caveat, ops ...bakery.Op) (*bakery.Macaroon, error) {
func (s *mockBakeryService) NewMacaroon(ctx stdcontext.Context, version bakery.Version, caveats []checkers.Caveat, ops ...bakery.Op) (*bakery.Macaroon, error) {
s.MethodCall(s, "NewMacaroon", caveats)
mac, err := macaroon.New(nil, []byte("id"), "", macaroon.LatestVersion)
if err != nil {
Expand Down
12 changes: 6 additions & 6 deletions apiserver/facades/client/client/statushistory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,12 @@ func (s *statusHistoryTestSuite) SetUpTest(c *gc.C) {
nil, // modelconfig API
nil, // resources
authorizer,
nil, // presence
nil, // statusSetter
nil, // toolsFinder
nil, // newEnviron
nil, // blockChecker
context.NewCloudCallContext(), // ProviderCallContext
nil, // presence
nil, // statusSetter
nil, // toolsFinder
nil, // newEnviron
nil, // blockChecker
context.NewEmptyCloudCallContext(), // ProviderCallContext
nil,
nil,
nil, // multiwatcher.Factory
Expand Down
2 changes: 1 addition & 1 deletion apiserver/facades/client/cloud/cloudV2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ func (s *cloudSuiteV2) SetUpTest(c *gc.C) {
}
s.statePool = &mockStatePool{
getF: func(modelUUID string) (credentialcommon.PersistentBackend, context.ProviderCallContext, error) {
return modelBackend(modelUUID), context.NewCloudCallContext(), nil
return modelBackend(modelUUID), context.NewEmptyCloudCallContext(), nil
},
}
}
Expand Down
2 changes: 1 addition & 1 deletion apiserver/facades/client/cloud/cloud_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ func (s *cloudSuite) SetUpTest(c *gc.C) {

s.statePool = &mockStatePool{
getF: func(modelUUID string) (credentialcommon.PersistentBackend, context.ProviderCallContext, error) {
return newModelBackend(c, aCloud, modelUUID), context.NewCloudCallContext(), nil
return newModelBackend(c, aCloud, modelUUID), context.NewEmptyCloudCallContext(), nil
},
}
s.setTestAPIForUser(c, names.NewUserTag("admin"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func (p *instanceTypesSuite) TestInstanceTypes(c *gc.C) {
}
pool := &mockStatePool{
getF: func(modelUUID string) (credentialcommon.PersistentBackend, context.ProviderCallContext, error) {
return newModelBackend(c, aCloud, modelUUID), context.NewCloudCallContext(), nil
return newModelBackend(c, aCloud, modelUUID), context.NewEmptyCloudCallContext(), nil
},
}
api, err := cloud.NewCloudAPI(backend, ctlrBackend, pool, authorizer)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func (p *instanceTypesSuite) TestInstanceTypes(c *gc.C) {
Authorizer: authorizer,
ModelTag: backend.ModelTag(),
},
context.NewCloudCallContext(),
context.NewEmptyCloudCallContext(),
common.NewResources(),
leadership,
nil,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ func (s *MachineManagerSuite) SetUpTest(c *gc.C) {
}
s.pool = &mockPool{}
s.authorizer = &apiservertesting.FakeAuthorizer{Tag: names.NewUserTag("admin")}
s.callContext = context.NewCloudCallContext()
s.callContext = context.NewEmptyCloudCallContext()
}

func (s *MachineManagerSuite) setup(c *gc.C) *gomock.Controller {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func (s *ListModelsWithInfoSuite) SetUpTest(c *gc.C) {
Tag: s.adminUser,
}

s.callContext = context.NewCloudCallContext()
s.callContext = context.NewEmptyCloudCallContext()
api, err := modelmanager.NewModelManagerAPI(s.st, &mockState{}, nil, nil, nil, s.authoriser, s.st.model, s.callContext)
c.Assert(err, jc.ErrorIsNil)
s.api = api
Expand Down
2 changes: 1 addition & 1 deletion apiserver/facades/client/modelmanager/modelinfo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ func (s *modelInfoSuite) SetUpTest(c *gc.C) {
},
}

s.callContext = context.NewCloudCallContext()
s.callContext = context.NewEmptyCloudCallContext()

var err error
s.modelmanager, err = modelmanager.NewModelManagerAPI(s.st, s.ctlrSt, nil, nil, nil, &s.authorizer, s.st.model, s.callContext)
Expand Down
4 changes: 2 additions & 2 deletions apiserver/facades/client/modelmanager/modelmanager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ func (s *modelManagerSuite) SetUpTest(c *gc.C) {
Tag: names.NewUserTag("admin"),
}

s.callContext = context.NewCloudCallContext()
s.callContext = context.NewEmptyCloudCallContext()

newBroker := func(args environs.OpenParams) (caas.Broker, error) {
s.caasBroker = &mockCaasBroker{namespace: args.Config.Name()}
Expand Down Expand Up @@ -922,7 +922,7 @@ func (s *modelManagerStateSuite) SetUpTest(c *gc.C) {
s.authoriser = apiservertesting.FakeAuthorizer{
Tag: s.AdminUserTag(c),
}
s.callContext = context.NewCloudCallContext()
s.callContext = context.NewEmptyCloudCallContext()
loggo.GetLogger("juju.apiserver.modelmanager").SetLogLevel(loggo.TRACE)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func (s *ValidateModelUpgradesSuite) SetUpTest(c *gc.C) {
Tag: s.adminUser,
}

s.callContext = context.NewCloudCallContext()
s.callContext = context.NewEmptyCloudCallContext()
}

// TestValidateModelUpgradesWithNoModelTags tests that we don't fail if we don't
Expand Down
2 changes: 1 addition & 1 deletion apiserver/facades/client/spaces/package_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func (s *APISuite) SetupMocks(c *gc.C, supportSpaces bool, providerSpaces bool)
ctrl := gomock.NewController(c)

s.resource = facademocks.NewMockResources(ctrl)
s.cloudCallContext = context.NewCloudCallContext()
s.cloudCallContext = context.NewEmptyCloudCallContext()
s.OpFactory = NewMockOpFactory(ctrl)
s.Constraints = NewMockConstraints(ctrl)

Expand Down
6 changes: 3 additions & 3 deletions apiserver/facades/client/spaces/reload_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func (s *ReloadSpacesAPISuite) TestReloadSpaces(c *gc.C) {
ctrl := gomock.NewController(c)
defer ctrl.Finish()

context := context.NewCloudCallContext()
context := context.NewEmptyCloudCallContext()
authorizer := func() error { return nil }

mockNetworkEnviron := environmocks.NewMockNetworkingEnviron(ctrl)
Expand All @@ -49,7 +49,7 @@ func (s *ReloadSpacesAPISuite) TestReloadSpacesWithNoEnviron(c *gc.C) {
ctrl := gomock.NewController(c)
defer ctrl.Finish()

context := context.NewCloudCallContext()
context := context.NewEmptyCloudCallContext()
authorizer := func() error { return nil }

mockNetworkEnviron := environmocks.NewMockNetworkingEnviron(ctrl)
Expand All @@ -70,7 +70,7 @@ func (s *ReloadSpacesAPISuite) TestReloadSpacesWithReloadSpaceError(c *gc.C) {
ctrl := gomock.NewController(c)
defer ctrl.Finish()

context := context.NewCloudCallContext()
context := context.NewEmptyCloudCallContext()
authorizer := func() error { return nil }

mockNetworkEnviron := environmocks.NewMockNetworkingEnviron(ctrl)
Expand Down
14 changes: 7 additions & 7 deletions apiserver/facades/client/spaces/spaces_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -667,7 +667,7 @@ func (s *LegacySuite) SetUpTest(c *gc.C) {
Controller: false,
}

s.callContext = context.NewCloudCallContext()
s.callContext = context.NewEmptyCloudCallContext()
s.blockChecker = mockBlockChecker{}
var err error
s.facade, err = spaces.NewAPIWithBacking(spaces.APIConfig{
Expand Down Expand Up @@ -708,7 +708,7 @@ func (s *LegacySuite) TestNewAPIWithBacking(c *gc.C) {
facade, err = spaces.NewAPIWithBacking(spaces.APIConfig{
Backing: &stubBacking{apiservertesting.BackingInstance},
Check: &s.blockChecker,
Context: context.NewCloudCallContext(),
Context: context.NewEmptyCloudCallContext(),
Resources: s.resources,
Authorizer: agentAuthorizer,
})
Expand Down Expand Up @@ -1101,7 +1101,7 @@ func (s *LegacySuite) TestSupportsSpacesModelConfigError(c *gc.C) {
errors.New("boom"), // Backing.ModelConfig()
)

err := spaces.SupportsSpaces(&stubBacking{apiservertesting.BackingInstance}, context.NewCloudCallContext())
err := spaces.SupportsSpaces(&stubBacking{apiservertesting.BackingInstance}, context.NewEmptyCloudCallContext())
c.Assert(err, gc.ErrorMatches, "getting environ: boom")
}

Expand All @@ -1112,7 +1112,7 @@ func (s *LegacySuite) TestSupportsSpacesEnvironNewError(c *gc.C) {
errors.New("boom"), // environs.New()
)

err := spaces.SupportsSpaces(&stubBacking{apiservertesting.BackingInstance}, context.NewCloudCallContext())
err := spaces.SupportsSpaces(&stubBacking{apiservertesting.BackingInstance}, context.NewEmptyCloudCallContext())
c.Assert(err, gc.ErrorMatches, "getting environ: boom")
}

Expand All @@ -1124,7 +1124,7 @@ func (s *LegacySuite) TestSupportsSpacesWithoutNetworking(c *gc.C) {
apiservertesting.WithoutSpaces,
apiservertesting.WithoutSubnets)

err := spaces.SupportsSpaces(&stubBacking{apiservertesting.BackingInstance}, context.NewCloudCallContext())
err := spaces.SupportsSpaces(&stubBacking{apiservertesting.BackingInstance}, context.NewEmptyCloudCallContext())
c.Assert(err, jc.Satisfies, errors.IsNotSupported)
}

Expand All @@ -1143,12 +1143,12 @@ func (s *LegacySuite) TestSupportsSpacesWithoutSpaces(c *gc.C) {
errors.New("boom"), // Backing.supportsSpaces()
)

err := spaces.SupportsSpaces(&stubBacking{apiservertesting.BackingInstance}, context.NewCloudCallContext())
err := spaces.SupportsSpaces(&stubBacking{apiservertesting.BackingInstance}, context.NewEmptyCloudCallContext())
c.Assert(err, jc.Satisfies, errors.IsNotSupported)
}

func (s *LegacySuite) TestSupportsSpaces(c *gc.C) {
err := spaces.SupportsSpaces(&stubBacking{apiservertesting.BackingInstance}, context.NewCloudCallContext())
err := spaces.SupportsSpaces(&stubBacking{apiservertesting.BackingInstance}, context.NewEmptyCloudCallContext())
c.Assert(err, jc.ErrorIsNil)
}

Expand Down
2 changes: 1 addition & 1 deletion apiserver/facades/client/sshclient/facade_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func (s *facadeSuite) SetUpTest(c *gc.C) {
s.authorizer.Tag = names.NewUserTag("igor")
s.authorizer.AdminTag = names.NewUserTag("igor")

s.callContext = context.NewCloudCallContext()
s.callContext = context.NewEmptyCloudCallContext()
facade, err := sshclient.InternalFacade(s.backend, s.authorizer, s.callContext)
c.Assert(err, jc.ErrorIsNil)
s.facade = facade
Expand Down
2 changes: 1 addition & 1 deletion apiserver/facades/client/storage/base_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func (s *baseStorageSuite) SetUpTest(c *gc.C) {
s.poolManager = s.constructPoolManager()
s.poolsInUse = []string{}

s.callContext = context.NewCloudCallContext()
s.callContext = context.NewEmptyCloudCallContext()
s.api = storage.NewStorageAPIForTest(s.state, state.ModelTypeIAAS, s.storageAccessor, s.registry, s.poolManager, s.authorizer, s.callContext)
s.apiCaas = storage.NewStorageAPIForTest(s.state, state.ModelTypeCAAS, s.storageAccessor, s.registry, s.poolManager, s.authorizer, s.callContext)
newAPI := storage.NewStorageAPIForTest(s.state, state.ModelTypeIAAS, s.storageAccessor, s.registry, s.poolManager, s.authorizer, s.callContext)
Expand Down
4 changes: 2 additions & 2 deletions apiserver/facades/client/subnets/subnets_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func (s *SubnetSuite) TestSubnetsByCIDR(c *gc.C) {
func (s *SubnetSuite) setupSubnetsAPI(c *gc.C) *gomock.Controller {
ctrl := gomock.NewController(c)
s.mockResource = facademocks.NewMockResources(ctrl)
s.mockCloudCallContext = context.NewCloudCallContext()
s.mockCloudCallContext = context.NewEmptyCloudCallContext()
s.mockBacking = mocks.NewMockBacking(ctrl)

s.mockAuthorizer = facademocks.NewMockAuthorizer(ctrl)
Expand Down Expand Up @@ -143,7 +143,7 @@ func (s *SubnetsSuite) SetUpTest(c *gc.C) {
Controller: false,
}

s.callContext = context.NewCloudCallContext()
s.callContext = context.NewEmptyCloudCallContext()
var err error
s.facade, err = subnets.NewAPIWithBacking(
&stubBacking{apiservertesting.BackingInstance},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func (s *Suite) SetUpTest(c *gc.C) {
Tag: s.Owner,
AdminTag: s.Owner,
}
s.callContext = context.NewCloudCallContext()
s.callContext = context.NewEmptyCloudCallContext()
s.facadeContext = facadetest.Context{
State_: s.State,
StatePool_: s.StatePool,
Expand Down
8 changes: 4 additions & 4 deletions caas/kubernetes/provider/constraints_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@ var _ = gc.Suite(&ConstraintsSuite{})

func (s *ConstraintsSuite) SetUpTest(c *gc.C) {
s.BaseSuite.SetUpTest(c)
s.callCtx = context.NewCloudCallContext()
s.callCtx = context.NewEmptyCloudCallContext()
}

func (s *ConstraintsSuite) TestConstraintsValidatorOkay(c *gc.C) {
ctrl := s.setupController(c)
defer ctrl.Finish()

validator, err := s.broker.ConstraintsValidator(context.NewCloudCallContext())
validator, err := s.broker.ConstraintsValidator(context.NewEmptyCloudCallContext())
c.Assert(err, jc.ErrorIsNil)

cons := constraints.MustParse("mem=64G")
Expand All @@ -44,7 +44,7 @@ func (s *ConstraintsSuite) TestConstraintsValidatorEmpty(c *gc.C) {
ctrl := s.setupController(c)
defer ctrl.Finish()

validator, err := s.broker.ConstraintsValidator(context.NewCloudCallContext())
validator, err := s.broker.ConstraintsValidator(context.NewEmptyCloudCallContext())
c.Assert(err, jc.ErrorIsNil)

unsupported, err := validator.Validate(constraints.Value{})
Expand All @@ -57,7 +57,7 @@ func (s *ConstraintsSuite) TestConstraintsValidatorUnsupported(c *gc.C) {
ctrl := s.setupController(c)
defer ctrl.Finish()

validator, err := s.broker.ConstraintsValidator(context.NewCloudCallContext())
validator, err := s.broker.ConstraintsValidator(context.NewEmptyCloudCallContext())
c.Assert(err, jc.ErrorIsNil)

cons := constraints.MustParse(strings.Join([]string{
Expand Down
14 changes: 7 additions & 7 deletions caas/kubernetes/provider/k8s.go
Original file line number Diff line number Diff line change
Expand Up @@ -479,7 +479,7 @@ func (*kubernetesClient) Provider() caas.ContainerEnvironProvider {
}

// Destroy is part of the Broker interface.
func (k *kubernetesClient) Destroy(callbacks envcontext.ProviderCallContext) (err error) {
func (k *kubernetesClient) Destroy(ctx envcontext.ProviderCallContext) (err error) {
defer func() {
if err != nil && k8serrors.ReasonForError(err) == v1.StatusReasonUnknown {
logger.Warningf("k8s cluster is not accessible: %v", err)
Expand All @@ -490,14 +490,14 @@ func (k *kubernetesClient) Destroy(callbacks envcontext.ProviderCallContext) (er
errChan := make(chan error, 1)
done := make(chan struct{})

ctx, cancel := context.WithCancel(context.Background())
destroyCtx, cancel := context.WithCancel(ctx)
defer cancel()

var wg sync.WaitGroup
wg.Add(1)
go k.deleteClusterScopeResourcesModelTeardown(ctx, &wg, errChan)
go k.deleteClusterScopeResourcesModelTeardown(destroyCtx, &wg, errChan)
wg.Add(1)
go k.deleteNamespaceModelTeardown(ctx, &wg, errChan)
go k.deleteNamespaceModelTeardown(destroyCtx, &wg, errChan)

go func() {
wg.Wait()
Expand All @@ -506,14 +506,14 @@ func (k *kubernetesClient) Destroy(callbacks envcontext.ProviderCallContext) (er

for {
select {
case <-callbacks.Dying():
return nil
case err = <-errChan:
if err != nil {
return errors.Trace(err)
}
case <-destroyCtx.Done():
return destroyCtx.Err()
case <-done:
return nil
return destroyCtx.Err()
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions caas/kubernetes/provider/k8s_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1733,12 +1733,12 @@ func (s *K8sBrokerSuite) assertDestroy(c *gc.C, isController bool, destroyFunc f

func (s *K8sBrokerSuite) TestDestroyController(c *gc.C) {
s.assertDestroy(c, true, func() error {
return s.broker.DestroyController(context.NewCloudCallContext(), testing.ControllerTag.Id())
return s.broker.DestroyController(context.NewEmptyCloudCallContext(), testing.ControllerTag.Id())
})
}

func (s *K8sBrokerSuite) TestDestroy(c *gc.C) {
s.assertDestroy(c, false, func() error { return s.broker.Destroy(context.NewCloudCallContext()) })
s.assertDestroy(c, false, func() error { return s.broker.Destroy(context.NewEmptyCloudCallContext()) })
}

func (s *K8sBrokerSuite) TestGetCurrentNamespace(c *gc.C) {
Expand Down
Loading

0 comments on commit 4f057ab

Please sign in to comment.