mirror of
https://github.com/cozystack/cozystack.git
synced 2026-11-30 04:06:34 +09:00
fix(api): skip an invalid ApplicationDefinition instead of failing startup
cozystack-api built its resource config from every ApplicationDefinition
and returned the first error, so one definition with an unparseable
release.cozystack.io/* timeout annotation stopped the API for every
tenant. The definition controller rolls cozystack-api when those
annotations change, so the bad definition also triggered the failing
rollout. Since a connected repository can install definitions, this is
no longer limited to an operator's own mistake.
Leave such a definition out of the served kinds and log it, the same way
a definition that loses its kind to an older one is left out. An
openAPISchema that is not valid JSON is rejected at the same point:
the OpenAPI post-processors would otherwise fail the whole spec on it.
Refs: #4976
Assisted-by: LLM
Signed-off-by: Adham Kiwan <adhamkiwan@outlook.com>
(cherry picked from commit 918a60462e)
Signed-off-by: Myasnikov Daniil <myasnikovdaniil2001@gmail.com>
This commit is contained in:
1 parent
3254b87209
commit
bdb3130fae
2 files changed
+82
-14
No files matched your search
+32
-11
@@ -25,6 +25,7 @@ import (
|
||||
"io"
|
||||
"net"
|
||||
"slices"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
v1alpha1 "github.com/cozystack/cozystack/api/v1alpha1"
|
||||
@@ -44,6 +45,7 @@ import (
|
||||
utilfeature "k8s.io/apiserver/pkg/util/feature"
|
||||
basecompatibility "k8s.io/component-base/compatibility"
|
||||
baseversion "k8s.io/component-base/version"
|
||||
"k8s.io/kube-openapi/pkg/validation/spec"
|
||||
netutils "k8s.io/utils/net"
|
||||
"sigs.k8s.io/controller-runtime/pkg/client"
|
||||
k8sconfig "sigs.k8s.io/controller-runtime/pkg/client/config"
|
||||
@@ -268,10 +270,7 @@ func (o *CozyServerOptions) Complete() error {
|
||||
delay = min(time.Duration(float64(delay)*1.5), maxDelay)
|
||||
}
|
||||
|
||||
resourceConfig, err := resourceConfigFrom(crdList.Items, hrFlags)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
resourceConfig, _ := resourceConfigFrom(crdList.Items, hrFlags)
|
||||
o.ResourceConfig = resourceConfig
|
||||
|
||||
return nil
|
||||
@@ -281,20 +280,29 @@ func (o *CozyServerOptions) Complete() error {
|
||||
// cozystack-api serves. A kind declared by more than one definition is
|
||||
// registered once, for the definition that owns it, rather than as two
|
||||
// resources for one GVK; the definitions left out are logged.
|
||||
func resourceConfigFrom(defs []v1alpha1.ApplicationDefinition, hrFlags helmReleaseFlagValues) (*config.ResourceConfig, error) {
|
||||
//
|
||||
// A definition that cannot be turned into a resource (an unparseable
|
||||
// release.cozystack.io/* annotation, an openAPISchema that is not valid JSON)
|
||||
// is left out and logged too, and returned in the error list. Failing startup
|
||||
// on it would let one definition take cozystack-api down for every tenant, and
|
||||
// definitions can come from a connected repository, not only from an operator.
|
||||
func resourceConfigFrom(defs []v1alpha1.ApplicationDefinition, hrFlags helmReleaseFlagValues) (*config.ResourceConfig, []error) {
|
||||
owned, skipped := ownedDefinitions(defs)
|
||||
for _, msg := range skipped {
|
||||
fmt.Printf("Skipping %s\n", msg)
|
||||
}
|
||||
resourceConfig := &config.ResourceConfig{}
|
||||
var invalid []error
|
||||
for _, crd := range owned {
|
||||
resource, err := buildResourceFromCRD(crd, hrFlags)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
fmt.Printf("Skipping invalid ApplicationDefinition %q: %v\n", crd.Name, err)
|
||||
invalid = append(invalid, err)
|
||||
continue
|
||||
}
|
||||
resourceConfig.Resources = append(resourceConfig.Resources, resource)
|
||||
}
|
||||
return resourceConfig, nil
|
||||
return resourceConfig, invalid
|
||||
}
|
||||
|
||||
// ownedDefinitions returns the definitions that own their application kind
|
||||
@@ -354,10 +362,10 @@ func buildResourceFromCRD(crd v1alpha1.ApplicationDefinition, hrFlags helmReleas
|
||||
// Kamaji-provisioned admin-kubeconfig Secret for Kubernetes tenants) need a
|
||||
// longer wait budget than the Flux default. Consumed by the REST storage
|
||||
// layer when building the HelmRelease Spec. The parser rejects
|
||||
// units Flux would reject at webhook time, so a bad annotation
|
||||
// surfaces as a loud startup failure instead of a silent drop to
|
||||
// defaults. helm-install-timeout covers both install and upgrade;
|
||||
// helm-upgrade-timeout overrides only the upgrade side for kinds
|
||||
// units Flux would reject at webhook time, so a bad annotation leaves
|
||||
// this kind out of cozystack-api (see resourceConfigFrom) instead of a
|
||||
// silent drop to defaults. helm-install-timeout covers both install and
|
||||
// upgrade; helm-upgrade-timeout overrides only the upgrade side for kinds
|
||||
// that need an asymmetric budget.
|
||||
installTimeout, err := config.ParseHelmTimeoutAnnotation(
|
||||
crd.Annotations[config.HelmInstallTimeoutAnnotation],
|
||||
@@ -400,6 +408,19 @@ func buildResourceFromCRD(crd v1alpha1.ApplicationDefinition, hrFlags helmReleas
|
||||
)
|
||||
}
|
||||
release.HelmServerSideApply = serverSideApply
|
||||
|
||||
// The OpenAPI post-processors parse this schema for every served kind and
|
||||
// fail the whole spec on one that does not parse, so reject it here, where
|
||||
// only this definition is left out.
|
||||
if raw := crd.Spec.Application.OpenAPISchema; strings.TrimSpace(raw) != "" {
|
||||
var schema spec.Schema
|
||||
if err := json.Unmarshal([]byte(raw), &schema); err != nil {
|
||||
return config.Resource{}, fmt.Errorf(
|
||||
"ApplicationDefinition %q has invalid spec.application.openAPISchema: %w",
|
||||
crd.Name, err,
|
||||
)
|
||||
}
|
||||
}
|
||||
return config.Resource{
|
||||
Application: config.ApplicationConfig{
|
||||
Kind: crd.Spec.Application.Kind,
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
@@ -8,6 +9,7 @@ import (
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
|
||||
"github.com/cozystack/cozystack/api/v1alpha1"
|
||||
"github.com/cozystack/cozystack/pkg/config"
|
||||
)
|
||||
|
||||
// TestOwnedDefinitionsDropsKindDuplicate pins that cozystack-api registers one
|
||||
@@ -51,14 +53,59 @@ func TestResourceConfigFromRegistersOwnerOnly(t *testing.T) {
|
||||
d.Spec.Release.ChartRef = &helmv2.CrossNamespaceSourceReference{Kind: "ExternalArtifact", Name: artifact, Namespace: "cozy-system"}
|
||||
return d
|
||||
}
|
||||
rc, err := resourceConfigFrom([]v1alpha1.ApplicationDefinition{
|
||||
rc, invalid := resourceConfigFrom([]v1alpha1.ApplicationDefinition{
|
||||
mk("a-redis-shadow", "shadow-artifact", base.Add(time.Hour)),
|
||||
mk("redis", "redis-artifact", base),
|
||||
}, helmReleaseFlagValues{})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
if len(invalid) != 0 {
|
||||
t.Fatal(invalid)
|
||||
}
|
||||
if len(rc.Resources) != 1 || rc.Resources[0].Release.ChartRef.Name != "redis-artifact" {
|
||||
t.Fatalf("resources = %+v, want the owner's redis-artifact only", rc.Resources)
|
||||
}
|
||||
}
|
||||
|
||||
// TestResourceConfigFromSkipsInvalidDefinition pins that one broken
|
||||
// ApplicationDefinition no longer stops cozystack-api from starting: it is left
|
||||
// out and reported, and every valid definition is still served.
|
||||
func TestResourceConfigFromSkipsInvalidDefinition(t *testing.T) {
|
||||
mk := func(name, kind string) v1alpha1.ApplicationDefinition {
|
||||
d := v1alpha1.ApplicationDefinition{ObjectMeta: metav1.ObjectMeta{Name: name}}
|
||||
d.Spec.Application.Kind = kind
|
||||
d.Spec.Release.ChartRef = &helmv2.CrossNamespaceSourceReference{Kind: "ExternalArtifact", Name: name, Namespace: "cozy-system"}
|
||||
return d
|
||||
}
|
||||
badTimeout := mk("bad-timeout", "BadTimeout")
|
||||
badTimeout.Annotations = map[string]string{config.HelmInstallTimeoutAnnotation: "ten minutes"}
|
||||
badSchema := mk("bad-schema", "BadSchema")
|
||||
badSchema.Spec.Application.OpenAPISchema = `{"type": "object",`
|
||||
// A valid, nonblank schema must still be accepted.
|
||||
redis := mk("redis", "Redis")
|
||||
redis.Spec.Application.OpenAPISchema = `{"type": "object", "properties": {"replicas": {"type": "integer"}}}`
|
||||
|
||||
rc, invalid := resourceConfigFrom([]v1alpha1.ApplicationDefinition{
|
||||
redis,
|
||||
badTimeout,
|
||||
badSchema,
|
||||
mk("postgres", "Postgres"),
|
||||
}, helmReleaseFlagValues{})
|
||||
|
||||
kinds := map[string]bool{}
|
||||
for _, r := range rc.Resources {
|
||||
kinds[r.Application.Kind] = true
|
||||
}
|
||||
if len(rc.Resources) != 2 || !kinds["Redis"] || !kinds["Postgres"] {
|
||||
t.Fatalf("served kinds = %v, want Redis and Postgres only", kinds)
|
||||
}
|
||||
if len(invalid) != 2 {
|
||||
t.Fatalf("invalid = %v, want one error each for bad-timeout and bad-schema", invalid)
|
||||
}
|
||||
for i, want := range []string{
|
||||
`ApplicationDefinition "bad-timeout" has invalid ` + config.HelmInstallTimeoutAnnotation + ` annotation`,
|
||||
`ApplicationDefinition "bad-schema" has invalid spec.application.openAPISchema`,
|
||||
} {
|
||||
if !strings.Contains(invalid[i].Error(), want) {
|
||||
t.Errorf("invalid[%d] = %q, want it to contain %q", i, invalid[i], want)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user