Skip to content

Commit 1604549

Browse files
committed
controller: Add overhead annotation using pod mutating webhook
In order for the Kata runtime to correctly deal with hot plugging and set a VM size that is compatible with the host cgroup size [1], we need to pass the known overhead (from the RuntimeClass) to the runtime using an annotation. This code implements a mutating web hook to modify the pod so that the annotation is added when the runtime class is Kata. Signed-off-by: Christophe de Dinechin <[email protected]>
1 parent 99235ad commit 1604549

File tree

11 files changed

+1772
-10
lines changed

11 files changed

+1772
-10
lines changed

api/v1/kataconfig_types.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,12 @@ type KataConfigSpec struct {
4444
// +optional
4545
// +kubebuilder:default:=false
4646
EnablePeerPods bool `json:"enablePeerPods"`
47+
48+
// +optional
49+
// +kubebuilder:default:=350
50+
// +kubebuilder:validation:Minimum=1
51+
// +kubebuilder:validation:Maximum=2048
52+
MemoryOverheadMB *int32 `json:"memoryOverheadMB,omitempty"`
4753
}
4854

4955
// KataConfigStatus defines the observed state of KataConfig

api/v1/kataconfig_webhook.go

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -61,16 +61,28 @@ func (r *KataConfig) ValidateCreate(ctx context.Context, obj runtime.Object) (ad
6161

6262
kataconfiglog.Info("validate create", "name", kataconfig.Name)
6363

64-
kataConfigList := &KataConfigList{}
65-
listOpts := []client.ListOption{
66-
client.InNamespace(corev1.NamespaceAll),
67-
}
68-
if err := clientInst.List(ctx, kataConfigList, listOpts...); err != nil {
69-
return nil, fmt.Errorf("Failed to list KataConfig custom resources: %v", err)
64+
// Skip client-dependent validation if clientInst is nil (e.g., during testing)
65+
if clientInst != nil {
66+
kataConfigList := &KataConfigList{}
67+
listOpts := []client.ListOption{
68+
client.InNamespace(corev1.NamespaceAll),
69+
}
70+
if err := clientInst.List(ctx, kataConfigList, listOpts...); err != nil {
71+
return nil, fmt.Errorf("Failed to list KataConfig custom resources: %v", err)
72+
}
73+
74+
if len(kataConfigList.Items) == 1 {
75+
return nil, fmt.Errorf("A KataConfig instance already exists, refusing to create a duplicate")
76+
}
7077
}
7178

72-
if len(kataConfigList.Items) == 1 {
73-
return nil, fmt.Errorf("A KataConfig instance already exists, refusing to create a duplicate")
79+
if r.Spec.MemoryOverheadMB != nil {
80+
if *r.Spec.MemoryOverheadMB < 60 {
81+
return nil, fmt.Errorf("memoryOverheadMB must be at least 60MB")
82+
}
83+
if *r.Spec.MemoryOverheadMB > 4096*1024 {
84+
return nil, fmt.Errorf("memoryOverheadMB must be at most 4GB")
85+
}
7486
}
7587

7688
return nil, nil
@@ -85,6 +97,15 @@ func (r *KataConfig) ValidateUpdate(ctx context.Context, oldObj, newObj runtime.
8597

8698
kataconfiglog.Info("validate update", "name", kataconfig.Name)
8799

100+
if r.Spec.MemoryOverheadMB != nil {
101+
if *r.Spec.MemoryOverheadMB < 60 {
102+
return nil, fmt.Errorf("memoryOverheadMB must be at least 60MB")
103+
}
104+
if *r.Spec.MemoryOverheadMB > 4096*1024 {
105+
return nil, fmt.Errorf("memoryOverheadMB must be at most 4GB")
106+
}
107+
}
108+
88109
// TODO(user): fill in your validation logic upon object update.
89110
return nil, nil
90111
}

0 commit comments

Comments
 (0)