Skip to content

Commit

Permalink
feat: Add nodeSelector and tolerations to RolloutSpec
Browse files Browse the repository at this point in the history
Signed-off-by: Jan Lauber <jan.lauber@protonmail.ch>
  • Loading branch information
janlauber committed May 22, 2024
1 parent 6d5ed58 commit 73c8f1a
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 0 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,13 @@ spec:
args: ["nginx", "-g", "daemon off;"]
command: ["nginx"]
rolloutStrategy: rollingUpdate # or "recreate"
nodeSelector:
kubernetes.io/hostname: minikube
tolerations:
- key: "storage"
operator: "Equal"
value: "ssd"
effect: "NoSchedule"
image:
registry: "docker.io"
repository: "nginx"
Expand Down
3 changes: 3 additions & 0 deletions api/v1alpha1/rollout_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
package v1alpha1

import (
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

Expand Down Expand Up @@ -127,6 +128,8 @@ type RolloutSpec struct {
Interfaces []InterfaceSpec `json:"interfaces,omitempty"`
ServiceAccountName string `json:"serviceAccountName"`
CronJobs []CronJobSpec `json:"cronjobs,omitempty"`
NodeSelector map[string]string `json:"nodeSelector,omitempty"`
Tolerations []corev1.Toleration `json:"tolerations,omitempty"`
}

type Resources struct {
Expand Down
15 changes: 15 additions & 0 deletions api/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

43 changes: 43 additions & 0 deletions config/crd/bases/one-click.dev_rollouts.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,10 @@ spec:
- port
type: object
type: array
nodeSelector:
additionalProperties:
type: string
type: object
resources:
properties:
limits:
Expand Down Expand Up @@ -322,6 +326,45 @@ spec:
type: object
serviceAccountName:
type: string
tolerations:
items:
description: The pod this Toleration is attached to tolerates any
taint that matches the triple <key,value,effect> using the matching
operator <operator>.
properties:
effect:
description: Effect indicates the taint effect to match. Empty
means match all taint effects. When specified, allowed values
are NoSchedule, PreferNoSchedule and NoExecute.
type: string
key:
description: Key is the taint key that the toleration applies
to. Empty means match all taint keys. If the key is empty,
operator must be Exists; this combination means to match all
values and all keys.
type: string
operator:
description: Operator represents a key's relationship to the
value. Valid operators are Exists and Equal. Defaults to Equal.
Exists is equivalent to wildcard for value, so that a pod
can tolerate all taints of a particular category.
type: string
tolerationSeconds:
description: TolerationSeconds represents the period of time
the toleration (which must be of effect NoExecute, otherwise
this field is ignored) tolerates the taint. By default, it
is not set, which means tolerate the taint forever (do not
evict). Zero and negative values will be treated as 0 (evict
immediately) by the system.
format: int64
type: integer
value:
description: Value is the taint value the toleration matches
to. If the operator is Exists, the value should be empty,
otherwise just a regular string.
type: string
type: object
type: array
volumes:
items:
properties:
Expand Down
26 changes: 26 additions & 0 deletions controllers/deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,8 @@ func (r *RolloutReconciler) deploymentForRollout(ctx context.Context, f *oneclic
}},
ServiceAccountName: f.Spec.ServiceAccountName,
ImagePullSecrets: imagePullSecrets,
NodeSelector: f.Spec.NodeSelector,
Tolerations: getTolerations(f.Spec.Tolerations),
},
},
Strategy: strategy,
Expand Down Expand Up @@ -313,11 +315,35 @@ func needsUpdate(current *appsv1.Deployment, f *oneclickiov1alpha1.Rollout) bool
return true
}

// Check node selector
if !reflect.DeepEqual(current.Spec.Template.Spec.NodeSelector, f.Spec.NodeSelector) {
return true
}

// Check tolerations
if !reflect.DeepEqual(current.Spec.Template.Spec.Tolerations, getTolerations(f.Spec.Tolerations)) {
return true
}

// Add more checks as necessary, e.g., labels, annotations, specific configuration, etc.

return false
}

func getTolerations(tolerations []corev1.Toleration) []corev1.Toleration {
var result []corev1.Toleration
for _, t := range tolerations {
result = append(result, corev1.Toleration{
Key: t.Key,
Operator: corev1.TolerationOperator(t.Operator),
Value: t.Value,
Effect: corev1.TaintEffect(t.Effect),
TolerationSeconds: t.TolerationSeconds,
})
}
return result
}

func volumesMatch(currentVolumes []corev1.Volume, desiredVolumes []oneclickiov1alpha1.VolumeSpec, f *oneclickiov1alpha1.Rollout) bool {
if len(currentVolumes) != len(desiredVolumes) {
return false
Expand Down

0 comments on commit 73c8f1a

Please sign in to comment.