Skip to content

Commit

Permalink
Adding top level CLI that combines all others
Browse files Browse the repository at this point in the history
  • Loading branch information
gallettilance committed Sep 26, 2019
1 parent 2f3ee84 commit eb27824
Show file tree
Hide file tree
Showing 9 changed files with 494 additions and 414 deletions.
116 changes: 2 additions & 114 deletions cmd/appregistry-server/main.go
Original file line number Diff line number Diff line change
@@ -1,46 +1,13 @@
package main

import (
"fmt"
"net"

"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"google.golang.org/grpc"
"google.golang.org/grpc/reflection"

"github.com/operator-framework/operator-registry/pkg/api"
health "github.com/operator-framework/operator-registry/pkg/api/grpc_health_v1"
"github.com/operator-framework/operator-registry/pkg/appregistry"
"github.com/operator-framework/operator-registry/pkg/lib/log"
"github.com/operator-framework/operator-registry/pkg/registry"
"github.com/operator-framework/operator-registry/pkg/server"
cli "github.com/operator-framework/operator-registry/pkg/cli"
)

func main() {
var rootCmd = &cobra.Command{
Short: "appregistry-server",
Long: `appregistry-server downloads operator manifest(s) from remote appregistry, builds a sqlite database containing these downloaded manifest(s) and serves a grpc API to query it`,

PreRunE: func(cmd *cobra.Command, args []string) error {
if debug, _ := cmd.Flags().GetBool("debug"); debug {
logrus.SetLevel(logrus.DebugLevel)
}
return nil
},

RunE: runCmdFunc,
}

rootCmd.Flags().Bool("debug", false, "enable debug logging")
rootCmd.Flags().StringP("kubeconfig", "k", "", "absolute path to kubeconfig file")
rootCmd.Flags().StringP("download-folder", "f", "downloaded", "directory where downloaded nested operator bundle(s) will be stored to be processed further")
rootCmd.Flags().StringP("database", "d", "bundles.db", "name of db to output")
rootCmd.Flags().StringSliceP("registry", "r", []string{}, "pipe delimited operator source - {base url with cnr prefix}|{quay registry namespace}|{secret namespace/secret name}")
rootCmd.Flags().StringP("packages", "o", "", "comma separated list of package(s) to be downloaded from the specified operator source(s)")
rootCmd.Flags().StringP("port", "p", "50051", "port number to serve on")
rootCmd.Flags().StringP("termination-log", "t", "/dev/termination-log", "path to a container termination log file")
rootCmd.Flags().Bool("strict", false, "fail on registry load errors")
rootCmd := cli.NewAppregistryServerCmd()

if err := rootCmd.Flags().MarkHidden("debug"); err != nil {
logrus.Panic(err.Error())
Expand All @@ -50,82 +17,3 @@ func main() {
logrus.Panic(err.Error())
}
}

func runCmdFunc(cmd *cobra.Command, args []string) error {
// Immediately set up termination log
terminationLogPath, err := cmd.Flags().GetString("termination-log")
if err != nil {
return err
}
err = log.AddDefaultWriterHooks(terminationLogPath)
if err != nil {
logrus.WithError(err).Warn("unable to set termination log path")
}
kubeconfig, err := cmd.Flags().GetString("kubeconfig")
if err != nil {
return err
}
downloadPath, err := cmd.Flags().GetString("download-folder")
if err != nil {
return err
}
port, err := cmd.Flags().GetString("port")
if err != nil {
return err
}

sources, err := cmd.Flags().GetStringSlice("registry")
if err != nil {
return err
}

packages, err := cmd.Flags().GetString("packages")
if err != nil {
return err
}
dbName, err := cmd.Flags().GetString("database")
if err != nil {
return err
}
strict, err := cmd.Flags().GetBool("strict")
if err != nil {
return err
}

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

loader, err := appregistry.NewLoader(kubeconfig, dbName, downloadPath, logger)
if err != nil {
logger.Fatalf("error initializing: %s", err)
}

store, err := loader.Load(sources, packages)
if err != nil {
err = fmt.Errorf("error loading manifests from appregistry: %s", err)
if strict {
logger.WithError(err).Fatal("strict mode enabled")
}
logger.WithError(err).Warn("strict mode disabled")
}
if store == nil {
logger.Warn("using empty querier")
store = registry.NewEmptyQuerier()
}

lis, err := net.Listen("tcp", ":"+port)
if err != nil {
logger.Fatalf("failed to listen: %s", err)
}
s := grpc.NewServer()

api.RegisterRegistryServer(s, server.NewRegistryServer(store))
health.RegisterHealthServer(s, server.NewHealthServer())
reflection.Register(s)

logger.Info("serving registry")
if err := s.Serve(lis); err != nil {
logger.Fatalf("failed to serve: %s", err)
}

return nil
}
155 changes: 3 additions & 152 deletions cmd/configmap-server/main.go
Original file line number Diff line number Diff line change
@@ -1,168 +1,19 @@
package main

import (
"context"
"fmt"
"net"

"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"google.golang.org/grpc"
"google.golang.org/grpc/reflection"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"

"github.com/operator-framework/operator-registry/pkg/api"
health "github.com/operator-framework/operator-registry/pkg/api/grpc_health_v1"
"github.com/operator-framework/operator-registry/pkg/lib/log"
"github.com/operator-framework/operator-registry/pkg/registry"
"github.com/operator-framework/operator-registry/pkg/server"
"github.com/operator-framework/operator-registry/pkg/sqlite"
cli "github.com/operator-framework/operator-registry/pkg/cli"
)

var rootCmd = &cobra.Command{
Short: "configmap-server",
Long: `configmap-server reads a configmap and builds a sqlite database containing operator manifests and serves a grpc API to query it`,

PreRunE: func(cmd *cobra.Command, args []string) error {
if debug, _ := cmd.Flags().GetBool("debug"); debug {
logrus.SetLevel(logrus.DebugLevel)
}
return nil
},

RunE: runCmdFunc,
}
func main() {
rootCmd := cli.NewConfigMapServerCmd()

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

func main() {
if err := rootCmd.Execute(); err != nil {
logrus.Panic(err.Error())
}
}

func runCmdFunc(cmd *cobra.Command, args []string) error {
// Immediately set up termination log
terminationLogPath, err := cmd.Flags().GetString("termination-log")
if err != nil {
return err
}
err = log.AddDefaultWriterHooks(terminationLogPath)
if err != nil {
logrus.WithError(err).Warn("unable to set termination log path")
}
kubeconfig, err := cmd.Flags().GetString("kubeconfig")
if err != nil {
return err
}
port, err := cmd.Flags().GetString("port")
if err != nil {
return err
}
configMapName, err := cmd.Flags().GetString("configMapName")
if err != nil {
return err
}
configMapNamespace, err := cmd.Flags().GetString("configMapNamespace")
if err != nil {
return err
}
dbName, err := cmd.Flags().GetString("database")
if err != nil {
return err
}
permissive, err := cmd.Flags().GetBool("permissive")
if err != nil {
return err
}
logger := logrus.WithFields(logrus.Fields{"configMapName": configMapName, "configMapNamespace": configMapNamespace, "port": port})

client := NewClientFromConfig(kubeconfig, logger.Logger)
configMap, err := client.CoreV1().ConfigMaps(configMapNamespace).Get(configMapName, metav1.GetOptions{})
if err != nil {
logger.Fatalf("error getting configmap: %s", err)
}

sqlLoader, err := sqlite.NewSQLLiteLoader(dbName)
if err != nil {
return err
}

configMapPopulator := sqlite.NewSQLLoaderForConfigMap(sqlLoader, *configMap)
if err := configMapPopulator.Populate(); err != nil {
err = fmt.Errorf("error loading manifests from configmap: %s", err)
if !permissive {
logger.WithError(err).Fatal("permissive mode disabled")
}
logger.WithError(err).Warn("permissive mode enabled")
}

var store registry.Query
store, err = sqlite.NewSQLLiteQuerier(dbName)
if err != nil {
logger.WithError(err).Warnf("failed to load db")
}
if store == nil {
store = registry.NewEmptyQuerier()
}

// sanity check that the db is available
tables, err := store.ListTables(context.TODO())
if err != nil {
logger.WithError(err).Warnf("couldn't list tables in db")
}
if len(tables) == 0 {
logger.Warn("no tables found in db")
}

lis, err := net.Listen("tcp", ":"+port)
if err != nil {
logger.Fatalf("failed to listen: %s", err)
}
s := grpc.NewServer()

api.RegisterRegistryServer(s, server.NewRegistryServer(store))
health.RegisterHealthServer(s, server.NewHealthServer())
reflection.Register(s)

logger.Info("serving registry")
if err := s.Serve(lis); err != nil {
logger.Fatalf("failed to serve: %s", err)
}
return nil
}

// NewClient creates a kubernetes client or bails out on on failures.
func NewClientFromConfig(kubeconfig string, logger *logrus.Logger) kubernetes.Interface {
var config *rest.Config
var err error

if kubeconfig != "" {
logger.Infof("Loading kube client config from path %q", kubeconfig)
config, err = clientcmd.BuildConfigFromFlags("", kubeconfig)
} else {
logger.Infof("Using in-cluster kube client config")
config, err = rest.InClusterConfig()
}

if err != nil {
logger.Fatalf("Cannot load config for REST client: %s", err)
}

return kubernetes.NewForConfigOrDie(config)
}
62 changes: 3 additions & 59 deletions cmd/initializer/main.go
Original file line number Diff line number Diff line change
@@ -1,73 +1,17 @@
package main

import (
"fmt"

"github.com/sirupsen/logrus"
"github.com/spf13/cobra"

"github.com/operator-framework/operator-registry/pkg/sqlite"
cli "github.com/operator-framework/operator-registry/pkg/cli"
)

var rootCmd = &cobra.Command{
Short: "initializer",
Long: `initializer takes a directory of OLM manifests and outputs a sqlite database containing them`,

PreRunE: func(cmd *cobra.Command, args []string) error {
if debug, _ := cmd.Flags().GetBool("debug"); debug {
logrus.SetLevel(logrus.DebugLevel)
}
return nil
},

RunE: runCmdFunc,
}
func main() {
rootCmd := cli.NewInitializerCmd()

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

func main() {
if err := rootCmd.Execute(); err != nil {
panic(err)
}
}

func runCmdFunc(cmd *cobra.Command, args []string) error {
outFilename, err := cmd.Flags().GetString("output")
if err != nil {
return err
}
manifestDir, err := cmd.Flags().GetString("manifests")
if err != nil {
return err
}
permissive, err := cmd.Flags().GetBool("permissive")
if err != nil {
return err
}

dbLoader, err := sqlite.NewSQLLiteLoader(outFilename)
if err != nil {
return err
}
defer dbLoader.Close()

loader := sqlite.NewSQLLoaderForDirectory(dbLoader, manifestDir)
if err := loader.Populate(); err != nil {
err = fmt.Errorf("error loading manifests from directory: %s", err)
if !permissive {
logrus.WithError(err).Fatal("permissive mode disabled")
return err
}
logrus.WithError(err).Warn("permissive mode enabled")
}

return nil
}
Loading

0 comments on commit eb27824

Please sign in to comment.