Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add simultaneous v1 and v1alpha2 support #139

Merged
merged 1 commit into from
Dec 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 14 additions & 7 deletions backend/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,14 @@ package backend

import (
"fmt"
runtimeapi_alpha "k8s.io/cri-api/pkg/apis/runtime/v1alpha2"
"net"
"os"
"strings"

"github.com/sirupsen/logrus"
"google.golang.org/grpc"
runtimeapi "k8s.io/cri-api/pkg/apis/runtime/v1alpha2"
runtimeapi "k8s.io/cri-api/pkg/apis/runtime/v1"
"k8s.io/kubernetes/pkg/kubelet/util"

"github.com/Mirantis/cri-dockerd/core"
Expand All @@ -34,19 +35,19 @@ import (
// grpc library default is 4MB
const maxMsgSize = 1024 * 1024 * 16

// CriDockerServer is the grpc backend of cri-dockerd.
type CriDockerServer struct {
// CriDockerService is the grpc backend of cri-dockerd.
type CriDockerService struct {
// endpoint is the endpoint to serve on.
endpoint string
// service is the docker service which implements runtime and image services.
service core.CRIService
service core.DockerService
// server is the grpc server.
server *grpc.Server
}

// NewCriDockerServer creates the cri-dockerd grpc backend.
func NewCriDockerServer(endpoint string, s core.CRIService) *CriDockerServer {
return &CriDockerServer{
func NewCriDockerServer(endpoint string, s core.DockerService) *CriDockerService {
return &CriDockerService{
endpoint: endpoint,
service: s,
}
Expand All @@ -65,7 +66,7 @@ func getListener(addr string) (net.Listener, error) {
}

// Start starts the cri-dockerd grpc backend.
func (s *CriDockerServer) Start() error {
func (s *CriDockerService) Start() error {
// Start the internal service.
if err := s.service.Start(); err != nil {
logrus.Error(err, "Unable to start cri-dockerd service")
Expand All @@ -82,6 +83,7 @@ func (s *CriDockerServer) Start() error {
grpc.MaxRecvMsgSize(maxMsgSize),
grpc.MaxSendMsgSize(maxMsgSize),
)

runtimeapi.RegisterRuntimeServiceServer(s.server, s.service)
runtimeapi.RegisterImageServiceServer(s.server, s.service)
go func() {
Expand All @@ -90,6 +92,11 @@ func (s *CriDockerServer) Start() error {
os.Exit(1)
}
}()

as := core.NewDockerServiceAlpha(s.service)
runtimeapi_alpha.RegisterRuntimeServiceServer(s.server, as)
runtimeapi_alpha.RegisterImageServiceServer(s.server, as)

handleNotify()
return nil
}
2 changes: 1 addition & 1 deletion cmd/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import (

"github.com/spf13/cobra"
"github.com/spf13/pflag"
runtimeapi "k8s.io/cri-api/pkg/apis/runtime/v1alpha2"
runtimeapi "k8s.io/cri-api/pkg/apis/runtime/v1"
)

const (
Expand Down
7 changes: 6 additions & 1 deletion config/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ const (
MaxContainerTerminationMessageLogLines = 80
)

//Security constants
// Security constants
const (
// ImagePolicyFailedOpenKey is added to pods created by failing open when the image policy
// webhook backend fails.
Expand Down Expand Up @@ -165,4 +165,9 @@ const (
// This annotation is used by the Attach Detach Controller to determine whether to use the in-tree or
// CSI Backend for a volume plugin on a specific node.
MigratedPluginsAnnotationKey = "storage.alpha.kubernetes.io/migrated-plugins"

// CRIVersion is the latest CRI version supported by the CRI plugin.
CRIVersion = "v1"
// CRIVersionAlpha is the alpha version of CRI supported by the CRI plugin.
CRIVersionAlpha = "v1alpha2"
)
3 changes: 1 addition & 2 deletions core/container_attach.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (
"context"
"github.com/Mirantis/cri-dockerd/libdocker"
"github.com/Mirantis/cri-dockerd/streaming"
v1 "k8s.io/cri-api/pkg/apis/runtime/v1alpha2"
v1 "k8s.io/cri-api/pkg/apis/runtime/v1"
)

// Attach prepares a streaming endpoint to attach to a running container, and returns the address.
Expand All @@ -37,4 +37,3 @@ func (ds *dockerService) Attach(
}
return ds.streamingServer.GetAttach(req)
}

2 changes: 1 addition & 1 deletion core/container_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import (
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/api/types/strslice"
v1 "k8s.io/cri-api/pkg/apis/runtime/v1alpha2"
v1 "k8s.io/cri-api/pkg/apis/runtime/v1"
)

// CreateContainer creates a new container in the given PodSandbox
Expand Down
3 changes: 1 addition & 2 deletions core/container_exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (
"github.com/Mirantis/cri-dockerd/streaming"
"github.com/Mirantis/cri-dockerd/utils"
"google.golang.org/grpc/codes"
v1 "k8s.io/cri-api/pkg/apis/runtime/v1alpha2"
v1 "k8s.io/cri-api/pkg/apis/runtime/v1"
"time"
)

Expand Down Expand Up @@ -79,4 +79,3 @@ func (ds *dockerService) Exec(
}
return ds.streamingServer.GetExec(req)
}

2 changes: 1 addition & 1 deletion core/container_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/filters"
"github.com/sirupsen/logrus"
v1 "k8s.io/cri-api/pkg/apis/runtime/v1alpha2"
v1 "k8s.io/cri-api/pkg/apis/runtime/v1"
)

// ListContainers lists all containers matching the filter.
Expand Down
4 changes: 2 additions & 2 deletions core/container_remove.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (
"fmt"
"github.com/Mirantis/cri-dockerd/libdocker"
"github.com/docker/docker/api/types"
v1 "k8s.io/cri-api/pkg/apis/runtime/v1alpha2"
v1 "k8s.io/cri-api/pkg/apis/runtime/v1"
"time"
)

Expand Down Expand Up @@ -92,4 +92,4 @@ func getContainerTimestamps(r *types.ContainerJSON) (time.Time, time.Time, time.
return createdAt, startedAt, finishedAt, err
}
return createdAt, startedAt, finishedAt, nil
}
}
3 changes: 1 addition & 2 deletions core/container_start.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ package core
import (
"context"
"fmt"
v1 "k8s.io/cri-api/pkg/apis/runtime/v1alpha2"
v1 "k8s.io/cri-api/pkg/apis/runtime/v1"
)

// StartContainer starts the container.
Expand Down Expand Up @@ -58,4 +58,3 @@ func transformStartContainerError(err error) error {
}
return err
}

3 changes: 1 addition & 2 deletions core/container_status.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (
"fmt"
"github.com/Mirantis/cri-dockerd/libdocker"
"github.com/sirupsen/logrus"
v1 "k8s.io/cri-api/pkg/apis/runtime/v1alpha2"
v1 "k8s.io/cri-api/pkg/apis/runtime/v1"
)

// ContainerStatus inspects the docker container and returns the status.
Expand Down Expand Up @@ -146,4 +146,3 @@ func (ds *dockerService) ContainerStatus(
}
return &res, nil
}

3 changes: 1 addition & 2 deletions core/container_stop.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ package core

import (
"context"
v1 "k8s.io/cri-api/pkg/apis/runtime/v1alpha2"
v1 "k8s.io/cri-api/pkg/apis/runtime/v1"
"time"
)

Expand All @@ -33,4 +33,3 @@ func (ds *dockerService) StopContainer(
}
return &v1.StopContainerResponse{}, nil
}

2 changes: 1 addition & 1 deletion core/container_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

runtimeapi "k8s.io/cri-api/pkg/apis/runtime/v1alpha2"
runtimeapi "k8s.io/cri-api/pkg/apis/runtime/v1"
containertest "k8s.io/kubernetes/pkg/kubelet/container/testing"
)

Expand Down
2 changes: 1 addition & 1 deletion core/container_update_resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (
"context"
"fmt"
"github.com/docker/docker/api/types/container"
v1 "k8s.io/cri-api/pkg/apis/runtime/v1alpha2"
v1 "k8s.io/cri-api/pkg/apis/runtime/v1"
)

func (ds *dockerService) UpdateContainerResources(
Expand Down
2 changes: 1 addition & 1 deletion core/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import (
dockercontainer "github.com/docker/docker/api/types/container"
dockerimagetypes "github.com/docker/docker/api/types/image"

runtimeapi "k8s.io/cri-api/pkg/apis/runtime/v1alpha2"
runtimeapi "k8s.io/cri-api/pkg/apis/runtime/v1"

digest "github.com/opencontainers/go-digest"
imagespec "github.com/opencontainers/image-spec/specs-go/v1"
Expand Down
2 changes: 1 addition & 1 deletion core/convert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (
dockertypes "github.com/docker/docker/api/types"
"github.com/stretchr/testify/assert"

runtimeapi "k8s.io/cri-api/pkg/apis/runtime/v1alpha2"
runtimeapi "k8s.io/cri-api/pkg/apis/runtime/v1"
)

func TestConvertDockerStatusToRuntimeAPIState(t *testing.T) {
Expand Down
54 changes: 44 additions & 10 deletions core/docker_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ import (
"github.com/sirupsen/logrus"

v1 "k8s.io/api/core/v1"
runtimeapi "k8s.io/cri-api/pkg/apis/runtime/v1alpha2"
runtimeapi "k8s.io/cri-api/pkg/apis/runtime/v1"
runtimeapi_alpha "k8s.io/cri-api/pkg/apis/runtime/v1alpha2"
)

const (
Expand Down Expand Up @@ -71,19 +72,20 @@ const (
defaultCgroupDriver = "cgroupfs"
)

// v1AlphaCRIService provides the interface necessary for cri.v1alpha2
type v1AlphaCRIService interface {
runtimeapi_alpha.RuntimeServiceServer
runtimeapi_alpha.ImageServiceServer
}

// CRIService includes all methods necessary for a CRI backend.
type CRIService interface {
runtimeapi.RuntimeServiceServer
runtimeapi.ImageServiceServer
Start() error
}

// DockerService is an interface that embeds the new RuntimeService and
// ImageService interfaces.
type DockerService interface {
CRIService

// For serving streaming calls.
type serviceCommon interface {
Start() error
http.Handler

// GetContainerLogs gets logs for a specific container.
Expand All @@ -108,9 +110,16 @@ type DockerService interface {
) (string, error)
}

// DockerService is an interface that embeds the new RuntimeService and
// ImageService interfaces.
type DockerService interface {
CRIService
serviceCommon
}

var internalLabelKeys = []string{containerTypeLabelKey, containerLogPathLabelKey, sandboxIDLabelKey}

// NewDockerService creates a new `DockerService` struct.
// NewDockerService creates a new `DockerService`
func NewDockerService(
clientConfig *config.ClientConfig,
podSandboxImage string,
Expand Down Expand Up @@ -273,6 +282,14 @@ type dockerService struct {
cleanupInfosLock sync.RWMutex
}

type dockerServiceAlpha struct {
ds DockerService
}

func NewDockerServiceAlpha(ds DockerService) v1AlphaCRIService {
return &dockerServiceAlpha{ds: ds}
}

// Version returns the runtime name, runtime version and runtime API version
func (ds *dockerService) Version(
_ context.Context,
Expand All @@ -286,7 +303,24 @@ func (ds *dockerService) Version(
Version: kubeAPIVersion,
RuntimeName: dockerRuntimeName,
RuntimeVersion: v.Version,
RuntimeApiVersion: v.APIVersion,
RuntimeApiVersion: config.CRIVersion,
}, nil
}

// Version returns the runtime name, runtime version and runtime API version
func (ds *dockerService) AlphaVersion(
_ context.Context,
r *runtimeapi.VersionRequest,
) (*runtimeapi_alpha.VersionResponse, error) {
v, err := ds.getDockerVersion()
if err != nil {
return nil, err
}
return &runtimeapi_alpha.VersionResponse{
Version: kubeAPIVersion,
RuntimeName: dockerRuntimeName,
RuntimeVersion: v.Version,
RuntimeApiVersion: config.CRIVersionAlpha,
}, nil
}

Expand Down
2 changes: 1 addition & 1 deletion core/docker_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"k8s.io/apimachinery/pkg/util/clock"
runtimeapi "k8s.io/cri-api/pkg/apis/runtime/v1alpha2"
runtimeapi "k8s.io/cri-api/pkg/apis/runtime/v1"
containertest "k8s.io/kubernetes/pkg/kubelet/container/testing"

"github.com/Mirantis/cri-dockerd/libdocker"
Expand Down
3 changes: 2 additions & 1 deletion core/helpers_linux.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//go:build linux
// +build linux

/*
Expand All @@ -24,7 +25,7 @@ import (
dockertypes "github.com/docker/docker/api/types"
dockercontainer "github.com/docker/docker/api/types/container"
"github.com/sirupsen/logrus"
runtimeapi "k8s.io/cri-api/pkg/apis/runtime/v1alpha2"
runtimeapi "k8s.io/cri-api/pkg/apis/runtime/v1"
)

// DefaultMemorySwap always returns 0 for no memory swap in a sandbox
Expand Down
2 changes: 1 addition & 1 deletion core/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

runtimeapi "k8s.io/cri-api/pkg/apis/runtime/v1alpha2"
runtimeapi "k8s.io/cri-api/pkg/apis/runtime/v1"
)

func TestLabelsAndAnnotationsRoundTrip(t *testing.T) {
Expand Down
3 changes: 2 additions & 1 deletion core/helpers_unsupported.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//go:build !linux && !windows
// +build !linux,!windows

/*
Expand All @@ -24,7 +25,7 @@ import (
"github.com/blang/semver"
dockertypes "github.com/docker/docker/api/types"
"github.com/sirupsen/logrus"
runtimeapi "k8s.io/cri-api/pkg/apis/runtime/v1alpha2"
runtimeapi "k8s.io/cri-api/pkg/apis/runtime/v1"
)

// DefaultMemorySwap always returns -1 for no memory swap in a sandbox
Expand Down
5 changes: 3 additions & 2 deletions core/helpers_windows.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//go:build windows
// +build windows

/*
Expand Down Expand Up @@ -28,11 +29,11 @@ import (
"runtime"

"github.com/blang/semver"
"github.com/sirupsen/logrus"
dockertypes "github.com/docker/docker/api/types"
dockercontainer "github.com/docker/docker/api/types/container"
dockerfilters "github.com/docker/docker/api/types/filters"
runtimeapi "k8s.io/cri-api/pkg/apis/runtime/v1alpha2"
"github.com/sirupsen/logrus"
runtimeapi "k8s.io/cri-api/pkg/apis/runtime/v1"
)

// DefaultMemorySwap always returns 0 for no memory swap in a sandbox
Expand Down
Loading