@@ -46,22 +46,21 @@ import (
4646 "sigs.k8s.io/controller-runtime/pkg/client"
4747 "sigs.k8s.io/controller-runtime/pkg/healthz"
4848 "sigs.k8s.io/controller-runtime/pkg/log"
49- "sigs.k8s.io/controller-runtime/pkg/metrics"
5049 "sigs.k8s.io/controller-runtime/pkg/metrics/filters"
5150 metricsserver "sigs.k8s.io/controller-runtime/pkg/metrics/server"
5251 crwebhook "sigs.k8s.io/controller-runtime/pkg/webhook"
5352
5453 ocv1 "github.com/operator-framework/operator-controller/api/v1"
55- corecontrollers "github.com/operator-framework/operator-controller/internal/catalogd/controllers/core "
54+ "github.com/operator-framework/operator-controller/internal/catalogd/controllers"
5655 "github.com/operator-framework/operator-controller/internal/catalogd/features"
5756 "github.com/operator-framework/operator-controller/internal/catalogd/garbagecollection"
58- "github.com/operator-framework/operator-controller/internal/catalogd/handlers"
59- catalogdmetrics "github.com/operator-framework/operator-controller/internal/catalogd/metrics"
60- "github.com/operator-framework/operator-controller/internal/catalogd/serverutil"
57+ "github.com/operator-framework/operator-controller/internal/catalogd/handler"
58+ v1 "github.com/operator-framework/operator-controller/internal/catalogd/handler/api/v1"
6159 "github.com/operator-framework/operator-controller/internal/catalogd/storage"
6260 "github.com/operator-framework/operator-controller/internal/catalogd/webhook"
6361 sharedcontrollers "github.com/operator-framework/operator-controller/internal/shared/controllers"
6462 fsutil "github.com/operator-framework/operator-controller/internal/shared/util/fs"
63+ http2 "github.com/operator-framework/operator-controller/internal/shared/util/http"
6564 imageutil "github.com/operator-framework/operator-controller/internal/shared/util/image"
6665 "github.com/operator-framework/operator-controller/internal/shared/util/pullsecretcache"
6766 sautil "github.com/operator-framework/operator-controller/internal/shared/util/sa"
@@ -328,61 +327,60 @@ func run(ctx context.Context) error {
328327 },
329328 }
330329
331- var localStorage storage.Instance
332- metrics .Registry .MustRegister (catalogdmetrics .RequestDurationMetric )
333-
334330 storeDir := filepath .Join (cfg .cacheDir , storageDir )
335331 if err := os .MkdirAll (storeDir , 0700 ); err != nil {
336332 setupLog .Error (err , "unable to create storage directory for catalogs" )
337333 return err
338334 }
339335
340- baseStorageURL , err := url .Parse (fmt .Sprintf ("%s/catalogs/" , cfg .externalAddr ))
336+ const catalogsSubPath = "catalogs"
337+ baseCatalogsURL , err := url .Parse (fmt .Sprintf ("%s/%s" , cfg .externalAddr , catalogsSubPath ))
341338 if err != nil {
342339 setupLog .Error (err , "unable to create base storage URL" )
343340 return err
344341 }
345342
346- indexer := storage .NewIndexer ()
347- handlersMap := map [string ]http.Handler {
348- "/all" : handlers .V1AllHandler (indexer ),
349- }
350-
351- if features .CatalogdFeatureGate .Enabled (features .APIV1MetasHandler ) {
352- handlersMap ["/metas" ] = handlers .V1MetasHandler (indexer )
353- }
354-
355- if features .CatalogdFeatureGate .Enabled (features .APIV1GraphQLHandler ) {
356- handlersMap ["/graphql" ] = handlers .V1GraphQLHandler ()
357- }
358-
359- localStorage = & storage.LocalDirV1 {
360- Indexer : indexer ,
361- Handlers : handlersMap ,
362- RootDir : storeDir ,
363- RootURL : baseStorageURL ,
364- }
343+ storageInstances := configureStorage (storeDir )
344+ catalogdHandler := handler .NewStandardHandler (
345+ newAPIV1Handler (catalogsSubPath , storageInstances ),
346+ )
365347
366348 // Config for the catalogd web server
367- catalogServerConfig := serverutil.CatalogServerConfig {
368- ExternalAddr : cfg .externalAddr ,
369- CatalogAddr : cfg .catalogServerAddr ,
370- CertFile : cfg .certFile ,
371- KeyFile : cfg .keyFile ,
372- LocalStorage : localStorage ,
349+ catalogServerConfig := http2.ServerConfig {
350+ Name : "catalogs" ,
351+ OnlyServeWhenLeader : true ,
352+ ListenAddr : cfg .catalogServerAddr ,
353+ Server : & http.Server {
354+ Handler : catalogdHandler ,
355+ ReadTimeout : 5 * time .Second ,
356+ WriteTimeout : 5 * time .Minute ,
357+ },
358+ }
359+ if cfg .certFile != "" && cfg .keyFile != "" {
360+ catalogServerConfig .TLSConfig = & tls.Config {
361+ GetCertificate : cw .GetCertificate ,
362+ MinVersion : tls .VersionTLS12 ,
363+ }
373364 }
374365
375- err = serverutil . AddCatalogServerToManager ( mgr , catalogServerConfig , cw )
366+ catalogServer , err := http2 . NewManagerServer ( catalogServerConfig )
376367 if err != nil {
377368 setupLog .Error (err , "unable to configure catalog server" )
378369 return err
379370 }
371+ if err := mgr .Add (catalogServer ); err != nil {
372+ setupLog .Error (err , "unable to add catalog server to manager" )
373+ return err
374+ }
380375
381- if err = (& corecontrollers .ClusterCatalogReconciler {
376+ if err = (& controllers .ClusterCatalogReconciler {
382377 Client : mgr .GetClient (),
383378 ImageCache : imageCache ,
384379 ImagePuller : imagePuller ,
385- Storage : localStorage ,
380+ Storage : storageInstances ,
381+ GetBaseURL : func (catalogName string ) string {
382+ return fmt .Sprintf ("%s/%s" , baseCatalogsURL , catalogName )
383+ },
386384 }).SetupWithManager (mgr ); err != nil {
387385 setupLog .Error (err , "unable to create controller" , "controller" , "ClusterCatalog" )
388386 return err
@@ -452,3 +450,25 @@ func podNamespace() string {
452450 }
453451 return string (namespace )
454452}
453+
454+ func configureStorage (storeDir string ) * storage.Instances {
455+ metasEnabled := features .CatalogdFeatureGate .Enabled (features .APIV1MetasHandler )
456+ graphqlEnabled := features .CatalogdFeatureGate .Enabled (features .APIV1GraphQLHandler )
457+
458+ return storage .NewInstances (
459+ storage .WithFiles (true , storeDir ),
460+ storage .WithIndices (metasEnabled || graphqlEnabled , storeDir ),
461+ storage .WithGraphQLSchemas (graphqlEnabled ),
462+ )
463+ }
464+
465+ func newAPIV1Handler (baseURLPath string , si * storage.Instances ) * v1.APIV1Handler {
466+ metasEnabled := features .CatalogdFeatureGate .Enabled (features .APIV1MetasHandler )
467+ graphqlEnabled := features .CatalogdFeatureGate .Enabled (features .APIV1GraphQLHandler )
468+
469+ return v1 .NewAPIV1Handler (baseURLPath , si ,
470+ v1 .WithAllHandler (true ),
471+ v1 .WithMetasHandler (metasEnabled ),
472+ v1 .WithGraphQLHandler (graphqlEnabled ),
473+ )
474+ }
0 commit comments