Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Manager's cache index related code is scattered in several places #721

Open
1 task
pmalek opened this issue Oct 9, 2024 · 0 comments
Open
1 task

Manager's cache index related code is scattered in several places #721

pmalek opened this issue Oct 9, 2024 · 0 comments

Comments

@pmalek
Copy link
Member

pmalek commented Oct 9, 2024

Problem statement

Currently manager's index related code is scattered across several places:

  • https://github.com/Kong/gateway-operator/blob/fb23412717ccc423ce63d384f6a520d365d8faa1/internal/utils/index/index.go
  • // SetupCacheIndicesForKonnectTypes sets up the cache indices for the controllers.
    // This is done only once because 1 manager's cache can only have one index with
    // a predefined key and so that different controllers can share the same indices.
    func SetupCacheIndicesForKonnectTypes(ctx context.Context, mgr manager.Manager, developmentMode bool) error {
    types := []struct {
    Object interface {
    client.Object
    GetTypeName() string
    }
    IndexOptions []konnect.ReconciliationIndexOption
    }{
    {
    Object: &configurationv1alpha1.KongPluginBinding{},
    IndexOptions: konnect.IndexOptionsForKongPluginBinding(),
    },
    {
    Object: &configurationv1alpha1.KongCredentialBasicAuth{},
    IndexOptions: konnect.IndexOptionsForCredentialsBasicAuth(),
    },
    {
    Object: &configurationv1alpha1.KongCredentialACL{},
    IndexOptions: konnect.IndexOptionsForCredentialsACL(),
    },
    {
    Object: &configurationv1alpha1.KongCredentialJWT{},
    IndexOptions: konnect.IndexOptionsForCredentialsJWT(),
    },
    {
    Object: &configurationv1alpha1.KongCredentialAPIKey{},
    IndexOptions: konnect.IndexOptionsForCredentialsAPIKey(),
    },
    {
    Object: &configurationv1alpha1.KongCredentialHMAC{},
    IndexOptions: konnect.IndexOptionsForCredentialsHMAC(),
    },
    {
    Object: &configurationv1.KongConsumer{},
    IndexOptions: konnect.IndexOptionsForKongConsumer(),
    },
    {
    Object: &configurationv1beta1.KongConsumerGroup{},
    IndexOptions: konnect.IndexOptionsForKongConsumerGroup(),
    },
    {
    Object: &configurationv1alpha1.KongService{},
    IndexOptions: konnect.IndexOptionsForKongService(),
    },
    {
    Object: &configurationv1alpha1.KongRoute{},
    IndexOptions: konnect.IndexOptionsForKongRoute(),
    },
    {
    Object: &configurationv1alpha1.KongUpstream{},
    IndexOptions: konnect.IndexOptionsForKongUpstream(),
    },
    {
    Object: &configurationv1alpha1.KongTarget{},
    IndexOptions: konnect.IndexOptionsForKongTarget(),
    },
    {
    Object: &configurationv1alpha1.KongSNI{},
    IndexOptions: konnect.IndexOptionsForKongSNI(),
    },
    {
    Object: &configurationv1alpha1.KongKey{},
    IndexOptions: konnect.IndexOptionsForKongKey(),
    },
    {
    Object: &configurationv1alpha1.KongKeySet{},
    IndexOptions: konnect.IndexOptionsForKongKeySet(),
    },
    {
    Object: &configurationv1alpha1.KongDataPlaneClientCertificate{},
    IndexOptions: konnect.IndexOptionsForKongDataPlaneCertificate(),
    },
    {
    Object: &configurationv1alpha1.KongVault{},
    IndexOptions: konnect.IndexOptionsForKongVault(),
    },
    {
    Object: &konnectv1alpha1.KonnectGatewayControlPlane{},
    IndexOptions: konnect.IndexOptionsForKonnectGatewayControlPlane(),
    },
    }
    for _, t := range types {
    var (
    entityTypeName = constraints.EntityTypeNameForObj(t.Object)
    logger = log.GetLogger(ctx, entityTypeName, developmentMode)
    )
    for _, ind := range t.IndexOptions {
    logger.Info("creating index", "indexField", ind.IndexField)
    err := mgr.
    GetCache().
    IndexField(ctx, ind.IndexObject, ind.IndexField, ind.ExtractValue)
    if err != nil {
    return fmt.Errorf("failed to setup cache indices for %s: %w", entityTypeName, err)
    }
    }
    }
    return nil
    }
  • func setupIndexes(ctx context.Context, mgr manager.Manager, cfg Config) error {
    if cfg.ControlPlaneControllerEnabled || cfg.GatewayControllerEnabled {
    log.GetLogger(ctx, "ControlPlane", cfg.DevelopmentMode).Info(
    "creating index",
    "indexField", index.DataPlaneNameIndex,
    )
    if err := index.DataPlaneNameOnControlPlane(ctx, mgr.GetCache()); err != nil {
    return fmt.Errorf("failed to setup index for DataPlane names on ControlPlane: %w", err)
    }
    if cfg.KongPluginInstallationControllerEnabled {
    log.GetLogger(ctx, "DataPlane", cfg.DevelopmentMode).Info(
    "creating index",
    "indexField", index.KongPluginInstallationsIndex,
    )
    if err := index.KongPluginInstallationsOnDataPlane(ctx, mgr.GetCache()); err != nil {
    return fmt.Errorf("failed to setup index for KongPluginInstallations on DataPlane: %w", err)
    }
    }
    }
    return nil
    }

This issue tracks the effort of putting all that code in 1 place and using single boilerplate for scaffolding.

Acceptance criteria

  • Manager's cache index related code is in 1 place.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant