Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add hostpath mount feature for persist pvcs onto host machine #29

Merged
merged 1 commit into from
Mar 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions cmd/multikf/cmd_add.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ func NewAddCommand(logger log.Logger, ioStreams genericclioptions.IOStreams) *co
withLabels string // with labels
exportPorts string // export ports on hostmachine
forceOverwrite bool // force overwrite existing config
useLocalPath string // with localpath
)

ensureNoGPUForVagrant := func(vag machine.MachineCURDFactory, useGPUs int) error {
Expand Down Expand Up @@ -58,6 +59,7 @@ func NewAddCommand(logger log.Logger, ioStreams genericclioptions.IOStreams) *co
auditEnabled: withAudit,
workers: withWorkers,
nodeLabels: withLabels,
localPath: useLocalPath,
})
if err != nil {
return err
Expand Down Expand Up @@ -95,6 +97,7 @@ func NewAddCommand(logger log.Logger, ioStreams genericclioptions.IOStreams) *co
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)")
cmd.Flags().IntVar(&withWorkers, "with_workers", 0, "use workers (default: 0)")
cmd.Flags().StringVar(&withLabels, "with_labels", "", "attach labels, format: key1=value1,key2=value2(default: )")
cmd.Flags().StringVar(&useLocalPath, "use_localpath", "", "mount local path to kind cluster")

return cmd
}
5 changes: 5 additions & 0 deletions cmd/multikf/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ type machineConfig struct {
auditEnabled bool
workers int
nodeLabels string
localPath string
}

func (m machineConfig) GetCPUs() int {
Expand Down Expand Up @@ -154,6 +155,10 @@ func (m machineConfig) GetNodeLabels() []machine.NodeLabel {
return nodeLabels
}

func (m machineConfig) GetLocalPath() string {
return m.localPath
}

type kubeflowPlugin struct {
withKubeflowDefaultPassword string
kubeflowVersion plugins.TypePluginVersion
Expand Down
1 change: 1 addition & 0 deletions pkg/machine/docker/hostmachine.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ func (h *HostMachine) prepareFiles() error {
filepath.Join(h.hostMachineDir, "audit-policy.yaml"),
h.options.GetWorkers(),
h.options.GetNodeLabels(),
h.options.GetLocalPath(),
)

vfolder := NewHostFolder(h.hostMachineDir)
Expand Down
3 changes: 2 additions & 1 deletion pkg/machine/docker/template/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ type DockerHostmachineTemplateConfig struct {
*pkgtemplateconfig.DefaultTemplateConfig
}

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, nodeLabels []machine.NodeLabel) *DockerHostmachineTemplateConfig {
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, nodeLabels []machine.NodeLabel, localPath string) *DockerHostmachineTemplateConfig {
return &DockerHostmachineTemplateConfig{
DefaultTemplateConfig: pkgtemplateconfig.NewDefaultTemplateConfig(
name,
Expand All @@ -24,6 +24,7 @@ func NewDockerHostmachineTemplateConfig(name string, cpus int, memory int, sshpo
auditFileAbsolutePath,
workerCount,
nodeLabels,
localPath,
),
}
}
Expand Down
1 change: 1 addition & 0 deletions pkg/machine/machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ type MachineConfiger interface {
AuditEnabled() bool
GetWorkers() int
GetNodeLabels() []NodeLabel
GetLocalPath() string
}

type ExportPortPair struct {
Expand Down
3 changes: 2 additions & 1 deletion pkg/machine/vagrant/template/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ type VagrantTemplateConfig struct {
*pkgtemplateconfig.DefaultTemplateConfig
}

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, nodeLabels []machine.NodeLabel) *VagrantTemplateConfig {
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, nodeLabels []machine.NodeLabel, localPath string) *VagrantTemplateConfig {
return &VagrantTemplateConfig{
DefaultTemplateConfig: pkgtemplateconfig.NewDefaultTemplateConfig(
name,
Expand All @@ -24,6 +24,7 @@ func NewVagrantTemplateConfig(name string, cpus int, memory int, sshport int, ku
auditFileAbsolutePath,
workerCount,
nodeLabels,
localPath,
),
}
}
Expand Down
1 change: 1 addition & 0 deletions pkg/machine/vagrant/vagrant.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,7 @@ func (v *VagrantMachine) prepareFiles() error {
"/tmp/audit-policy.yaml", /*for vagrant, we will copy the file under /tmp and run local installation*/
v.options.GetWorkers(),
v.options.GetNodeLabels(),
v.options.GetLocalPath(),
)

vfolder := NewVagrantFolder(v.vagrantMachineDir)
Expand Down
13 changes: 10 additions & 3 deletions pkg/template/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,10 @@ type DefaultTemplateConfig struct {
auditFileAbsolutePath string
workerCount int
nodeLabels []machine.NodeLabel
localPath string
}

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, nodeLabels []machine.NodeLabel) *DefaultTemplateConfig {
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, nodeLabels []machine.NodeLabel, localPath string) *DefaultTemplateConfig {
return &DefaultTemplateConfig{
name: name,
cpus: cpus,
Expand All @@ -40,6 +41,7 @@ func NewDefaultTemplateConfig(name string, cpus int, memory int, sshport int, ku
auditFileAbsolutePath: auditFileAbsolutePath,
workerCount: workerCount,
nodeLabels: nodeLabels,
localPath: localPath,
}
}

Expand Down Expand Up @@ -87,8 +89,9 @@ func (t *DefaultTemplateConfig) GetWorkers() []template.Worker {
ids := make([]template.Worker, t.workerCount, t.workerCount)
for i := 0; i < t.workerCount; i++ {
ids[i] = template.Worker{
Id: fmt.Sprintf("%d", i),
UseGPU: t.GetGPUs() > 0,
Id: fmt.Sprintf("%d", i),
UseGPU: t.GetGPUs() > 0,
LocalPath: t.LocalPath(),
}
}
return ids
Expand All @@ -97,3 +100,7 @@ func (t *DefaultTemplateConfig) GetWorkers() []template.Worker {
func (t *DefaultTemplateConfig) GetNodeLabels() []machine.NodeLabel {
return t.nodeLabels
}

func (t *DefaultTemplateConfig) LocalPath() string {
return t.localPath
}
17 changes: 15 additions & 2 deletions pkg/template/kind_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ type KindConfiger interface {
AuditEnabler
WorkersGetter
NodeLabelsGetter
LocalPathGetter
}

func (k *KindFileTemplate) Populate(v interface{}) error {
Expand All @@ -52,6 +53,7 @@ func (k *KindFileTemplate) Populate(v interface{}) error {
k.ExportPorts = c.GetExportPorts()
k.AuditEnabled = c.AuditEnabled()
k.AuditFileAbsolutePath = c.AuditFileAbsolutePath()
k.LocalPath = c.LocalPath()
k.Workers = c.GetWorkers()

nodeLabels := c.GetNodeLabels()
Expand All @@ -72,6 +74,7 @@ type KindFileTemplate struct {
ExportPorts []machine.ExportPortPair
AuditEnabled bool
AuditFileAbsolutePath string
LocalPath string
Workers []Worker
NodeLabels []string
}
Expand Down Expand Up @@ -123,17 +126,27 @@ nodes:
hostPort: {{ $p.HostPort }}
protocol: TCP
{{- end}}
{{- if .AuditEnabled}}
# mount the local file on the control plane
{{- if or .AuditEnabled .LocalPath}}
extraMounts:
{{- if or .AuditEnabled }}
- hostPath: {{.AuditFileAbsolutePath}}
containerPath: /etc/kubernetes/policies/audit-policy.yaml
readOnly: true
{{- end}}
{{- if ne .LocalPath ""}}
- hostPath: {{.LocalPath}}
containerPath: /var/local-path-provisioner
{{- end}}
{{- end}}
{{- range .Workers }}
- role: worker
image: kindest/node:v1.23.12@sha256:9402cf1330bbd3a0d097d2033fa489b2abe40d479cc5ef47d0b6a6960613148a
gpus: {{ .UseGPU}}
{{- if ne .LocalPath ""}}
extraMounts:
- hostPath: {{.LocalPath}}
containerPath: /var/local-path-provisioner
{{- end}}
{{- end}}
networking:
apiServerAddress: {{.KubeAPIIP}}
Expand Down
36 changes: 29 additions & 7 deletions pkg/template/kind_template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,19 +38,26 @@ func (s staticConfig) AuditFileAbsolutePath() string {
return ""
}

func (s staticConfig) LocalPath() string {
return "/mnt/test"
}

func (s staticConfig) GetWorkers() []Worker {
return []Worker{
Worker{
Id: "1",
UseGPU: true,
Id: "1",
UseGPU: true,
LocalPath: s.LocalPath(),
},
Worker{
Id: "2",
UseGPU: true,
Id: "2",
UseGPU: true,
LocalPath: s.LocalPath(),
},
Worker{
Id: "3",
UseGPU: true,
Id: "3",
UseGPU: true,
LocalPath: s.LocalPath(),
},
}
}
Expand Down Expand Up @@ -112,15 +119,27 @@ nodes:
- containerPort: 8083
hostPort: 443
protocol: TCP
extraMounts:
- hostPath: /mnt/test
containerPath: /var/local-path-provisioner
- role: worker
image: kindest/node:v1.23.12@sha256:9402cf1330bbd3a0d097d2033fa489b2abe40d479cc5ef47d0b6a6960613148a
gpus: true
extraMounts:
- hostPath: /mnt/test
containerPath: /var/local-path-provisioner
- role: worker
image: kindest/node:v1.23.12@sha256:9402cf1330bbd3a0d097d2033fa489b2abe40d479cc5ef47d0b6a6960613148a
gpus: true
extraMounts:
- hostPath: /mnt/test
containerPath: /var/local-path-provisioner
- role: worker
image: kindest/node:v1.23.12@sha256:9402cf1330bbd3a0d097d2033fa489b2abe40d479cc5ef47d0b6a6960613148a
gpus: true
extraMounts:
- hostPath: /mnt/test
containerPath: /var/local-path-provisioner
networking:
apiServerAddress: 1.2.3.4
apiServerPort: 8443
Expand Down Expand Up @@ -172,6 +191,10 @@ func (s auditConfig) GetNodeLabels() []machine.NodeLabel {
return []machine.NodeLabel{}
}

func (s auditConfig) LocalPath() string {
return ""
}

func TestKindTemplateWithAudit(t *testing.T) {
kt := NewKindTemplate()
assert.NoError(t, kt.Populate(auditConfig{}))
Expand Down Expand Up @@ -220,7 +243,6 @@ nodes:
- containerPort: 8081
hostPort: 80
protocol: TCP
# mount the local file on the control plane
extraMounts:
- hostPath: foo.bar.yaml
containerPath: /etc/kubernetes/policies/audit-policy.yaml
Expand Down
9 changes: 7 additions & 2 deletions pkg/template/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,15 @@ type WorkersGetter interface {
}

type Worker struct {
Id string
UseGPU bool
Id string
UseGPU bool
LocalPath string
}

type NodeLabelsGetter interface {
GetNodeLabels() []machine.NodeLabel
}

type LocalPathGetter interface {
LocalPath() string
}