From 03837190c2c46cd13ba25559ed5e41753b12084d Mon Sep 17 00:00:00 2001 From: Angel Misevski Date: Mon, 22 Nov 2021 12:51:27 -0500 Subject: [PATCH] Add support for node selectors and pod tolerations via attribute Signed-off-by: Angel Misevski --- pkg/constants/attributes.go | 12 ++++++++++++ pkg/provision/workspace/deployment.go | 16 ++++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/pkg/constants/attributes.go b/pkg/constants/attributes.go index 8405b62e7..444fa5b5d 100644 --- a/pkg/constants/attributes.go +++ b/pkg/constants/attributes.go @@ -17,10 +17,22 @@ package constants // Constants that are used in attributes on DevWorkspace elements (components, endpoints, etc.) const ( + // PodTolerationsAttribute defines tolerations that should be applied to the DevWorkspace's pod. Tolerations + // should be defined in the same format as in Kubernetes, e.g. + // - key: "example-key" + // operator: "Exists" + // effect: "NoSchedule" + PodTolerationsAttribute = "pod-tolerations" + + // NodeSelectorAttribute defines the node selector that should be used for the DevWorkspace's pod. Should be + // defined as a key-value map. + NodeSelectorAttribute = "node-selector" + // PluginSourceAttribute is an attribute added to components, commands, and projects in a flattened // DevWorkspace representation to signify where the respective component came from (i.e. which plugin // or parent imported it) PluginSourceAttribute = "controller.devfile.io/imported-by" + // EndpointURLAttribute is an attribute added to endpoints to denote the endpoint on the cluster that // was created to route to this endpoint EndpointURLAttribute = "controller.devfile.io/endpoint-url" diff --git a/pkg/provision/workspace/deployment.go b/pkg/provision/workspace/deployment.go index 71418df0f..3b63847bd 100644 --- a/pkg/provision/workspace/deployment.go +++ b/pkg/provision/workspace/deployment.go @@ -324,6 +324,22 @@ func getSpecDeployment( deployment.Spec.Template.Annotations = maputils.Append(deployment.Spec.Template.Annotations, constants.DevWorkspaceRestrictedAccessAnnotation, restrictedAccess) } + if workspace.Spec.Template.Attributes.Exists(constants.PodTolerationsAttribute) { + var tolerations []corev1.Toleration + if err := workspace.Spec.Template.Attributes.GetInto(constants.PodTolerationsAttribute, &tolerations); err != nil { + return nil, fmt.Errorf("failed to parse %s attribute: %w", constants.PodTolerationsAttribute, err) + } + deployment.Spec.Template.Spec.Tolerations = tolerations + } + + if workspace.Spec.Template.Attributes.Exists(constants.NodeSelectorAttribute) { + nodeSelector := map[string]string{} + if err := workspace.Spec.Template.Attributes.GetInto(constants.NodeSelectorAttribute, &nodeSelector); err != nil { + return nil, fmt.Errorf("failed to parse %s attribute: %w", constants.NodeSelectorAttribute, err) + } + deployment.Spec.Template.Spec.NodeSelector = nodeSelector + } + err = controllerutil.SetControllerReference(workspace, deployment, scheme) if err != nil { return nil, err