Skip to content

Commit

Permalink
Merge branch '2.9' (early part) into merge-2.9-develop-2021-12-10
Browse files Browse the repository at this point in the history
  • Loading branch information
hpidcock committed Dec 10, 2021
2 parents b802085 + d6e9bf5 commit 1b8158f
Show file tree
Hide file tree
Showing 107 changed files with 3,488 additions and 650 deletions.
4 changes: 4 additions & 0 deletions acceptancetests/jujupy/k8s_provider/gke.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,10 @@ def log_remaining(remaining, msg=''):
max_node_count=3,
),
)],
network=f"projects/{self.default_params['project_id']}/global/networks/with-subnets",
ip_allocation_policy=dict(
use_ip_aliases=True,
),
)
logger.info('creating cluster -> %s', cluster)
r = self.driver.create_cluster(
Expand Down
16 changes: 16 additions & 0 deletions agent/addons/addons.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/juju/worker/v3/dependency"
"github.com/prometheus/client_golang/prometheus"

"github.com/juju/juju/cmd/jujud/agent/engine"
"github.com/juju/juju/core/machinelock"
"github.com/juju/juju/core/presence"
"github.com/juju/juju/core/raftlease"
Expand Down Expand Up @@ -104,3 +105,18 @@ func NewPrometheusRegistry() (*prometheus.Registry, error) {
}
return r, nil
}

// RegisterEngineMetrics registers the metrics sink on a prometheus registerer,
// ensuring that we cleanup when the worker has stopped.
func RegisterEngineMetrics(registry prometheus.Registerer, metrics prometheus.Collector, worker worker.Worker, sink engine.MetricSink) error {
if err := registry.Register(metrics); err != nil {
return errors.Annotatef(err, "failed to register engine metrics")
}

go func() {
_ = worker.Wait()
_ = sink.Unregister()
_ = registry.Unregister(metrics)
}()
return nil
}
50 changes: 50 additions & 0 deletions agent/addons/addons_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"runtime"
"time"

"github.com/golang/mock/gomock"
"github.com/juju/clock"
"github.com/juju/errors"
"github.com/juju/loggo"
Expand All @@ -15,6 +16,7 @@ import (
jc "github.com/juju/testing/checkers"
"github.com/juju/worker/v3"
"github.com/juju/worker/v3/dependency"
"github.com/prometheus/client_golang/prometheus"
gc "gopkg.in/check.v1"

"github.com/juju/juju/agent/addons"
Expand Down Expand Up @@ -76,6 +78,7 @@ func (s *introspectionSuite) TestStartSuccess(c *gc.C) {
IsFatal: agenterrors.IsFatal,
WorstError: agenterrors.MoreImportantError,
Clock: clock.WallClock,
Metrics: dependency.DefaultMetrics(),
Logger: loggo.GetLogger("juju.worker.dependency"),
}
engine, err := dependency.NewEngine(config)
Expand Down Expand Up @@ -125,3 +128,50 @@ func (d *dummyWorker) Wait() error {
<-d.done
return nil
}

type registerSuite struct {
testing.IsolationSuite
}

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

func (s *registerSuite) TestRegisterEngineMetrics(c *gc.C) {
ctrl := gomock.NewController(c)
defer ctrl.Finish()

done := make(chan struct{}, 1)

collector := dummyCollector{}

registry := NewMockRegisterer(ctrl)
registry.EXPECT().Register(collector)
registry.EXPECT().Unregister(collector).Do(func(_ prometheus.Collector) {
close(done)
})
sink := NewMockMetricSink(ctrl)
sink.EXPECT().Unregister()

worker := &dummyWorker{
done: make(chan struct{}, 1),
}

err := addons.RegisterEngineMetrics(registry, collector, worker, sink)
c.Assert(err, jc.ErrorIsNil)

worker.Kill()

select {
case <-done:
case <-time.After(testing.ShortWait):
}
}

type dummyCollector struct{}

// Describe is part of the prometheus.Collector interface.
func (dummyCollector) Describe(ch chan<- *prometheus.Desc) {
}

// Collect is part of the prometheus.Collector interface.
func (dummyCollector) Collect(ch chan<- prometheus.Metric) {
}
60 changes: 60 additions & 0 deletions agent/addons/engine_mock_test.go

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

3 changes: 3 additions & 0 deletions agent/addons/package_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ import (
gc "gopkg.in/check.v1"
)

//go:generate go run github.com/golang/mock/mockgen -package addons_test -destination prometheus_mock_test.go github.com/prometheus/client_golang/prometheus Registerer
//go:generate go run github.com/golang/mock/mockgen -package addons_test -destination engine_mock_test.go github.com/juju/juju/cmd/jujud/agent/engine MetricSink

func Test(t *testing.T) {
gc.TestingT(t)
}
79 changes: 79 additions & 0 deletions agent/addons/prometheus_mock_test.go

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

4 changes: 4 additions & 0 deletions api/controller/legacy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,10 @@ func (s *legacySuite) TestWatchAllModels(c *gc.C) {
case float64:
modelInfo.Config[config.NetBondReconfigureDelayKey] = int(val)
}
switch val := modelInfo.Config[config.NumProvisionWorkersKey].(type) {
case float64:
modelInfo.Config[config.NumProvisionWorkersKey] = int(val)
}

expectedStatus := params.StatusInfo{
Current: status.Status,
Expand Down
2 changes: 1 addition & 1 deletion api/facadeversions.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ var facadeVersions = map[string]int{
"Pinger": 1,
"Provisioner": 11,
"ProxyUpdater": 2,
"RaftLease": 1,
"RaftLease": 2,
"Reboot": 2,
"RelationStatusWatcher": 1,
"RelationUnitsWatcher": 1,
Expand Down
21 changes: 14 additions & 7 deletions api/raftlease/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"github.com/juju/juju/api"
"github.com/juju/juju/api/base"
apiservererrors "github.com/juju/juju/apiserver/errors"
"github.com/juju/juju/apiserver/params"
"github.com/juju/juju/core/lease"
"github.com/juju/juju/core/raftlease"
"github.com/juju/juju/pubsub/apiserver"
Expand Down Expand Up @@ -491,7 +492,7 @@ func NewRemote(config RemoteConfig) Remote {

// RaftLeaseApplier defines a client for applying leases.
type RaftLeaseApplier interface {
ApplyLease(command string) error
ApplyLease(params.LeaseOperationCommand) error
}

type remote struct {
Expand Down Expand Up @@ -545,19 +546,25 @@ func (r *remote) Request(ctx context.Context, command *raftlease.Command) error
return lease.ErrDropped
}

bytes, err := command.Marshal()
if err != nil {
return errors.Trace(err)
}

// Check that the context hasn't been canceled before applying the lease.
select {
case <-ctx.Done():
return ctx.Err()
default:
}

return r.client.ApplyLease(string(bytes))
return r.client.ApplyLease(params.LeaseOperationCommand{
Version: command.Version,
Operation: command.Operation,
Namespace: command.Namespace,
ModelUUID: command.ModelUUID,
Lease: command.Lease,
Holder: command.Holder,
Duration: command.Duration,
OldTime: command.OldTime,
NewTime: command.NewTime,
PinEntity: command.PinEntity,
})
}

// Kill is part of the worker.Worker interface.
Expand Down
3 changes: 2 additions & 1 deletion api/raftlease/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (

"github.com/juju/juju/api"
apiservererrors "github.com/juju/juju/apiserver/errors"
"github.com/juju/juju/apiserver/params"
"github.com/juju/juju/core/lease"
"github.com/juju/juju/core/raftlease"
"github.com/juju/juju/pubsub/apiserver"
Expand Down Expand Up @@ -583,7 +584,7 @@ func (s *RaftLeaseRemoteSuite) TestRequest(c *gc.C) {
client: s.raftLeaseApplier,
}

s.raftLeaseApplier.EXPECT().ApplyLease("version: 0\noperation: \"\"\n").Return(nil)
s.raftLeaseApplier.EXPECT().ApplyLease(params.LeaseOperationCommand{}).Return(nil)

err := remote.Request(context.TODO(), &raftlease.Command{})
c.Assert(err, jc.ErrorIsNil)
Expand Down
12 changes: 5 additions & 7 deletions api/raftlease/raftlease.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ import (

const facadeName = "RaftLease"

// API provides access to the pubsub API.
// API provides access to the raft lease API.
type API struct {
facade base.FacadeCaller
caller base.APICaller
}

// NewAPI creates a new client-side pubsub API.
// NewAPI creates a new client-side raft lease API.
func NewAPI(caller base.APICaller) *API {
facadeCaller := base.NewFacadeCaller(caller, facadeName)
return &API{
Expand All @@ -32,12 +32,10 @@ func NewAPI(caller base.APICaller) *API {
// ApplyLease attempts to apply a lease against the given controller. If the
// controller is not the leader, then an error to redirect to a new leader will
// be given.
func (api *API) ApplyLease(command string) error {
func (api *API) ApplyLease(command params.LeaseOperationCommand) error {
var results params.ErrorResults
err := api.facade.FacadeCall("ApplyLease", params.LeaseOperations{
Operations: []params.LeaseOperation{{
Command: command,
}},
err := api.facade.FacadeCall("ApplyLease", params.LeaseOperationsV2{
Operations: []params.LeaseOperationCommand{command},
}, &results)
if err != nil {
return errors.Trace(apiservererrors.RestoreError(err))
Expand Down
Loading

0 comments on commit 1b8158f

Please sign in to comment.