Skip to content

Commit

Permalink
[kjobctl] Add interactive builder (#2480)
Browse files Browse the repository at this point in the history
* Add interactive builder

* Avoid overwriting volumes, volumeMounts and envVars

* Refactor pod spec build

* Set command and requests only to first primary container

* Fix tests
  • Loading branch information
IrvingMg authored Jul 2, 2024
1 parent 8f3bfcb commit 650fb68
Show file tree
Hide file tree
Showing 9 changed files with 527 additions and 78 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ type SupportedMode struct {
// requiredFlags point which cli flags are required to be passed in order to fill the gaps in the templates.
// Possible values are cmd, parallelism, completions, request, localqueue.
//
// cmd and requests values are going to be added only to the first primary container.
//
// +optional
// +listType=set
RequiredFlags []Flag `json:"requiredFlags,omitempty"`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ spec:
description: |-
requiredFlags point which cli flags are required to be passed in order to fill the gaps in the templates.
Possible values are cmd, parallelism, completions, request, localqueue.
cmd and requests values are going to be added only to the first primary container.
items:
enum:
- cmd
Expand Down
45 changes: 44 additions & 1 deletion cmd/experimental/kjobctl/pkg/builder/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,8 +197,11 @@ func (b *Builder) Do(ctx context.Context) (runtime.Object, error) {

var bImpl builder

if b.modeName == v1alpha1.JobMode {
switch b.modeName {
case v1alpha1.JobMode:
bImpl = newJobBuilder(b)
case v1alpha1.InteractiveMode:
bImpl = newInteractiveBuilder(b)
}

if bImpl == nil {
Expand All @@ -215,3 +218,43 @@ func (b *Builder) Do(ctx context.Context) (runtime.Object, error) {

return bImpl.build(ctx)
}

func (b *Builder) buildPodSpec(templateSpec corev1.PodSpec) corev1.PodSpec {
bundle := mergeBundles(b.volumeBundles)

templateSpec.Volumes = append(templateSpec.Volumes, bundle.Spec.Volumes...)
for i := range templateSpec.Containers {
container := &templateSpec.Containers[i]

if i == 0 && len(b.command) > 0 {
container.Command = b.command
}

if i == 0 && len(b.requests) > 0 {
container.Resources.Requests = b.requests
}

container.VolumeMounts = append(container.VolumeMounts, bundle.Spec.ContainerVolumeMounts...)
container.Env = append(container.Env, bundle.Spec.EnvVars...)
}

for i := range templateSpec.InitContainers {
initContainer := &templateSpec.InitContainers[i]

initContainer.VolumeMounts = append(initContainer.VolumeMounts, bundle.Spec.ContainerVolumeMounts...)
initContainer.Env = append(initContainer.Env, bundle.Spec.EnvVars...)
}

return templateSpec
}

func mergeBundles(bundles []v1alpha1.VolumeBundle) v1alpha1.VolumeBundle {
var volumeBundle v1alpha1.VolumeBundle
for _, b := range bundles {
volumeBundle.Spec.Volumes = append(volumeBundle.Spec.Volumes, b.Spec.Volumes...)
volumeBundle.Spec.ContainerVolumeMounts = append(volumeBundle.Spec.ContainerVolumeMounts, b.Spec.ContainerVolumeMounts...)
volumeBundle.Spec.EnvVars = append(volumeBundle.Spec.EnvVars, b.Spec.EnvVars...)
}

return volumeBundle
}
70 changes: 70 additions & 0 deletions cmd/experimental/kjobctl/pkg/builder/interactive_builder.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
Copyright 2024 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package builder

import (
"context"

corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"

"sigs.k8s.io/kueue/cmd/experimental/kjobctl/pkg/constants"
kueueconstants "sigs.k8s.io/kueue/pkg/controller/constants"
)

type interactiveBuilder struct {
*Builder
}

var _ builder = (*interactiveBuilder)(nil)

func (b *interactiveBuilder) build(ctx context.Context) (runtime.Object, error) {
template, err := b.k8sClientset.CoreV1().PodTemplates(b.profile.Namespace).Get(ctx, string(b.mode.Template), metav1.GetOptions{})
if err != nil {
return nil, err
}

pod := &corev1.Pod{
TypeMeta: metav1.TypeMeta{
Kind: "Pod",
APIVersion: "v1",
},
ObjectMeta: metav1.ObjectMeta{
Namespace: b.profile.Namespace,
GenerateName: b.profile.Name + "-",
Labels: map[string]string{},
},
Spec: template.Template.Spec,
}

if b.profile != nil {
pod.Labels[constants.ProfileLabel] = b.profile.Name
}

pod.Spec = b.buildPodSpec(pod.Spec)

if len(b.localQueue) > 0 {
pod.ObjectMeta.Labels[kueueconstants.QueueLabel] = b.localQueue
}

return pod, nil
}

func newInteractiveBuilder(b *Builder) *interactiveBuilder {
return &interactiveBuilder{Builder: b}
}
Loading

0 comments on commit 650fb68

Please sign in to comment.