Skip to content

Commit 5c274bb

Browse files
authored
[1.15 PATCH] Allow configuring the scheduler host on init (#1521)
* feat: Allow configuring the scheduler advertised address on init Signed-off-by: Albert Callarisa <[email protected]> * Rename field to make it clearer Signed-off-by: Albert Callarisa <[email protected]> * Replace SprintF with concatenation as per linter recommendation. Signed-off-by: Albert Callarisa <[email protected]> --------- Signed-off-by: Albert Callarisa <[email protected]>
1 parent 16cc1d1 commit 5c274bb

File tree

5 files changed

+107
-47
lines changed

5 files changed

+107
-47
lines changed

README.md

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ Runtime version: v1.0.0
141141

142142
#### Install with mariner images
143143

144-
You can install Dapr Runtime using mariner images using the `--image-variant` flag.
144+
You can install Dapr Runtime using mariner images using the `--image-variant` flag.
145145

146146
```bash
147147
# Installing Dapr with Mariner images
@@ -158,6 +158,17 @@ You can install Dapr runtime by pulling docker images from a given private regis
158158
dapr init --image-registry example.io/<username>
159159
```
160160

161+
#### Install with a custom scheduler host and port
162+
163+
You can install Dapr runtime with a custom scheduler host and port by using `--scheduler-override-broadcast-host-port` flag.
164+
165+
```bash
166+
dapr init --scheduler-override-broadcast-host-port 192.168.42.42:50006
167+
```
168+
169+
> Note: The default host is `localhost`.
170+
171+
161172
#### Install in airgap environment
162173

163174
You can install Dapr runtime in airgap (offline) environment using a pre-downloaded [installer bundle](https://github.com/dapr/installer-bundle/releases). You need to download the archived bundle for your OS beforehand (e.g., daprbundle_linux_amd64.tar.gz,) and unpack it. Thereafter use the local Dapr CLI binary in the bundle with `--from-dir` flag in the init command to point to the extracted bundle location to initialize Dapr.
@@ -180,7 +191,7 @@ docker run --name "dapr_zipkin" --restart always -d -p 9411:9411 openzipkin/zipk
180191
docker run --name "dapr_redis" --restart always -d -p 6379:6379 redis
181192
```
182193

183-
Alternatively to the above, you can also have slim installation as well to install dapr without running any Docker containers in airgap mode.
194+
Alternatively to the above, you can also have slim installation as well to install dapr without running any Docker containers in airgap mode.
184195

185196
```bash
186197
./dapr init --slim --from-dir .
@@ -297,7 +308,7 @@ Output should look like as follows:
297308
All available [Helm Chart values](https://github.com/dapr/dapr/tree/master/charts/dapr#configuration) can be set by using the `--set` flag:
298309

299310
```bash
300-
dapr init -k --set global.tag=1.0.0 --set dapr_operator.logLevel=error
311+
dapr init -k --set global.tag=1.0.0 --set dapr_operator.logLevel=error
301312
```
302313

303314
#### Installing to a custom namespace
@@ -363,7 +374,7 @@ The example above shows how to upgrade from your current version to version `1.0
363374
All available [Helm Chart values](https://github.com/dapr/dapr/tree/master/charts/dapr#configuration) can be set by using the `--set` flag:
364375

365376
```bash
366-
dapr upgrade -k --runtime-version=1.0.0 --set global.tag=my-tag --set dapr_operator.logLevel=error
377+
dapr upgrade -k --runtime-version=1.0.0 --set global.tag=my-tag --set dapr_operator.logLevel=error
367378
```
368379

369380
*Note: do not use the `dapr upgrade` command if you're upgrading from 0.x versions of Dapr*

cmd/init.go

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -29,23 +29,24 @@ import (
2929
)
3030

3131
var (
32-
kubernetesMode bool
33-
wait bool
34-
timeout uint
35-
slimMode bool
36-
devMode bool
37-
runtimeVersion string
38-
dashboardVersion string
39-
allNamespaces bool
40-
initNamespace string
41-
resourceNamespace string
42-
enableMTLS bool
43-
enableHA bool
44-
values []string
45-
fromDir string
46-
containerRuntime string
47-
imageVariant string
48-
schedulerVolume string
32+
kubernetesMode bool
33+
wait bool
34+
timeout uint
35+
slimMode bool
36+
devMode bool
37+
runtimeVersion string
38+
dashboardVersion string
39+
allNamespaces bool
40+
initNamespace string
41+
resourceNamespace string
42+
enableMTLS bool
43+
enableHA bool
44+
values []string
45+
fromDir string
46+
containerRuntime string
47+
imageVariant string
48+
schedulerVolume string
49+
schedulerOverrideBroadcastHostPort string
4950
)
5051

5152
var InitCmd = &cobra.Command{
@@ -171,7 +172,13 @@ dapr init --runtime-path <path-to-install-directory>
171172
print.FailureStatusEvent(os.Stdout, "Invalid container runtime. Supported values are docker and podman.")
172173
os.Exit(1)
173174
}
174-
err := standalone.Init(runtimeVersion, dashboardVersion, dockerNetwork, slimMode, imageRegistryURI, fromDir, containerRuntime, imageVariant, daprRuntimePath, &schedulerVolume)
175+
176+
schedulerHostPort := &schedulerOverrideBroadcastHostPort
177+
if schedulerOverrideBroadcastHostPort == "" {
178+
schedulerHostPort = nil
179+
}
180+
181+
err := standalone.Init(runtimeVersion, dashboardVersion, dockerNetwork, slimMode, imageRegistryURI, fromDir, containerRuntime, imageVariant, daprRuntimePath, &schedulerVolume, schedulerHostPort)
175182
if err != nil {
176183
print.FailureStatusEvent(os.Stderr, err.Error())
177184
os.Exit(1)
@@ -221,6 +228,7 @@ func init() {
221228
InitCmd.Flags().StringVarP(&fromDir, "from-dir", "", "", "Use Dapr artifacts from local directory for self-hosted installation")
222229
InitCmd.Flags().StringVarP(&imageVariant, "image-variant", "", "", "The image variant to use for the Dapr runtime, for example: mariner")
223230
InitCmd.Flags().StringVarP(&schedulerVolume, "scheduler-volume", "", "dapr_scheduler", "Self-hosted only. Specify a volume for the scheduler service data directory.")
231+
InitCmd.Flags().StringVarP(&schedulerOverrideBroadcastHostPort, "scheduler-override-broadcast-host-port", "", "", "Self-hosted only. Specify the scheduler broadcast host and port, for example: 192.168.42.42:50006. If not specified, it uses localhost:50006 (6060 for Windows).")
224232
InitCmd.Flags().BoolP("help", "h", false, "Print this help message")
225233
InitCmd.Flags().StringArrayVar(&values, "set", []string{}, "set values on the command line (can specify multiple or separate values with commas: key1=val1,key2=val2)")
226234
InitCmd.Flags().String("image-registry", "", "Custom/private docker image repository URL")

pkg/standalone/standalone.go

Lines changed: 30 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -133,17 +133,18 @@ type componentMetadataItem struct {
133133
}
134134

135135
type initInfo struct {
136-
fromDir string
137-
installDir string
138-
bundleDet *bundleDetails
139-
slimMode bool
140-
runtimeVersion string
141-
dashboardVersion string
142-
dockerNetwork string
143-
imageRegistryURL string
144-
containerRuntime string
145-
imageVariant string
146-
schedulerVolume *string
136+
fromDir string
137+
installDir string
138+
bundleDet *bundleDetails
139+
slimMode bool
140+
runtimeVersion string
141+
dashboardVersion string
142+
dockerNetwork string
143+
imageRegistryURL string
144+
containerRuntime string
145+
imageVariant string
146+
schedulerVolume *string
147+
schedulerOverrideBroadcastHostPort *string
147148
}
148149

149150
type daprImageInfo struct {
@@ -185,7 +186,7 @@ func isSchedulerIncluded(runtimeVersion string) (bool, error) {
185186
}
186187

187188
// Init installs Dapr on a local machine using the supplied runtimeVersion.
188-
func Init(runtimeVersion, dashboardVersion string, dockerNetwork string, slimMode bool, imageRegistryURL string, fromDir string, containerRuntime string, imageVariant string, daprInstallPath string, schedulerVolume *string) error {
189+
func Init(runtimeVersion, dashboardVersion string, dockerNetwork string, slimMode bool, imageRegistryURL string, fromDir string, containerRuntime string, imageVariant string, daprInstallPath string, schedulerVolume *string, schedulerOverrideBroadcastHostPort *string) error {
189190
var err error
190191
var bundleDet bundleDetails
191192
containerRuntime = strings.TrimSpace(containerRuntime)
@@ -296,17 +297,18 @@ func Init(runtimeVersion, dashboardVersion string, dockerNetwork string, slimMod
296297

297298
info := initInfo{
298299
// values in bundleDet can be nil if fromDir is empty, so must be used in conjunction with fromDir.
299-
bundleDet: &bundleDet,
300-
fromDir: fromDir,
301-
installDir: installDir,
302-
slimMode: slimMode,
303-
runtimeVersion: runtimeVersion,
304-
dashboardVersion: dashboardVersion,
305-
dockerNetwork: dockerNetwork,
306-
imageRegistryURL: imageRegistryURL,
307-
containerRuntime: containerRuntime,
308-
imageVariant: imageVariant,
309-
schedulerVolume: schedulerVolume,
300+
bundleDet: &bundleDet,
301+
fromDir: fromDir,
302+
installDir: installDir,
303+
slimMode: slimMode,
304+
runtimeVersion: runtimeVersion,
305+
dashboardVersion: dashboardVersion,
306+
dockerNetwork: dockerNetwork,
307+
imageRegistryURL: imageRegistryURL,
308+
containerRuntime: containerRuntime,
309+
imageVariant: imageVariant,
310+
schedulerVolume: schedulerVolume,
311+
schedulerOverrideBroadcastHostPort: schedulerOverrideBroadcastHostPort,
310312
}
311313
for _, step := range initSteps {
312314
// Run init on the configurations and containers.
@@ -684,7 +686,11 @@ func runSchedulerService(wg *sync.WaitGroup, errorChan chan<- error, info initIn
684686
}
685687

686688
if schedulerOverrideHostPort(info) {
687-
args = append(args, fmt.Sprintf("--override-broadcast-host-port=localhost:%v", osPort))
689+
if info.schedulerOverrideBroadcastHostPort != nil {
690+
args = append(args, "--override-broadcast-host-port="+*info.schedulerOverrideBroadcastHostPort)
691+
} else {
692+
args = append(args, fmt.Sprintf("--override-broadcast-host-port=localhost:%v", osPort))
693+
}
688694
}
689695

690696
_, err = utils.RunCmdAndWait(runtimeCmd, args...)

pkg/standalone/standalone_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"github.com/stretchr/testify/assert"
2121

2222
"github.com/dapr/cli/utils"
23+
"github.com/dapr/kit/ptr"
2324
)
2425

2526
func TestStandaloneConfig(t *testing.T) {
@@ -325,7 +326,7 @@ func TestInitLogActualContainerRuntimeName(t *testing.T) {
325326
t.Skip("Skipping test as container runtime is available")
326327
}
327328

328-
err := Init(latestVersion, latestVersion, "", false, "", "", test.containerRuntime, "", "", nil)
329+
err := Init(latestVersion, latestVersion, "", false, "", "", test.containerRuntime, "", "", nil, ptr.Of("localhost:50006"))
329330
assert.Error(t, err)
330331
assert.Contains(t, err.Error(), test.containerRuntime)
331332
})

tests/e2e/standalone/init_test.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,26 @@ func TestStandaloneInit(t *testing.T) {
210210
verifyTCPLocalhost(t, schedulerPort)
211211
})
212212

213+
t.Run("init with custom scheduler host", func(t *testing.T) {
214+
if isSlimMode() {
215+
t.Skip("Skipping scheduler host test because of slim installation")
216+
}
217+
218+
// Ensure a clean environment
219+
must(t, cmdUninstall, "failed to uninstall Dapr")
220+
221+
customBroadcastHostPort := "192.168.42.42:50006"
222+
args := []string{
223+
"--scheduler-override-broadcast-host-port", customBroadcastHostPort,
224+
}
225+
output, err := cmdInit(args...)
226+
t.Log(output)
227+
require.NoError(t, err, "init failed")
228+
assert.Contains(t, output, "Success! Dapr is up and running.")
229+
230+
verifySchedulerBroadcastHostPort(t, customBroadcastHostPort)
231+
})
232+
213233
t.Run("init without runtime-version flag with mariner images", func(t *testing.T) {
214234
// Ensure a clean environment
215235
must(t, cmdUninstall, "failed to uninstall Dapr")
@@ -440,3 +460,17 @@ func verifyTCPLocalhost(t *testing.T, port int) {
440460
}
441461
}, time.Second*10, time.Millisecond*10)
442462
}
463+
464+
// verifySchedulerBroadcastHostPort verifies that the scheduler container was started with the correct broadcast host and port.
465+
func verifySchedulerBroadcastHostPort(t *testing.T, expectedBroadcastHostPort string) {
466+
t.Helper()
467+
468+
cli, err := dockerClient.NewClientWithOpts(dockerClient.FromEnv)
469+
require.NoError(t, err)
470+
471+
containerInfo, err := cli.ContainerInspect(context.Background(), "dapr_scheduler")
472+
require.NoError(t, err)
473+
474+
expectedArg := "--override-broadcast-host-port=" + expectedBroadcastHostPort
475+
assert.Contains(t, containerInfo.Args, expectedArg, "Expected scheduler argument %s not found in container args: %v", expectedArg, containerInfo.Args)
476+
}

0 commit comments

Comments
 (0)