Skip to content

Commit 680976a

Browse files
authoredAug 15, 2019
Merge pull request #74 from njhale/permissive
Permissive/Strict Options
2 parents b51ff88 + 97b76b5 commit 680976a

File tree

4 files changed

+108
-60
lines changed

4 files changed

+108
-60
lines changed
 

‎cmd/appregistry-server/main.go

+11-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package main
22

33
import (
4+
"fmt"
45
"net"
56

67
"github.com/sirupsen/logrus"
@@ -39,6 +40,7 @@ func main() {
3940
rootCmd.Flags().StringP("packages", "o", "", "comma separated list of package(s) to be downloaded from the specified operator source(s)")
4041
rootCmd.Flags().StringP("port", "p", "50051", "port number to serve on")
4142
rootCmd.Flags().StringP("termination-log", "t", "/dev/termination-log", "path to a container termination log file")
43+
rootCmd.Flags().Bool("strict", false, "fail on registry load errors")
4244

4345
if err := rootCmd.Flags().MarkHidden("debug"); err != nil {
4446
logrus.Panic(err.Error())
@@ -85,6 +87,10 @@ func runCmdFunc(cmd *cobra.Command, args []string) error {
8587
if err != nil {
8688
return err
8789
}
90+
strict, err := cmd.Flags().GetBool("strict")
91+
if err != nil {
92+
return err
93+
}
8894

8995
logger := logrus.WithFields(logrus.Fields{"type": "appregistry", "port": port})
9096

@@ -95,7 +101,11 @@ func runCmdFunc(cmd *cobra.Command, args []string) error {
95101

96102
store, err := loader.Load(sources, packages)
97103
if err != nil {
98-
logger.WithError(err).Warn("error loading app registry manifests")
104+
err = fmt.Errorf("error loading manifests from appregistry: %s", err)
105+
if strict {
106+
logger.WithError(err).Fatal("strict mode enabled")
107+
}
108+
logger.WithError(err).Warn("strict mode disabled")
99109
}
100110
if store == nil {
101111
logger.Warn("using empty querier")

‎cmd/configmap-server/main.go

+39-22
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package main
22

33
import (
44
"context"
5+
"fmt"
56
"net"
67

78
"github.com/sirupsen/logrus"
@@ -16,36 +17,40 @@ import (
1617
"github.com/operator-framework/operator-registry/pkg/api"
1718
health "github.com/operator-framework/operator-registry/pkg/api/grpc_health_v1"
1819
"github.com/operator-framework/operator-registry/pkg/lib/log"
20+
"github.com/operator-framework/operator-registry/pkg/registry"
1921
"github.com/operator-framework/operator-registry/pkg/server"
2022
"github.com/operator-framework/operator-registry/pkg/sqlite"
2123
)
2224

23-
func main() {
24-
var rootCmd = &cobra.Command{
25-
Short: "configmap-server",
26-
Long: `configmap-server reads a configmap and builds a sqlite database containing operator manifests and serves a grpc API to query it`,
25+
var rootCmd = &cobra.Command{
26+
Short: "configmap-server",
27+
Long: `configmap-server reads a configmap and builds a sqlite database containing operator manifests and serves a grpc API to query it`,
2728

28-
PreRunE: func(cmd *cobra.Command, args []string) error {
29-
if debug, _ := cmd.Flags().GetBool("debug"); debug {
30-
logrus.SetLevel(logrus.DebugLevel)
31-
}
32-
return nil
33-
},
29+
PreRunE: func(cmd *cobra.Command, args []string) error {
30+
if debug, _ := cmd.Flags().GetBool("debug"); debug {
31+
logrus.SetLevel(logrus.DebugLevel)
32+
}
33+
return nil
34+
},
3435

35-
RunE: runCmdFunc,
36-
}
36+
RunE: runCmdFunc,
37+
}
3738

39+
func init() {
3840
rootCmd.Flags().Bool("debug", false, "enable debug logging")
3941
rootCmd.Flags().StringP("kubeconfig", "k", "", "absolute path to kubeconfig file")
4042
rootCmd.Flags().StringP("database", "d", "bundles.db", "name of db to output")
4143
rootCmd.Flags().StringP("configMapName", "c", "", "name of a configmap")
4244
rootCmd.Flags().StringP("configMapNamespace", "n", "", "namespace of a configmap")
4345
rootCmd.Flags().StringP("port", "p", "50051", "port number to serve on")
4446
rootCmd.Flags().StringP("termination-log", "t", "/dev/termination-log", "path to a container termination log file")
47+
rootCmd.Flags().Bool("permissive", false, "allow registry load errors")
4548
if err := rootCmd.Flags().MarkHidden("debug"); err != nil {
4649
logrus.Panic(err.Error())
4750
}
51+
}
4852

53+
func main() {
4954
if err := rootCmd.Execute(); err != nil {
5055
logrus.Panic(err.Error())
5156
}
@@ -59,7 +64,7 @@ func runCmdFunc(cmd *cobra.Command, args []string) error {
5964
}
6065
err = log.AddDefaultWriterHooks(terminationLogPath)
6166
if err != nil {
62-
return err
67+
logrus.WithError(err).Warn("unable to set termination log path")
6368
}
6469
kubeconfig, err := cmd.Flags().GetString("kubeconfig")
6570
if err != nil {
@@ -81,12 +86,16 @@ func runCmdFunc(cmd *cobra.Command, args []string) error {
8186
if err != nil {
8287
return err
8388
}
89+
permissive, err := cmd.Flags().GetBool("permissive")
90+
if err != nil {
91+
return err
92+
}
8493
logger := logrus.WithFields(logrus.Fields{"configMapName": configMapName, "configMapNamespace": configMapNamespace, "port": port})
8594

8695
client := NewClientFromConfig(kubeconfig, logger.Logger)
8796
configMap, err := client.CoreV1().ConfigMaps(configMapNamespace).Get(configMapName, metav1.GetOptions{})
8897
if err != nil {
89-
logger.Fatalf("error getting configmap: %v", err)
98+
logger.Fatalf("error getting configmap: %s", err)
9099
}
91100

92101
sqlLoader, err := sqlite.NewSQLLiteLoader(dbName)
@@ -96,26 +105,34 @@ func runCmdFunc(cmd *cobra.Command, args []string) error {
96105

97106
configMapPopulator := sqlite.NewSQLLoaderForConfigMap(sqlLoader, *configMap)
98107
if err := configMapPopulator.Populate(); err != nil {
99-
return err
108+
err = fmt.Errorf("error loading manifests from configmap: %s", err)
109+
if !permissive {
110+
logger.WithError(err).Fatal("permissive mode disabled")
111+
}
112+
logger.WithError(err).Warn("permissive mode enabled")
100113
}
101114

102-
store, err := sqlite.NewSQLLiteQuerier(dbName)
115+
var store registry.Query
116+
store, err = sqlite.NewSQLLiteQuerier(dbName)
103117
if err != nil {
104-
logger.Fatalf("failed to load db: %v", err)
118+
logger.WithError(err).Warnf("failed to load db")
119+
}
120+
if store == nil {
121+
store = registry.NewEmptyQuerier()
105122
}
106123

107124
// sanity check that the db is available
108125
tables, err := store.ListTables(context.TODO())
109126
if err != nil {
110-
logger.Fatalf("couldn't list tables in db, incorrect config: %v", err)
127+
logger.WithError(err).Warnf("couldn't list tables in db")
111128
}
112129
if len(tables) == 0 {
113-
logger.Fatal("no tables found in db")
130+
logger.Warn("no tables found in db")
114131
}
115132

116133
lis, err := net.Listen("tcp", ":"+port)
117134
if err != nil {
118-
logger.Fatalf("failed to listen: %v", err)
135+
logger.Fatalf("failed to listen: %s", err)
119136
}
120137
s := grpc.NewServer()
121138

@@ -125,7 +142,7 @@ func runCmdFunc(cmd *cobra.Command, args []string) error {
125142

126143
logger.Info("serving registry")
127144
if err := s.Serve(lis); err != nil {
128-
logger.Fatalf("failed to serve: %v", err)
145+
logger.Fatalf("failed to serve: %s", err)
129146
}
130147
return nil
131148
}
@@ -144,7 +161,7 @@ func NewClientFromConfig(kubeconfig string, logger *logrus.Logger) kubernetes.In
144161
}
145162

146163
if err != nil {
147-
logger.Fatalf("Cannot load config for REST client: %v", err)
164+
logger.Fatalf("Cannot load config for REST client: %s", err)
148165
}
149166

150167
return kubernetes.NewForConfigOrDie(config)

‎cmd/initializer/main.go

+30-16
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,39 @@
11
package main
22

33
import (
4+
"fmt"
5+
46
"github.com/sirupsen/logrus"
57
"github.com/spf13/cobra"
68

79
"github.com/operator-framework/operator-registry/pkg/sqlite"
810
)
911

10-
func main() {
11-
var rootCmd = &cobra.Command{
12-
Short: "initializer",
13-
Long: `initializer takes a directory of OLM manifests and outputs a sqlite database containing them`,
14-
15-
PreRunE: func(cmd *cobra.Command, args []string) error {
16-
if debug, _ := cmd.Flags().GetBool("debug"); debug {
17-
logrus.SetLevel(logrus.DebugLevel)
18-
}
19-
return nil
20-
},
21-
22-
RunE: runCmdFunc,
23-
}
12+
var rootCmd = &cobra.Command{
13+
Short: "initializer",
14+
Long: `initializer takes a directory of OLM manifests and outputs a sqlite database containing them`,
15+
16+
PreRunE: func(cmd *cobra.Command, args []string) error {
17+
if debug, _ := cmd.Flags().GetBool("debug"); debug {
18+
logrus.SetLevel(logrus.DebugLevel)
19+
}
20+
return nil
21+
},
22+
23+
RunE: runCmdFunc,
24+
}
2425

26+
func init() {
2527
rootCmd.Flags().Bool("debug", false, "enable debug logging")
2628
rootCmd.Flags().StringP("manifests", "m", "manifests", "relative path to directory of manifests")
2729
rootCmd.Flags().StringP("output", "o", "bundles.db", "relative path to a sqlite file to create or overwrite")
30+
rootCmd.Flags().Bool("permissive", false, "allow registry load errors")
2831
if err := rootCmd.Flags().MarkHidden("debug"); err != nil {
2932
panic(err)
3033
}
34+
}
3135

36+
func main() {
3237
if err := rootCmd.Execute(); err != nil {
3338
panic(err)
3439
}
@@ -43,16 +48,25 @@ func runCmdFunc(cmd *cobra.Command, args []string) error {
4348
if err != nil {
4449
return err
4550
}
51+
permissive, err := cmd.Flags().GetBool("permissive")
52+
if err != nil {
53+
return err
54+
}
4655

4756
dbLoader, err := sqlite.NewSQLLiteLoader(outFilename)
4857
if err != nil {
49-
logrus.Fatal(err)
58+
return err
5059
}
5160
defer dbLoader.Close()
5261

5362
loader := sqlite.NewSQLLoaderForDirectory(dbLoader, manifestDir)
5463
if err := loader.Populate(); err != nil {
55-
logrus.Fatal(err)
64+
err = fmt.Errorf("error loading manifests from directory: %s", err)
65+
if !permissive {
66+
logrus.WithError(err).Fatal("permissive mode disabled")
67+
return err
68+
}
69+
logrus.WithError(err).Warn("permissive mode enabled")
5670
}
5771

5872
return nil

‎cmd/registry-server/main.go

+28-21
Original file line numberDiff line numberDiff line change
@@ -12,33 +12,36 @@ import (
1212

1313
"github.com/operator-framework/operator-registry/pkg/api"
1414
health "github.com/operator-framework/operator-registry/pkg/api/grpc_health_v1"
15+
"github.com/operator-framework/operator-registry/pkg/registry"
1516
"github.com/operator-framework/operator-registry/pkg/server"
1617
"github.com/operator-framework/operator-registry/pkg/sqlite"
1718
)
1819

19-
func main() {
20-
var rootCmd = &cobra.Command{
21-
Short: "registry-server",
22-
Long: `registry loads a sqlite database containing operator manifests and serves a grpc API to query it`,
23-
24-
PreRunE: func(cmd *cobra.Command, args []string) error {
25-
if debug, _ := cmd.Flags().GetBool("debug"); debug {
26-
logrus.SetLevel(logrus.DebugLevel)
27-
}
28-
return nil
29-
},
30-
31-
RunE: runCmdFunc,
32-
}
20+
var rootCmd = &cobra.Command{
21+
Short: "registry-server",
22+
Long: `registry loads a sqlite database containing operator manifests and serves a grpc API to query it`,
23+
24+
PreRunE: func(cmd *cobra.Command, args []string) error {
25+
if debug, _ := cmd.Flags().GetBool("debug"); debug {
26+
logrus.SetLevel(logrus.DebugLevel)
27+
}
28+
return nil
29+
},
30+
31+
RunE: runCmdFunc,
32+
}
3333

34+
func init() {
3435
rootCmd.Flags().Bool("debug", false, "enable debug logging")
3536
rootCmd.Flags().StringP("database", "d", "bundles.db", "relative path to sqlite db")
3637
rootCmd.Flags().StringP("port", "p", "50051", "port number to serve on")
3738
rootCmd.Flags().StringP("termination-log", "t", "/dev/termination-log", "path to a container termination log file")
3839
if err := rootCmd.Flags().MarkHidden("debug"); err != nil {
3940
logrus.Panic(err.Error())
4041
}
42+
}
4143

44+
func main() {
4245
if err := rootCmd.Execute(); err != nil {
4346
logrus.Panic(err.Error())
4447
}
@@ -52,7 +55,7 @@ func runCmdFunc(cmd *cobra.Command, args []string) error {
5255
}
5356
err = log.AddDefaultWriterHooks(terminationLogPath)
5457
if err != nil {
55-
return err
58+
logrus.WithError(err).Warn("unable to set termination log path")
5659
}
5760
dbName, err := cmd.Flags().GetString("database")
5861
if err != nil {
@@ -66,23 +69,27 @@ func runCmdFunc(cmd *cobra.Command, args []string) error {
6669

6770
logger := logrus.WithFields(logrus.Fields{"database": dbName, "port": port})
6871

69-
store, err := sqlite.NewSQLLiteQuerier(dbName)
72+
var store registry.Query
73+
store, err = sqlite.NewSQLLiteQuerier(dbName)
7074
if err != nil {
71-
logger.Fatalf("failed to load db: %v", err)
75+
logger.WithError(err).Warnf("failed to load db")
76+
}
77+
if store == nil {
78+
store = registry.NewEmptyQuerier()
7279
}
7380

7481
// sanity check that the db is available
7582
tables, err := store.ListTables(context.TODO())
7683
if err != nil {
77-
logger.Fatalf("couldn't list tables in db, incorrect config: %v", err)
84+
logger.WithError(err).Warnf("couldn't list tables in db")
7885
}
7986
if len(tables) == 0 {
80-
logger.Fatal("no tables found in db")
87+
logger.Warn("no tables found in db")
8188
}
8289

8390
lis, err := net.Listen("tcp", ":"+port)
8491
if err != nil {
85-
logger.Fatalf("failed to listen: %v", err)
92+
logger.Fatalf("failed to listen: %s", err)
8693
}
8794
s := grpc.NewServer()
8895

@@ -91,7 +98,7 @@ func runCmdFunc(cmd *cobra.Command, args []string) error {
9198
reflection.Register(s)
9299
logger.Info("serving registry")
93100
if err := s.Serve(lis); err != nil {
94-
logger.Fatalf("failed to serve: %v", err)
101+
logger.Fatalf("failed to serve: %s", err)
95102
}
96103

97104
return nil

0 commit comments

Comments
 (0)
Please sign in to comment.