kubewarden-controller icon indicating copy to clipboard operation
kubewarden-controller copied to clipboard

Use config structs to initialize reconcilers in the main setup function

Open fabriziosestito opened this issue 2 weeks ago • 0 comments

Currently, we rely on several initialization functions to set up reconcilers, webhooks, and manager-related resources. These functions accept numerous parameters—and in some cases, they directly inject them into the reconcilers. This approach, however, isn’t very ergonomic. Instead, we could generate configuration structs from the CLI and use them to propagate the configuration variables in a cleaner way.

Setup Functions:

Example

func main() {
  // parse CLI flags

    config := Configuration{
        DeploymentsNamespace: deploymentsNamespace,
        WebhookServiceName: webhookServiceName,
        AlwaysAcceptAdmissionReviewsOnDeploymentsNS: alwaysAcceptAdmissionReviewsOnDeploymentsNamespace,
        FeatureGateAdmissionWebhookMatchConditions: featureGateAdmissionWebhookMatchConditions,
        Telemetry: otelConfiguration,
        MutualTLS: MTLSConfig{
            Enabled: enableMutualTLS,
            ClientCAConfigMapName: clientCAConfigMapName,
            ClientCAConfigMapNS: clientCAConfigMapNamespace,
        },
    }

    mgrOpts := ManagerOptions{
        MetricsAddr: metricsAddr,
        ProbeAddr: probeAddr,
        EnableLeaderElection: enableLeaderElection,
        DeploymentsNamespace: deploymentsNamespace,
        EnableMutualTLS: enableMutualTLS,
    }

    mgr, err := setupManager(mgrOpts)
    if err != nil {
        // handle error
    }

    if err := setupReconcilers(mgr, config); err != nil {
        // handle error
    }

    if err := setupWebhooks(mgr, config.DeploymentsNamespace); err != nil {
        // handle error
    }
}

func setupReconciler(mgr mgr, config Configuration) {
  reconciler := SomethingReconciler {
    Config: Configuration,
    ... // other dependencies
  }
}

fabriziosestito avatar Feb 10 '25 14:02 fabriziosestito