-
Notifications
You must be signed in to change notification settings - Fork 249
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding top level CLI that combines all others
- Loading branch information
1 parent
2f3ee84
commit eb27824
Showing
9 changed files
with
494 additions
and
414 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
Oops, something went wrong.