Move runtime to core/runtime

Signed-off-by: Derek McGowan <derek@mcg.dev>
This commit is contained in:
Derek McGowan
2024-01-17 09:58:04 -08:00
parent df9b0a0675
commit dbc74db6a1
93 changed files with 63 additions and 63 deletions

View File

@@ -0,0 +1,37 @@
/*
Copyright The containerd 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 logging
import (
"context"
"io"
)
// Config of the container logs
type Config struct {
ID string
Namespace string
Stdout io.Reader
Stderr io.Reader
}
// LoggerFunc is implemented by custom v2 logging binaries.
//
// ready should be called when the logging binary finishes its setup and the container can be started.
//
// An example implementation of LoggerFunc: https://github.com/containerd/containerd/tree/main/runtime/v2#logging
type LoggerFunc func(ctx context.Context, cfg *Config, ready func() error) error

View File

@@ -0,0 +1,64 @@
//go:build !windows
/*
Copyright The containerd 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 logging
import (
"context"
"fmt"
"os"
"os/signal"
"golang.org/x/sys/unix"
)
// Run the logging driver
func Run(fn LoggerFunc) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
config := &Config{
ID: os.Getenv("CONTAINER_ID"),
Namespace: os.Getenv("CONTAINER_NAMESPACE"),
Stdout: os.NewFile(3, "CONTAINER_STDOUT"),
Stderr: os.NewFile(4, "CONTAINER_STDERR"),
}
var (
sigCh = make(chan os.Signal, 32)
errCh = make(chan error, 1)
wait = os.NewFile(5, "CONTAINER_WAIT")
)
signal.Notify(sigCh, unix.SIGTERM)
go func() {
errCh <- fn(ctx, config, wait.Close)
}()
for {
select {
case <-sigCh:
cancel()
case err := <-errCh:
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
os.Exit(0)
}
}
}

View File

@@ -0,0 +1,97 @@
/*
Copyright The containerd 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 logging
import (
"context"
"errors"
"fmt"
"net"
"os"
"os/signal"
"syscall"
"github.com/Microsoft/go-winio"
)
// Run the logging driver
func Run(fn LoggerFunc) {
err := runInternal(fn)
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
os.Exit(0)
}
func runInternal(fn LoggerFunc) error {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
var (
soutPipe, serrPipe, waitPipe string
sout, serr, wait net.Conn
ok bool
err error
)
if soutPipe, ok = os.LookupEnv("CONTAINER_STDOUT"); !ok {
return errors.New("'CONTAINER_STDOUT' environment variable missing")
}
if sout, err = winio.DialPipeContext(ctx, soutPipe); err != nil {
return fmt.Errorf("unable to dial stdout pipe: %w", err)
}
if serrPipe, ok = os.LookupEnv("CONTAINER_STDERR"); !ok {
return errors.New("'CONTAINER_STDERR' environment variable missing")
}
if serr, err = winio.DialPipeContext(ctx, serrPipe); err != nil {
return fmt.Errorf("unable to dial stderr pipe: %w", err)
}
waitPipe = os.Getenv("CONTAINER_WAIT")
if wait, err = winio.DialPipeContext(ctx, waitPipe); err != nil {
return fmt.Errorf("unable to dial wait pipe: %w", err)
}
config := &Config{
ID: os.Getenv("CONTAINER_ID"),
Namespace: os.Getenv("CONTAINER_NAMESPACE"),
Stdout: sout,
Stderr: serr,
}
var (
sigCh = make(chan os.Signal, 2)
errCh = make(chan error, 1)
)
signal.Notify(sigCh, os.Interrupt, syscall.SIGTERM)
go func() {
errCh <- fn(ctx, config, wait.Close)
}()
for {
select {
case <-sigCh:
cancel()
case err = <-errCh:
return err
}
}
}