Merge pull request #8093 from mxpv/instrument

Extract CRI instrument into separate package
This commit is contained in:
Derek McGowan 2023-02-12 21:45:13 -08:00 committed by GitHub
commit c6cf6b2522
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 66 additions and 1751 deletions

View File

@ -14,7 +14,7 @@
limitations under the License. limitations under the License.
*/ */
package sbserver package instrument
import ( import (
"context" "context"
@ -29,23 +29,49 @@ import (
ctrdutil "github.com/containerd/containerd/pkg/cri/util" ctrdutil "github.com/containerd/containerd/pkg/cri/util"
) )
// instrumentedService wraps service with containerd namespace and logs. const (
type instrumentedService struct { // criSpanPrefix is a prefix for CRI server specific spans
c *criService criSpanPrefix = "pkg.cri.server"
)
// criService is an CRI server dependency to be wrapped with instrumentation.
type criService interface {
GRPCServices
IsInitialized() bool
// AlphaVersion returns the runtime name, runtime version and runtime API version.
AlphaVersion(ctx context.Context, r *runtime_alpha.VersionRequest) (*runtime_alpha.VersionResponse, error)
} }
func newInstrumentedService(c *criService) grpcServices { // GRPCServices are all the grpc services provided by cri containerd.
type GRPCServices interface {
runtime.RuntimeServiceServer
runtime.ImageServiceServer
}
type GRPCAlphaServices interface {
runtime_alpha.RuntimeServiceServer
runtime_alpha.ImageServiceServer
}
// instrumentedService wraps service with containerd namespace and logs.
type instrumentedService struct {
c criService
}
func NewService(c criService) GRPCServices {
return &instrumentedService{c: c} return &instrumentedService{c: c}
} }
// instrumentedAlphaService wraps service with containerd namespace and logs. // instrumentedAlphaService wraps service with containerd namespace and logs.
type instrumentedAlphaService struct { type instrumentedAlphaService struct {
c *criService c criService
runtime_alpha.UnimplementedRuntimeServiceServer runtime_alpha.UnimplementedRuntimeServiceServer
runtime_alpha.UnimplementedImageServiceServer runtime_alpha.UnimplementedImageServiceServer
} }
func newInstrumentedAlphaService(c *criService) grpcAlphaServices { func NewAlphaService(c criService) GRPCAlphaServices {
return &instrumentedAlphaService{c: c} return &instrumentedAlphaService{c: c}
} }
@ -54,7 +80,7 @@ func newInstrumentedAlphaService(c *criService) grpcAlphaServices {
// initialized. // initialized.
// NOTE(random-liu): All following functions MUST check initialized at the beginning. // NOTE(random-liu): All following functions MUST check initialized at the beginning.
func (in *instrumentedService) checkInitialized() error { func (in *instrumentedService) checkInitialized() error {
if in.c.initialized.IsSet() { if in.c.IsInitialized() {
return nil return nil
} }
return errors.New("server is not initialized yet") return errors.New("server is not initialized yet")
@ -65,7 +91,7 @@ func (in *instrumentedService) checkInitialized() error {
// initialized. // initialized.
// NOTE(random-liu): All following functions MUST check initialized at the beginning. // NOTE(random-liu): All following functions MUST check initialized at the beginning.
func (in *instrumentedAlphaService) checkInitialized() error { func (in *instrumentedAlphaService) checkInitialized() error {
if in.c.initialized.IsSet() { if in.c.IsInitialized() {
return nil return nil
} }
return errors.New("server is not initialized yet") return errors.New("server is not initialized yet")

View File

@ -98,9 +98,6 @@ const (
// runtimeRunhcsV1 is the runtime type for runhcs. // runtimeRunhcsV1 is the runtime type for runhcs.
runtimeRunhcsV1 = "io.containerd.runhcs.v1" runtimeRunhcsV1 = "io.containerd.runhcs.v1"
// name prefix for CRI sbserver specific spans
criSpanPrefix = "pkg.cri.sbserver"
) )
// makeSandboxName generates sandbox name from sandbox metadata. The name // makeSandboxName generates sandbox name from sandbox metadata. The name

View File

@ -29,6 +29,7 @@ import (
"github.com/containerd/containerd" "github.com/containerd/containerd"
"github.com/containerd/containerd/oci" "github.com/containerd/containerd/oci"
"github.com/containerd/containerd/pkg/cri/instrument"
"github.com/containerd/containerd/pkg/cri/sbserver/podsandbox" "github.com/containerd/containerd/pkg/cri/sbserver/podsandbox"
"github.com/containerd/containerd/pkg/cri/streaming" "github.com/containerd/containerd/pkg/cri/streaming"
"github.com/containerd/containerd/pkg/kmutex" "github.com/containerd/containerd/pkg/kmutex"
@ -56,24 +57,16 @@ import (
// defaultNetworkPlugin is used for the default CNI configuration // defaultNetworkPlugin is used for the default CNI configuration
const defaultNetworkPlugin = "default" const defaultNetworkPlugin = "default"
// grpcServices are all the grpc services provided by cri containerd.
type grpcServices interface {
runtime.RuntimeServiceServer
runtime.ImageServiceServer
}
type grpcAlphaServices interface {
runtime_alpha.RuntimeServiceServer
runtime_alpha.ImageServiceServer
}
// CRIService is the interface implement CRI remote service server. // CRIService is the interface implement CRI remote service server.
type CRIService interface { type CRIService interface {
Run() error runtime.RuntimeServiceServer
// io.Closer is used by containerd to gracefully stop cri service. runtime.ImageServiceServer
// Closer is used by containerd to gracefully stop cri service.
io.Closer io.Closer
Run() error
Register(*grpc.Server) error Register(*grpc.Server) error
grpcServices
} }
// criService implements CRIService. // criService implements CRIService.
@ -329,13 +322,20 @@ func (c *criService) Close() error {
return nil return nil
} }
// IsInitialized indicates whether CRI service has finished initialization.
func (c *criService) IsInitialized() bool {
return c.initialized.IsSet()
}
func (c *criService) register(s *grpc.Server) error { func (c *criService) register(s *grpc.Server) error {
instrumented := newInstrumentedService(c) instrumented := instrument.NewService(c)
runtime.RegisterRuntimeServiceServer(s, instrumented) runtime.RegisterRuntimeServiceServer(s, instrumented)
runtime.RegisterImageServiceServer(s, instrumented) runtime.RegisterImageServiceServer(s, instrumented)
instrumentedAlpha := newInstrumentedAlphaService(c)
instrumentedAlpha := instrument.NewAlphaService(c)
runtime_alpha.RegisterRuntimeServiceServer(s, instrumentedAlpha) runtime_alpha.RegisterRuntimeServiceServer(s, instrumentedAlpha)
runtime_alpha.RegisterImageServiceServer(s, instrumentedAlpha) runtime_alpha.RegisterImageServiceServer(s, instrumentedAlpha)
return nil return nil
} }

View File

@ -94,9 +94,6 @@ const (
// runtimeRunhcsV1 is the runtime type for runhcs. // runtimeRunhcsV1 is the runtime type for runhcs.
runtimeRunhcsV1 = "io.containerd.runhcs.v1" runtimeRunhcsV1 = "io.containerd.runhcs.v1"
// name prefix for CRI server specific spans
criSpanPrefix = "pkg.cri.server"
) )
// makeSandboxName generates sandbox name from sandbox metadata. The name // makeSandboxName generates sandbox name from sandbox metadata. The name

File diff suppressed because it is too large Load Diff

View File

@ -28,6 +28,7 @@ import (
"github.com/containerd/containerd" "github.com/containerd/containerd"
"github.com/containerd/containerd/oci" "github.com/containerd/containerd/oci"
"github.com/containerd/containerd/pkg/cri/instrument"
"github.com/containerd/containerd/pkg/cri/streaming" "github.com/containerd/containerd/pkg/cri/streaming"
"github.com/containerd/containerd/pkg/kmutex" "github.com/containerd/containerd/pkg/kmutex"
"github.com/containerd/containerd/pkg/nri" "github.com/containerd/containerd/pkg/nri"
@ -54,25 +55,16 @@ import (
// defaultNetworkPlugin is used for the default CNI configuration // defaultNetworkPlugin is used for the default CNI configuration
const defaultNetworkPlugin = "default" const defaultNetworkPlugin = "default"
// grpcServices are all the grpc services provided by cri containerd.
type grpcServices interface {
runtime.RuntimeServiceServer
runtime.ImageServiceServer
}
type grpcAlphaServices interface {
runtime_alpha.RuntimeServiceServer
runtime_alpha.ImageServiceServer
}
// CRIService is the interface implement CRI remote service server. // CRIService is the interface implement CRI remote service server.
type CRIService interface { type CRIService interface {
runtime.RuntimeServiceServer
runtime.ImageServiceServer
// Closer is used by containerd to gracefully stop cri service.
io.Closer
Run() error Run() error
// io.Closer is used by containerd to gracefully stop cri service.
io.Closer
Register(*grpc.Server) error Register(*grpc.Server) error
grpcServices
} }
// criService implements CRIService. // criService implements CRIService.
@ -319,13 +311,20 @@ func (c *criService) Close() error {
return nil return nil
} }
// IsInitialized indicates whether CRI service has finished initialization.
func (c *criService) IsInitialized() bool {
return c.initialized.IsSet()
}
func (c *criService) register(s *grpc.Server) error { func (c *criService) register(s *grpc.Server) error {
instrumented := newInstrumentedService(c) instrumented := instrument.NewService(c)
runtime.RegisterRuntimeServiceServer(s, instrumented) runtime.RegisterRuntimeServiceServer(s, instrumented)
runtime.RegisterImageServiceServer(s, instrumented) runtime.RegisterImageServiceServer(s, instrumented)
instrumentedAlpha := newInstrumentedAlphaService(c)
instrumentedAlpha := instrument.NewAlphaService(c)
runtime_alpha.RegisterRuntimeServiceServer(s, instrumentedAlpha) runtime_alpha.RegisterRuntimeServiceServer(s, instrumentedAlpha)
runtime_alpha.RegisterImageServiceServer(s, instrumentedAlpha) runtime_alpha.RegisterImageServiceServer(s, instrumentedAlpha)
return nil return nil
} }