refactor: introduce dedicated config package for cmds

Signed-off-by: Philippe Scorsolini <p.scorsolini@gmail.com>
This commit is contained in:
Philippe Scorsolini 2023-08-17 12:02:09 +02:00
parent c994789856
commit 1efafa1c41
5 changed files with 34 additions and 14 deletions

View File

@ -0,0 +1,23 @@
/*
Copyright 2022 The Crossplane Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
// Package config contains the global config for all commands
package config
// Args contains the default registry used to pull XFN containers.
type Args struct {
Registry string `short:"r" help:"Default registry used to fetch containers when not specified in tag." default:"${default_registry}" env:"REGISTRY"`
}

View File

@ -26,6 +26,7 @@ import (
"github.com/crossplane/crossplane-runtime/pkg/logging" "github.com/crossplane/crossplane-runtime/pkg/logging"
"github.com/crossplane/function-runtime-oci/cmd/function-runtime-oci/internal/config"
"github.com/crossplane/function-runtime-oci/cmd/function-runtime-oci/run" "github.com/crossplane/function-runtime-oci/cmd/function-runtime-oci/run"
"github.com/crossplane/function-runtime-oci/cmd/function-runtime-oci/spark" "github.com/crossplane/function-runtime-oci/cmd/function-runtime-oci/spark"
"github.com/crossplane/function-runtime-oci/cmd/function-runtime-oci/start" "github.com/crossplane/function-runtime-oci/cmd/function-runtime-oci/start"
@ -44,8 +45,8 @@ var KongVars = kong.Vars{
var cli struct { var cli struct {
Debug debugFlag `short:"d" help:"Print verbose logging statements."` Debug debugFlag `short:"d" help:"Print verbose logging statements."`
Version versionFlag `short:"v" help:"Print version and quit."` Version versionFlag `short:"v" help:"Print version and quit."`
Registry string `short:"r" help:"Default registry used to fetch containers when not specified in tag." default:"${default_registry}" env:"REGISTRY"` config.Args `embed:""`
Start start.Command `cmd:"" help:"Start listening for Composition Function runs over gRPC." default:"1"` Start start.Command `cmd:"" help:"Start listening for Composition Function runs over gRPC." default:"1"`
Run run.Command `cmd:"" help:"Run a Composition Function."` Run run.Command `cmd:"" help:"Run a Composition Function."`
@ -76,8 +77,9 @@ func main() {
kong.Name("function-runtime-oci"), kong.Name("function-runtime-oci"),
kong.Description("Crossplane Composition Functions."), kong.Description("Crossplane Composition Functions."),
kong.BindTo(logging.NewLogrLogger(zl), (*logging.Logger)(nil)), kong.BindTo(logging.NewLogrLogger(zl), (*logging.Logger)(nil)),
kong.BindTo(cli.Args, (*config.Args)(nil)),
kong.UsageOnError(), kong.UsageOnError(),
KongVars, KongVars,
) )
ctx.FatalIfErrorf(ctx.Run(&start.Args{Registry: cli.Registry})) ctx.FatalIfErrorf(ctx.Run())
} }

View File

@ -29,7 +29,7 @@ import (
"github.com/crossplane/crossplane-runtime/pkg/errors" "github.com/crossplane/crossplane-runtime/pkg/errors"
"github.com/crossplane/function-runtime-oci/cmd/function-runtime-oci/start" "github.com/crossplane/function-runtime-oci/cmd/function-runtime-oci/internal/config"
"github.com/crossplane/function-runtime-oci/internal/container" "github.com/crossplane/function-runtime-oci/internal/container"
"github.com/crossplane/function-runtime-oci/internal/proto/v1alpha1" "github.com/crossplane/function-runtime-oci/internal/proto/v1alpha1"
) )
@ -60,7 +60,7 @@ type Command struct {
} }
// Run a Composition container function. // Run a Composition container function.
func (c *Command) Run(args *start.Args) error { func (c *Command) Run(args *config.Args) error {
// If we don't have CAP_SETUID or CAP_SETGID, we'll only be able to map our // If we don't have CAP_SETUID or CAP_SETGID, we'll only be able to map our
// own UID and GID to root inside the user namespace. // own UID and GID to root inside the user namespace.
rootUID := os.Getuid() rootUID := os.Getuid()

View File

@ -34,7 +34,7 @@ import (
"github.com/crossplane/crossplane-runtime/pkg/errors" "github.com/crossplane/crossplane-runtime/pkg/errors"
"github.com/crossplane/function-runtime-oci/cmd/function-runtime-oci/start" "github.com/crossplane/function-runtime-oci/cmd/function-runtime-oci/internal/config"
"github.com/crossplane/function-runtime-oci/internal/oci" "github.com/crossplane/function-runtime-oci/internal/oci"
"github.com/crossplane/function-runtime-oci/internal/oci/spec" "github.com/crossplane/function-runtime-oci/internal/oci/spec"
"github.com/crossplane/function-runtime-oci/internal/oci/store" "github.com/crossplane/function-runtime-oci/internal/oci/store"
@ -81,7 +81,7 @@ type Command struct {
// Run a Composition Function inside an unprivileged user namespace. Reads a // Run a Composition Function inside an unprivileged user namespace. Reads a
// protocol buffer serialized RunFunctionRequest from stdin, and writes a // protocol buffer serialized RunFunctionRequest from stdin, and writes a
// protocol buffer serialized RunFunctionResponse to stdout. // protocol buffer serialized RunFunctionResponse to stdout.
func (c *Command) Run(args *start.Args) error { //nolint:gocyclo // TODO(negz): Refactor some of this out into functions, add tests. func (c *Command) Run(args *config.Args) error { //nolint:gocyclo // TODO(negz): Refactor some of this out into functions, add tests.
pb, err := io.ReadAll(os.Stdin) pb, err := io.ReadAll(os.Stdin)
if err != nil { if err != nil {
return errors.Wrap(err, errReadRequest) return errors.Wrap(err, errReadRequest)

View File

@ -25,6 +25,7 @@ import (
"github.com/crossplane/crossplane-runtime/pkg/errors" "github.com/crossplane/crossplane-runtime/pkg/errors"
"github.com/crossplane/crossplane-runtime/pkg/logging" "github.com/crossplane/crossplane-runtime/pkg/logging"
"github.com/crossplane/function-runtime-oci/cmd/function-runtime-oci/internal/config"
"github.com/crossplane/function-runtime-oci/internal/container" "github.com/crossplane/function-runtime-oci/internal/container"
) )
@ -33,12 +34,6 @@ const (
errListenAndServe = "cannot listen for and serve gRPC API" errListenAndServe = "cannot listen for and serve gRPC API"
) )
// Args contains the default registry used to pull function-runtime-oci
// containers.
type Args struct {
Registry string
}
// Command starts a gRPC API to run Composition Functions. // Command starts a gRPC API to run Composition Functions.
type Command struct { type Command struct {
CacheDir string `short:"c" help:"Directory used for caching function images and containers." default:"/function-runtime-oci"` CacheDir string `short:"c" help:"Directory used for caching function images and containers." default:"/function-runtime-oci"`
@ -49,7 +44,7 @@ type Command struct {
} }
// Run a Composition Function gRPC API. // Run a Composition Function gRPC API.
func (c *Command) Run(args *Args, log logging.Logger) error { func (c *Command) Run(args *config.Args, log logging.Logger) error {
// If we don't have CAP_SETUID or CAP_SETGID, we'll only be able to map our // If we don't have CAP_SETUID or CAP_SETGID, we'll only be able to map our
// own UID and GID to root inside the user namespace. // own UID and GID to root inside the user namespace.
rootUID := os.Getuid() rootUID := os.Getuid()