From 052c97d12d922765bb1835f2a0ddf8f7a8aa6d0e Mon Sep 17 00:00:00 2001 From: Karol Szwaj Date: Mon, 9 Feb 2026 14:21:00 +0100 Subject: [PATCH 1/4] feat: add SchemaUpdatePolicy for BoundSchemas On-behalf-of: @SAP karol.szwaj@sap.com Signed-off-by: Karol Szwaj --- .../serviceexportrequest_controller.go | 7 +++ .../serviceexportrequest_reconcile.go | 63 +++++++++++++------ backend/http/handler.go | 7 ++- .../resources/apiexport-kube-bind.io.yaml | 4 +- ...apiserviceexportrequests.kube-bind.io.yaml | 16 ++++- ...piserviceexporttemplates.kube-bind.io.yaml | 13 +++- ...kube-bind.io_apiserviceexportrequests.yaml | 14 +++++ ...ube-bind.io_apiserviceexporttemplates.yaml | 11 ++++ ...kube-bind.io_apiserviceexportrequests.yaml | 14 +++++ ...ube-bind.io_apiserviceexporttemplates.yaml | 11 ++++ .../v1alpha2/apiserviceexportrequest_types.go | 10 +++ .../apiserviceexporttemplate_types.go | 18 ++++++ .../kubebind/v1alpha2/helpers/boundschema.go | 12 +++- 13 files changed, 172 insertions(+), 28 deletions(-) diff --git a/backend/controllers/serviceexportrequest/serviceexportrequest_controller.go b/backend/controllers/serviceexportrequest/serviceexportrequest_controller.go index 318584c0f..1c809e58f 100644 --- a/backend/controllers/serviceexportrequest/serviceexportrequest_controller.go +++ b/backend/controllers/serviceexportrequest/serviceexportrequest_controller.go @@ -20,6 +20,7 @@ import ( "context" "fmt" "reflect" + "time" "k8s.io/apimachinery/pkg/api/errors" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" @@ -239,6 +240,12 @@ func (r *APIServiceExportRequestReconciler) Reconcile(ctx context.Context, req m logger.Info("APIServiceExportRequest status updated", "namespace", apiServiceExportRequest.Namespace, "name", apiServiceExportRequest.Name) } + // When SchemaUpdatePolicy is Always, periodically re-reconcile to pick up + // source CRD changes and sync them into BoundSchemas. + if apiServiceExportRequest.Spec.SchemaUpdatePolicy == kubebindv1alpha2.SchemaUpdatePolicyAlways { + return ctrl.Result{RequeueAfter: 30 * time.Second}, nil + } + return ctrl.Result{}, nil } diff --git a/backend/controllers/serviceexportrequest/serviceexportrequest_reconcile.go b/backend/controllers/serviceexportrequest/serviceexportrequest_reconcile.go index 9cd71cb7e..cd1d28118 100644 --- a/backend/controllers/serviceexportrequest/serviceexportrequest_reconcile.go +++ b/backend/controllers/serviceexportrequest/serviceexportrequest_reconcile.go @@ -154,7 +154,7 @@ func (r *reconciler) ensureBoundSchemas(ctx context.Context, cl client.Client, e boundSchema.Spec.InformerScope = r.informerScope boundSchema.ResourceVersion = "" - if err := r.createOrUpdateBoundSchema(ctx, cl, export, boundSchema); err != nil { + if err := r.createOrUpdateBoundSchema(ctx, cl, export, boundSchema, req.Spec.SchemaUpdatePolicy); err != nil { return err } } @@ -164,38 +164,64 @@ func (r *reconciler) ensureBoundSchemas(ctx context.Context, cl client.Client, e return nil } -func (r *reconciler) createOrUpdateBoundSchema(ctx context.Context, cl client.Client, export *kubebindv1alpha2.APIServiceExport, desired *kubebindv1alpha2.BoundSchema) error { +func (r *reconciler) createOrUpdateBoundSchema(ctx context.Context, cl client.Client, export *kubebindv1alpha2.APIServiceExport, desired *kubebindv1alpha2.BoundSchema, schemaUpdatePolicy kubebindv1alpha2.SchemaUpdatePolicy) error { logger := klog.FromContext(ctx) + // If namespaced isolation is configured for cluster-scoped objects, + // we need to rewrite the BoundSchema's scope accordingly. + if desired.Spec.Scope == apiextensionsv1.NamespaceScoped && r.isolation == kubebindv1alpha2.IsolationNamespaced { + desired.Spec.Scope = apiextensionsv1.ClusterScoped + } + + hash, err := helpers.BoundSchemaSpecHash(&desired.Spec) + if err != nil { + return err + } + existing, err := r.getBoundSchema(ctx, cl, desired.Namespace, desired.Name) if err != nil && !apierrors.IsNotFound(err) && !strings.Contains(err.Error(), "no matches for kind") { return err } if existing != nil { - if export == nil { - return nil + var needsUpdate bool + if export != nil { + existingRefs := len(existing.GetOwnerReferences()) + if err := controllerutil.SetControllerReference(export, existing, cl.Scheme()); err != nil { + return fmt.Errorf("failed to set owner reference on BoundSchema %s: %w", desired.Name, err) + } + if len(existing.GetOwnerReferences()) != existingRefs { + needsUpdate = true + } } - if err := controllerutil.SetControllerReference(export, existing, cl.Scheme()); err != nil { - return fmt.Errorf("failed to set owner reference on BoundSchema %s: %w", desired.Name, err) + + if schemaUpdatePolicy == kubebindv1alpha2.SchemaUpdatePolicyAlways { + if existing.Annotations[kubebindv1alpha2.SourceSpecHashAnnotationKey] != hash { + existing.Spec = desired.Spec + if existing.Annotations == nil { + existing.Annotations = make(map[string]string) + } + existing.Annotations[kubebindv1alpha2.SourceSpecHashAnnotationKey] = hash + needsUpdate = true + } } - if err := r.updateBoundSchema(ctx, cl, existing); err != nil { - return fmt.Errorf("failed to update BoundSchema %s with owner reference: %w", desired.Name, err) + + if needsUpdate { + if err := r.updateBoundSchema(ctx, cl, existing); err != nil { + return fmt.Errorf("failed to update BoundSchema %s: %w", desired.Name, err) + } + logger.V(6).Info("Updated existing BoundSchema", + "boundSchema", desired.Name, + "namespace", desired.Namespace) } - logger.V(6).Info("Updated owner reference on existing BoundSchema", - "boundSchema", desired.Name, - "export", export.Name, - "namespace", desired.Namespace) return nil } - // If namespaced isolation is configured for cluster-scoped objects, - // we need to rewrite the BoundSchema's scope accordingly. For all - // other isolation strategies, as well as for namespaced schemas, - // no changes are necessary. - if desired.Spec.Scope == apiextensionsv1.NamespaceScoped && r.isolation == kubebindv1alpha2.IsolationNamespaced { - desired.Spec.Scope = apiextensionsv1.ClusterScoped + // Create path: new BoundSchema. + if desired.Annotations == nil { + desired.Annotations = make(map[string]string) } + desired.Annotations[kubebindv1alpha2.SourceSpecHashAnnotationKey] = hash return r.createBoundSchema(ctx, cl, desired) } @@ -233,7 +259,6 @@ func (r *reconciler) ensureExports(ctx context.Context, cl client.Client, existi return nil } - // https://github.com/kube-bind/kube-bind/issues/297 To fix. hash, err := helpers.BoundSchemasSpecHash(schemas) if err != nil { return err diff --git a/backend/http/handler.go b/backend/http/handler.go index 3a9ea5d37..f8dcbb9c4 100644 --- a/backend/http/handler.go +++ b/backend/http/handler.go @@ -393,9 +393,10 @@ func (h *handler) handleBind(w http.ResponseWriter, r *http.Request) { Name: bindRequest.Name, }, Spec: kubebindv1alpha2.APIServiceExportRequestSpec{ - Resources: template.Spec.Resources, - PermissionClaims: template.Spec.PermissionClaims, - Namespaces: template.Spec.Namespaces, + Resources: template.Spec.Resources, + PermissionClaims: template.Spec.PermissionClaims, + Namespaces: template.Spec.Namespaces, + SchemaUpdatePolicy: template.Spec.SchemaUpdatePolicy, }, } diff --git a/contrib/kcp/deploy/resources/apiexport-kube-bind.io.yaml b/contrib/kcp/deploy/resources/apiexport-kube-bind.io.yaml index c7dc24a21..f32f71694 100644 --- a/contrib/kcp/deploy/resources/apiexport-kube-bind.io.yaml +++ b/contrib/kcp/deploy/resources/apiexport-kube-bind.io.yaml @@ -62,7 +62,7 @@ spec: crd: {} - group: kube-bind.io name: apiserviceexportrequests - schema: v251112-503d98b.apiserviceexportrequests.kube-bind.io + schema: v260220-f03e1ee.apiserviceexportrequests.kube-bind.io storage: crd: {} - group: kube-bind.io @@ -72,7 +72,7 @@ spec: crd: {} - group: kube-bind.io name: apiserviceexporttemplates - schema: v251112-503d98b.apiserviceexporttemplates.kube-bind.io + schema: v260220-f03e1ee.apiserviceexporttemplates.kube-bind.io storage: crd: {} - group: kube-bind.io diff --git a/contrib/kcp/deploy/resources/apiresourceschema-apiserviceexportrequests.kube-bind.io.yaml b/contrib/kcp/deploy/resources/apiresourceschema-apiserviceexportrequests.kube-bind.io.yaml index 86311c057..a9c4abdfb 100644 --- a/contrib/kcp/deploy/resources/apiresourceschema-apiserviceexportrequests.kube-bind.io.yaml +++ b/contrib/kcp/deploy/resources/apiresourceschema-apiserviceexportrequests.kube-bind.io.yaml @@ -1,7 +1,7 @@ apiVersion: apis.kcp.io/v1alpha1 kind: APIResourceSchema metadata: - name: v251112-503d98b.apiserviceexportrequests.kube-bind.io + name: v260220-f03e1ee.apiserviceexportrequests.kube-bind.io spec: conversion: strategy: None @@ -417,6 +417,20 @@ spec: x-kubernetes-validations: - message: resources are immutable rule: self == oldSelf + schemaUpdatePolicy: + default: Never + description: |- + schemaUpdatePolicy defines how BoundSchemas should be updated when the source schema changes. + This is propagated from the APIServiceExportTemplate. + Never means schemas are immutable after creation. + Always means schemas are updated when the source content changes. + enum: + - Never + - Always + type: string + x-kubernetes-validations: + - message: schemaUpdatePolicy is immutable + rule: self == oldSelf required: - resources type: object diff --git a/contrib/kcp/deploy/resources/apiresourceschema-apiserviceexporttemplates.kube-bind.io.yaml b/contrib/kcp/deploy/resources/apiresourceschema-apiserviceexporttemplates.kube-bind.io.yaml index d1b35a8fc..f99970791 100644 --- a/contrib/kcp/deploy/resources/apiresourceschema-apiserviceexporttemplates.kube-bind.io.yaml +++ b/contrib/kcp/deploy/resources/apiresourceschema-apiserviceexporttemplates.kube-bind.io.yaml @@ -1,7 +1,7 @@ apiVersion: apis.kcp.io/v1alpha1 kind: APIResourceSchema metadata: - name: v251112-503d98b.apiserviceexporttemplates.kube-bind.io + name: v260220-f03e1ee.apiserviceexporttemplates.kube-bind.io spec: group: kube-bind.io names: @@ -244,6 +244,17 @@ spec: type: object minItems: 1 type: array + schemaUpdatePolicy: + default: Never + description: |- + SchemaUpdatePolicy defines how BoundSchemas should be updated. + Never means schemas are immutable after creation. + Always means schemas are updated when the source content changes. + Default: Never + enum: + - Never + - Always + type: string scope: allOf: - enum: diff --git a/deploy/charts/backend/crds/kube-bind.io_apiserviceexportrequests.yaml b/deploy/charts/backend/crds/kube-bind.io_apiserviceexportrequests.yaml index 3196ffcdc..d5d06e244 100644 --- a/deploy/charts/backend/crds/kube-bind.io_apiserviceexportrequests.yaml +++ b/deploy/charts/backend/crds/kube-bind.io_apiserviceexportrequests.yaml @@ -421,6 +421,20 @@ spec: x-kubernetes-validations: - message: resources are immutable rule: self == oldSelf + schemaUpdatePolicy: + default: Never + description: |- + schemaUpdatePolicy defines how BoundSchemas should be updated when the source schema changes. + This is propagated from the APIServiceExportTemplate. + Never means schemas are immutable after creation. + Always means schemas are updated when the source content changes. + enum: + - Never + - Always + type: string + x-kubernetes-validations: + - message: schemaUpdatePolicy is immutable + rule: self == oldSelf required: - resources type: object diff --git a/deploy/charts/backend/crds/kube-bind.io_apiserviceexporttemplates.yaml b/deploy/charts/backend/crds/kube-bind.io_apiserviceexporttemplates.yaml index e1735d72c..735d6c75e 100644 --- a/deploy/charts/backend/crds/kube-bind.io_apiserviceexporttemplates.yaml +++ b/deploy/charts/backend/crds/kube-bind.io_apiserviceexporttemplates.yaml @@ -248,6 +248,17 @@ spec: type: object minItems: 1 type: array + schemaUpdatePolicy: + default: Never + description: |- + SchemaUpdatePolicy defines how BoundSchemas should be updated. + Never means schemas are immutable after creation. + Always means schemas are updated when the source content changes. + Default: Never + enum: + - Never + - Always + type: string scope: allOf: - enum: diff --git a/deploy/crd/kube-bind.io_apiserviceexportrequests.yaml b/deploy/crd/kube-bind.io_apiserviceexportrequests.yaml index fc4997665..22ecb0a05 100644 --- a/deploy/crd/kube-bind.io_apiserviceexportrequests.yaml +++ b/deploy/crd/kube-bind.io_apiserviceexportrequests.yaml @@ -422,6 +422,20 @@ spec: x-kubernetes-validations: - message: resources are immutable rule: self == oldSelf + schemaUpdatePolicy: + default: Never + description: |- + schemaUpdatePolicy defines how BoundSchemas should be updated when the source schema changes. + This is propagated from the APIServiceExportTemplate. + Never means schemas are immutable after creation. + Always means schemas are updated when the source content changes. + enum: + - Never + - Always + type: string + x-kubernetes-validations: + - message: schemaUpdatePolicy is immutable + rule: self == oldSelf required: - resources type: object diff --git a/deploy/crd/kube-bind.io_apiserviceexporttemplates.yaml b/deploy/crd/kube-bind.io_apiserviceexporttemplates.yaml index e1735d72c..735d6c75e 100644 --- a/deploy/crd/kube-bind.io_apiserviceexporttemplates.yaml +++ b/deploy/crd/kube-bind.io_apiserviceexporttemplates.yaml @@ -248,6 +248,17 @@ spec: type: object minItems: 1 type: array + schemaUpdatePolicy: + default: Never + description: |- + SchemaUpdatePolicy defines how BoundSchemas should be updated. + Never means schemas are immutable after creation. + Always means schemas are updated when the source content changes. + Default: Never + enum: + - Never + - Always + type: string scope: allOf: - enum: diff --git a/sdk/apis/kubebind/v1alpha2/apiserviceexportrequest_types.go b/sdk/apis/kubebind/v1alpha2/apiserviceexportrequest_types.go index dff74d20d..a02abaeb6 100644 --- a/sdk/apis/kubebind/v1alpha2/apiserviceexportrequest_types.go +++ b/sdk/apis/kubebind/v1alpha2/apiserviceexportrequest_types.go @@ -124,6 +124,16 @@ type APIServiceExportRequestSpec struct { // +optional // +kubebuilder:validation:Optional Namespaces []Namespaces `json:"namespaces,omitempty"` + + // schemaUpdatePolicy defines how BoundSchemas should be updated when the source schema changes. + // This is propagated from the APIServiceExportTemplate. + // Never means schemas are immutable after creation. + // Always means schemas are updated when the source content changes. + // +optional + // +kubebuilder:default="Never" + // +kubebuilder:validation:Enum=Never;Always + // +kubebuilder:validation:XValidation:rule="self == oldSelf",message="schemaUpdatePolicy is immutable" + SchemaUpdatePolicy SchemaUpdatePolicy `json:"schemaUpdatePolicy,omitempty"` } type Namespaces struct { diff --git a/sdk/apis/kubebind/v1alpha2/apiserviceexporttemplate_types.go b/sdk/apis/kubebind/v1alpha2/apiserviceexporttemplate_types.go index 7d7dc8426..122f8c947 100644 --- a/sdk/apis/kubebind/v1alpha2/apiserviceexporttemplate_types.go +++ b/sdk/apis/kubebind/v1alpha2/apiserviceexporttemplate_types.go @@ -86,6 +86,15 @@ type APIServiceExportTemplateSpec struct { // +optional // +kubebuilder:validation:Optional Namespaces []Namespaces `json:"namespaces,omitempty"` + + // SchemaUpdatePolicy defines how BoundSchemas should be updated. + // Never means schemas are immutable after creation. + // Always means schemas are updated when the source content changes. + // Default: Never + // +optional + // +kubebuilder:default="Never" + // +kubebuilder:validation:Enum=Never;Always + SchemaUpdatePolicy SchemaUpdatePolicy `json:"schemaUpdatePolicy,omitempty"` } // APIServiceExportTemplateStatus stores status information about an APIServiceExportTemplate. @@ -103,3 +112,12 @@ type APIServiceExportTemplateList struct { Items []APIServiceExportTemplate `json:"items"` } + +type SchemaUpdatePolicy string + +const ( + // SchemaUpdatePolicyNever means BoundSchemas are immutable after creation (default). + SchemaUpdatePolicyNever SchemaUpdatePolicy = "Never" + // SchemaUpdatePolicyAlways means BoundSchemas are updated when the source schema content changes. + SchemaUpdatePolicyAlways SchemaUpdatePolicy = "Always" +) diff --git a/sdk/apis/kubebind/v1alpha2/helpers/boundschema.go b/sdk/apis/kubebind/v1alpha2/helpers/boundschema.go index 15ebaa7c8..5f815b29d 100644 --- a/sdk/apis/kubebind/v1alpha2/helpers/boundschema.go +++ b/sdk/apis/kubebind/v1alpha2/helpers/boundschema.go @@ -118,11 +118,19 @@ func UnstructuredToBoundSchema(u unstructured.Unstructured) (*kubebindv1alpha2.B return boundSchema, nil } +func BoundSchemaSpecHash(spec *kubebindv1alpha2.BoundSchemaSpec) (string, error) { + hash := sha256.New() + if err := json.NewEncoder(hash).Encode(spec); err != nil { + return "", fmt.Errorf("failed to encode schema spec: %w", err) + } + return fmt.Sprintf("%x", hash.Sum(nil)), nil +} + func BoundSchemasSpecHash(schemas []*kubebindv1alpha2.BoundSchema) (string, error) { hash := sha256.New() for _, schema := range schemas { - if err := json.NewEncoder(hash).Encode(schema); err != nil { - return "", fmt.Errorf("failed to encode schema %s: %w", schema.Name, err) + if err := json.NewEncoder(hash).Encode(schema.Spec); err != nil { + return "", fmt.Errorf("failed to encode schema spec %s: %w", schema.Name, err) } } return fmt.Sprintf("%x", hash.Sum(nil)), nil From 1c64e9ab8d1a02ba860a45dbc58ae006cda17113 Mon Sep 17 00:00:00 2001 From: Karol Szwaj Date: Tue, 24 Feb 2026 12:31:08 +0100 Subject: [PATCH 2/4] make reconcile interval configurable Signed-off-by: Karol Szwaj On-behalf-of: @SAP karol.szwaj@sap.com --- .../serviceexportrequest_controller.go | 10 +++++--- .../serviceexportrequest_reconcile.go | 10 +++++--- backend/options/options.go | 25 ++++++++++++------- backend/server.go | 1 + .../v1alpha2/apiserviceexport_types.go | 3 +++ .../apiserviceexporttemplate_types.go | 6 +++++ 6 files changed, 39 insertions(+), 16 deletions(-) diff --git a/backend/controllers/serviceexportrequest/serviceexportrequest_controller.go b/backend/controllers/serviceexportrequest/serviceexportrequest_controller.go index 1c809e58f..e6e4ab67e 100644 --- a/backend/controllers/serviceexportrequest/serviceexportrequest_controller.go +++ b/backend/controllers/serviceexportrequest/serviceexportrequest_controller.go @@ -63,6 +63,7 @@ func NewAPIServiceExportRequestReconciler( scope kubebindv1alpha2.InformerScope, isolation kubebindv1alpha2.Isolation, schemaSource string, + schemaSyncInterval time.Duration, ) (*APIServiceExportRequestReconciler, error) { // Set up field indexers for APIServiceExportRequests if err := mgr.GetFieldIndexer().IndexField(ctx, &kubebindv1alpha2.APIServiceExportRequest{}, indexers.ServiceExportRequestByServiceExport, @@ -81,9 +82,10 @@ func NewAPIServiceExportRequestReconciler( informerScope: scope, isolation: isolation, reconciler: reconciler{ - informerScope: scope, - isolation: isolation, - schemaSource: schemaSource, + informerScope: scope, + isolation: isolation, + schemaSource: schemaSource, + schemaSyncInterval: schemaSyncInterval, getBoundSchema: func(ctx context.Context, cl client.Client, namespace, name string) (*kubebindv1alpha2.BoundSchema, error) { var schema kubebindv1alpha2.BoundSchema key := types.NamespacedName{Namespace: namespace, Name: name} @@ -243,7 +245,7 @@ func (r *APIServiceExportRequestReconciler) Reconcile(ctx context.Context, req m // When SchemaUpdatePolicy is Always, periodically re-reconcile to pick up // source CRD changes and sync them into BoundSchemas. if apiServiceExportRequest.Spec.SchemaUpdatePolicy == kubebindv1alpha2.SchemaUpdatePolicyAlways { - return ctrl.Result{RequeueAfter: 30 * time.Second}, nil + return ctrl.Result{RequeueAfter: r.reconciler.schemaSyncInterval}, nil } return ctrl.Result{}, nil diff --git a/backend/controllers/serviceexportrequest/serviceexportrequest_reconcile.go b/backend/controllers/serviceexportrequest/serviceexportrequest_reconcile.go index cd1d28118..4374e3dbb 100644 --- a/backend/controllers/serviceexportrequest/serviceexportrequest_reconcile.go +++ b/backend/controllers/serviceexportrequest/serviceexportrequest_reconcile.go @@ -41,9 +41,10 @@ import ( ) type reconciler struct { - informerScope kubebindv1alpha2.InformerScope - isolation kubebindv1alpha2.Isolation - schemaSource string + informerScope kubebindv1alpha2.InformerScope + isolation kubebindv1alpha2.Isolation + schemaSource string + schemaSyncInterval time.Duration getBoundSchema func(ctx context.Context, cl client.Client, namespace, name string) (*kubebindv1alpha2.BoundSchema, error) createBoundSchema func(ctx context.Context, cl client.Client, schema *kubebindv1alpha2.BoundSchema) error @@ -185,6 +186,9 @@ func (r *reconciler) createOrUpdateBoundSchema(ctx context.Context, cl client.Cl if existing != nil { var needsUpdate bool + // When export is nil (APIServiceExport not yet created), we skip owner-reference + // management but still proceed with schema sync so BoundSchemas are ready + // before the export is created on the next reconcile pass. if export != nil { existingRefs := len(existing.GetOwnerReferences()) if err := controllerutil.SetControllerReference(export, existing, cl.Scheme()); err != nil { diff --git a/backend/options/options.go b/backend/options/options.go index 1490515f9..0e675d2ff 100644 --- a/backend/options/options.go +++ b/backend/options/options.go @@ -73,6 +73,10 @@ type ExtraOptions struct { FrontendDisabled bool TokenExpiry time.Duration + + // SchemaSyncInterval is how often the serviceexportrequest controller re-reconciles + // to detect source schema changes when SchemaUpdatePolicy is Always. + SchemaSyncInterval time.Duration } type completedOptions struct { @@ -104,15 +108,16 @@ func NewOptions() *Options { ProviderKcp: providerkcp.NewOptions(), ExtraOptions: ExtraOptions{ - Provider: "kubernetes", - NamespacePrefix: "cluster-", - PrettyName: "Backend", - ConsumerScope: string(kubebindv1alpha2.NamespacedScope), - Isolation: string(kubebindv1alpha2.IsolationPrefixed), - SchemaSource: CustomResourceDefinitionSource.String(), - Frontend: "embedded", // Not used, but indicates to use embedded frontend using SPA. - TokenExpiry: 1 * time.Hour, - FrontendDisabled: false, + Provider: "kubernetes", + NamespacePrefix: "cluster-", + PrettyName: "Backend", + ConsumerScope: string(kubebindv1alpha2.NamespacedScope), + Isolation: string(kubebindv1alpha2.IsolationPrefixed), + SchemaSource: CustomResourceDefinitionSource.String(), + Frontend: "embedded", // Not used, but indicates to use embedded frontend using SPA. + TokenExpiry: 1 * time.Hour, + FrontendDisabled: false, + SchemaSyncInterval: 5 * time.Minute, }, } return opts @@ -180,6 +185,8 @@ func (options *Options) AddFlags(fs *pflag.FlagSet) { values), ) + fs.DurationVar(&options.SchemaSyncInterval, "schema-sync-interval", options.SchemaSyncInterval, "How often to re-sync BoundSchemas when SchemaUpdatePolicy is Always. Default is 5m.") + fs.StringVar(&options.TestingAutoSelect, "testing-auto-select", options.TestingAutoSelect, ". that is automatically selected on th bind screen for testing") fs.MarkHidden("testing-auto-select") //nolint:errcheck } diff --git a/backend/server.go b/backend/server.go index 0525be2fa..0773b5f2b 100644 --- a/backend/server.go +++ b/backend/server.go @@ -221,6 +221,7 @@ func NewServer(ctx context.Context, c *Config) (*Server, error) { kubebindv1alpha2.InformerScope(c.Options.ConsumerScope), kubebindv1alpha2.Isolation(c.Options.Isolation), c.Options.SchemaSource, + c.Options.SchemaSyncInterval, ) if err != nil { return nil, fmt.Errorf("error setting up ServiceExportRequest Controller: %w", err) diff --git a/sdk/apis/kubebind/v1alpha2/apiserviceexport_types.go b/sdk/apis/kubebind/v1alpha2/apiserviceexport_types.go index 76141bb08..b9a6b9ec0 100644 --- a/sdk/apis/kubebind/v1alpha2/apiserviceexport_types.go +++ b/sdk/apis/kubebind/v1alpha2/apiserviceexport_types.go @@ -23,6 +23,9 @@ import ( ) const ( + // SourceSpecHashAnnotationKey is the annotation key used on BoundSchemas and APIServiceExports + // to store a SHA256 hash of the schema spec content. This is used to detect schema drift + // when SchemaUpdatePolicy is Always. SourceSpecHashAnnotationKey = "kube-bind.io/source-spec-hash" // Label is used to indicate which side owns the resource. By changing the label value, diff --git a/sdk/apis/kubebind/v1alpha2/apiserviceexporttemplate_types.go b/sdk/apis/kubebind/v1alpha2/apiserviceexporttemplate_types.go index 122f8c947..9fca3c2f0 100644 --- a/sdk/apis/kubebind/v1alpha2/apiserviceexporttemplate_types.go +++ b/sdk/apis/kubebind/v1alpha2/apiserviceexporttemplate_types.go @@ -91,6 +91,12 @@ type APIServiceExportTemplateSpec struct { // Never means schemas are immutable after creation. // Always means schemas are updated when the source content changes. // Default: Never + // + // NOTE: This field is intentionally mutable on the template so that providers + // can change the default for future binding requests. Already-created + // APIServiceExportRequests use CEL validation (self == oldSelf) to lock in + // the policy chosen at binding time. + // // +optional // +kubebuilder:default="Never" // +kubebuilder:validation:Enum=Never;Always From 9b665ffd6b99ebbced079b2a814b1fa033f0f683 Mon Sep 17 00:00:00 2001 From: Karol Szwaj Date: Tue, 24 Feb 2026 14:00:28 +0100 Subject: [PATCH 3/4] fix codegen Signed-off-by: Karol Szwaj --- .../serviceexportrequest/serviceexportrequest_reconcile.go | 2 ++ contrib/kcp/deploy/resources/apiexport-kube-bind.io.yaml | 2 +- ...ourceschema-apiserviceexporttemplates.kube-bind.io.yaml | 7 ++++++- .../crds/kube-bind.io_apiserviceexporttemplates.yaml | 5 +++++ deploy/crd/kube-bind.io_apiserviceexporttemplates.yaml | 5 +++++ 5 files changed, 19 insertions(+), 2 deletions(-) diff --git a/backend/controllers/serviceexportrequest/serviceexportrequest_reconcile.go b/backend/controllers/serviceexportrequest/serviceexportrequest_reconcile.go index 4374e3dbb..99663ecd5 100644 --- a/backend/controllers/serviceexportrequest/serviceexportrequest_reconcile.go +++ b/backend/controllers/serviceexportrequest/serviceexportrequest_reconcile.go @@ -189,6 +189,8 @@ func (r *reconciler) createOrUpdateBoundSchema(ctx context.Context, cl client.Cl // When export is nil (APIServiceExport not yet created), we skip owner-reference // management but still proceed with schema sync so BoundSchemas are ready // before the export is created on the next reconcile pass. + // For policy=Never this is a no-op (needsUpdate stays false); for policy=Always + // it allows hash-based spec sync to run independently of export lifecycle. if export != nil { existingRefs := len(existing.GetOwnerReferences()) if err := controllerutil.SetControllerReference(export, existing, cl.Scheme()); err != nil { diff --git a/contrib/kcp/deploy/resources/apiexport-kube-bind.io.yaml b/contrib/kcp/deploy/resources/apiexport-kube-bind.io.yaml index f32f71694..fb9f65b22 100644 --- a/contrib/kcp/deploy/resources/apiexport-kube-bind.io.yaml +++ b/contrib/kcp/deploy/resources/apiexport-kube-bind.io.yaml @@ -72,7 +72,7 @@ spec: crd: {} - group: kube-bind.io name: apiserviceexporttemplates - schema: v260220-f03e1ee.apiserviceexporttemplates.kube-bind.io + schema: v260224-1c64e9a.apiserviceexporttemplates.kube-bind.io storage: crd: {} - group: kube-bind.io diff --git a/contrib/kcp/deploy/resources/apiresourceschema-apiserviceexporttemplates.kube-bind.io.yaml b/contrib/kcp/deploy/resources/apiresourceschema-apiserviceexporttemplates.kube-bind.io.yaml index f99970791..bf94d3258 100644 --- a/contrib/kcp/deploy/resources/apiresourceschema-apiserviceexporttemplates.kube-bind.io.yaml +++ b/contrib/kcp/deploy/resources/apiresourceschema-apiserviceexporttemplates.kube-bind.io.yaml @@ -1,7 +1,7 @@ apiVersion: apis.kcp.io/v1alpha1 kind: APIResourceSchema metadata: - name: v260220-f03e1ee.apiserviceexporttemplates.kube-bind.io + name: v260224-1c64e9a.apiserviceexporttemplates.kube-bind.io spec: group: kube-bind.io names: @@ -251,6 +251,11 @@ spec: Never means schemas are immutable after creation. Always means schemas are updated when the source content changes. Default: Never + + NOTE: This field is intentionally mutable on the template so that providers + can change the default for future binding requests. Already-created + APIServiceExportRequests use CEL validation (self == oldSelf) to lock in + the policy chosen at binding time. enum: - Never - Always diff --git a/deploy/charts/backend/crds/kube-bind.io_apiserviceexporttemplates.yaml b/deploy/charts/backend/crds/kube-bind.io_apiserviceexporttemplates.yaml index 735d6c75e..2d254f037 100644 --- a/deploy/charts/backend/crds/kube-bind.io_apiserviceexporttemplates.yaml +++ b/deploy/charts/backend/crds/kube-bind.io_apiserviceexporttemplates.yaml @@ -255,6 +255,11 @@ spec: Never means schemas are immutable after creation. Always means schemas are updated when the source content changes. Default: Never + + NOTE: This field is intentionally mutable on the template so that providers + can change the default for future binding requests. Already-created + APIServiceExportRequests use CEL validation (self == oldSelf) to lock in + the policy chosen at binding time. enum: - Never - Always diff --git a/deploy/crd/kube-bind.io_apiserviceexporttemplates.yaml b/deploy/crd/kube-bind.io_apiserviceexporttemplates.yaml index 735d6c75e..2d254f037 100644 --- a/deploy/crd/kube-bind.io_apiserviceexporttemplates.yaml +++ b/deploy/crd/kube-bind.io_apiserviceexporttemplates.yaml @@ -255,6 +255,11 @@ spec: Never means schemas are immutable after creation. Always means schemas are updated when the source content changes. Default: Never + + NOTE: This field is intentionally mutable on the template so that providers + can change the default for future binding requests. Already-created + APIServiceExportRequests use CEL validation (self == oldSelf) to lock in + the policy chosen at binding time. enum: - Never - Always From 473370c42a52656f154b2d4aac395fe51923ff6e Mon Sep 17 00:00:00 2001 From: Karol Szwaj Date: Wed, 25 Feb 2026 11:36:12 +0100 Subject: [PATCH 4/4] add improvements to the code Signed-off-by: Karol Szwaj On-behalf-of: @SAP karol.szwaj@sap.com --- .../serviceexportrequest_reconcile.go | 11 +++++------ backend/options/options.go | 4 ++++ 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/backend/controllers/serviceexportrequest/serviceexportrequest_reconcile.go b/backend/controllers/serviceexportrequest/serviceexportrequest_reconcile.go index 99663ecd5..d67e26068 100644 --- a/backend/controllers/serviceexportrequest/serviceexportrequest_reconcile.go +++ b/backend/controllers/serviceexportrequest/serviceexportrequest_reconcile.go @@ -189,14 +189,13 @@ func (r *reconciler) createOrUpdateBoundSchema(ctx context.Context, cl client.Cl // When export is nil (APIServiceExport not yet created), we skip owner-reference // management but still proceed with schema sync so BoundSchemas are ready // before the export is created on the next reconcile pass. - // For policy=Never this is a no-op (needsUpdate stays false); for policy=Always + // For policy=Never this is a no-op; for policy=Always // it allows hash-based spec sync to run independently of export lifecycle. if export != nil { - existingRefs := len(existing.GetOwnerReferences()) - if err := controllerutil.SetControllerReference(export, existing, cl.Scheme()); err != nil { - return fmt.Errorf("failed to set owner reference on BoundSchema %s: %w", desired.Name, err) - } - if len(existing.GetOwnerReferences()) != existingRefs { + if !metav1.IsControlledBy(existing, export) { + if err := controllerutil.SetControllerReference(export, existing, cl.Scheme()); err != nil { + return fmt.Errorf("failed to set owner reference on BoundSchema %s: %w", desired.Name, err) + } needsUpdate = true } } diff --git a/backend/options/options.go b/backend/options/options.go index 0e675d2ff..96b64ce7a 100644 --- a/backend/options/options.go +++ b/backend/options/options.go @@ -306,5 +306,9 @@ func (options *CompletedOptions) Validate() error { return fmt.Errorf("invalid schema source: %q", options.SchemaSource) } + if options.SchemaSyncInterval < 10*time.Second { + return fmt.Errorf("--schema-sync-interval must be at least 10s, got %v", options.SchemaSyncInterval) + } + return nil }