From d6ffe9fbd11a7960e5a0c7e1bcde68d3ad341884 Mon Sep 17 00:00:00 2001 From: Philippe Scorsolini Date: Thu, 17 Aug 2023 11:49:30 +0200 Subject: [PATCH] refactor: rename function-runtime-oci package to container Signed-off-by: Philippe Scorsolini --- cmd/function-runtime-oci/run/run.go | 6 +-- cmd/function-runtime-oci/start/start.go | 16 ++++---- .../container.go | 38 +++++++++---------- .../container_linux.go | 4 +- .../container_nonlinux.go | 4 +- .../container_nonunix.go | 2 +- .../container_unix.go | 2 +- .../doc.go | 4 +- 8 files changed, 38 insertions(+), 38 deletions(-) rename internal/{function-runtime-oci => container}/container.go (72%) rename internal/{function-runtime-oci => container}/container_linux.go (97%) rename internal/{function-runtime-oci => container}/container_nonlinux.go (87%) rename internal/{function-runtime-oci => container}/container_nonunix.go (96%) rename internal/{function-runtime-oci => container}/container_unix.go (98%) rename internal/{function-runtime-oci => container}/doc.go (84%) diff --git a/cmd/function-runtime-oci/run/run.go b/cmd/function-runtime-oci/run/run.go index d64f72c..fb59f1c 100644 --- a/cmd/function-runtime-oci/run/run.go +++ b/cmd/function-runtime-oci/run/run.go @@ -30,7 +30,7 @@ import ( "github.com/crossplane/crossplane-runtime/pkg/errors" "github.com/crossplane/function-runtime-oci/cmd/function-runtime-oci/start" - "github.com/crossplane/function-runtime-oci/internal/function-runtime-oci" + "github.com/crossplane/function-runtime-oci/internal/container" "github.com/crossplane/function-runtime-oci/internal/proto/v1alpha1" ) @@ -65,7 +65,7 @@ func (c *Command) Run(args *start.Args) error { // own UID and GID to root inside the user namespace. rootUID := os.Getuid() rootGID := os.Getgid() - setuid := function_runtime_oci.HasCapSetUID() && function_runtime_oci.HasCapSetGID() // We're using 'setuid' as shorthand for both here. + setuid := container.HasCapSetUID() && container.HasCapSetGID() // We're using 'setuid' as shorthand for both here. if setuid { rootUID = c.MapRootUID rootGID = c.MapRootGID @@ -91,7 +91,7 @@ func (c *Command) Run(args *start.Args) error { return errors.Wrap(err, errAuthCfg) } - f := function_runtime_oci.NewContainerRunner(function_runtime_oci.SetUID(setuid), function_runtime_oci.MapToRoot(rootUID, rootGID), function_runtime_oci.WithCacheDir(filepath.Clean(c.CacheDir)), function_runtime_oci.WithRegistry(args.Registry)) + f := container.NewRunner(container.SetUID(setuid), container.MapToRoot(rootUID, rootGID), container.WithCacheDir(filepath.Clean(c.CacheDir)), container.WithRegistry(args.Registry)) rsp, err := f.RunFunction(context.Background(), &v1alpha1.RunFunctionRequest{ Image: c.Image, Input: c.FunctionIO, diff --git a/cmd/function-runtime-oci/start/start.go b/cmd/function-runtime-oci/start/start.go index 077dc4a..ed5f9c1 100644 --- a/cmd/function-runtime-oci/start/start.go +++ b/cmd/function-runtime-oci/start/start.go @@ -25,7 +25,7 @@ import ( "github.com/crossplane/crossplane-runtime/pkg/errors" "github.com/crossplane/crossplane-runtime/pkg/logging" - "github.com/crossplane/function-runtime-oci/internal/function-runtime-oci" + "github.com/crossplane/function-runtime-oci/internal/container" ) // Error strings @@ -54,18 +54,18 @@ func (c *Command) Run(args *Args, log logging.Logger) error { // own UID and GID to root inside the user namespace. rootUID := os.Getuid() rootGID := os.Getgid() - setuid := function_runtime_oci.HasCapSetUID() && function_runtime_oci.HasCapSetGID() // We're using 'setuid' as shorthand for both here. + setuid := container.HasCapSetUID() && container.HasCapSetGID() // We're using 'setuid' as shorthand for both here. if setuid { rootUID = c.MapRootUID rootGID = c.MapRootGID } // TODO(negz): Expose a healthz endpoint and otel metrics. - f := function_runtime_oci.NewContainerRunner( - function_runtime_oci.SetUID(setuid), - function_runtime_oci.MapToRoot(rootUID, rootGID), - function_runtime_oci.WithCacheDir(filepath.Clean(c.CacheDir)), - function_runtime_oci.WithLogger(log), - function_runtime_oci.WithRegistry(args.Registry)) + f := container.NewRunner( + container.SetUID(setuid), + container.MapToRoot(rootUID, rootGID), + container.WithCacheDir(filepath.Clean(c.CacheDir)), + container.WithLogger(log), + container.WithRegistry(args.Registry)) return errors.Wrap(f.ListenAndServe(c.Network, c.Address), errListenAndServe) } diff --git a/internal/function-runtime-oci/container.go b/internal/container/container.go similarity index 72% rename from internal/function-runtime-oci/container.go rename to internal/container/container.go index ec58ec3..a8fd5b9 100644 --- a/internal/function-runtime-oci/container.go +++ b/internal/container/container.go @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and limitations under the License. */ -package function_runtime_oci +package container import ( "io" @@ -36,9 +36,9 @@ const ( const defaultCacheDir = "/function-runtime-oci" -// An ContainerRunner runs a Composition Function packaged as an OCI image by +// A Runner runs a Composition Function packaged as an OCI image by // extracting it and running it as a 'rootless' container. -type ContainerRunner struct { +type Runner struct { v1alpha1.UnimplementedContainerizedFunctionRunnerServiceServer log logging.Logger @@ -50,13 +50,13 @@ type ContainerRunner struct { registry string } -// A ContainerRunnerOption configures a new ContainerRunner. -type ContainerRunnerOption func(*ContainerRunner) +// A RunnerOption configures a new Runner. +type RunnerOption func(*Runner) // MapToRoot configures what UID and GID should map to root (UID/GID 0) in the // user namespace in which the function will be run. -func MapToRoot(uid, gid int) ContainerRunnerOption { - return func(r *ContainerRunner) { +func MapToRoot(uid, gid int) RunnerOption { + return func(r *Runner) { r.rootUID = uid r.rootGID = gid } @@ -65,40 +65,40 @@ func MapToRoot(uid, gid int) ContainerRunnerOption { // SetUID indicates that the container runner should attempt operations that // require CAP_SETUID and CAP_SETGID, for example creating a user namespace that // maps arbitrary UIDs and GIDs to the parent namespace. -func SetUID(s bool) ContainerRunnerOption { - return func(r *ContainerRunner) { +func SetUID(s bool) RunnerOption { + return func(r *Runner) { r.setuid = s } } // WithCacheDir specifies the directory used for caching function images and // containers. -func WithCacheDir(d string) ContainerRunnerOption { - return func(r *ContainerRunner) { +func WithCacheDir(d string) RunnerOption { + return func(r *Runner) { r.cache = d } } // WithRegistry specifies the default registry used to retrieve function images and // containers. -func WithRegistry(dr string) ContainerRunnerOption { - return func(r *ContainerRunner) { +func WithRegistry(dr string) RunnerOption { + return func(r *Runner) { r.registry = dr } } // WithLogger configures which logger the container runner should use. Logging // is disabled by default. -func WithLogger(l logging.Logger) ContainerRunnerOption { - return func(cr *ContainerRunner) { +func WithLogger(l logging.Logger) RunnerOption { + return func(cr *Runner) { cr.log = l } } -// NewContainerRunner returns a new Runner that runs functions as rootless +// NewRunner returns a new Runner that runs functions as rootless // containers. -func NewContainerRunner(o ...ContainerRunnerOption) *ContainerRunner { - r := &ContainerRunner{cache: defaultCacheDir, log: logging.NewNopLogger()} +func NewRunner(o ...RunnerOption) *Runner { + r := &Runner{cache: defaultCacheDir, log: logging.NewNopLogger()} for _, fn := range o { fn(r) } @@ -107,7 +107,7 @@ func NewContainerRunner(o ...ContainerRunnerOption) *ContainerRunner { } // ListenAndServe gRPC connections at the supplied address. -func (r *ContainerRunner) ListenAndServe(network, address string) error { +func (r *Runner) ListenAndServe(network, address string) error { r.log.Debug("Listening", "network", network, "address", address) lis, err := net.Listen(network, address) if err != nil { diff --git a/internal/function-runtime-oci/container_linux.go b/internal/container/container_linux.go similarity index 97% rename from internal/function-runtime-oci/container_linux.go rename to internal/container/container_linux.go index dad3801..f4045a9 100644 --- a/internal/function-runtime-oci/container_linux.go +++ b/internal/container/container_linux.go @@ -16,7 +16,7 @@ See the License for the specific language governing permissions and limitations under the License. */ -package function_runtime_oci +package container import ( "bytes" @@ -82,7 +82,7 @@ func HasCapSetGID() bool { // RunFunction runs a function as a rootless OCI container. Functions that // return non-zero, or that cannot be executed in the first place (e.g. because // they cannot be fetched from the registry) will return an error. -func (r *ContainerRunner) RunFunction(ctx context.Context, req *v1alpha1.RunFunctionRequest) (*v1alpha1.RunFunctionResponse, error) { +func (r *Runner) RunFunction(ctx context.Context, req *v1alpha1.RunFunctionRequest) (*v1alpha1.RunFunctionResponse, error) { r.log.Debug("Running function", "image", req.Image) /* diff --git a/internal/function-runtime-oci/container_nonlinux.go b/internal/container/container_nonlinux.go similarity index 87% rename from internal/function-runtime-oci/container_nonlinux.go rename to internal/container/container_nonlinux.go index 680701c..0ddb4c9 100644 --- a/internal/function-runtime-oci/container_nonlinux.go +++ b/internal/container/container_nonlinux.go @@ -16,7 +16,7 @@ See the License for the specific language governing permissions and limitations under the License. */ -package function_runtime_oci +package container import ( "context" @@ -35,6 +35,6 @@ func HasCapSetUID() bool { return false } func HasCapSetGID() bool { return false } // RunFunction returns an error on non-Linux. -func (r *ContainerRunner) RunFunction(_ context.Context, _ *v1alpha1.RunFunctionRequest) (*v1alpha1.RunFunctionResponse, error) { +func (r *Runner) RunFunction(_ context.Context, _ *v1alpha1.RunFunctionRequest) (*v1alpha1.RunFunctionResponse, error) { return nil, errors.New(errLinuxOnly) } diff --git a/internal/function-runtime-oci/container_nonunix.go b/internal/container/container_nonunix.go similarity index 96% rename from internal/function-runtime-oci/container_nonunix.go rename to internal/container/container_nonunix.go index c77c298..f4cf1bb 100644 --- a/internal/function-runtime-oci/container_nonunix.go +++ b/internal/container/container_nonunix.go @@ -16,7 +16,7 @@ See the License for the specific language governing permissions and limitations under the License. */ -package function_runtime_oci +package container import ( "os/exec" diff --git a/internal/function-runtime-oci/container_unix.go b/internal/container/container_unix.go similarity index 98% rename from internal/function-runtime-oci/container_unix.go rename to internal/container/container_unix.go index 5b2fa22..dd52e35 100644 --- a/internal/function-runtime-oci/container_unix.go +++ b/internal/container/container_unix.go @@ -16,7 +16,7 @@ See the License for the specific language governing permissions and limitations under the License. */ -package function_runtime_oci +package container import ( "os/exec" diff --git a/internal/function-runtime-oci/doc.go b/internal/container/doc.go similarity index 84% rename from internal/function-runtime-oci/doc.go rename to internal/container/doc.go index 4dfe0c2..47861ea 100644 --- a/internal/function-runtime-oci/doc.go +++ b/internal/container/doc.go @@ -14,6 +14,6 @@ See the License for the specific language governing permissions and limitations under the License. */ -// Package function-runtime-oci is the reference implementation of Composition +// Package container is the reference implementation of Composition // Functions. -package function_runtime_oci +package container