Move pkg/registrar to internal/registrar
Signed-off-by: Derek McGowan <derek@mcg.dev>
This commit is contained in:
@@ -32,6 +32,7 @@ import (
|
||||
|
||||
containerd "github.com/containerd/containerd/v2/client"
|
||||
"github.com/containerd/containerd/v2/core/sandbox"
|
||||
"github.com/containerd/containerd/v2/internal/registrar"
|
||||
criconfig "github.com/containerd/containerd/v2/pkg/cri/config"
|
||||
"github.com/containerd/containerd/v2/pkg/cri/nri"
|
||||
"github.com/containerd/containerd/v2/pkg/cri/server/podsandbox"
|
||||
@@ -43,7 +44,6 @@ import (
|
||||
ctrdutil "github.com/containerd/containerd/v2/pkg/cri/util"
|
||||
"github.com/containerd/containerd/v2/pkg/oci"
|
||||
osinterface "github.com/containerd/containerd/v2/pkg/os"
|
||||
"github.com/containerd/containerd/v2/pkg/registrar"
|
||||
)
|
||||
|
||||
// defaultNetworkPlugin is used for the default CNI configuration
|
||||
|
||||
@@ -24,13 +24,13 @@ import (
|
||||
|
||||
"github.com/containerd/containerd/v2/api/types"
|
||||
"github.com/containerd/containerd/v2/core/sandbox"
|
||||
"github.com/containerd/containerd/v2/internal/registrar"
|
||||
containerstore "github.com/containerd/containerd/v2/pkg/cri/store/container"
|
||||
"github.com/containerd/containerd/v2/pkg/cri/store/label"
|
||||
sandboxstore "github.com/containerd/containerd/v2/pkg/cri/store/sandbox"
|
||||
servertesting "github.com/containerd/containerd/v2/pkg/cri/testing"
|
||||
"github.com/containerd/containerd/v2/pkg/errdefs"
|
||||
ostesting "github.com/containerd/containerd/v2/pkg/os/testing"
|
||||
"github.com/containerd/containerd/v2/pkg/registrar"
|
||||
"github.com/containerd/containerd/v2/platforms"
|
||||
)
|
||||
|
||||
|
||||
@@ -1,101 +0,0 @@
|
||||
/*
|
||||
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 registrar
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"sync"
|
||||
)
|
||||
|
||||
// Registrar stores one-to-one name<->key mappings.
|
||||
// Names and keys must be unique.
|
||||
// Registrar is safe for concurrent access.
|
||||
type Registrar struct {
|
||||
lock sync.Mutex
|
||||
nameToKey map[string]string
|
||||
keyToName map[string]string
|
||||
}
|
||||
|
||||
// NewRegistrar creates a new Registrar with the empty indexes.
|
||||
func NewRegistrar() *Registrar {
|
||||
return &Registrar{
|
||||
nameToKey: make(map[string]string),
|
||||
keyToName: make(map[string]string),
|
||||
}
|
||||
}
|
||||
|
||||
// Reserve registers a name<->key mapping, name or key must not
|
||||
// be empty.
|
||||
// Reserve is idempotent.
|
||||
// Attempting to reserve a conflict key<->name mapping results
|
||||
// in an error.
|
||||
// A name<->key reservation is globally unique.
|
||||
func (r *Registrar) Reserve(name, key string) error {
|
||||
r.lock.Lock()
|
||||
defer r.lock.Unlock()
|
||||
|
||||
if name == "" || key == "" {
|
||||
return fmt.Errorf("invalid name %q or key %q", name, key)
|
||||
}
|
||||
|
||||
if k, exists := r.nameToKey[name]; exists {
|
||||
if k != key {
|
||||
return fmt.Errorf("name %q is reserved for %q", name, k)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
if n, exists := r.keyToName[key]; exists {
|
||||
if n != name {
|
||||
return fmt.Errorf("key %q is reserved for %q", key, n)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
r.nameToKey[name] = key
|
||||
r.keyToName[key] = name
|
||||
return nil
|
||||
}
|
||||
|
||||
// ReleaseByName releases the reserved name<->key mapping by name.
|
||||
// Once released, the name and the key can be reserved again.
|
||||
func (r *Registrar) ReleaseByName(name string) {
|
||||
r.lock.Lock()
|
||||
defer r.lock.Unlock()
|
||||
|
||||
key, exists := r.nameToKey[name]
|
||||
if !exists {
|
||||
return
|
||||
}
|
||||
|
||||
delete(r.nameToKey, name)
|
||||
delete(r.keyToName, key)
|
||||
}
|
||||
|
||||
// ReleaseByKey release the reserved name<->key mapping by key.
|
||||
func (r *Registrar) ReleaseByKey(key string) {
|
||||
r.lock.Lock()
|
||||
defer r.lock.Unlock()
|
||||
|
||||
name, exists := r.keyToName[key]
|
||||
if !exists {
|
||||
return
|
||||
}
|
||||
|
||||
delete(r.nameToKey, name)
|
||||
delete(r.keyToName, key)
|
||||
}
|
||||
@@ -1,54 +0,0 @@
|
||||
/*
|
||||
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 registrar
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
assertlib "github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestRegistrar(t *testing.T) {
|
||||
r := NewRegistrar()
|
||||
assert := assertlib.New(t)
|
||||
|
||||
t.Logf("should be able to reserve a name<->key mapping")
|
||||
assert.NoError(r.Reserve("test-name-1", "test-id-1"))
|
||||
|
||||
t.Logf("should be able to reserve a new name<->key mapping")
|
||||
assert.NoError(r.Reserve("test-name-2", "test-id-2"))
|
||||
|
||||
t.Logf("should be able to reserve the same name<->key mapping")
|
||||
assert.NoError(r.Reserve("test-name-1", "test-id-1"))
|
||||
|
||||
t.Logf("should not be able to reserve conflict name<->key mapping")
|
||||
assert.Error(r.Reserve("test-name-1", "test-id-conflict"))
|
||||
assert.Error(r.Reserve("test-name-conflict", "test-id-2"))
|
||||
|
||||
t.Logf("should be able to release name<->key mapping by key")
|
||||
r.ReleaseByKey("test-id-1")
|
||||
|
||||
t.Logf("should be able to release name<->key mapping by name")
|
||||
r.ReleaseByName("test-name-2")
|
||||
|
||||
t.Logf("should be able to reserve new name<->key mapping after release")
|
||||
assert.NoError(r.Reserve("test-name-1", "test-id-new"))
|
||||
assert.NoError(r.Reserve("test-name-new", "test-id-2"))
|
||||
|
||||
t.Logf("should be able to reserve same name/key name<->key")
|
||||
assert.NoError(r.Reserve("same-name-id", "same-name-id"))
|
||||
}
|
||||
Reference in New Issue
Block a user