mirror of
https://github.com/cozystack/cozystack.git
synced 2026-11-30 04:06:34 +09:00
fix(api): validate workload memory before storing an application
A memory quantity without a unit is measured in bytes. Reject new values
below 1Mi before storing them, while allowing unrelated API edits to
applications that already carry a smaller value. The check runs against
the live release right before the write, so a stale value read at the
start of Update cannot overwrite memory another writer has fixed since.
Assisted-by: LLM
Signed-off-by: Yan Bondarenko <202671653+yankawai@users.noreply.github.com>
(cherry picked from commit 46c70422c4)
Signed-off-by: Myasnikov Daniil <myasnikovdaniil2001@gmail.com>
This commit is contained in:
1 parent
e13f9c6cba
commit
e65cec9418
3 files changed
+293
No files matched your search
@@ -0,0 +1,99 @@
|
||||
/*
|
||||
Copyright 2024 The Cozystack Authors.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package application
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"reflect"
|
||||
|
||||
apiextv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
|
||||
structuralschema "k8s.io/apiextensions-apiserver/pkg/apiserver/schema"
|
||||
apierrors "k8s.io/apimachinery/pkg/api/errors"
|
||||
"k8s.io/apimachinery/pkg/api/resource"
|
||||
"k8s.io/apimachinery/pkg/util/validation/field"
|
||||
|
||||
appsv1alpha1 "github.com/cozystack/cozystack/pkg/apis/apps/v1alpha1"
|
||||
)
|
||||
|
||||
var minimumWorkloadMemory = resource.MustParse("1Mi")
|
||||
|
||||
func workloadValues(values *apiextv1.JSON) (map[string]any, error) {
|
||||
if values == nil || len(values.Raw) == 0 {
|
||||
return nil, nil
|
||||
}
|
||||
var result map[string]any
|
||||
decoder := json.NewDecoder(bytes.NewReader(values.Raw))
|
||||
decoder.UseNumber()
|
||||
err := decoder.Decode(&result)
|
||||
return result, err
|
||||
}
|
||||
|
||||
func (r *REST) validateWorkloadMemory(app *appsv1alpha1.Application, previous *apiextv1.JSON) error {
|
||||
if r.specSchema == nil || app.Spec == nil {
|
||||
return nil
|
||||
}
|
||||
values, err := workloadValues(app.Spec)
|
||||
if err != nil {
|
||||
return apierrors.NewBadRequest("invalid application spec")
|
||||
}
|
||||
old, err := workloadValues(previous)
|
||||
if err != nil {
|
||||
return apierrors.NewInternalError(fmt.Errorf("cannot decode stored application spec"))
|
||||
}
|
||||
if errs := changedWorkloadMemory(values, old, r.specSchema, field.NewPath("spec")); len(errs) > 0 {
|
||||
return apierrors.NewInvalid(r.gvk.GroupKind(), app.Name, errs)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Only walk declared object fields. Arbitrary chart settings and tenant resourceQuotas
|
||||
// can also contain keys named "memory", with different semantics.
|
||||
func changedWorkloadMemory(values, old map[string]any, schema *structuralschema.Structural, path *field.Path) field.ErrorList {
|
||||
var errs field.ErrorList
|
||||
for _, key := range sortedKeys(values) {
|
||||
child, declared := schema.Properties[key]
|
||||
current, ok := values[key].(map[string]any)
|
||||
if !declared || !ok {
|
||||
continue
|
||||
}
|
||||
previous, _ := old[key].(map[string]any)
|
||||
where := path.Child(key)
|
||||
_, preset := schema.Properties["resourcesPreset"]
|
||||
if key == "resources" && preset && child.Properties["memory"].XIntOrString && child.Properties["cpu"].XIntOrString {
|
||||
memory := current["memory"]
|
||||
if memory == nil || reflect.DeepEqual(memory, previous["memory"]) {
|
||||
continue
|
||||
}
|
||||
q, err := resource.ParseQuantity(fmt.Sprint(memory))
|
||||
if err != nil {
|
||||
errs = append(errs, field.Invalid(where.Child("memory"), memory, "expected a Kubernetes quantity"))
|
||||
continue
|
||||
}
|
||||
if q.Cmp(minimumWorkloadMemory) < 0 {
|
||||
prior, err := resource.ParseQuantity(fmt.Sprint(previous["memory"]))
|
||||
if err != nil || prior.Cmp(q) != 0 {
|
||||
errs = append(errs, field.Invalid(where.Child("memory"), memory, "must be at least 1Mi; use a unit such as 512Mi or 8Gi"))
|
||||
}
|
||||
}
|
||||
continue
|
||||
}
|
||||
errs = append(errs, changedWorkloadMemory(current, previous, &child, where)...)
|
||||
}
|
||||
return errs
|
||||
}
|
||||
@@ -0,0 +1,176 @@
|
||||
/*
|
||||
Copyright 2024 The Cozystack Authors.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package application
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"testing"
|
||||
|
||||
appsv1alpha1 "github.com/cozystack/cozystack/pkg/apis/apps/v1alpha1"
|
||||
helmv2 "github.com/fluxcd/helm-controller/api/v2"
|
||||
apiextv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
|
||||
apierrors "k8s.io/apimachinery/pkg/api/errors"
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"k8s.io/apimachinery/pkg/runtime/schema"
|
||||
"k8s.io/apiserver/pkg/endpoints/request"
|
||||
"sigs.k8s.io/controller-runtime/pkg/client"
|
||||
"sigs.k8s.io/controller-runtime/pkg/client/interceptor"
|
||||
)
|
||||
|
||||
func TestWorkloadMemoryAdmission(t *testing.T) {
|
||||
for _, tc := range []struct {
|
||||
name, chart, old, next string
|
||||
upsert, reject bool
|
||||
}{
|
||||
{"new bare bytes", "tcp-balancer", "", `{"resources":{"memory":8}}`, false, true},
|
||||
{"upsert respects floor", "tcp-balancer", "", `{"resources":{"memory":"8"}}`, true, true},
|
||||
{"new zero", "tcp-balancer", "", `{"resources":{"memory":0}}`, false, true},
|
||||
{"minimum", "tcp-balancer", "", `{"resources":{"memory":"1Mi"}}`, false, false},
|
||||
{"bytes above minimum", "tcp-balancer", "", `{"resources":{"memory":1048576}}`, false, false},
|
||||
{"unchanged scaled to zero", "tcp-balancer", `{"resources":{"memory":8},"replicas":0}`, `{"resources":{"memory":8},"replicas":0,"other":true}`, false, false},
|
||||
{"same quantity spelling", "tcp-balancer", `{"resources":{"memory":8}}`, `{"resources":{"memory":"8"}}`, false, false},
|
||||
{"changed zero", "tcp-balancer", `{"resources":{"memory":8}}`, `{"resources":{"memory":0}}`, false, true},
|
||||
{"repaired", "tcp-balancer", `{"resources":{"memory":8}}`, `{"resources":{"memory":"8Gi"}}`, false, false},
|
||||
{"back to preset", "tcp-balancer", `{"resources":{"memory":8}}`, `{"resourcesPreset":"t1.nano"}`, false, false},
|
||||
{"nested component", "kubernetes", "", `{"controlPlane":{"apiServer":{"resources":{"memory":"1023Ki"}}}}`, false, true},
|
||||
{"quota zero", "tenant", "", `{"resourceQuotas":{"memory":0}}`, false, false},
|
||||
{"free form config", "nats", "", `{"config":{"merge":{"resourcesPreset":"example","resources":{"memory":8}}}}`, false, false},
|
||||
} {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
app := &appsv1alpha1.Application{
|
||||
TypeMeta: metav1.TypeMeta{Kind: kubernetesKind, APIVersion: "apps.cozystack.io/v1alpha1"},
|
||||
ObjectMeta: metav1.ObjectMeta{Name: "good-name", Namespace: "tenant-foo"},
|
||||
}
|
||||
app.Spec = &apiextv1.JSON{Raw: []byte(tc.next)}
|
||||
var stored []client.Object
|
||||
if tc.old != "" {
|
||||
stored = []client.Object{&helmv2.HelmRelease{ObjectMeta: metav1.ObjectMeta{Name: "kubernetes-" + app.Name, Namespace: app.Namespace, Labels: map[string]string{
|
||||
ApplicationKindLabel: kubernetesKind, ApplicationGroupLabel: appsv1alpha1.GroupName, ApplicationNameLabel: app.Name,
|
||||
}}, Spec: helmv2.HelmReleaseSpec{Values: &apiextv1.JSON{Raw: []byte(tc.old)}}}}
|
||||
}
|
||||
r := newKubernetesWarnREST(t, stored...)
|
||||
r.specSchema = chartSpecSchema(t, tc.chart)
|
||||
ctx := request.WithNamespace(context.Background(), app.Namespace)
|
||||
var err error
|
||||
if tc.old != "" || tc.upsert {
|
||||
_, _, err = r.Update(ctx, app.Name, newDefaultUpdatedObjectInfo(app), nil, nil, tc.upsert, &metav1.UpdateOptions{})
|
||||
} else {
|
||||
_, err = r.Create(ctx, app, nil, &metav1.CreateOptions{})
|
||||
}
|
||||
if tc.reject && !apierrors.IsInvalid(err) || !tc.reject && err != nil {
|
||||
t.Fatalf("reject=%v, got %v", tc.reject, err)
|
||||
}
|
||||
if tc.reject {
|
||||
list := &helmv2.HelmReleaseList{}
|
||||
if err := r.c.List(ctx, list); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(list.Items) != len(stored) || len(stored) > 0 && string(list.Items[0].Spec.Values.Raw) != tc.old {
|
||||
t.Fatal("a rejected request changed storage")
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// A caller that read the app before another writer fixed its memory still
|
||||
// carries the old value. It must not put that value back, whether the fix lands
|
||||
// before Update reads the live release or between that read and the write.
|
||||
func TestWorkloadMemoryRecheckedAgainstLiveRelease(t *testing.T) {
|
||||
const stale, fixed = `{"resources":{"memory":8}}`, `{"resources":{"memory":"512Mi"}}`
|
||||
conflict := func(name string) error {
|
||||
return apierrors.NewConflict(schema.GroupResource{Group: "helm.toolkit.fluxcd.io", Resource: "helmreleases"}, name, errors.New("simulated concurrent write"))
|
||||
}
|
||||
setValues := func(ctx context.Context, c client.WithWatch, key client.ObjectKey, values string) error {
|
||||
live := &helmv2.HelmRelease{}
|
||||
if err := c.Get(ctx, key, live); err != nil {
|
||||
return err
|
||||
}
|
||||
live.Spec.Values = &apiextv1.JSON{Raw: []byte(values)}
|
||||
return c.Update(ctx, live)
|
||||
}
|
||||
for _, tc := range []struct {
|
||||
name, next, want string
|
||||
reject bool
|
||||
funcs func() interceptor.Funcs
|
||||
}{
|
||||
{"fixed after the first read", `{"resources":{"memory":8},"other":true}`, fixed, true, func() interceptor.Funcs {
|
||||
gets := 0
|
||||
return interceptor.Funcs{Get: func(ctx context.Context, c client.WithWatch, key client.ObjectKey, obj client.Object, opts ...client.GetOption) error {
|
||||
err := c.Get(ctx, key, obj, opts...)
|
||||
if _, ok := obj.(*helmv2.HelmRelease); ok && err == nil {
|
||||
if gets++; gets == 1 {
|
||||
return setValues(ctx, c, key, fixed)
|
||||
}
|
||||
}
|
||||
return err
|
||||
}}
|
||||
}},
|
||||
{"fixed between the live read and the write", `{"resources":{"memory":8},"other":true}`, fixed, true, func() interceptor.Funcs {
|
||||
writes := 0
|
||||
return interceptor.Funcs{Update: func(ctx context.Context, c client.WithWatch, obj client.Object, opts ...client.UpdateOption) error {
|
||||
if _, ok := obj.(*helmv2.HelmRelease); ok {
|
||||
if writes++; writes == 1 {
|
||||
if err := setValues(ctx, c, client.ObjectKeyFromObject(obj), fixed); err != nil {
|
||||
return err
|
||||
}
|
||||
return conflict(obj.GetName())
|
||||
}
|
||||
}
|
||||
return c.Update(ctx, obj, opts...)
|
||||
}}
|
||||
}},
|
||||
{"unrelated edit after a conflict", `{"resources":{"memory":8},"other":true}`, `{"resources":{"memory":8},"other":true}`, false, func() interceptor.Funcs {
|
||||
writes := 0
|
||||
return interceptor.Funcs{Update: func(ctx context.Context, c client.WithWatch, obj client.Object, opts ...client.UpdateOption) error {
|
||||
if _, ok := obj.(*helmv2.HelmRelease); ok {
|
||||
if writes++; writes == 1 {
|
||||
return conflict(obj.GetName())
|
||||
}
|
||||
}
|
||||
return c.Update(ctx, obj, opts...)
|
||||
}}
|
||||
}},
|
||||
} {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
app := &appsv1alpha1.Application{
|
||||
TypeMeta: metav1.TypeMeta{Kind: kubernetesKind, APIVersion: "apps.cozystack.io/v1alpha1"},
|
||||
ObjectMeta: metav1.ObjectMeta{Name: "good-name", Namespace: "tenant-foo"},
|
||||
Spec: &apiextv1.JSON{Raw: []byte(tc.next)},
|
||||
}
|
||||
stored := &helmv2.HelmRelease{ObjectMeta: metav1.ObjectMeta{Name: "kubernetes-" + app.Name, Namespace: app.Namespace, Labels: map[string]string{
|
||||
ApplicationKindLabel: kubernetesKind, ApplicationGroupLabel: appsv1alpha1.GroupName, ApplicationNameLabel: app.Name,
|
||||
}}, Spec: helmv2.HelmReleaseSpec{Values: &apiextv1.JSON{Raw: []byte(stale)}}}
|
||||
r := newKubernetesWarnREST(t, stored)
|
||||
r.specSchema = chartSpecSchema(t, "tcp-balancer")
|
||||
r.c = interceptor.NewClient(r.w, tc.funcs())
|
||||
ctx := request.WithNamespace(context.Background(), app.Namespace)
|
||||
_, _, err := r.Update(ctx, app.Name, newDefaultUpdatedObjectInfo(app), nil, nil, false, &metav1.UpdateOptions{})
|
||||
if tc.reject && !apierrors.IsInvalid(err) || !tc.reject && err != nil {
|
||||
t.Fatalf("reject=%v, got %v", tc.reject, err)
|
||||
}
|
||||
got := &helmv2.HelmRelease{}
|
||||
if err := r.w.Get(ctx, client.ObjectKeyFromObject(stored), got); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if string(got.Spec.Values.Raw) != tc.want {
|
||||
t.Fatalf("stored values %s, want %s", got.Spec.Values.Raw, tc.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -213,6 +213,10 @@ func (r *REST) Create(ctx context.Context, obj runtime.Object, createValidation
|
||||
}
|
||||
}
|
||||
|
||||
if err := r.validateWorkloadMemory(app, nil); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Validate that values don't contain reserved keys (starting with "_")
|
||||
if err := validateNoInternalKeys(app.Spec); err != nil {
|
||||
return nil, apierrors.NewBadRequest(err.Error())
|
||||
@@ -537,6 +541,7 @@ func (r *REST) Update(ctx context.Context, name string, objInfo rest.UpdatedObje
|
||||
return nil, false, err
|
||||
}
|
||||
|
||||
previousValues := oldObj.(*appsv1alpha1.Application).Spec.DeepCopy()
|
||||
previousFinalizers := slices.Clone(oldObj.(*appsv1alpha1.Application).Finalizers)
|
||||
|
||||
// Update the Application object
|
||||
@@ -559,6 +564,10 @@ func (r *REST) Update(ctx context.Context, name string, objInfo rest.UpdatedObje
|
||||
// Update because Kubernetes names are immutable. Validating here would block
|
||||
// updates to pre-existing resources whose names don't conform to the new rules.
|
||||
|
||||
if err := r.validateWorkloadMemory(app, previousValues); err != nil {
|
||||
return nil, false, err
|
||||
}
|
||||
|
||||
// Validate that values don't contain reserved keys (starting with "_")
|
||||
if err := validateNoInternalKeys(app.Spec); err != nil {
|
||||
return nil, false, apierrors.NewBadRequest(err.Error())
|
||||
@@ -614,6 +623,12 @@ func (r *REST) Update(ctx context.Context, name string, objInfo rest.UpdatedObje
|
||||
if helmRelease.ResourceVersion == "" {
|
||||
helmRelease.SetResourceVersion(cur.GetResourceVersion())
|
||||
}
|
||||
// The memory check above compared against the read at the start of Update.
|
||||
// Compare again against the live release, so a stale value below 1Mi cannot
|
||||
// overwrite memory that another writer has fixed since.
|
||||
if err := r.validateWorkloadMemory(app, cur.Spec.Values); err != nil {
|
||||
return nil, false, err
|
||||
}
|
||||
r.applyLiveMetadata(helmRelease, cur, app, previousFinalizers)
|
||||
|
||||
// Suspension is not part of the Application, so the rebuilt object always
|
||||
@@ -654,6 +669,9 @@ func (r *REST) Update(ctx context.Context, name string, objInfo rest.UpdatedObje
|
||||
if getErr := r.c.Get(ctx, client.ObjectKey{Namespace: helmRelease.Namespace, Name: helmRelease.Name}, cur, &client.GetOptions{Raw: &metav1.GetOptions{}}); getErr != nil {
|
||||
return getErr
|
||||
}
|
||||
if memErr := r.validateWorkloadMemory(app, cur.Spec.Values); memErr != nil {
|
||||
return memErr
|
||||
}
|
||||
helmRelease = converted.DeepCopy()
|
||||
r.applyLiveMetadata(helmRelease, cur, app, previousFinalizers)
|
||||
helmRelease.SetResourceVersion(cur.GetResourceVersion())
|
||||
|
||||
Reference in new issue
Block a user