diff --git a/api/v1alpha/annotations.go b/api/v1alpha/annotations.go
index 100dca8a..ec1c3011 100644
--- a/api/v1alpha/annotations.go
+++ b/api/v1alpha/annotations.go
@@ -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"
)
diff --git a/api/v1alpha/runtimeclass_types.go b/api/v1alpha/runtimeclass_types.go
index 868b796e..25fe7daa 100644
--- a/api/v1alpha/runtimeclass_types.go
+++ b/api/v1alpha/runtimeclass_types.go
@@ -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"`
diff --git a/config/base/crd/bases/compute.datumapis.com_runtimeclasses.yaml b/config/base/crd/bases/compute.datumapis.com_runtimeclasses.yaml
index ebcf6a93..0d755589 100644
--- a/config/base/crd/bases/compute.datumapis.com_runtimeclasses.yaml
+++ b/config/base/crd/bases/compute.datumapis.com_runtimeclasses.yaml
@@ -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: |-
diff --git a/docs/api/runtimeclasses.md b/docs/api/runtimeclasses.md
index d8ec7564..c9388611 100644
--- a/docs/api/runtimeclasses.md
+++ b/docs/api/runtimeclasses.md
@@ -139,7 +139,8 @@ their declaration.
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.
false |
diff --git a/internal/cmd/compute/deploy/deploy.go b/internal/cmd/compute/deploy/deploy.go
index 1734b6e8..b171dd71 100644
--- a/internal/cmd/compute/deploy/deploy.go
+++ b/internal/cmd/compute/deploy/deploy.go
@@ -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
diff --git a/internal/cmd/compute/deploy/image_preflight.go b/internal/cmd/compute/deploy/image_preflight.go
index d8202468..debc93b6 100644
--- a/internal/cmd/compute/deploy/image_preflight.go
+++ b/internal/cmd/compute/deploy/image_preflight.go
@@ -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,
@@ -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
}
@@ -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
}
}
diff --git a/internal/cmd/compute/deploy/image_preflight_test.go b/internal/cmd/compute/deploy/image_preflight_test.go
index 6298bf45..a54ceaad 100644
--- a/internal/cmd/compute/deploy/image_preflight_test.go
+++ b/internal/cmd/compute/deploy/image_preflight_test.go
@@ -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),
diff --git a/internal/cmd/compute/logs/logs.go b/internal/cmd/compute/logs/logs.go
index 12b1879d..fc588c72 100644
--- a/internal/cmd/compute/logs/logs.go
+++ b/internal/cmd/compute/logs/logs.go
@@ -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 (
@@ -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 {
diff --git a/internal/cmd/compute/util/completion.go b/internal/cmd/compute/util/completion.go
index 92419e5b..834adebb 100644
--- a/internal/cmd/compute/util/completion.go
+++ b/internal/cmd/compute/util/completion.go
@@ -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"
)
@@ -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) {
diff --git a/internal/validation/instanceconsolesession_validation.go b/internal/validation/instanceconsolesession_validation.go
index 9f106753..575751c7 100644
--- a/internal/validation/instanceconsolesession_validation.go
+++ b/internal/validation/instanceconsolesession_validation.go
@@ -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 == "" {
diff --git a/internal/validation/runtimeclass_validation.go b/internal/validation/runtimeclass_validation.go
index 7cf9b85c..83895c24 100644
--- a/internal/validation/runtimeclass_validation.go
+++ b/internal/validation/runtimeclass_validation.go
@@ -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
diff --git a/internal/webhook/v1alpha/workload_webhook.go b/internal/webhook/v1alpha/workload_webhook.go
index 221d2dc9..e83b2170 100644
--- a/internal/webhook/v1alpha/workload_webhook.go
+++ b/internal/webhook/v1alpha/workload_webhook.go
@@ -3,6 +3,7 @@ package webhook
import (
"context"
"fmt"
+ "slices"
admissionv1 "k8s.io/api/admission/v1"
"k8s.io/apimachinery/pkg/api/errors"
@@ -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) {
@@ -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
diff --git a/internal/webhook/v1alpha/workload_webhook_test.go b/internal/webhook/v1alpha/workload_webhook_test.go
index bbf298ea..a24041ce 100644
--- a/internal/webhook/v1alpha/workload_webhook_test.go
+++ b/internal/webhook/v1alpha/workload_webhook_test.go
@@ -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.
diff --git a/pkg/runtimeclass/catalog.go b/pkg/runtimeclass/catalog.go
index bd70db70..8bbaa177 100644
--- a/pkg/runtimeclass/catalog.go
+++ b/pkg/runtimeclass/catalog.go
@@ -3,7 +3,7 @@
package runtimeclass
import (
- "sort"
+ "slices"
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -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.
//
@@ -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 {
@@ -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.
diff --git a/pkg/runtimeclass/catalog_test.go b/pkg/runtimeclass/catalog_test.go
index b17b88ca..6d270cf5 100644
--- a/pkg/runtimeclass/catalog_test.go
+++ b/pkg/runtimeclass/catalog_test.go
@@ -27,6 +27,11 @@ func class(name string, isDefault bool) computev1alpha.RuntimeClass {
}
}
+func supersededBy(c computev1alpha.RuntimeClass, replacement string) computev1alpha.RuntimeClass {
+ c.Annotations = map[string]string{computev1alpha.RuntimeClassSupersededByAnnotation: replacement}
+ return c
+}
+
func TestCatalogFind(t *testing.T) {
catalog := Catalog{class(testClassAzurite, true), class(testClassBasalt, false)}
@@ -70,6 +75,14 @@ func TestCatalogDefault(t *testing.T) {
"an empty catalog": {
want: "",
},
+ "a superseded class and its replacement both mark themselves": {
+ catalog: Catalog{supersededBy(class(testClassAzurite, true), testClassBasalt), class(testClassBasalt, true)},
+ want: testClassBasalt,
+ },
+ "only a superseded class marks itself": {
+ catalog: Catalog{supersededBy(class(testClassAzurite, true), testClassBasalt), class(testClassBasalt, false)},
+ want: "",
+ },
}
for name, tc := range cases {
@@ -94,6 +107,59 @@ func TestCatalogNames(t *testing.T) {
}
}
+func TestCatalogResolve(t *testing.T) {
+ catalog := Catalog{class(testClassAzurite, true), class(testClassBasalt, false)}
+
+ cases := map[string]struct {
+ name string
+ want string
+ }{
+ "no name gets the default": {name: "", want: testClassAzurite},
+ "a published name": {name: testClassBasalt, want: testClassBasalt},
+ "a name the catalog lacks": {name: testClassCitrine, want: ""},
+ }
+
+ for name, tc := range cases {
+ t.Run(name, func(t *testing.T) {
+ got := ""
+ if resolved := catalog.Resolve(tc.name); resolved != nil {
+ got = resolved.Name
+ }
+ if got != tc.want {
+ t.Errorf("Resolve(%q) = %q, want %q", tc.name, got, tc.want)
+ }
+ })
+ }
+}
+
+func TestCatalogOffered(t *testing.T) {
+ catalog := Catalog{
+ class(testClassCitrine, false),
+ supersededBy(class(testClassAzurite, true), testClassCitrine),
+ class(testClassBasalt, true),
+ }
+
+ names := catalog.Offered()
+ if len(names) != 2 || names[0] != testClassBasalt || names[1] != testClassCitrine {
+ t.Errorf("Offered() = %v, want the classes that aren't superseded, in sorted order", names)
+ }
+}
+
+func TestSupersededBy(t *testing.T) {
+ superseded := supersededBy(class(testClassAzurite, false), testClassBasalt)
+ current := class(testClassBasalt, false)
+
+ if got := SupersededBy(&superseded); got != testClassBasalt {
+ t.Errorf("SupersededBy(azurite) = %q, want %q", got, testClassBasalt)
+ }
+ if got := SupersededBy(¤t); got != "" {
+ t.Errorf("SupersededBy(basalt) = %q, want empty", got)
+ }
+ if got := SupersededBy(nil); got != "" {
+ t.Errorf("SupersededBy(nil) = %q, want empty", got)
+ }
+}
+
// TestCatalogClaimedBy covers how a provider finds the classes it is
// responsible for. Selection uses the controller name published on each class,
// never a match against a class name.