Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"context"
"fmt"
"reflect"
"time"

"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down Expand Up @@ -62,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,
Expand All @@ -80,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}
Expand Down Expand Up @@ -239,6 +242,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: r.reconciler.schemaSyncInterval}, nil
}

return ctrl.Result{}, nil
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -154,7 +155,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
}
}
Expand All @@ -164,38 +165,68 @@ 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
// 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; for policy=Always
// it allows hash-based spec sync to run independently of export lifecycle.
if export != nil {
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
}
}
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)
}
Expand Down Expand Up @@ -233,7 +264,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
Expand Down
7 changes: 4 additions & 3 deletions backend/http/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
}

Expand Down
29 changes: 20 additions & 9 deletions backend/options/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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, "<resource>.<group> that is automatically selected on th bind screen for testing")
fs.MarkHidden("testing-auto-select") //nolint:errcheck
}
Expand Down Expand Up @@ -299,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
}
1 change: 1 addition & 0 deletions backend/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions contrib/kcp/deploy/resources/apiexport-kube-bind.io.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -72,7 +72,7 @@ spec:
crd: {}
- group: kube-bind.io
name: apiserviceexporttemplates
schema: v251112-503d98b.apiserviceexporttemplates.kube-bind.io
schema: v260224-1c64e9a.apiserviceexporttemplates.kube-bind.io
storage:
crd: {}
- group: kube-bind.io
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
apiVersion: apis.kcp.io/v1alpha1
kind: APIResourceSchema
metadata:
name: v251112-503d98b.apiserviceexporttemplates.kube-bind.io
name: v260224-1c64e9a.apiserviceexporttemplates.kube-bind.io
spec:
group: kube-bind.io
names:
Expand Down Expand Up @@ -244,6 +244,22 @@ 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

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
type: string
scope:
allOf:
- enum:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,22 @@ 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

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
type: string
scope:
allOf:
- enum:
Expand Down
14 changes: 14 additions & 0 deletions deploy/crd/kube-bind.io_apiserviceexportrequests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading