Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions api/v1alpha/annotations.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,4 +122,9 @@ const (
// cell the session was delivered to. The session controller sets it at
// delivery.
InstanceConsoleSessionLocationAnnotation = AnnotationNamespace + "/location"

// RuntimeClassSupersededByAnnotation names the class that replaces this
// one. A superseded class keeps working for workloads that use it, but it
// is no longer offered or used as the default.
RuntimeClassSupersededByAnnotation = AnnotationNamespace + "/superseded-by"
)
3 changes: 2 additions & 1 deletion api/v1alpha/runtimeclass_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,8 @@ type RuntimeClassSpec struct {
// Admission stamps the default onto a workload and never resolves it at
// read time. Moving the marker changes what new workloads get and leaves
// running ones in the tier, cost, and startup profile they were created
// with. At most one class in the catalog may set it.
// with. At most one class in the catalog may set it, not counting classes
// annotated as superseded, which are never the default.
//
// +kubebuilder:validation:Optional
Default bool `json:"default,omitempty"`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,8 @@ spec:
Admission stamps the default onto a workload and never resolves it at
read time. Moving the marker changes what new workloads get and leaves
running ones in the tier, cost, and startup profile they were created
with. At most one class in the catalog may set it.
with. At most one class in the catalog may set it, not counting classes
annotated as superseded, which are never the default.
type: boolean
defaultSecurityContext:
description: |-
Expand Down
3 changes: 2 additions & 1 deletion docs/api/runtimeclasses.md
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,8 @@ their declaration.<br/>
Admission stamps the default onto a workload and never resolves it at
read time. Moving the marker changes what new workloads get and leaves
running ones in the tier, cost, and startup profile they were created
with. At most one class in the catalog may set it.<br/>
with. At most one class in the catalog may set it, not counting classes
annotated as superseded, which are never the default.<br/>
</td>
<td>false</td>
</tr><tr>
Expand Down
1 change: 1 addition & 0 deletions internal/cmd/compute/deploy/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,7 @@ by a manifest carry forward too. Remove one explicitly with a trailing
_ = cmd.RegisterFlagCompletionFunc("location", util.CompletePlacementLocations)
_ = cmd.RegisterFlagCompletionFunc("location-selector", util.CompleteLocationSelector)
_ = cmd.RegisterFlagCompletionFunc("city", util.CompleteCityCodes)
_ = cmd.RegisterFlagCompletionFunc("runtime-class", util.CompleteRuntimeClasses)

// --port stays registered for one release so that using it produces the
// migration error rather than "unknown flag". It is hidden rather than
Expand Down
19 changes: 4 additions & 15 deletions internal/cmd/compute/deploy/image_preflight.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (

computev1alpha "go.datum.net/compute/api/v1alpha"
"go.datum.net/compute/internal/cmd/compute/build"
"go.datum.net/compute/pkg/runtimeclass"
)

// isolationUnikernel is the RuntimeClass isolation boundary of unikernels,
Expand Down Expand Up @@ -45,7 +46,7 @@ func checkImageRuntimeIn(ctx context.Context, c client.Client, image, class stri
if err := c.List(ctx, &classes); err != nil {
return nil
}
selected := selectRuntimeClass(classes.Items, class)
selected := runtimeclass.Catalog(classes.Items).Resolve(class)
if selected == nil {
return nil
}
Expand Down Expand Up @@ -109,28 +110,16 @@ func planRuntimeClass(ctx context.Context, c client.Client, class string, creati
}
var classes computev1alpha.RuntimeClassList
if err := c.List(ctx, &classes); err == nil {
if selected := selectRuntimeClass(classes.Items, ""); selected != nil {
if selected := runtimeclass.Catalog(classes.Items).Default(); selected != nil {
return selected.Name + " (platform default)"
}
}
return "platform default"
}

// selectRuntimeClass returns the class named, or the catalog default when
// none is, the way admission fills in an empty class.
func selectRuntimeClass(classes []computev1alpha.RuntimeClass, name string) *computev1alpha.RuntimeClass {
for i := range classes {
c := &classes[i]
if c.Name == name || (name == "" && c.Spec.Default) {
return c
}
}
return nil
}

func classWithBoundary(classes []computev1alpha.RuntimeClass, match func(string) bool) string {
for _, c := range classes {
if match(c.Spec.Isolation.Boundary) {
if runtimeclass.SupersededBy(&c) == "" && match(c.Spec.Isolation.Boundary) {
return c.Name
}
}
Expand Down
22 changes: 22 additions & 0 deletions internal/cmd/compute/deploy/image_preflight_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,28 @@ func TestCheckManifestImages(t *testing.T) {
}
}

// TestSupersededRuntimeClass checks that a superseded class is neither the
// plan's default nor a suggestion, even when it lists first.
func TestSupersededRuntimeClass(t *testing.T) {
bar := runtimeClass("bar", "unikernel", true)
bar.Annotations = map[string]string{computev1alpha.RuntimeClassSupersededByAnnotation: "foo"}
kube := newFakeClient(t,
bar,
runtimeClass("foo", "unikernel", true),
runtimeClass(classGeneralPurpose, "virtual-machine", false),
)

if got := planRuntimeClass(context.Background(), kube, "", true); got != "foo (platform default)" {
t.Errorf("planRuntimeClass() = %q, want the replacement as the default", got)
}

lookup := func(context.Context, string) ([]string, error) { return []string{platformCompute}, nil }
err := checkImageRuntime(context.Background(), kube, "ghcr.io/acme/api:1", classGeneralPurpose, lookup)
if err == nil || !strings.Contains(err.Error(), "--runtime-class=foo") {
t.Errorf("checkImageRuntime() = %v, want it to suggest --runtime-class=foo", err)
}
}

func TestPlanRuntimeClass(t *testing.T) {
catalog := newFakeClient(t,
runtimeClass(classGeneralPurpose, "virtual-machine", false),
Expand Down
11 changes: 5 additions & 6 deletions internal/cmd/compute/logs/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (

computev1alpha "go.datum.net/compute/api/v1alpha"
"go.datum.net/compute/internal/cmd/compute/util"
"go.datum.net/compute/pkg/runtimeclass"
)

const (
Expand Down Expand Up @@ -445,13 +446,11 @@ func isolationOf(ctx context.Context, kube client.Client, wl *computev1alpha.Wor
if err := kube.List(ctx, &classes); err != nil {
return ""
}
name := wl.Spec.Template.Spec.Runtime.Class
for _, c := range classes.Items {
if c.Name == name || (name == "" && c.Spec.Default) {
return c.Spec.Isolation.Boundary
}
class := runtimeclass.Catalog(classes.Items).Resolve(wl.Spec.Template.Spec.Runtime.Class)
if class == nil {
return ""
}
return ""
return class.Spec.Isolation.Boundary
}

func narrowToGeneration(ctx context.Context, q logsapi.Querier, sel *logql.Selector, previous bool, w window, p *printer) error {
Expand Down
15 changes: 15 additions & 0 deletions internal/cmd/compute/util/completion.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (

computev1alpha "go.datum.net/compute/api/v1alpha"
"go.datum.net/compute/internal/locations"
"go.datum.net/compute/pkg/runtimeclass"
"go.datum.net/datumctl/plugin"
locationsv1alpha1 "go.miloapis.com/locations/api/v1alpha1"
)
Expand Down Expand Up @@ -205,6 +206,20 @@ func projectedLocations(cmd *cobra.Command) (locationsv1alpha1.LocationList, boo
return list, true
}

// CompleteRuntimeClasses completes --runtime-class with the classes the project
// offers. Superseded classes still work but aren't offered.
func CompleteRuntimeClasses(cmd *cobra.Command, _ []string, _ string) ([]string, cobra.ShellCompDirective) {
c, err := NewClient(ProjectFromCmd(cmd))
if err != nil {
return nil, cobra.ShellCompDirectiveNoFileComp
}
var list computev1alpha.RuntimeClassList
if err := c.List(context.Background(), &list); err != nil {
return nil, cobra.ShellCompDirectiveNoFileComp
}
return runtimeclass.Catalog(list.Items).Offered(), cobra.ShellCompDirectiveNoFileComp
}

// CompleteOutputFormats returns a ValidArgsFunction that completes -o/--output
// to the given allowed values.
func CompleteOutputFormats(allowed ...string) func(*cobra.Command, []string, string) ([]string, cobra.ShellCompDirective) {
Expand Down
5 changes: 1 addition & 4 deletions internal/validation/instanceconsolesession_validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,7 @@ func validateSessionRuntimeClass(
className = instance.Labels[computev1alpha.RuntimeClassLabel]
}

class := catalog.Find(className)
if className == "" {
class = catalog.Default()
}
class := catalog.Resolve(className)

capabilities := runtimeclass.CapabilitiesFrom(class)
if capabilities.Class == "" {
Expand Down
2 changes: 1 addition & 1 deletion internal/validation/runtimeclass_validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func validateRuntimeClassSelection(
}

catalog := opts.RuntimeClasses
offered := catalog.Names()
offered := catalog.Offered()

// The mutating webhook stamps the catalog's default class. An empty value
// here means the catalog publishes no default, so the instance has no
Expand Down
18 changes: 17 additions & 1 deletion internal/webhook/v1alpha/workload_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package webhook
import (
"context"
"fmt"
"slices"

admissionv1 "k8s.io/api/admission/v1"
"k8s.io/apimachinery/pkg/api/errors"
Expand Down Expand Up @@ -185,7 +186,10 @@ func (r *workloadWebhook) ValidateCreate(ctx context.Context, workload *computev
return nil, errors.NewInvalid(workload.GroupVersionKind().GroupKind(), workload.Name, errs)
}

return workloadInstanceTypeWarnings(true, workload, instanceTypes), nil
return slices.Concat(
workloadInstanceTypeWarnings(true, workload, instanceTypes),
supersededRuntimeClassWarnings(workload, runtimeClasses),
), nil
}

func (r *workloadWebhook) ValidateUpdate(ctx context.Context, oldWorkload *computev1alpha.Workload, newWorkload *computev1alpha.Workload) (admission.Warnings, error) {
Expand Down Expand Up @@ -418,6 +422,18 @@ func (r *workloadWebhook) instanceTypeCatalogWhenEnabled(ctx context.Context) (i
return r.instanceTypeCatalog(ctx)
}

// supersededRuntimeClassWarnings warns when a new workload names a superseded
// class. The class still works, so the workload is admitted. Only create calls
// this, because the class can't change afterwards.
func supersededRuntimeClassWarnings(workload *computev1alpha.Workload, catalog runtimeclass.Catalog) admission.Warnings {
name := workload.Spec.Template.Spec.Runtime.Class
replacement := runtimeclass.SupersededBy(catalog.Find(name))
if replacement == "" {
return nil
}
return admission.Warnings{fmt.Sprintf("runtime class %q is superseded by %q; use %q for new workloads", name, replacement, replacement)}
}

const warningNoInstanceTypeSelected = "no instance type selected; the workload will run on the platform's default instance type"

// workloadInstanceTypeWarnings returns the admission warnings that accompany an
Expand Down
27 changes: 27 additions & 0 deletions internal/webhook/v1alpha/workload_webhook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -618,6 +618,33 @@ func TestWorkloadInstanceTypeWarnings(t *testing.T) {
}
}

func TestSupersededRuntimeClassWarnings(t *testing.T) {
superseded := runtimeClass(testClassAzurite, false)
superseded.Annotations = map[string]string{computev1alpha.RuntimeClassSupersededByAnnotation: testClassBasalt}
catalog := runtimeclass.Catalog{superseded, runtimeClass(testClassBasalt, false)}

cases := map[string]struct {
class string
want admission.Warnings
}{
"a superseded class": {class: testClassAzurite, want: admission.Warnings{`runtime class "azurite" is superseded by "basalt"; use "basalt" for new workloads`}},
"a current class": {class: testClassBasalt},
"a class not in the catalog": {class: testClassCitrine},
}

for name, tc := range cases {
t.Run(name, func(t *testing.T) {
workload := &computev1alpha.Workload{}
workload.Spec.Template.Spec.Runtime.Class = tc.class

got := supersededRuntimeClassWarnings(workload, catalog)
if diff := cmp.Diff(tc.want, got); diff != "" {
t.Errorf("warnings diff (-want +got):\n%s", diff)
}
})
}
}

// TestWorkloadInstanceTypeWarnings_GateOff verifies that nothing is announced
// while instance type selection is disabled, even for an omitted type on create
// or a type some stale catalog would call deprecated.
Expand Down
39 changes: 36 additions & 3 deletions pkg/runtimeclass/catalog.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
package runtimeclass

import (
"sort"
"slices"

"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand All @@ -29,6 +29,15 @@ func (c Catalog) Find(name string) *computev1alpha.RuntimeClass {
return nil
}

// Resolve returns the class an instance naming this class runs in: the default
// when the name is empty, otherwise the class with that name.
func (c Catalog) Resolve(name string) *computev1alpha.RuntimeClass {
if name == "" {
return c.Default()
}
return c.Find(name)
}

// Default returns the class an instance runs in when it selects none. It
// returns nil when the catalog does not mark exactly one class as the default.
//
Expand All @@ -38,10 +47,12 @@ func (c Catalog) Find(name string) *computev1alpha.RuntimeClass {
// would place new workloads in a tier with different isolation, startup time,
// and cost, so Default returns nil and the caller asks the customer to choose
// from the published names.
//
// A superseded class is never the default, even when it's marked as one.
func (c Catalog) Default() *computev1alpha.RuntimeClass {
var marked *computev1alpha.RuntimeClass
for i := range c {
if !c[i].Spec.Default {
if !c[i].Spec.Default || SupersededBy(&c[i]) != "" {
continue
}
if marked != nil {
Expand All @@ -60,10 +71,32 @@ func (c Catalog) Names() []string {
for i := range c {
names = append(names, c[i].Name)
}
sort.Strings(names)
slices.Sort(names)
return names
}

// Offered returns the names a customer should choose from, in sorted order.
// Superseded classes still work but aren't offered.
func (c Catalog) Offered() []string {
var names []string
for i := range c {
if SupersededBy(&c[i]) == "" {
names = append(names, c[i].Name)
}
}
slices.Sort(names)
return names
}

// SupersededBy returns the name of the class that replaces this one, or ""
// when nothing does.
func SupersededBy(class *computev1alpha.RuntimeClass) string {
if class == nil {
return ""
}
return class.Annotations[computev1alpha.RuntimeClassSupersededByAnnotation]
}

// ClaimedBy returns the classes a controller implements. A provider calls
// ClaimedBy to find the classes it must report on, so changing which classes a
// provider serves is a catalog edit rather than a rebuild.
Expand Down
Loading
Loading