Skip to content

Commit

Permalink
tuning spec update change
Browse files Browse the repository at this point in the history
Signed-off-by: Bangqi Zhu <bangqizhu@microsoft.com>
  • Loading branch information
Bangqi Zhu committed Aug 27, 2024
1 parent bf5bc95 commit 2688875
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 96 deletions.
40 changes: 4 additions & 36 deletions api/v1alpha1/workspace_validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ package v1alpha1
import (
"context"
"fmt"
"github.com/azure/kaito/pkg/utils/consts"
"reflect"
"regexp"
"sort"
"strconv"
"strings"

"github.com/azure/kaito/pkg/utils/consts"

"github.com/azure/kaito/pkg/utils"
"github.com/azure/kaito/pkg/utils/plugin"

Expand Down Expand Up @@ -183,7 +183,7 @@ func (r *TuningSpec) validateUpdate(old *TuningSpec) (errs *apis.FieldError) {
if r.Output == nil {
errs = errs.Also(apis.ErrMissingField("Output"))
} else {
errs = errs.Also(r.Output.validateUpdate(old.Output).ViaField("Output"))
errs = errs.Also(r.Output.validateUpdate().ViaField("Output"))
}
if !reflect.DeepEqual(old.Preset, r.Preset) {
errs = errs.Also(apis.ErrGeneric("Preset cannot be changed", "Preset"))
Expand Down Expand Up @@ -235,33 +235,7 @@ func (r *DataSource) validateUpdate(old *DataSource, isTuning bool) (errs *apis.
if r.Volume != nil {
errs = errs.Also(apis.ErrInvalidValue("Volume support is not implemented yet", "Volume"))
}
oldURLs := make([]string, len(old.URLs))
copy(oldURLs, old.URLs)
sort.Strings(oldURLs)

newURLs := make([]string, len(r.URLs))
copy(newURLs, r.URLs)
sort.Strings(newURLs)

if !reflect.DeepEqual(oldURLs, newURLs) {
errs = errs.Also(apis.ErrInvalidValue("URLs field cannot be changed once set", "URLs"))
}
// TODO: check if the Volume is changed
if old.Image != r.Image {
errs = errs.Also(apis.ErrInvalidValue("Image field cannot be changed once set", "Image"))
}

oldSecrets := make([]string, len(old.ImagePullSecrets))
copy(oldSecrets, old.ImagePullSecrets)
sort.Strings(oldSecrets)

newSecrets := make([]string, len(r.ImagePullSecrets))
copy(newSecrets, r.ImagePullSecrets)
sort.Strings(newSecrets)

if !reflect.DeepEqual(oldSecrets, newSecrets) {
errs = errs.Also(apis.ErrInvalidValue("ImagePullSecrets field cannot be changed once set", "ImagePullSecrets"))
}
return errs
}

Expand Down Expand Up @@ -298,18 +272,12 @@ func (r *DataDestination) validateCreate() (errs *apis.FieldError) {
return errs
}

func (r *DataDestination) validateUpdate(old *DataDestination) (errs *apis.FieldError) {
func (r *DataDestination) validateUpdate() (errs *apis.FieldError) {
// TODO: Implement Volumes
if r.Volume != nil {
errs = errs.Also(apis.ErrInvalidValue("Volume support is not implemented yet", "Volume"))
}
if old.Image != r.Image {
errs = errs.Also(apis.ErrInvalidValue("Image field cannot be changed once set", "Image"))
}

if old.ImagePushSecret != r.ImagePushSecret {
errs = errs.Also(apis.ErrInvalidValue("ImagePushSecret field cannot be changed once set", "ImagePushSecret"))
}
return errs
}

Expand Down
57 changes: 1 addition & 56 deletions api/v1alpha1/workspace_validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1302,39 +1302,6 @@ func TestDataSourceValidateUpdate(t *testing.T) {
wantErr: true,
errFields: []string{"Name"},
},
{
name: "URLs changed",
oldSource: &DataSource{
URLs: []string{"http://example.com/old"},
},
newSource: &DataSource{
URLs: []string{"http://example.com/new"},
},
wantErr: true,
errFields: []string{"URLs"},
},
{
name: "Image changed",
oldSource: &DataSource{
Image: "old-image:latest",
},
newSource: &DataSource{
Image: "new-image:latest",
},
wantErr: true,
errFields: []string{"Image"},
},
{
name: "ImagePullSecrets changed",
oldSource: &DataSource{
ImagePullSecrets: []string{"old-secret"},
},
newSource: &DataSource{
ImagePullSecrets: []string{"new-secret"},
},
wantErr: true,
errFields: []string{"ImagePullSecrets"},
},
}

for _, tt := range tests {
Expand Down Expand Up @@ -1450,33 +1417,11 @@ func TestDataDestinationValidateUpdate(t *testing.T) {
},
wantErr: false,
},
{
name: "Image changed",
oldDest: &DataDestination{
Image: "old-image:latest",
},
newDest: &DataDestination{
Image: "new-image:latest",
},
wantErr: true,
errFields: []string{"Image"},
},
{
name: "ImagePushSecret changed",
oldDest: &DataDestination{
ImagePushSecret: "old-secret",
},
newDest: &DataDestination{
ImagePushSecret: "new-secret",
},
wantErr: true,
errFields: []string{"ImagePushSecret"},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
errs := tt.newDest.validateUpdate(tt.oldDest)
errs := tt.newDest.validateUpdate()
hasErrs := errs != nil

if hasErrs != tt.wantErr {
Expand Down
20 changes: 19 additions & 1 deletion pkg/controllers/workspace_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -703,15 +703,33 @@ func (c *WorkspaceReconciler) applyTuning(ctx context.Context, wObj *kaitov1alph

tuningParam := model.GetTuningParameters()
existingObj := &batchv1.Job{}
revisionStr := wObj.Annotations[kaitov1alpha1.WorkspaceRevisionAnnotation]

Check warning on line 706 in pkg/controllers/workspace_controller.go

View check run for this annotation

Codecov / codecov/patch

pkg/controllers/workspace_controller.go#L706

Added line #L706 was not covered by tests
if err = resources.GetResource(ctx, wObj.Name, wObj.Namespace, c.Client, existingObj); err == nil {
klog.InfoS("A tuning workload already exists for workspace", "workspace", klog.KObj(wObj))

if existingObj.Annotations[kaitov1alpha1.WorkspaceRevisionAnnotation] != revisionStr {
deletePolicy := metav1.DeletePropagationForeground
if err := c.Delete(ctx, existingObj, &client.DeleteOptions{
PropagationPolicy: &deletePolicy,
}); err != nil {
return

Check warning on line 715 in pkg/controllers/workspace_controller.go

View check run for this annotation

Codecov / codecov/patch

pkg/controllers/workspace_controller.go#L710-L715

Added lines #L710 - L715 were not covered by tests
}

var workloadObj client.Object
workloadObj, err = tuning.CreatePresetTuning(ctx, wObj, revisionStr, tuningParam, c.Client)
if err != nil {
return

Check warning on line 721 in pkg/controllers/workspace_controller.go

View check run for this annotation

Codecov / codecov/patch

pkg/controllers/workspace_controller.go#L718-L721

Added lines #L718 - L721 were not covered by tests
}
existingObj = workloadObj.(*batchv1.Job)

Check warning on line 723 in pkg/controllers/workspace_controller.go

View check run for this annotation

Codecov / codecov/patch

pkg/controllers/workspace_controller.go#L723

Added line #L723 was not covered by tests
}

if err = resources.CheckResourceStatus(existingObj, c.Client, tuningParam.ReadinessTimeout); err != nil {
return
}
} else if apierrors.IsNotFound(err) {
var workloadObj client.Object
// Need to create a new workload
workloadObj, err = tuning.CreatePresetTuning(ctx, wObj, tuningParam, c.Client)
workloadObj, err = tuning.CreatePresetTuning(ctx, wObj, revisionStr, tuningParam, c.Client)

Check warning on line 732 in pkg/controllers/workspace_controller.go

View check run for this annotation

Codecov / codecov/patch

pkg/controllers/workspace_controller.go#L732

Added line #L732 was not covered by tests
if err != nil {
return
}
Expand Down
5 changes: 4 additions & 1 deletion pkg/resources/manifests.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ func GenerateStatefulSetManifest(ctx context.Context, workspaceObj *kaitov1alpha
return ss
}

func GenerateTuningJobManifest(ctx context.Context, wObj *kaitov1alpha1.Workspace, imageName string,
func GenerateTuningJobManifest(ctx context.Context, wObj *kaitov1alpha1.Workspace, revisionNum string, imageName string,
imagePullSecretRefs []corev1.LocalObjectReference, replicas int, commands []string, containerPorts []corev1.ContainerPort,
livenessProbe, readinessProbe *corev1.Probe, resourceRequirements corev1.ResourceRequirements, tolerations []corev1.Toleration,
initContainers []corev1.Container, sidecarContainers []corev1.Container, volumes []corev1.Volume, volumeMounts []corev1.VolumeMount,
Expand Down Expand Up @@ -225,6 +225,9 @@ func GenerateTuningJobManifest(ctx context.Context, wObj *kaitov1alpha1.Workspac
Name: wObj.Name,
Namespace: wObj.Namespace,
Labels: labels,
Annotations: map[string]string{
kaitov1alpha1.WorkspaceRevisionAnnotation: revisionNum,
},

Check warning on line 230 in pkg/resources/manifests.go

View check run for this annotation

Codecov / codecov/patch

pkg/resources/manifests.go#L228-L230

Added lines #L228 - L230 were not covered by tests
OwnerReferences: []v1.OwnerReference{
{
APIVersion: kaitov1alpha1.GroupVersion.String(),
Expand Down
4 changes: 2 additions & 2 deletions pkg/tuning/preset-tuning.go
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ func setupDefaultSharedVolumes(workspaceObj *kaitov1alpha1.Workspace, cmName str
return volumes, volumeMounts
}

func CreatePresetTuning(ctx context.Context, workspaceObj *kaitov1alpha1.Workspace,
func CreatePresetTuning(ctx context.Context, workspaceObj *kaitov1alpha1.Workspace, revisionNum string,
tuningObj *model.PresetParam, kubeClient client.Client) (client.Object, error) {
cm, err := EnsureTuningConfigMap(ctx, workspaceObj, kubeClient)
if err != nil {
Expand Down Expand Up @@ -348,7 +348,7 @@ func CreatePresetTuning(ctx context.Context, workspaceObj *kaitov1alpha1.Workspa
Value: "k_proj,q_proj,v_proj,o_proj,gate_proj,down_proj,up_proj",
})
}
jobObj := resources.GenerateTuningJobManifest(ctx, workspaceObj, tuningImage, imagePullSecrets, *workspaceObj.Resource.Count, commands,
jobObj := resources.GenerateTuningJobManifest(ctx, workspaceObj, revisionNum, tuningImage, imagePullSecrets, *workspaceObj.Resource.Count, commands,

Check warning on line 351 in pkg/tuning/preset-tuning.go

View check run for this annotation

Codecov / codecov/patch

pkg/tuning/preset-tuning.go#L351

Added line #L351 was not covered by tests
containerPorts, nil, nil, resourceReq, tolerations, initContainers, sidecarContainers, volumes, volumeMounts, envVars)

err = resources.CreateResource(ctx, jobObj, kubeClient)
Expand Down

0 comments on commit 2688875

Please sign in to comment.