Skip to content

Commit 29b7a69

Browse files
joelanfordtmshort
authored andcommitted
opm serve: use pre-existing cache, if set and up-to-date (openshift#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]> Signed-off-by: Joe Lanford <[email protected]> Upstream-repository: operator-registry Upstream-commit: 494b68e62a814a891821aeb2bd28f33abc1624ff
1 parent 8d1c828 commit 29b7a69

File tree

12 files changed

+206
-863
lines changed

12 files changed

+206
-863
lines changed

pkg/manifests/csv.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ metadata:
55
name: packageserver
66
namespace: openshift-operator-lifecycle-manager
77
labels:
8-
olm.version: 0.0.0-f8dd7c4e6b8762e41dd59f9e727c52acd9cffe59
8+
olm.version: 0.0.0-7829ec69880557119c98ba10ad52cb9264e4f278
99
olm.clusteroperator.name: operator-lifecycle-manager-packageserver
1010
annotations:
1111
include.release.openshift.io/self-managed-high-availability: "true"
@@ -159,7 +159,7 @@ spec:
159159
- packageserver
160160
topologyKey: "kubernetes.io/hostname"
161161
maturity: alpha
162-
version: 0.0.0-f8dd7c4e6b8762e41dd59f9e727c52acd9cffe59
162+
version: 0.0.0-7829ec69880557119c98ba10ad52cb9264e4f278
163163
apiservicedefinitions:
164164
owned:
165165
- group: packages.operators.coreos.com

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

Lines changed: 24 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"context"
66
"errors"
77
"fmt"
8-
"io"
98
"net"
109
"net/http"
1110
endpoint "net/http/pprof"
@@ -20,18 +19,17 @@ import (
2019

2120
"github.com/operator-framework/operator-registry/pkg/api"
2221
health "github.com/operator-framework/operator-registry/pkg/api/grpc_health_v1"
23-
"github.com/operator-framework/operator-registry/pkg/cache"
2422
"github.com/operator-framework/operator-registry/pkg/lib/dns"
2523
"github.com/operator-framework/operator-registry/pkg/lib/graceful"
2624
"github.com/operator-framework/operator-registry/pkg/lib/log"
25+
"github.com/operator-framework/operator-registry/pkg/registry"
2726
"github.com/operator-framework/operator-registry/pkg/server"
2827
)
2928

3029
type serve struct {
31-
configDir string
32-
cacheDir string
33-
cacheOnly bool
34-
cacheEnforceIntegrity bool
30+
configDir string
31+
cacheDir string
32+
cacheOnly bool
3533

3634
port string
3735
terminationLog string
@@ -61,19 +59,15 @@ startup. Changes made to the declarative config after the this command starts
6159
will not be reflected in the served content.
6260
`,
6361
Args: cobra.ExactArgs(1),
64-
PreRun: func(_ *cobra.Command, args []string) {
62+
PreRunE: func(_ *cobra.Command, args []string) error {
6563
s.configDir = args[0]
6664
if s.debug {
6765
logger.SetLevel(logrus.DebugLevel)
6866
}
67+
return nil
6968
},
70-
Run: func(cmd *cobra.Command, _ []string) {
71-
if !cmd.Flags().Changed("cache-enforce-integrity") {
72-
s.cacheEnforceIntegrity = s.cacheDir != "" && !s.cacheOnly
73-
}
74-
if err := s.run(cmd.Context()); err != nil {
75-
logger.Fatal(err)
76-
}
69+
RunE: func(cmd *cobra.Command, _ []string) error {
70+
return s.run(cmd.Context())
7771
},
7872
}
7973

@@ -83,15 +77,12 @@ will not be reflected in the served content.
8377
cmd.Flags().StringVar(&s.pprofAddr, "pprof-addr", "", "address of startup profiling endpoint (addr:port format)")
8478
cmd.Flags().StringVar(&s.cacheDir, "cache-dir", "", "if set, sync and persist server cache directory")
8579
cmd.Flags().BoolVar(&s.cacheOnly, "cache-only", false, "sync the serve cache and exit without serving")
86-
cmd.Flags().BoolVar(&s.cacheEnforceIntegrity, "cache-enforce-integrity", false, "exit with error if cache is not present or has been invalidated. (default: true when --cache-dir is set and --cache-only is false, false otherwise), ")
8780
return cmd
8881
}
8982

9083
func (s *serve) run(ctx context.Context) error {
9184
p := newProfilerInterface(s.pprofAddr, s.logger)
92-
if err := p.startEndpoint(); err != nil {
93-
return fmt.Errorf("could not start pprof endpoint: %v", err)
94-
}
85+
p.startEndpoint()
9586
if err := p.startCpuProfileCache(); err != nil {
9687
return fmt.Errorf("could not start CPU profile: %v", err)
9788
}
@@ -109,45 +100,18 @@ func (s *serve) run(ctx context.Context) error {
109100

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

112-
if s.cacheDir == "" && s.cacheEnforceIntegrity {
113-
return fmt.Errorf("--cache-dir must be specified with --cache-enforce-integrity")
114-
}
115-
116-
if s.cacheDir == "" {
117-
s.cacheDir, err = os.MkdirTemp("", "opm-serve-cache-")
118-
if err != nil {
119-
return err
120-
}
121-
defer os.RemoveAll(s.cacheDir)
122-
}
123-
124-
store, err := cache.New(s.cacheDir)
103+
store, err := registry.NewQuerierFromFS(os.DirFS(s.configDir), s.cacheDir)
104+
defer store.Close()
125105
if err != nil {
126106
return err
127107
}
128-
if storeCloser, ok := store.(io.Closer); ok {
129-
defer storeCloser.Close()
130-
}
131-
if s.cacheEnforceIntegrity {
132-
if err := store.CheckIntegrity(os.DirFS(s.configDir)); err != nil {
133-
return err
134-
}
135-
if err := store.Load(); err != nil {
136-
return err
137-
}
138-
} else {
139-
if err := cache.LoadOrRebuild(store, os.DirFS(s.configDir)); err != nil {
140-
return err
141-
}
142-
}
143-
144108
if s.cacheOnly {
145109
return nil
146110
}
147111

148112
lis, err := net.Listen("tcp", ":"+s.port)
149113
if err != nil {
150-
return fmt.Errorf("failed to listen: %s", err)
114+
s.logger.Fatalf("failed to listen: %s", err)
151115
}
152116

153117
grpcServer := grpc.NewServer()
@@ -161,9 +125,7 @@ func (s *serve) run(ctx context.Context) error {
161125
return grpcServer.Serve(lis)
162126
}, func() {
163127
grpcServer.GracefulStop()
164-
if err := p.stopEndpoint(ctx); err != nil {
165-
s.logger.Warnf("error shutting down pprof server: %v", err)
166-
}
128+
p.stopEndpoint(p.logger.Context)
167129
})
168130

169131
}
@@ -181,8 +143,7 @@ type profilerInterface struct {
181143
cacheReady bool
182144
cacheLock sync.RWMutex
183145

184-
logger *logrus.Entry
185-
closeErr chan error
146+
logger *logrus.Entry
186147
}
187148

188149
func newProfilerInterface(a string, log *logrus.Entry) *profilerInterface {
@@ -197,10 +158,10 @@ func (p *profilerInterface) isEnabled() bool {
197158
return p.addr != ""
198159
}
199160

200-
func (p *profilerInterface) startEndpoint() error {
161+
func (p *profilerInterface) startEndpoint() {
201162
// short-circuit if not enabled
202163
if !p.isEnabled() {
203-
return nil
164+
return
204165
}
205166

206167
mux := http.NewServeMux()
@@ -216,22 +177,14 @@ func (p *profilerInterface) startEndpoint() error {
216177
Handler: mux,
217178
}
218179

219-
lis, err := net.Listen("tcp", p.addr)
220-
if err != nil {
221-
return err
222-
}
223-
224-
p.closeErr = make(chan error)
180+
// goroutine exits with main
225181
go func() {
226-
p.closeErr <- func() error {
227-
p.logger.Info("starting pprof endpoint")
228-
if err := p.server.Serve(lis); err != nil && !errors.Is(err, http.ErrServerClosed) {
229-
return err
230-
}
231-
return nil
232-
}()
182+
183+
p.logger.Info("starting pprof endpoint")
184+
if err := p.server.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) {
185+
p.logger.Fatal(err)
186+
}
233187
}()
234-
return nil
235188
}
236189

237190
func (p *profilerInterface) startCpuProfileCache() error {
@@ -265,14 +218,10 @@ func (p *profilerInterface) httpHandler(w http.ResponseWriter, r *http.Request)
265218
w.Write(p.cache.Bytes())
266219
}
267220

268-
func (p *profilerInterface) stopEndpoint(ctx context.Context) error {
269-
if !p.isEnabled() {
270-
return nil
271-
}
221+
func (p *profilerInterface) stopEndpoint(ctx context.Context) {
272222
if err := p.server.Shutdown(ctx); err != nil {
273-
return err
223+
p.logger.Fatal(err)
274224
}
275-
return <-p.closeErr
276225
}
277226

278227
func (p *profilerInterface) isCacheReady() bool {

0 commit comments

Comments
 (0)