Skip to content

Commit 84f3451

Browse files
authored
feat: add multiple node support with --with_workers=N (#26)
1 parent 4a2c923 commit 84f3451

File tree

11 files changed

+67
-10
lines changed

11 files changed

+67
-10
lines changed

cmd/multikf/cmd_add.go

+3
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ func NewAddCommand(logger log.Logger, ioStreams genericclioptions.IOStreams) *co
2222
withKubeflowDefaultPassword string // with kubeflow defaultpassword
2323
withIP string // with specific IP
2424
withAudit bool // with audit enabled
25+
withWorkers int // with workers
2526
exportPorts string // export ports on hostmachine
2627
forceOverwrite bool // force overwrite existing config
2728
)
@@ -54,6 +55,7 @@ func NewAddCommand(logger log.Logger, ioStreams genericclioptions.IOStreams) *co
5455
exportPorts: exportPorts,
5556
forceOverwrite: forceOverwrite,
5657
auditEnabled: withAudit,
58+
workers: withWorkers,
5759
})
5860
if err != nil {
5961
return err
@@ -89,6 +91,7 @@ func NewAddCommand(logger log.Logger, ioStreams genericclioptions.IOStreams) *co
8991
cmd.Flags().IntVar(&useGPUs, "use_gpus", 0, "use gpu resources (default: 0), possible value (0 or 1)")
9092
cmd.Flags().StringVar(&withIP, "with_ip", "0.0.0.0", "with a specific ip address for kubeapi (default: 0.0.0.0)")
9193
cmd.Flags().StringVar(&exportPorts, "export_ports", "", "export ports to host, delimited by comma(example: 8443:443 stands for mapping host port 8443 to container port 443)")
94+
cmd.Flags().IntVar(&withWorkers, "with_workers", 0, "use workers (default: 0)")
9295

9396
return cmd
9497
}

cmd/multikf/helper.go

+9
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,10 @@ func newMachineFactoryWithProvisioner(p machine.Provisioner, logger log.Logger)
5858
return vag, nil
5959
}
6060

61+
var (
62+
_ machine.MachineConfiger = &machineConfig{}
63+
)
64+
6165
type machineConfig struct {
6266
logger log.Logger
6367
cpus int
@@ -68,6 +72,7 @@ type machineConfig struct {
6872
defaultPassword string
6973
forceOverwrite bool
7074
auditEnabled bool
75+
workers int
7176
}
7277

7378
func (m machineConfig) GetCPUs() int {
@@ -124,6 +129,10 @@ func (m machineConfig) GetForceOverwriteConfig() bool {
124129
return m.forceOverwrite
125130
}
126131

132+
func (m machineConfig) GetWorkers() int {
133+
return m.workers
134+
}
135+
127136
type kubeflowPlugin struct {
128137
withKubeflowDefaultPassword string
129138
kubeflowVersion plugins.TypePluginVersion

pkg/machine/docker/hostmachine.go

+1
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ func (h *HostMachine) prepareFiles() error {
116116
h.options.GetExportPorts(),
117117
h.options.AuditEnabled(),
118118
filepath.Join(h.hostMachineDir, "audit-policy.yaml"),
119+
h.options.GetWorkers(),
119120
)
120121

121122
vfolder := NewHostFolder(h.hostMachineDir)

pkg/machine/docker/template/config.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ type DockerHostmachineTemplateConfig struct {
99
*pkgtemplateconfig.DefaultTemplateConfig
1010
}
1111

12-
func NewDockerHostmachineTemplateConfig(name string, cpus int, memory int, sshport int, kubeApiPort int, kubeApiIP string, gpus int, exportPorts []machine.ExportPortPair, auditEnabled bool, auditFileAbsolutePath string) *DockerHostmachineTemplateConfig {
12+
func NewDockerHostmachineTemplateConfig(name string, cpus int, memory int, sshport int, kubeApiPort int, kubeApiIP string, gpus int, exportPorts []machine.ExportPortPair, auditEnabled bool, auditFileAbsolutePath string, workerCount int) *DockerHostmachineTemplateConfig {
1313
return &DockerHostmachineTemplateConfig{
1414
DefaultTemplateConfig: pkgtemplateconfig.NewDefaultTemplateConfig(
1515
name,
@@ -22,6 +22,7 @@ func NewDockerHostmachineTemplateConfig(name string, cpus int, memory int, sshpo
2222
exportPorts,
2323
auditEnabled,
2424
auditFileAbsolutePath,
25+
workerCount,
2526
),
2627
}
2728
}

pkg/machine/machine.go

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ type MachineConfiger interface {
1616
GetExportPorts() []ExportPortPair
1717
GetForceOverwriteConfig() bool
1818
AuditEnabled() bool
19+
GetWorkers() int
1920
}
2021

2122
type ExportPortPair struct {

pkg/machine/vagrant/template/config.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ type VagrantTemplateConfig struct {
99
*pkgtemplateconfig.DefaultTemplateConfig
1010
}
1111

12-
func NewVagrantTemplateConfig(name string, cpus int, memory int, sshport int, kubeApiPort int, kubeApiIP string, gpus int, exportPorts []machine.ExportPortPair, auditEnabled bool, auditFileAbsolutePath string) *VagrantTemplateConfig {
12+
func NewVagrantTemplateConfig(name string, cpus int, memory int, sshport int, kubeApiPort int, kubeApiIP string, gpus int, exportPorts []machine.ExportPortPair, auditEnabled bool, auditFileAbsolutePath string, workerCount int) *VagrantTemplateConfig {
1313
return &VagrantTemplateConfig{
1414
DefaultTemplateConfig: pkgtemplateconfig.NewDefaultTemplateConfig(
1515
name,
@@ -22,6 +22,7 @@ func NewVagrantTemplateConfig(name string, cpus int, memory int, sshport int, ku
2222
exportPorts,
2323
auditEnabled,
2424
auditFileAbsolutePath,
25+
workerCount,
2526
),
2627
}
2728
}

pkg/machine/vagrant/vagrant.go

+1
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,7 @@ func (v *VagrantMachine) prepareFiles() error {
206206
v.options.GetExportPorts(),
207207
v.options.AuditEnabled(),
208208
"/tmp/audit-policy.yaml", /*for vagrant, we will copy the file under /tmp and run local installation*/
209+
v.options.GetWorkers(),
209210
)
210211

211212
vfolder := NewVagrantFolder(v.vagrantMachineDir)

pkg/template/config/config.go

+19-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
package config
22

3-
import "github.com/footprintai/multikf/pkg/machine"
3+
import (
4+
"github.com/footprintai/multikf/pkg/machine"
5+
"github.com/footprintai/multikf/pkg/template"
6+
)
7+
8+
var (
9+
_ template.KindConfiger = &DefaultTemplateConfig{}
10+
)
411

512
type DefaultTemplateConfig struct {
613
name string
@@ -13,9 +20,10 @@ type DefaultTemplateConfig struct {
1320
exportPorts []machine.ExportPortPair
1421
auditEnabled bool
1522
auditFileAbsolutePath string
23+
workerCount int
1624
}
1725

18-
func NewDefaultTemplateConfig(name string, cpus int, memory int, sshport int, kubeApiPort int, kubeApiIP string, gpus int, exportPorts []machine.ExportPortPair, auditEnabled bool, auditFileAbsolutePath string) *DefaultTemplateConfig {
26+
func NewDefaultTemplateConfig(name string, cpus int, memory int, sshport int, kubeApiPort int, kubeApiIP string, gpus int, exportPorts []machine.ExportPortPair, auditEnabled bool, auditFileAbsolutePath string, workerCount int) *DefaultTemplateConfig {
1927
return &DefaultTemplateConfig{
2028
name: name,
2129
cpus: cpus,
@@ -27,6 +35,7 @@ func NewDefaultTemplateConfig(name string, cpus int, memory int, sshport int, ku
2735
exportPorts: exportPorts,
2836
auditEnabled: auditEnabled,
2937
auditFileAbsolutePath: auditFileAbsolutePath,
38+
workerCount: workerCount,
3039
}
3140
}
3241

@@ -69,3 +78,11 @@ func (t *DefaultTemplateConfig) AuditEnabled() bool {
6978
func (t *DefaultTemplateConfig) AuditFileAbsolutePath() string {
7079
return t.auditFileAbsolutePath
7180
}
81+
82+
func (t *DefaultTemplateConfig) GetWorkerIDs() []int {
83+
ids := make([]int, t.workerCount, t.workerCount)
84+
for i := 0; i < t.workerCount; i++ {
85+
ids[i] = i
86+
}
87+
return ids
88+
}

pkg/template/kind_template.go

+10-3
Original file line numberDiff line numberDiff line change
@@ -29,27 +29,29 @@ func (k *KindFileTemplate) Execute(w io.Writer) error {
2929
return nil
3030
}
3131

32-
type kindConfig interface {
32+
type KindConfiger interface {
3333
NameGetter
3434
KubeAPIPortGetter
3535
KubeAPIIPGetter
3636
GpuGetter
3737
ExportPortsGetter
3838
AuditEnabler
39+
WorkerIDsGetter
3940
}
4041

4142
func (k *KindFileTemplate) Populate(v interface{}) error {
42-
if _, isKindConfiger := v.(kindConfig); !isKindConfiger {
43+
if _, isKindConfiger := v.(KindConfiger); !isKindConfiger {
4344
return fmt.Errorf("not implements kindConfig interface")
4445
}
45-
c := v.(kindConfig)
46+
c := v.(KindConfiger)
4647
k.Name = c.GetName()
4748
k.KubeAPIPort = c.GetKubeAPIPort()
4849
k.KubeAPIIP = c.GetKubeAPIIP()
4950
k.UseGPU = c.GetGPUs() > 0
5051
k.ExportPorts = c.GetExportPorts()
5152
k.AuditEnabled = c.AuditEnabled()
5253
k.AuditFileAbsolutePath = c.AuditFileAbsolutePath()
54+
k.WorkerIDs = c.GetWorkerIDs()
5355

5456
return nil
5557
}
@@ -63,6 +65,7 @@ type KindFileTemplate struct {
6365
ExportPorts []machine.ExportPortPair
6466
AuditEnabled bool
6567
AuditFileAbsolutePath string
68+
WorkerIDs []int
6669
}
6770

6871
var kindDefaultFileTemplate string = `
@@ -116,6 +119,10 @@ nodes:
116119
containerPath: /etc/kubernetes/policies/audit-policy.yaml
117120
readOnly: true
118121
{{- end}}
122+
{{- range .WorkerIDs }}
123+
- role: worker
124+
image: kindest/node:v1.23.12@sha256:9402cf1330bbd3a0d097d2033fa489b2abe40d479cc5ef47d0b6a6960613148a
125+
{{- end}}
119126
networking:
120127
apiServerAddress: {{.KubeAPIIP}}
121128
apiServerPort: {{.KubeAPIPort}}

pkg/template/kind_template_test.go

+15-3
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ func (s staticConfig) AuditFileAbsolutePath() string {
3434
return ""
3535
}
3636

37+
func (s staticConfig) GetWorkerIDs() []int {
38+
return []int{1, 2, 3}
39+
}
40+
3741
func (s staticConfig) GetExportPorts() []machine.ExportPortPair {
3842
return []machine.ExportPortPair{
3943
machine.ExportPortPair{
@@ -67,7 +71,6 @@ nodes:
6771
nodeRegistration:
6872
kubeletExtraArgs:
6973
node-labels: "ingress-ready=true"
70-
# image: footprintai/kind-node:v1.21.9
7174
image: kindest/node:v1.23.12@sha256:9402cf1330bbd3a0d097d2033fa489b2abe40d479cc5ef47d0b6a6960613148a
7275
gpus: true
7376
extraPortMappings:
@@ -77,6 +80,12 @@ nodes:
7780
- containerPort: 8083
7881
hostPort: 443
7982
protocol: TCP
83+
- role: worker
84+
image: kindest/node:v1.23.12@sha256:9402cf1330bbd3a0d097d2033fa489b2abe40d479cc5ef47d0b6a6960613148a
85+
- role: worker
86+
image: kindest/node:v1.23.12@sha256:9402cf1330bbd3a0d097d2033fa489b2abe40d479cc5ef47d0b6a6960613148a
87+
- role: worker
88+
image: kindest/node:v1.23.12@sha256:9402cf1330bbd3a0d097d2033fa489b2abe40d479cc5ef47d0b6a6960613148a
8089
networking:
8190
apiServerAddress: 1.2.3.4
8291
apiServerPort: 8443
@@ -117,6 +126,10 @@ func (s auditConfig) AuditFileAbsolutePath() string {
117126
return "foo.bar.yaml"
118127
}
119128

129+
func (s auditConfig) GetWorkerIDs() []int {
130+
return []int{}
131+
}
132+
120133
func TestKindTemplateWithAudit(t *testing.T) {
121134
kt := NewKindTemplate()
122135
assert.NoError(t, kt.Populate(auditConfig{}))
@@ -159,8 +172,7 @@ nodes:
159172
nodeRegistration:
160173
kubeletExtraArgs:
161174
node-labels: "ingress-ready=true"
162-
# image: footprintai/kind-node:v1.21.9
163-
image: kindest/node:v1.21.14
175+
image: kindest/node:v1.23.12@sha256:9402cf1330bbd3a0d097d2033fa489b2abe40d479cc5ef47d0b6a6960613148a
164176
gpus: false
165177
extraPortMappings:
166178
- containerPort: 8081

pkg/template/template.go

+4
Original file line numberDiff line numberDiff line change
@@ -49,3 +49,7 @@ type AuditEnabler interface {
4949
AuditEnabled() bool
5050
AuditFileAbsolutePath() string
5151
}
52+
53+
type WorkerIDsGetter interface {
54+
GetWorkerIDs() []int
55+
}

0 commit comments

Comments
 (0)