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

Runtime cleanup (Shim manager and task service) #7280

Merged
merged 2 commits into from
Aug 13, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
142 changes: 142 additions & 0 deletions runtime/nsmap.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
/*
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 runtime

import (
"context"
"fmt"
"sync"

"github.com/containerd/containerd/errdefs"
"github.com/containerd/containerd/namespaces"
)

type object interface {
ID() string
}

// NSMap extends Map type with a notion of namespaces passed via Context.
type NSMap[T object] struct {
mu sync.Mutex
objects map[string]map[string]T
}

// NewNSMap returns a new NSMap
func NewNSMap[T object]() *NSMap[T] {
return &NSMap[T]{
objects: make(map[string]map[string]T),
}
}

// Get a task
func (m *NSMap[T]) Get(ctx context.Context, id string) (T, error) {
m.mu.Lock()
defer m.mu.Unlock()
namespace, err := namespaces.NamespaceRequired(ctx)
var t T
if err != nil {
return t, err
}
tasks, ok := m.objects[namespace]
if !ok {
return t, errdefs.ErrNotFound
}
t, ok = tasks[id]
if !ok {
return t, errdefs.ErrNotFound
}
return t, nil
}

// GetAll objects under a namespace
func (m *NSMap[T]) GetAll(ctx context.Context, noNS bool) ([]T, error) {
m.mu.Lock()
defer m.mu.Unlock()
var o []T
if noNS {
for ns := range m.objects {
for _, t := range m.objects[ns] {
o = append(o, t)
}
}
return o, nil
}
namespace, err := namespaces.NamespaceRequired(ctx)
if err != nil {
return nil, err
}
tasks, ok := m.objects[namespace]
if !ok {
return o, nil
}
for _, t := range tasks {
o = append(o, t)
}
return o, nil
}

// Add a task
func (m *NSMap[T]) Add(ctx context.Context, t T) error {
namespace, err := namespaces.NamespaceRequired(ctx)
if err != nil {
return err
}
return m.AddWithNamespace(namespace, t)
}

// AddWithNamespace adds a task with the provided namespace
func (m *NSMap[T]) AddWithNamespace(namespace string, t T) error {
m.mu.Lock()
defer m.mu.Unlock()

id := t.ID()
if _, ok := m.objects[namespace]; !ok {
m.objects[namespace] = make(map[string]T)
}
if _, ok := m.objects[namespace][id]; ok {
return fmt.Errorf("%s: %w", id, errdefs.ErrAlreadyExists)
}
m.objects[namespace][id] = t
return nil
}

// Delete a task
func (m *NSMap[T]) Delete(ctx context.Context, id string) {
m.mu.Lock()
defer m.mu.Unlock()
namespace, err := namespaces.NamespaceRequired(ctx)
if err != nil {
return
}
tasks, ok := m.objects[namespace]
if ok {
delete(tasks, id)
}
}

func (m *NSMap[T]) IsEmpty() bool {
m.mu.Lock()
defer m.mu.Unlock()

for ns := range m.objects {
if len(m.objects[ns]) > 0 {
return false
}
}

return true
}
144 changes: 0 additions & 144 deletions runtime/task_list.go

This file was deleted.

4 changes: 2 additions & 2 deletions runtime/v1/linux/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ func New(ic *plugin.InitContext) (interface{}, error) {
r := &Runtime{
root: ic.Root,
state: ic.State,
tasks: runtime.NewTaskList(),
tasks: runtime.NewNSMap[runtime.Task](),
containers: metadata.NewContainerStore(m.(*metadata.DB)),
address: ic.Address,
events: ep.(*exchange.Exchange),
Expand All @@ -148,7 +148,7 @@ type Runtime struct {
state string
address string

tasks *runtime.TaskList
tasks *runtime.NSMap[runtime.Task]
containers containers.Store
events *exchange.Exchange

Expand Down
4 changes: 2 additions & 2 deletions runtime/v1/linux/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,11 @@ type Task struct {
namespace string
cg cgroups.Cgroup
events *exchange.Exchange
tasks *runtime.TaskList
tasks *runtime.NSMap[runtime.Task]
bundle *bundle
}

func newTask(id, namespace string, pid int, shim *client.Client, events *exchange.Exchange, list *runtime.TaskList, bundle *bundle) (*Task, error) {
func newTask(id, namespace string, pid int, shim *client.Client, events *exchange.Exchange, list *runtime.NSMap[runtime.Task], bundle *bundle) (*Task, error) {
var (
err error
cg cgroups.Cgroup
Expand Down