Skip to content

Commit

Permalink
feat(host-network): 🚀 enable host network on request
Browse files Browse the repository at this point in the history
  • Loading branch information
tunahanertekin authored Sep 4, 2024
2 parents d1902a7 + bfef7b0 commit be7a945
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 8 deletions.
9 changes: 8 additions & 1 deletion internal/resources/v1alpha1/notebook.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,13 @@ func GetNotebookPod(notebook *robotv1alpha1.Notebook, podNamespacedName *types.N
if ports, ok := robot.Spec.AdditionalConfigs[internal.NOTEBOOK_CUSTOM_PORT_RANGE_KEY]; ok {
containerCfg.InjectCustomPortConfiguration(&nbContainer, ports)
}
// apply host network selection
useHostNetwork := false
if hostNetwork, ok := robot.Spec.AdditionalConfigs[internal.HOST_NETWORK_SELECTION_KEY]; ok {
if hostNetwork.Value == "true" {
useHostNetwork = true
}
}

nbPod := corev1.Pod{
ObjectMeta: metav1.ObjectMeta{
Expand All @@ -90,7 +97,7 @@ func GetNotebookPod(notebook *robotv1alpha1.Notebook, podNamespacedName *types.N
Labels: labels,
},
Spec: corev1.PodSpec{
// HostNetwork: notebook.Spec.Privileged,
HostNetwork: useHostNetwork,
Containers: []corev1.Container{
nbContainer,
},
Expand Down
10 changes: 9 additions & 1 deletion internal/resources/v1alpha1/robot_ide.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,14 +82,22 @@ func GetRobotIDEPod(robotIDE *robotv1alpha1.RobotIDE, podNamespacedName *types.N
containerCfg.InjectCustomPortConfiguration(&ideContainer, ports)
}

// apply host network selection
useHostNetwork := false
if hostNetwork, ok := robot.Spec.AdditionalConfigs[internal.HOST_NETWORK_SELECTION_KEY]; ok {
if hostNetwork.Value == "true" {
useHostNetwork = true
}
}

idePod := corev1.Pod{
ObjectMeta: metav1.ObjectMeta{
Name: podNamespacedName.Name,
Namespace: podNamespacedName.Namespace,
Labels: labels,
},
Spec: corev1.PodSpec{
// HostNetwork: robotIDE.Spec.Privileged,
HostNetwork: useHostNetwork,
Containers: []corev1.Container{
ideContainer,
},
Expand Down
9 changes: 8 additions & 1 deletion internal/resources/v1alpha1/robot_vdi.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,13 @@ func GetRobotVDIPod(robotVDI *robotv1alpha1.RobotVDI, podNamespacedName *types.N
if ports, ok := robot.Spec.AdditionalConfigs[internal.VDI_CUSTOM_PORT_RANGE_KEY]; ok {
containerCfg.InjectCustomPortConfiguration(&vdiContainer, ports)
}
// apply host network selection
useHostNetwork := false
if hostNetwork, ok := robot.Spec.AdditionalConfigs[internal.HOST_NETWORK_SELECTION_KEY]; ok {
if hostNetwork.Value == "true" {
useHostNetwork = true
}
}

vdiPod := &corev1.Pod{
ObjectMeta: metav1.ObjectMeta{
Expand All @@ -149,7 +156,7 @@ func GetRobotVDIPod(robotVDI *robotv1alpha1.RobotVDI, podNamespacedName *types.N
Labels: labels,
},
Spec: corev1.PodSpec{
// HostNetwork: robotVDI.Spec.Privileged,
HostNetwork: useHostNetwork,
Containers: []corev1.Container{
vdiContainer,
},
Expand Down
12 changes: 7 additions & 5 deletions internal/shared.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,15 +176,17 @@ const (
VDI_CUSTOM_PORT_RANGE_KEY = "VDI_CUSTOM_PORT_RANGE"
NOTEBOOK_CUSTOM_PORT_RANGE_KEY = "NOTEBOOK_CUSTOM_PORT_RANGE"
SHARED_MEMORY_SIZE_KEY = "SHARED_MEMORY_SIZE"
HOST_NETWORK_SELECTION_KEY = "USE_HOST_NETWORK"
)

// regex
const (
GRANT_PERMISSION_REGEX = "^(/([A-Za-z0-9./_-])+:)*(/[A-Za-z0-9./_-]+)$"
PERSISTENT_DIRS_REGEX = "^(/([A-Za-z0-9./_-])+:)*(/[A-Za-z0-9./_-]+)$"
HOST_DIRS_REGEX = "^(((/[A-Za-z0-9./_-]+):(/[A-Za-z0-9./_-]+))+,)*(((/[A-Za-z0-9./_-]+):(/[A-Za-z0-9./_-]+))+)$"
CUSTOM_PORT_RANGE_REGEX = "^([a-z0-9]{4}-[0-9]{5}:[0-9]{2,5}/)*([a-z0-9]{4}-[0-9]{5}:[0-9]{2,5})$"
SHARED_MEMORY_SIZE_REGEX = "^([0-9]{1,2}Gi)$"
GRANT_PERMISSION_REGEX = "^(/([A-Za-z0-9./_-])+:)*(/[A-Za-z0-9./_-]+)$"
PERSISTENT_DIRS_REGEX = "^(/([A-Za-z0-9./_-])+:)*(/[A-Za-z0-9./_-]+)$"
HOST_DIRS_REGEX = "^(((/[A-Za-z0-9./_-]+):(/[A-Za-z0-9./_-]+))+,)*(((/[A-Za-z0-9./_-]+):(/[A-Za-z0-9./_-]+))+)$"
CUSTOM_PORT_RANGE_REGEX = "^([a-z0-9]{4}-[0-9]{5}:[0-9]{2,5}/)*([a-z0-9]{4}-[0-9]{5}:[0-9]{2,5})$"
SHARED_MEMORY_SIZE_REGEX = "^([0-9]{1,2}Gi)$"
HOST_NETWORK_SELECTION_REGEX = "^(true|false)$"
)

// file browser ports
Expand Down
10 changes: 10 additions & 0 deletions pkg/api/roboscale.io/v1alpha1/robot_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,16 @@ func (r *Robot) checkAdditionalConfigs() error {
}
}

if val, ok := r.Spec.AdditionalConfigs[internal.HOST_NETWORK_SELECTION_KEY]; ok && val.ConfigType == AdditionalConfigTypeOperator {
matched, err := regexp.MatchString(internal.HOST_NETWORK_SELECTION_REGEX, val.Value)
if !matched {
return errors.New("cannot set host network, use this pattern " + internal.HOST_NETWORK_SELECTION_REGEX)
}
if err != nil {
return err
}
}

return nil
}

Expand Down

0 comments on commit be7a945

Please sign in to comment.