Skip to content

Commit f30cfde

Browse files
Merge pull request #106 from kevinrizza/index-command
Add index build commands
2 parents 50a37f9 + 1be113b commit f30cfde

File tree

411 files changed

+219321
-108
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

411 files changed

+219321
-108
lines changed

Makefile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@ container-codegen:
4242
docker cp temp-codegen:/codegen/pkg/api/. ./pkg/api
4343
docker rm temp-codegen
4444

45+
generate-fakes:
46+
go install -mod=vendor ./vendor/github.com/maxbrunsfeld/counterfeiter/v6
47+
go generate ./...
48+
4549
clean:
4650
@rm -rf ./bin
4751

cmd/opm/index/add.go

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
package index
2+
3+
import (
4+
"github.com/sirupsen/logrus"
5+
"github.com/spf13/cobra"
6+
7+
"github.com/operator-framework/operator-registry/pkg/lib/indexer"
8+
)
9+
10+
func newIndexAddCmd() *cobra.Command {
11+
indexCmd := &cobra.Command{
12+
Use: "add",
13+
Short: "add an operator bundle to an index",
14+
Long: `add an operator bundle to an index`,
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: runIndexAddCmdFunc,
24+
}
25+
26+
indexCmd.Flags().Bool("debug", false, "enable debug logging")
27+
indexCmd.Flags().Bool("generate", false, "if enabled, just creates the dockerfile and saves it to local disk")
28+
indexCmd.Flags().StringP("out-dockerfile", "d", "", "if generating the dockerfile, this flag is used to (optionally) specify a dockerfile name")
29+
indexCmd.Flags().StringP("from-index", "f", "", "previous index to add to")
30+
indexCmd.Flags().StringSliceP("bundles", "b", nil, "comma separated list of bundles to add")
31+
if err := indexCmd.MarkFlagRequired("bundles"); err != nil {
32+
logrus.Panic("Failed to set required `bundles` flag for `index add`")
33+
}
34+
indexCmd.Flags().StringP("binary-image", "i", "", "container image for on-image `opm` command")
35+
indexCmd.Flags().StringP("container-tool", "c", "podman", "tool to interact with container images (save, build, etc.). One of: [docker, podman]")
36+
indexCmd.Flags().StringP("tag", "t", "", "custom tag for container image being built")
37+
indexCmd.Flags().Bool("permissive", false, "allow registry load errors")
38+
39+
if err := indexCmd.Flags().MarkHidden("debug"); err != nil {
40+
logrus.Panic(err.Error())
41+
}
42+
43+
return indexCmd
44+
45+
}
46+
47+
func runIndexAddCmdFunc(cmd *cobra.Command, args []string) error {
48+
generate, err := cmd.Flags().GetBool("generate")
49+
if err != nil {
50+
return err
51+
}
52+
53+
outDockerfile, err := cmd.Flags().GetString("out-dockerfile")
54+
if err != nil {
55+
return err
56+
}
57+
58+
fromIndex, err := cmd.Flags().GetString("from-index")
59+
if err != nil {
60+
return err
61+
}
62+
63+
bundles, err := cmd.Flags().GetStringSlice("bundles")
64+
if err != nil {
65+
return err
66+
}
67+
68+
binaryImage, err := cmd.Flags().GetString("binary-image")
69+
if err != nil {
70+
return err
71+
}
72+
73+
containerTool, err := cmd.Flags().GetString("container-tool")
74+
if err != nil {
75+
return err
76+
}
77+
78+
tag, err := cmd.Flags().GetString("tag")
79+
if err != nil {
80+
return err
81+
}
82+
83+
84+
permissive, err := cmd.Flags().GetBool("permissive")
85+
if err != nil {
86+
return err
87+
}
88+
89+
logger := logrus.WithFields(logrus.Fields{"bundles": bundles})
90+
91+
logger.Info("building the index")
92+
93+
indexAdder := indexer.NewIndexAdder(containerTool, logger)
94+
95+
request := indexer.AddToIndexRequest{
96+
Generate: generate,
97+
FromIndex: fromIndex,
98+
BinarySourceImage: binaryImage,
99+
OutDockerfile: outDockerfile,
100+
Tag: tag,
101+
Bundles: bundles,
102+
Permissive: permissive,
103+
}
104+
105+
err = indexAdder.AddToIndex(request)
106+
if err != nil {
107+
return err
108+
}
109+
110+
return nil
111+
}

cmd/opm/index/cmd.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package index
2+
3+
import (
4+
"github.com/sirupsen/logrus"
5+
"github.com/spf13/cobra"
6+
)
7+
8+
// NewOpmIndexCmd returns the opm index command
9+
func NewOpmIndexCmd() *cobra.Command {
10+
rootCmd := &cobra.Command{
11+
Use: "index",
12+
Short: "generate operator index container images",
13+
Long: `generate operator index container images from preexisting operator bundles`,
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+
23+
rootCmd.AddCommand(newIndexAddCmd())
24+
rootCmd.AddCommand(newIndexDeleteCmd())
25+
26+
return rootCmd
27+
}

cmd/opm/index/delete.go

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
package index
2+
3+
import (
4+
"github.com/sirupsen/logrus"
5+
"github.com/spf13/cobra"
6+
7+
"github.com/operator-framework/operator-registry/pkg/lib/indexer"
8+
)
9+
10+
func newIndexDeleteCmd() *cobra.Command {
11+
indexCmd := &cobra.Command{
12+
Use: "rm",
13+
Short: "delete an entire operator from an index",
14+
Long: `delete an entire operator from an index`,
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: runIndexDeleteCmdFunc,
24+
}
25+
26+
indexCmd.Flags().Bool("debug", false, "enable debug logging")
27+
indexCmd.Flags().Bool("generate", false, "if enabled, just creates the dockerfile and saves it to local disk")
28+
indexCmd.Flags().StringP("out-dockerfile", "d", "", "if generating the dockerfile, this flag is used to (optionally) specify a dockerfile name")
29+
indexCmd.Flags().StringP("from-index", "f", "", "previous index to delete from")
30+
if err := indexCmd.MarkFlagRequired("from-index"); err != nil {
31+
logrus.Panic("Failed to set required `from-index` flag for `index delete`")
32+
}
33+
indexCmd.Flags().StringSliceP("operators", "o", nil, "comma separated list of operators to delete")
34+
if err := indexCmd.MarkFlagRequired("operators"); err != nil {
35+
logrus.Panic("Failed to set required `operators` flag for `index delete`")
36+
}
37+
indexCmd.Flags().StringP("binary-image", "i", "", "container image for on-image `opm` command")
38+
indexCmd.Flags().StringP("container-tool", "c", "podman", "tool to interact with container images (save, build, etc.). One of: [docker, podman]")
39+
indexCmd.Flags().StringP("tag", "t", "", "custom tag for container image being built")
40+
indexCmd.Flags().Bool("permissive", false, "allow registry load errors")
41+
42+
if err := indexCmd.Flags().MarkHidden("debug"); err != nil {
43+
logrus.Panic(err.Error())
44+
}
45+
46+
return indexCmd
47+
48+
}
49+
50+
func runIndexDeleteCmdFunc(cmd *cobra.Command, args []string) error {
51+
generate, err := cmd.Flags().GetBool("generate")
52+
if err != nil {
53+
return err
54+
}
55+
56+
outDockerfile, err := cmd.Flags().GetString("out-dockerfile")
57+
if err != nil {
58+
return err
59+
}
60+
61+
fromIndex, err := cmd.Flags().GetString("from-index")
62+
if err != nil {
63+
return err
64+
}
65+
66+
operators, err := cmd.Flags().GetStringSlice("operators")
67+
if err != nil {
68+
return err
69+
}
70+
71+
binaryImage, err := cmd.Flags().GetString("binary-image")
72+
if err != nil {
73+
return err
74+
}
75+
76+
containerTool, err := cmd.Flags().GetString("container-tool")
77+
if err != nil {
78+
return err
79+
}
80+
81+
tag, err := cmd.Flags().GetString("tag")
82+
if err != nil {
83+
return err
84+
}
85+
86+
permissive, err := cmd.Flags().GetBool("permissive")
87+
if err != nil {
88+
return err
89+
}
90+
91+
logger := logrus.WithFields(logrus.Fields{"operators": operators})
92+
93+
logger.Info("building the index")
94+
95+
indexDeleter := indexer.NewIndexDeleter(containerTool, logger)
96+
97+
request := indexer.DeleteFromIndexRequest{
98+
Generate: generate,
99+
FromIndex: fromIndex,
100+
BinarySourceImage: binaryImage,
101+
OutDockerfile: outDockerfile,
102+
Operators: operators,
103+
Tag: tag,
104+
Permissive: permissive,
105+
}
106+
107+
err = indexDeleter.DeleteFromIndex(request)
108+
if err != nil {
109+
return err
110+
}
111+
112+
return nil
113+
}

cmd/opm/main.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88

99
"github.com/operator-framework/operator-registry/cmd/opm/alpha"
1010
"github.com/operator-framework/operator-registry/cmd/opm/registry"
11+
"github.com/operator-framework/operator-registry/cmd/opm/index"
1112
)
1213

1314
func main() {
@@ -25,6 +26,7 @@ func main() {
2526

2627
rootCmd.AddCommand(registry.NewOpmRegistryCmd())
2728
rootCmd.AddCommand(alpha.NewCmd())
29+
rootCmd.AddCommand(index.NewOpmIndexCmd())
2830

2931
rootCmd.Flags().Bool("debug", false, "enable debug logging")
3032
if err := rootCmd.Flags().MarkHidden("debug"); err != nil {

cmd/opm/registry/add.go

Lines changed: 17 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,10 @@
11
package registry
22

33
import (
4-
"context"
5-
"database/sql"
6-
"fmt"
4+
"github.com/operator-framework/operator-registry/pkg/lib/registry"
75

86
"github.com/sirupsen/logrus"
97
"github.com/spf13/cobra"
10-
11-
"github.com/operator-framework/operator-registry/pkg/sqlite"
128
)
139

1410
func newRegistryAddCmd() *cobra.Command {
@@ -31,6 +27,7 @@ func newRegistryAddCmd() *cobra.Command {
3127
rootCmd.Flags().StringP("database", "d", "bundles.db", "relative path to database file")
3228
rootCmd.Flags().StringSliceP("bundle-images", "b", []string{}, "comma separated list of links to bundle image")
3329
rootCmd.Flags().Bool("permissive", false, "allow registry load errors")
30+
rootCmd.Flags().StringP("container-tool", "c", "podman", "tool to interact with container images (save, build, etc.). One of: [docker, podman]")
3431

3532
return rootCmd
3633
}
@@ -50,32 +47,27 @@ func addFunc(cmd *cobra.Command, args []string) error {
5047
return err
5148
}
5249

53-
var errs []error
54-
55-
db, err := sql.Open("sqlite3", fromFilename)
50+
containerTool, err := cmd.Flags().GetString("container-tool")
5651
if err != nil {
5752
return err
5853
}
59-
defer db.Close()
6054

61-
dbLoader, err := sqlite.NewSQLLiteLoader(db)
62-
if err != nil {
63-
return err
64-
}
65-
if err := dbLoader.Migrate(context.TODO()); err != nil {
66-
return err
55+
request := registry.AddToRegistryRequest{
56+
Bundles: bundleImages,
57+
InputDatabase: fromFilename,
58+
Permissive: permissive,
59+
ContainerTool: containerTool,
6760
}
6861

69-
for _, bundleImage := range bundleImages {
70-
loader := sqlite.NewSQLLoaderForImage(dbLoader, bundleImage)
71-
if err := loader.Populate(); err != nil {
72-
err = fmt.Errorf("error loading bundle from image: %s", err)
73-
if !permissive {
74-
logrus.WithError(err).Fatal("permissive mode disabled")
75-
errs = append(errs, err)
76-
}
77-
logrus.WithError(err).Warn("permissive mode enabled")
78-
}
62+
logger := logrus.WithFields(logrus.Fields{"bundles": bundleImages})
63+
64+
logger.Info("adding to the registry")
65+
66+
registryAdder := registry.NewRegistryAdder(logger)
67+
68+
err = registryAdder.AddToRegistry(request)
69+
if err != nil {
70+
return err
7971
}
8072
return nil
8173
}

0 commit comments

Comments
 (0)