Merge pull request #698 from liubin/feature/146-add-cgroup-v2-for-agent

agent: add cgroup v2 support
This commit is contained in:
Peng Tao
2020-09-18 14:45:38 +08:00
committed by GitHub
17 changed files with 1513 additions and 1338 deletions

View File

@@ -7,6 +7,7 @@ package containerdshim
import (
"context"
"os"
"path"
"time"
@@ -15,6 +16,8 @@ import (
"github.com/containerd/containerd/mount"
"github.com/sirupsen/logrus"
"google.golang.org/grpc/codes"
"github.com/kata-containers/kata-containers/src/runtime/virtcontainers/pkg/oci"
)
func wait(s *service, c *container, execID string) (int32, error) {
@@ -146,12 +149,26 @@ func watchOOMEvents(ctx context.Context, s *service) {
// If the GetOOMEvent call is not implemented, then the agent is most likely an older version,
// stop attempting to get OOM events.
// for rust agent, the response code is not found
if isGRPCErrorCode(codes.NotFound, err) {
if isGRPCErrorCode(codes.NotFound, err) || err.Error() == "Dead agent" {
return
}
continue
}
// write oom file for CRI-O
if c, ok := s.containers[containerID]; ok && oci.IsCRIOContainerManager(c.spec) {
oomPath := path.Join(c.bundle, "oom")
shimLog.Infof("write oom file to notify CRI-O: %s", oomPath)
f, err := os.OpenFile(oomPath, os.O_CREATE, 0666)
if err != nil {
shimLog.WithError(err).Warnf("failed to write oom file %s", oomPath)
} else {
f.Close()
}
}
// publish event for containerd
s.send(&events.TaskOOM{
ContainerID: containerID,
})

View File

@@ -1036,3 +1036,13 @@ func GetOCIConfig(status vc.ContainerStatus) (specs.Spec, error) {
return *status.Spec, nil
}
// IsCRIOContainerManager check if a Pod is created from CRI-O
func IsCRIOContainerManager(spec *specs.Spec) bool {
if val, ok := spec.Annotations[crioAnnotations.ContainerType]; ok {
if val == crioAnnotations.ContainerTypeSandbox || val == crioAnnotations.ContainerTypeContainer {
return true
}
}
return false
}

View File

@@ -17,6 +17,7 @@ import (
"testing"
"github.com/cri-o/cri-o/pkg/annotations"
crioAnnotations "github.com/cri-o/cri-o/pkg/annotations"
specs "github.com/opencontainers/runtime-spec/specs-go"
"github.com/stretchr/testify/assert"
"golang.org/x/sys/unix"
@@ -866,3 +867,34 @@ func TestAddRuntimeAnnotations(t *testing.T) {
assert.Equal(config.NetworkConfig.DisableNewNetNs, true)
assert.Equal(config.NetworkConfig.InterworkingModel, vc.NetXConnectMacVtapModel)
}
func TestIsCRIOContainerManager(t *testing.T) {
assert := assert.New(t)
testCases := []struct {
annotations map[string]string
result bool
}{
{
annotations: map[string]string{crioAnnotations.ContainerType: "abc"},
result: false,
},
{
annotations: map[string]string{crioAnnotations.ContainerType: crioAnnotations.ContainerTypeSandbox},
result: true,
},
{
annotations: map[string]string{crioAnnotations.ContainerType: crioAnnotations.ContainerTypeContainer},
result: true,
},
}
for i := range testCases {
tc := testCases[i]
ocispec := specs.Spec{
Annotations: tc.annotations,
}
result := IsCRIOContainerManager(&ocispec)
assert.Equal(tc.result, result, "test case %d", (i + 1))
}
}

View File

@@ -1203,10 +1203,10 @@ func (s *Sandbox) CreateContainer(contConfig ContainerConfig) (VCContainer, erro
// Rollback if error happens.
if err != nil {
logger := s.Logger().WithFields(logrus.Fields{"container-id": c.id, "sandox-id": s.id, "rollback": true})
logger.Warning("Cleaning up partially created container")
logger.WithError(err).Error("Cleaning up partially created container")
if err2 := c.stop(true); err2 != nil {
logger.WithError(err2).Warning("Could not delete container")
logger.WithError(err2).Error("Could not delete container")
}
logger.Debug("Removing stopped container from sandbox store")
@@ -1894,9 +1894,11 @@ func (s *Sandbox) updateResources() error {
return err
}
s.Logger().Debugf("Request to hypervisor to update oldCPUs/newCPUs: %d/%d", oldCPUs, newCPUs)
// If the CPUs were increased, ask agent to online them
if oldCPUs < newCPUs {
vcpusAdded := newCPUs - oldCPUs
s.Logger().Debugf("Request to onlineCPUMem with %d CPUs", vcpusAdded)
if err := s.agent.onlineCPUMem(vcpusAdded, true); err != nil {
return err
}