Skip to content

Commit b8c182d

Browse files
committed
feat(container-init): inject cloud-config initialization
1 parent 1dc39c0 commit b8c182d

10 files changed

Lines changed: 605 additions & 11 deletions
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package v1alpha1
2+
3+
import corev1 "k8s.io/api/core/v1"
4+
5+
// ContainerInitDefaults opts a container image into cloud-config delivery.
6+
type ContainerInitDefaults struct {
7+
// Mode selects normal startup, injected entrypoint wrapping, or data-only
8+
// delivery to an image whose own entrypoint runs container-init.
9+
// +kubebuilder:validation:Enum=disabled;injected;baked-in
10+
Mode string `json:"mode"`
11+
}
12+
13+
// ContainerInitSpec is a creation-time snapshot, independent of later Image
14+
// edits. Inline UserData is visible in the Workspace CR; a Secret reference can
15+
// instead keep private configuration out of that document.
16+
// +kubebuilder:validation:XValidation:rule="!(has(self.userData) && has(self.userDataSecretRef))",message="Choose inline userData or userDataSecretRef, not both"
17+
// +kubebuilder:validation:XValidation:rule="self.mode == 'disabled' || (has(self.userData) && size(self.userData) > 0) || has(self.userDataSecretRef)",message="Enabled container initialization requires user-data"
18+
type ContainerInitSpec struct {
19+
// +kubebuilder:validation:Enum=disabled;injected;baked-in
20+
Mode string `json:"mode"`
21+
// UserData is an inline #cloud-config document, mounted through a generated
22+
// Workspace-owned Secret. Maximum 256 KiB (also enforced in bytes at runtime).
23+
// +kubebuilder:validation:MaxLength=262144
24+
// +optional
25+
UserData string `json:"userData,omitempty"`
26+
// UserDataSecretRef references a same-namespace Secret with a user-data key.
27+
// The referenced Secret is independently managed and is never adopted/deleted.
28+
// +kubebuilder:validation:XValidation:rule="has(self.name) && size(self.name) > 0",message="A user-data Secret name is required"
29+
// +optional
30+
UserDataSecretRef *corev1.LocalObjectReference `json:"userDataSecretRef,omitempty"`
31+
}

‎api/v1alpha1/image_types.go‎

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,14 @@ type ImageSpec struct {
5050
// Default command-line args injected at workspace creation.
5151
// +optional
5252
DefaultArgs []string `json:"defaultArgs,omitempty"`
53+
// DefaultCommand is the explicit container entrypoint at workspace creation.
54+
// Injected container-init requires a resolved command, not OCI discovery.
55+
// +optional
56+
DefaultCommand []string `json:"defaultCommand,omitempty"`
57+
// DefaultContainerInit opts container/scratch workspaces into initialization.
58+
// DefaultUserData alone does not enable container initialization.
59+
// +optional
60+
DefaultContainerInit *ContainerInitDefaults `json:"defaultContainerInit,omitempty"`
5361
// Default environment variables injected at workspace creation.
5462
// Supports {{namespace}} and {{name}} placeholders.
5563
// +optional
@@ -82,9 +90,8 @@ type ImageSpec struct {
8290
// seeded into the guest at first boot.
8391
// +optional
8492
DefaultCloudInit bool `json:"defaultCloudInit,omitempty"`
85-
// DefaultUserData is reserved user-data for the guest (cloud-init). Empty by
86-
// default; later used to seed first-boot configuration when DefaultCloudInit
87-
// is true. Not yet consumed by the controller.
93+
// DefaultUserData is cloud-config for VM cloud-init or an explicitly enabled
94+
// DefaultContainerInit. Container defaults are snapshotted at creation time.
8895
// +optional
8996
DefaultUserData string `json:"defaultUserData,omitempty"`
9097
// DefaultHomedir is the default home directory for the default user.

‎api/v1alpha1/workspace_types.go‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,13 @@ import (
2424
// WorkspaceSpec defines the desired state of Workspace.
2525
// Modelled closely on Kubeflow Notebook spec - wraps a full PodSpec.
2626
// +kubebuilder:validation:XValidation:rule="!has(self.vmProfile) || self.type == 'vm'",message="VM guest profiles require type vm"
27+
// +kubebuilder:validation:XValidation:rule="!has(self.containerInit) || self.type != 'vm'",message="Container initialization requires container or scratch type"
2728
// +kubebuilder:validation:XValidation:rule="!has(self.vmProfile) || (size(self.template.spec.containers) == 1 && self.template.spec.containers[0].image == self.vmProfile.image)",message="The main image must match the resolved VM source"
2829
type WorkspaceSpec struct {
30+
// ContainerInit is resolved at creation and applied to the main container.
31+
// Direct Workspace CRs can supply it without an Image catalog lookup.
32+
// +optional
33+
ContainerInit *ContainerInitSpec `json:"containerInit,omitempty"`
2934
// ExecutionRevocationEpoch requests a minimum execution epoch. The request
3035
// itself is monotonic so a pending revocation cannot be undone before the
3136
// controller acknowledges it in status. It never supplies authority.

‎api/v1alpha1/zz_generated.deepcopy.go‎

Lines changed: 50 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎cmd/main.go‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,9 @@ func main() {
100100
var enableHTTP2 bool
101101
var tlsOpts []func(*tls.Config)
102102
var managementBootstrapNamespace string
103+
var containerInitImage string
104+
flag.StringVar(&containerInitImage, "container-init-image", os.Getenv("KW_CONTAINER_INIT_IMAGE"),
105+
"Pinned installer image for opt-in container-init injection; empty disables injected mode")
103106
flag.StringVar(&managementBootstrapNamespace, "management-bootstrap-namespace",
104107
os.Getenv("KW_MANAGEMENT_BOOTSTRAP_NAMESPACE"),
105108
"Protected namespace for opt-in management bootstrap Secrets; empty disables provisioning")
@@ -268,6 +271,7 @@ func main() {
268271
EventRecorder: mgr.GetEventRecorderFor("workspace-controller"),
269272
APIReader: mgr.GetAPIReader(),
270273
ManagementBootstrapNamespace: managementBootstrapNamespace,
274+
ContainerInitImage: containerInitImage,
271275
}).SetupWithManager(mgr); err != nil {
272276
setupLog.Error(err, "unable to create controller", "controller", "Workspace")
273277
os.Exit(1)

‎config/crd/bases/kubeworkspaces.io_images.yaml‎

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,30 @@ spec:
9292
user-data (e.g. a user/password from DefaultUser/DefaultPassword) can be
9393
seeded into the guest at first boot.
9494
type: boolean
95+
defaultCommand:
96+
description: |-
97+
DefaultCommand is the explicit container entrypoint at workspace creation.
98+
Injected container-init requires a resolved command, not OCI discovery.
99+
items:
100+
type: string
101+
type: array
102+
defaultContainerInit:
103+
description: |-
104+
DefaultContainerInit opts container/scratch workspaces into initialization.
105+
DefaultUserData alone does not enable container initialization.
106+
properties:
107+
mode:
108+
description: |-
109+
Mode selects normal startup, injected entrypoint wrapping, or data-only
110+
delivery to an image whose own entrypoint runs container-init.
111+
enum:
112+
- disabled
113+
- injected
114+
- baked-in
115+
type: string
116+
required:
117+
- mode
118+
type: object
95119
defaultCredentials:
96120
description: DefaultCredentials are the default login credentials
97121
for this image.
@@ -1609,9 +1633,8 @@ spec:
16091633
type: string
16101634
defaultUserData:
16111635
description: |-
1612-
DefaultUserData is reserved user-data for the guest (cloud-init). Empty by
1613-
default; later used to seed first-boot configuration when DefaultCloudInit
1614-
is true. Not yet consumed by the controller.
1636+
DefaultUserData is cloud-config for VM cloud-init or an explicitly enabled
1637+
DefaultContainerInit. Container defaults are snapshotted at creation time.
16151638
type: string
16161639
description:
16171640
description: Description of the image.

‎config/crd/bases/kubeworkspaces.io_workspaces.yaml‎

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,51 @@ spec:
5050
WorkspaceSpec defines the desired state of Workspace.
5151
Modelled closely on Kubeflow Notebook spec - wraps a full PodSpec.
5252
properties:
53+
containerInit:
54+
description: |-
55+
ContainerInit is resolved at creation and applied to the main container.
56+
Direct Workspace CRs can supply it without an Image catalog lookup.
57+
properties:
58+
mode:
59+
enum:
60+
- disabled
61+
- injected
62+
- baked-in
63+
type: string
64+
userData:
65+
description: |-
66+
UserData is an inline #cloud-config document, mounted through a generated
67+
Workspace-owned Secret. Maximum 256 KiB (also enforced in bytes at runtime).
68+
maxLength: 262144
69+
type: string
70+
userDataSecretRef:
71+
description: |-
72+
UserDataSecretRef references a same-namespace Secret with a user-data key.
73+
The referenced Secret is independently managed and is never adopted/deleted.
74+
properties:
75+
name:
76+
default: ""
77+
description: |-
78+
Name of the referent.
79+
This field is effectively required, but due to backwards compatibility is
80+
allowed to be empty. Instances of this type with an empty value here are
81+
almost certainly wrong.
82+
More info: https://kubernetes.io/docs/concepts/overview/working-with-objects/names/#names
83+
type: string
84+
type: object
85+
x-kubernetes-map-type: atomic
86+
x-kubernetes-validations:
87+
- message: A user-data Secret name is required
88+
rule: has(self.name) && size(self.name) > 0
89+
required:
90+
- mode
91+
type: object
92+
x-kubernetes-validations:
93+
- message: Choose inline userData or userDataSecretRef, not both
94+
rule: '!(has(self.userData) && has(self.userDataSecretRef))'
95+
- message: Enabled container initialization requires user-data
96+
rule: self.mode == 'disabled' || (has(self.userData) && size(self.userData)
97+
> 0) || has(self.userDataSecretRef)
5398
executionRevocationEpoch:
5499
default: 0
55100
description: |-
@@ -8231,6 +8276,8 @@ spec:
82318276
x-kubernetes-validations:
82328277
- message: VM guest profiles require type vm
82338278
rule: '!has(self.vmProfile) || self.type == ''vm'''
8279+
- message: Container initialization requires container or scratch type
8280+
rule: '!has(self.containerInit) || self.type != ''vm'''
82348281
- message: The main image must match the resolved VM source
82358282
rule: '!has(self.vmProfile) || (size(self.template.spec.containers)
82368283
== 1 && self.template.spec.containers[0].image == self.vmProfile.image)'

0 commit comments

Comments
 (0)