diff --git a/cmd/jimmctl/cmd/controllerinfo.go b/cmd/jimmctl/cmd/controllerinfo.go index 4d15d74ff..f6e186aa0 100644 --- a/cmd/jimmctl/cmd/controllerinfo.go +++ b/cmd/jimmctl/cmd/controllerinfo.go @@ -21,10 +21,13 @@ var ( controller-info command writes controller information contained in the juju client store to a yaml file. - If a --local flag is specified, the output controller - public address will use the first available local API address - and the local CA cert of the controller, see examples below - for usage. + If a public address is specified, the output controller information + will contain the public address provided and omit a CA cert, this assumes + that the server is secured with a public certificate. + + Use the --local flag if the server is not configured with a public cert. + + See examples below for usage. Examples: jimmctl controller-info @@ -85,6 +88,9 @@ func (c *controllerInfoCommand) Init(args []string) error { if c.local && len(c.publicAddress) > 0 { return errors.New("cannot set both public address and local flag") } + if !c.local && len(c.publicAddress) == 0 { + return errors.New("provide either a public address or use --local") + } return nil } @@ -111,13 +117,8 @@ func (c *controllerInfoCommand) Run(ctxt *cmd.Context) error { info.TLSHostname = c.tlsHostname info.PublicAddress = c.publicAddress if c.local { - info.PublicAddress = controller.APIEndpoints[0] info.CACertificate = controller.CACert } - if info.PublicAddress == "" { - return errors.New("public address must be set") - } - data, err := yaml.Marshal(info) if err != nil { return errors.Mask(err) diff --git a/cmd/jimmctl/cmd/controllerinfo_test.go b/cmd/jimmctl/cmd/controllerinfo_test.go index 32c98d428..6928ee256 100644 --- a/cmd/jimmctl/cmd/controllerinfo_test.go +++ b/cmd/jimmctl/cmd/controllerinfo_test.go @@ -199,7 +199,7 @@ func (s *controllerInfoSuite) TestControllerInfoMissingPublicAddressAndNoLocalFl fname := path.Join(dir, "test.yaml") _, err = cmdtesting.RunCommand(c, cmd.NewControllerInfoCommandForTesting(store), "controller-1", fname) - c.Assert(err, gc.ErrorMatches, "public address must be set") + c.Assert(err, gc.ErrorMatches, "provide either a public address or use --local") } func (s *controllerInfoSuite) TestControllerInfoCannotProvideAddrAndLocalFlag(c *gc.C) { diff --git a/internal/rpc/dial.go b/internal/rpc/dial.go index 5994f3412..19c3714f7 100644 --- a/internal/rpc/dial.go +++ b/internal/rpc/dial.go @@ -12,6 +12,7 @@ import ( "sync" "github.com/gorilla/websocket" + "github.com/juju/juju/core/network" "github.com/juju/names/v5" "github.com/juju/zaputil" "github.com/juju/zaputil/zapctx" @@ -84,10 +85,9 @@ func Dial(ctx context.Context, ctl *dbmodel.Controller, modelTag names.ModelTag, var urls []string for _, hps := range ctl.Addresses { for _, hp := range hps { - if hp.Scope != "public" && hp.Scope != "" { - continue + if maybeReachable(hp.Scope) { + urls = append(urls, websocketURL(fmt.Sprintf("%s:%d", hp.Value, hp.Port), modelTag, finalPath)) } - urls = append(urls, websocketURL(fmt.Sprintf("%s:%d", hp.Value, hp.Port), modelTag, finalPath)) } } zapctx.Debug(ctx, "Dialling all URLs", zap.Any("urls", urls)) @@ -98,6 +98,21 @@ func Dial(ctx context.Context, ctl *dbmodel.Controller, modelTag names.ModelTag, return conn, nil } +// maybeReachable decides what kinds of links JIMM should try to connect via. +// Local IPs like localhost for example are excluded but public IPs and Cloud local IPs are potentially reachable. +func maybeReachable(scope string) bool { + switch scope { + case string(network.ScopeCloudLocal): + return true + case string(network.ScopePublic): + return true + case "": + return true + default: + return false + } +} + func websocketURL(s string, mt names.ModelTag, finalPath string) string { u := url.URL{ Scheme: "wss", diff --git a/local/jimm/add-controller.sh b/local/jimm/add-controller.sh index 152d4a169..23f89aca3 100755 --- a/local/jimm/add-controller.sh +++ b/local/jimm/add-controller.sh @@ -29,7 +29,7 @@ echo "Switching juju controller to $JIMM_CONTROLLER_NAME" juju switch "$JIMM_CONTROLLER_NAME" echo echo "Retrieving controller info for $CONTROLLER_NAME" -./jimmctl controller-info --local "$CONTROLLER_NAME" "$CONTROLLER_YAML_PATH" +./jimmctl controller-info --local "$CONTROLLER_NAME" "$CONTROLLER_YAML_PATH" --tls-hostname juju-apiserver if [[ -f "$CONTROLLER_YAML_PATH" ]]; then echo "Controller info retrieved." else