Skip to content

Commit

Permalink
Merge commit from fork
Browse files Browse the repository at this point in the history
chore: merge 3.4 into 3.5
  • Loading branch information
hpidcock authored Oct 1, 2024
2 parents ab94320 + 31b4b09 commit 02fd198
Show file tree
Hide file tree
Showing 29 changed files with 220 additions and 162 deletions.
18 changes: 7 additions & 11 deletions agent/addons/addons.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
package addons

import (
"path"
"runtime"

"github.com/juju/clock"
"github.com/juju/errors"
"github.com/juju/loggo"
"github.com/juju/names/v5"
"github.com/juju/worker/v3"
"github.com/juju/worker/v3/dependency"
"github.com/prometheus/client_golang/prometheus"
Expand All @@ -22,17 +22,14 @@ import (

var logger = loggo.GetLogger("juju.cmd.jujud.agent.addons")

// DefaultIntrospectionSocketName returns the socket name to use for the
// abstract domain socket that the introspection worker serves requests
// over.
func DefaultIntrospectionSocketName(entityTag names.Tag) string {
return "jujud-" + entityTag.String()
}
// IntrospectionSocketName is the name of the socket file inside
// the agent's directory used for introspection calls.
const IntrospectionSocketName = "introspection.socket"

// IntrospectionConfig defines the various components that the introspection
// worker reports on or needs to start up.
type IntrospectionConfig struct {
AgentTag names.Tag
AgentDir string
Engine *dependency.Engine
StatePoolReporter introspection.Reporter
PubSubReporter introspection.Reporter
Expand All @@ -43,8 +40,7 @@ type IntrospectionConfig struct {
LocalHub introspection.SimpleHub
CentralHub introspection.StructuredHub

NewSocketName func(names.Tag) string
WorkerFunc func(config introspection.Config) (worker.Worker, error)
WorkerFunc func(config introspection.Config) (worker.Worker, error)
}

// StartIntrospection creates the introspection worker. It cannot and should
Expand All @@ -59,7 +55,7 @@ func StartIntrospection(cfg IntrospectionConfig) error {
return nil
}

socketName := cfg.NewSocketName(cfg.AgentTag)
socketName := path.Join(cfg.AgentDir, IntrospectionSocketName)
w, err := cfg.WorkerFunc(introspection.Config{
SocketName: socketName,
DepEngine: cfg.Engine,
Expand Down
16 changes: 4 additions & 12 deletions agent/addons/addons_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"github.com/juju/clock"
"github.com/juju/errors"
"github.com/juju/loggo"
"github.com/juju/names/v5"
"github.com/juju/testing"
jc "github.com/juju/testing/checkers"
"github.com/juju/worker/v3"
Expand Down Expand Up @@ -55,8 +54,7 @@ func (s *introspectionSuite) TestStartError(c *gc.C) {
}

cfg := addons.IntrospectionConfig{
AgentTag: names.NewMachineTag("42"),
NewSocketName: addons.DefaultIntrospectionSocketName,
AgentDir: c.MkDir(),
WorkerFunc: func(_ introspection.Config) (worker.Worker, error) {
return nil, errors.New("boom")
},
Expand Down Expand Up @@ -85,9 +83,8 @@ func (s *introspectionSuite) TestStartSuccess(c *gc.C) {
c.Assert(err, jc.ErrorIsNil)

cfg := addons.IntrospectionConfig{
AgentTag: names.NewMachineTag("42"),
Engine: engine,
NewSocketName: func(tag names.Tag) string { return "bananas" },
AgentDir: c.MkDir(),
Engine: engine,
WorkerFunc: func(cfg introspection.Config) (worker.Worker, error) {
fake.config = cfg
return fake, nil
Expand All @@ -98,7 +95,7 @@ func (s *introspectionSuite) TestStartSuccess(c *gc.C) {
c.Assert(err, jc.ErrorIsNil)

c.Check(fake.config.DepEngine, gc.Equals, engine)
c.Check(fake.config.SocketName, gc.Equals, "bananas")
c.Check(fake.config.SocketName, jc.HasSuffix, "introspection.socket")

// Stopping the engine causes the introspection worker to stop.
engine.Kill()
Expand All @@ -110,11 +107,6 @@ func (s *introspectionSuite) TestStartSuccess(c *gc.C) {
}
}

func (s *introspectionSuite) TestDefaultIntrospectionSocketName(c *gc.C) {
name := addons.DefaultIntrospectionSocketName(names.NewMachineTag("42"))
c.Assert(name, gc.Equals, "jujud-machine-42")
}

type dummyWorker struct {
config introspection.Config
done chan struct{}
Expand Down
1 change: 1 addition & 0 deletions cmd/containeragent/initialize/package_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ func (*importSuite) TestImports(c *gc.C) {
"environs/tags",
"feature",
"juju/osenv",
"juju/sockets",
"logfwd",
"logfwd/syslog",
"mongo",
Expand Down
2 changes: 1 addition & 1 deletion cmd/containeragent/main_nix.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ func main() {
return cmd.Main(&run.RunCommand{MachineLock: lock}, ctx, args[1:])
},
jujuIntrospect: func(ctx *cmd.Context, args []string) int {
return cmd.Main(introspect.New(nil), ctx, args[1:])
return cmd.Main(introspect.New(), ctx, args[1:])
},
jujuDumpLogs: func(ctx *cmd.Context, args []string) int {
return cmd.Main(dumplogs.NewCommand(), ctx, args[1:])
Expand Down
3 changes: 1 addition & 2 deletions cmd/containeragent/unit/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -294,10 +294,9 @@ func (c *containerUnitAgent) workers(sigTermCh chan os.Signal) (worker.Worker, e
return nil, err
}
if err := addons.StartIntrospection(addons.IntrospectionConfig{
AgentTag: c.CurrentConfig().Tag(),
AgentDir: agentConfig.Dir(),
Engine: eng,
MachineLock: c.machineLock,
NewSocketName: addons.DefaultIntrospectionSocketName,
PrometheusGatherer: c.prometheusRegistry,
WorkerFunc: introspection.NewWorker,
Clock: c.clk,
Expand Down
18 changes: 6 additions & 12 deletions cmd/jujud-controller/agent/machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,6 @@ func (a *machineAgentCmd) Info() *cmd.Info {
func MachineAgentFactoryFn(
agentConfWriter agentconf.AgentConfigWriter,
bufferedLogger *logsender.BufferedLogWriter,
newIntrospectionSocketName func(names.Tag) string,
preUpgradeSteps upgrades.PreUpgradeStepsFunc,
rootDir string,
) machineAgentFactoryFnType {
Expand All @@ -287,7 +286,6 @@ func MachineAgentFactoryFn(
Logger: logger,
}),
looputil.NewLoopDeviceManager(),
newIntrospectionSocketName,
preUpgradeSteps,
rootDir,
isCaasAgent,
Expand All @@ -302,7 +300,6 @@ func NewMachineAgent(
bufferedLogger *logsender.BufferedLogWriter,
runner *worker.Runner,
loopDeviceManager looputil.LoopDeviceManager,
newIntrospectionSocketName func(names.Tag) string,
preUpgradeSteps upgrades.PreUpgradeStepsFunc,
rootDir string,
isCaasAgent bool,
Expand All @@ -322,7 +319,6 @@ func NewMachineAgent(
rootDir: rootDir,
initialUpgradeCheckComplete: gate.NewLock(),
loopDeviceManager: loopDeviceManager,
newIntrospectionSocketName: newIntrospectionSocketName,
prometheusRegistry: prometheusRegistry,
mongoTxnCollector: mongometrics.NewTxnCollector(),
mongoDialCollector: mongometrics.NewDialCollector(),
Expand Down Expand Up @@ -403,12 +399,11 @@ type MachineAgent struct {
mongoInitMutex sync.Mutex
mongoInitialized bool

loopDeviceManager looputil.LoopDeviceManager
newIntrospectionSocketName func(names.Tag) string
prometheusRegistry *prometheus.Registry
mongoTxnCollector *mongometrics.TxnCollector
mongoDialCollector *mongometrics.DialCollector
preUpgradeSteps upgrades.PreUpgradeStepsFunc
loopDeviceManager looputil.LoopDeviceManager
prometheusRegistry *prometheus.Registry
mongoTxnCollector *mongometrics.TxnCollector
mongoDialCollector *mongometrics.DialCollector
preUpgradeSteps upgrades.PreUpgradeStepsFunc

centralHub *pubsub.StructuredHub
pubsubMetrics *centralhub.PubsubMetrics
Expand Down Expand Up @@ -650,12 +645,11 @@ func (a *MachineAgent) makeEngineCreator(
return nil, err
}
if err := addons.StartIntrospection(addons.IntrospectionConfig{
AgentTag: a.CurrentConfig().Tag(),
AgentDir: a.CurrentConfig().Dir(),
Engine: engine,
StatePoolReporter: &statePoolReporter,
PubSubReporter: pubsubReporter,
MachineLock: a.machineLock,
NewSocketName: a.newIntrospectionSocketName,
PrometheusGatherer: a.prometheusRegistry,
PresenceRecorder: presenceRecorder,
WorkerFunc: introspection.NewWorker,
Expand Down
1 change: 0 additions & 1 deletion cmd/jujud-controller/agent/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,6 @@ func NewTestMachineAgentFactory(
rootDir: rootDir,
initialUpgradeCheckComplete: gate.NewLock(),
loopDeviceManager: &mockLoopDeviceManager{},
newIntrospectionSocketName: addons.DefaultIntrospectionSocketName,
prometheusRegistry: prometheusRegistry,
mongoTxnCollector: mongometrics.NewTxnCollector(),
mongoDialCollector: mongometrics.NewDialCollector(),
Expand Down
3 changes: 0 additions & 3 deletions cmd/jujud-controller/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import (
"github.com/juju/utils/v3/exec"
"github.com/juju/version/v2"

"github.com/juju/juju/agent/addons"
k8sexec "github.com/juju/juju/caas/kubernetes/provider/exec"
jujucmd "github.com/juju/juju/cmd"
agentcmd "github.com/juju/juju/cmd/jujud-controller/agent"
Expand Down Expand Up @@ -267,7 +266,6 @@ func jujuDMain(args []string, ctx *cmd.Context) (code int, err error) {
machineAgentFactory := agentcmd.MachineAgentFactoryFn(
agentConf,
bufferedLogger,
addons.DefaultIntrospectionSocketName,
upgrades.PreUpgradeSteps,
"",
)
Expand Down Expand Up @@ -345,7 +343,6 @@ func jujuControllerMain(args []string, ctx *cmd.Context) (code int, err error) {
machineAgentFactory := agentcmd.MachineAgentFactoryFn(
agentConf,
bufferedLogger,
addons.DefaultIntrospectionSocketName,
upgrades.PreUpgradeSteps,
"",
)
Expand Down
3 changes: 1 addition & 2 deletions cmd/jujud/agent/caasoperator.go
Original file line number Diff line number Diff line change
Expand Up @@ -258,10 +258,9 @@ func (op *CaasOperatorAgent) Workers() (worker.Worker, error) {
return nil, err
}
if err := addons.StartIntrospection(addons.IntrospectionConfig{
AgentTag: op.CurrentConfig().Tag(),
AgentDir: agentConfig.Dir(),
Engine: engine,
MachineLock: op.machineLock,
NewSocketName: addons.DefaultIntrospectionSocketName,
PrometheusGatherer: op.prometheusRegistry,
WorkerFunc: introspection.NewWorker,
Clock: clock.WallClock,
Expand Down
10 changes: 3 additions & 7 deletions cmd/jujud/agent/machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,6 @@ func (a *machineAgentCmd) Info() *cmd.Info {
func MachineAgentFactoryFn(
agentConfWriter agentconf.AgentConfigWriter,
bufferedLogger *logsender.BufferedLogWriter,
newIntrospectionSocketName func(names.Tag) string,
preUpgradeSteps upgrades.PreUpgradeStepsFunc,
rootDir string,
) machineAgentFactoryFnType {
Expand All @@ -247,7 +246,6 @@ func MachineAgentFactoryFn(
Logger: logger,
}),
looputil.NewLoopDeviceManager(),
newIntrospectionSocketName,
preUpgradeSteps,
rootDir,
isCaasAgent,
Expand All @@ -262,7 +260,6 @@ func NewMachineAgent(
bufferedLogger *logsender.BufferedLogWriter,
runner *worker.Runner,
loopDeviceManager looputil.LoopDeviceManager,
newIntrospectionSocketName func(names.Tag) string,
preUpgradeSteps upgrades.PreUpgradeStepsFunc,
rootDir string,
isCaasAgent bool,
Expand All @@ -282,7 +279,6 @@ func NewMachineAgent(
rootDir: rootDir,
initialUpgradeCheckComplete: gate.NewLock(),
loopDeviceManager: loopDeviceManager,
newIntrospectionSocketName: newIntrospectionSocketName,
prometheusRegistry: prometheusRegistry,
preUpgradeSteps: preUpgradeSteps,
isCaasAgent: isCaasAgent,
Expand Down Expand Up @@ -491,9 +487,10 @@ func (a *MachineAgent) makeEngineCreator(
agentName string, previousAgentVersion version.Number,
) func() (worker.Worker, error) {
return func() (worker.Worker, error) {
agentConfig := a.CurrentConfig()
engineConfigFunc := engine.DependencyEngineConfig
metrics := engine.NewMetrics()
controllerMetricsSink := metrics.ForModel(a.CurrentConfig().Model())
controllerMetricsSink := metrics.ForModel(agentConfig.Model())
engine, err := dependency.NewEngine(engineConfigFunc(controllerMetricsSink))
if err != nil {
return nil, err
Expand Down Expand Up @@ -568,11 +565,10 @@ func (a *MachineAgent) makeEngineCreator(
return nil, err
}
if err := addons.StartIntrospection(addons.IntrospectionConfig{
AgentTag: a.CurrentConfig().Tag(),
AgentDir: agentConfig.Dir(),
Engine: engine,
StatePoolReporter: &statePoolReporter,
MachineLock: a.machineLock,
NewSocketName: a.newIntrospectionSocketName,
PrometheusGatherer: a.prometheusRegistry,
WorkerFunc: introspection.NewWorker,
Clock: clock.WallClock,
Expand Down
1 change: 0 additions & 1 deletion cmd/jujud/agent/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,6 @@ func NewTestMachineAgentFactory(
rootDir: rootDir,
initialUpgradeCheckComplete: gate.NewLock(),
loopDeviceManager: &mockLoopDeviceManager{},
newIntrospectionSocketName: addons.DefaultIntrospectionSocketName,
prometheusRegistry: prometheusRegistry,
preUpgradeSteps: preUpgradeSteps,
isCaasAgent: isCAAS,
Expand Down
17 changes: 5 additions & 12 deletions cmd/jujud/introspect/introspect.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"net/http/httputil"
"net/url"
"os"
"path"
"path/filepath"
"strings"

Expand All @@ -19,6 +20,7 @@ import (
"github.com/juju/names/v5"
"github.com/kr/pretty"

"github.com/juju/juju/agent"
"github.com/juju/juju/agent/addons"
apiagent "github.com/juju/juju/api/agent/agent"
jujucmd "github.com/juju/juju/cmd"
Expand All @@ -35,16 +37,11 @@ type IntrospectCommand struct {
verbose bool
post bool
form url.Values

// IntrospectionSocketName returns the socket name
// for a given tag. If IntrospectionSocketName is nil,
// agent.DefaultIntrospectionSocketName is used.
IntrospectionSocketName func(names.Tag) string
}

// New initializes IntrospectCommand.
func New(sockNameGetter func(names.Tag) string) cmd.Command {
return &IntrospectCommand{IntrospectionSocketName: sockNameGetter}
func New() cmd.Command {
return &IntrospectCommand{}
}

const introspectCommandDoc = `
Expand Down Expand Up @@ -127,11 +124,7 @@ func (c *IntrospectCommand) Run(ctx *cmd.Context) error {
return err
}

getSocketName := c.IntrospectionSocketName
if getSocketName == nil {
getSocketName = addons.DefaultIntrospectionSocketName
}
socketName := "@" + getSocketName(tag)
socketName := path.Join(agent.Dir(c.dataDir, tag), addons.IntrospectionSocketName)
if c.listen != "" {
listener, err := net.Listen("tcp", c.listen)
if err != nil {
Expand Down
Loading

0 comments on commit 02fd198

Please sign in to comment.