Skip to content

Commit b94e073

Browse files
joelanfordgrokspawn
authored andcommitted
opm serve: use pre-existing cache, if set and up-to-date (#1005)
* opm serve: use pre-existing cache, if set and up-to-date Signed-off-by: Joe Lanford <[email protected]> * refactor to leave NewQuerier function untouched Signed-off-by: Joe Lanford <[email protected]> Upstream-repository: operator-registry Upstream-commit: 215f413e76e3fb544b0d450acdbab41c756dbf59
1 parent 90c49bc commit b94e073

File tree

10 files changed

+1029
-363
lines changed

10 files changed

+1029
-363
lines changed

staging/operator-registry/alpha/action/generate_dockerfile.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,11 @@ FROM {{.BaseImage}}
4545
4646
# Configure the entrypoint and command
4747
ENTRYPOINT ["/bin/opm"]
48-
CMD ["serve", "/configs"]
48+
CMD ["serve", "/configs", "--cache-dir=/tmp/cache"]
4949
50-
# Copy declarative config root into image at /configs
50+
# Copy declarative config root into image at /configs and pre-populate serve cache
5151
ADD {{.IndexDir}} /configs
52+
RUN ["/bin/opm", "serve", "/configs", "--cache-dir=/tmp/cache", "--cache-only"]
5253
5354
# Set DC-specific label for the location of the DC root directory
5455
# in the image

staging/operator-registry/alpha/action/generate_dockerfile_test.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,11 @@ FROM foo
5050
5151
# Configure the entrypoint and command
5252
ENTRYPOINT ["/bin/opm"]
53-
CMD ["serve", "/configs"]
53+
CMD ["serve", "/configs", "--cache-dir=/tmp/cache"]
5454
55-
# Copy declarative config root into image at /configs
55+
# Copy declarative config root into image at /configs and pre-populate serve cache
5656
ADD bar /configs
57+
RUN ["/bin/opm", "serve", "/configs", "--cache-dir=/tmp/cache", "--cache-only"]
5758
5859
# Set DC-specific label for the location of the DC root directory
5960
# in the image
@@ -76,10 +77,11 @@ FROM foo
7677
7778
# Configure the entrypoint and command
7879
ENTRYPOINT ["/bin/opm"]
79-
CMD ["serve", "/configs"]
80+
CMD ["serve", "/configs", "--cache-dir=/tmp/cache"]
8081
81-
# Copy declarative config root into image at /configs
82+
# Copy declarative config root into image at /configs and pre-populate serve cache
8283
ADD bar /configs
84+
RUN ["/bin/opm", "serve", "/configs", "--cache-dir=/tmp/cache", "--cache-only"]
8385
8486
# Set DC-specific label for the location of the DC root directory
8587
# in the image

staging/operator-registry/cmd/opm/serve/serve.go

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,17 @@ import (
66
"errors"
77
"fmt"
88
"net"
9-
"os"
10-
"sync"
11-
129
"net/http"
1310
endpoint "net/http/pprof"
11+
"os"
1412
"runtime/pprof"
13+
"sync"
1514

1615
"github.com/sirupsen/logrus"
1716
"github.com/spf13/cobra"
1817
"google.golang.org/grpc"
1918
"google.golang.org/grpc/reflection"
2019

21-
"github.com/operator-framework/operator-registry/alpha/declcfg"
2220
"github.com/operator-framework/operator-registry/pkg/api"
2321
health "github.com/operator-framework/operator-registry/pkg/api/grpc_health_v1"
2422
"github.com/operator-framework/operator-registry/pkg/lib/dns"
@@ -30,6 +28,8 @@ import (
3028

3129
type serve struct {
3230
configDir string
31+
cacheDir string
32+
cacheOnly bool
3333

3434
port string
3535
terminationLog string
@@ -75,6 +75,8 @@ will not be reflected in the served content.
7575
cmd.Flags().StringVarP(&s.terminationLog, "termination-log", "t", "/dev/termination-log", "path to a container termination log file")
7676
cmd.Flags().StringVarP(&s.port, "port", "p", "50051", "port number to serve on")
7777
cmd.Flags().StringVar(&s.pprofAddr, "pprof-addr", "", "address of startup profiling endpoint (addr:port format)")
78+
cmd.Flags().StringVar(&s.cacheDir, "cache-dir", "", "if set, sync and persist server cache directory")
79+
cmd.Flags().BoolVar(&s.cacheOnly, "cache-only", false, "sync the serve cache and exit without serving")
7880
return cmd
7981
}
8082

@@ -100,20 +102,14 @@ func (s *serve) run(ctx context.Context) error {
100102

101103
s.logger = s.logger.WithFields(logrus.Fields{"configs": s.configDir, "port": s.port})
102104

103-
cfg, err := declcfg.LoadFS(os.DirFS(s.configDir))
104-
if err != nil {
105-
return fmt.Errorf("load declarative config directory: %v", err)
106-
}
107-
108-
m, err := declcfg.ConvertToModel(*cfg)
109-
if err != nil {
110-
return fmt.Errorf("could not build index model from declarative config: %v", err)
111-
}
112-
store, err := registry.NewQuerier(m)
105+
store, err := registry.NewQuerierFromFS(os.DirFS(s.configDir), s.cacheDir)
113106
defer store.Close()
114107
if err != nil {
115108
return err
116109
}
110+
if s.cacheOnly {
111+
return nil
112+
}
117113

118114
lis, err := net.Listen("tcp", ":"+s.port)
119115
if err != nil {

0 commit comments

Comments
 (0)