Refactor pkg packages

Signed-off-by: Maksym Pavlenko <pavlenko.maksym@gmail.com>
This commit is contained in:
Maksym Pavlenko
2020-10-08 17:30:17 -07:00
parent cb9572ee57
commit 3d02441a79
43 changed files with 35 additions and 35 deletions

42
pkg/cri/util/deep_copy.go Normal file
View File

@@ -0,0 +1,42 @@
/*
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 util
import (
"encoding/json"
"github.com/pkg/errors"
)
// DeepCopy makes a deep copy from src into dst.
func DeepCopy(dst interface{}, src interface{}) error {
if dst == nil {
return errors.New("dst cannot be nil")
}
if src == nil {
return errors.New("src cannot be nil")
}
bytes, err := json.Marshal(src)
if err != nil {
return errors.Wrap(err, "unable to marshal src")
}
err = json.Unmarshal(bytes, dst)
if err != nil {
return errors.Wrap(err, "unable to unmarshal into dst")
}
return nil
}

View File

@@ -0,0 +1,63 @@
/*
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 util
import (
"testing"
"github.com/stretchr/testify/assert"
)
type A struct {
String string
Int int
Strings []string
Ints map[string]int
As map[string]*A
}
func TestCopy(t *testing.T) {
src := &A{
String: "Hello World",
Int: 5,
Strings: []string{"A", "B"},
Ints: map[string]int{"A": 1, "B": 2, "C": 4},
As: map[string]*A{
"One": {String: "2"},
"Two": {String: "3"},
},
}
dst := &A{
Strings: []string{"C"},
Ints: map[string]int{"B": 3, "C": 4},
As: map[string]*A{"One": {String: "1", Int: 5}},
}
expected := &A{
String: "Hello World",
Int: 5,
Strings: []string{"A", "B"},
Ints: map[string]int{"A": 1, "B": 2, "C": 4},
As: map[string]*A{
"One": {String: "2"},
"Two": {String: "3"},
},
}
assert.NotEqual(t, expected, dst)
err := DeepCopy(dst, src)
assert.NoError(t, err)
assert.Equal(t, expected, dst)
}

29
pkg/cri/util/id.go Normal file
View File

@@ -0,0 +1,29 @@
/*
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 util
import (
"encoding/hex"
"math/rand"
)
// GenerateID generates a random unique id.
func GenerateID() string {
b := make([]byte, 32)
rand.Read(b)
return hex.EncodeToString(b)
}

33
pkg/cri/util/image.go Normal file
View File

@@ -0,0 +1,33 @@
/*
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 util
import (
"github.com/containerd/containerd/reference/docker"
)
// NormalizeImageRef normalizes the image reference following the docker convention. This is added
// mainly for backward compatibility.
// The reference returned can only be either tagged or digested. For reference contains both tag
// and digest, the function returns digested reference, e.g. docker.io/library/busybox:latest@
// sha256:7cc4b5aefd1d0cadf8d97d4350462ba51c694ebca145b08d7d41b41acc8db5aa will be returned as
// docker.io/library/busybox@sha256:7cc4b5aefd1d0cadf8d97d4350462ba51c694ebca145b08d7d41b41acc8db5aa.
//
// Deprecated: use github.com/containerd/containerd/reference/docker.ParseDockerRef() instead
func NormalizeImageRef(ref string) (docker.Named, error) {
return docker.ParseDockerRef(ref)
}

View File

@@ -0,0 +1,84 @@
/*
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 util
import (
"testing"
"github.com/containerd/containerd/reference"
"github.com/stretchr/testify/assert"
)
func TestNormalizeImageRef(t *testing.T) {
for _, test := range []struct {
input string
expect string
}{
{ // has nothing
input: "busybox",
expect: "docker.io/library/busybox:latest",
},
{ // only has tag
input: "busybox:latest",
expect: "docker.io/library/busybox:latest",
},
{ // only has digest
input: "busybox@sha256:e6693c20186f837fc393390135d8a598a96a833917917789d63766cab6c59582",
expect: "docker.io/library/busybox@sha256:e6693c20186f837fc393390135d8a598a96a833917917789d63766cab6c59582",
},
{ // only has path
input: "library/busybox",
expect: "docker.io/library/busybox:latest",
},
{ // only has hostname
input: "docker.io/busybox",
expect: "docker.io/library/busybox:latest",
},
{ // has no tag
input: "docker.io/library/busybox",
expect: "docker.io/library/busybox:latest",
},
{ // has no path
input: "docker.io/busybox:latest",
expect: "docker.io/library/busybox:latest",
},
{ // has no hostname
input: "library/busybox:latest",
expect: "docker.io/library/busybox:latest",
},
{ // full reference
input: "docker.io/library/busybox:latest",
expect: "docker.io/library/busybox:latest",
},
{ // gcr reference
input: "gcr.io/library/busybox",
expect: "gcr.io/library/busybox:latest",
},
{ // both tag and digest
input: "gcr.io/library/busybox:latest@sha256:e6693c20186f837fc393390135d8a598a96a833917917789d63766cab6c59582",
expect: "gcr.io/library/busybox@sha256:e6693c20186f837fc393390135d8a598a96a833917917789d63766cab6c59582",
},
} {
t.Logf("TestCase %q", test.input)
normalized, err := NormalizeImageRef(test.input)
assert.NoError(t, err)
output := normalized.String()
assert.Equal(t, test.expect, output)
_, err = reference.Parse(output)
assert.NoError(t, err, "%q should be containerd supported reference", output)
}
}

59
pkg/cri/util/strings.go Normal file
View File

@@ -0,0 +1,59 @@
/*
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 util
import "strings"
// InStringSlice checks whether a string is inside a string slice.
// Comparison is case insensitive.
func InStringSlice(ss []string, str string) bool {
for _, s := range ss {
if strings.EqualFold(s, str) {
return true
}
}
return false
}
// SubtractStringSlice subtracts string from string slice.
// Comparison is case insensitive.
func SubtractStringSlice(ss []string, str string) []string {
var res []string
for _, s := range ss {
if strings.EqualFold(s, str) {
continue
}
res = append(res, s)
}
return res
}
// MergeStringSlices merges 2 string slices into one and remove duplicated elements.
func MergeStringSlices(a []string, b []string) []string {
set := map[string]struct{}{}
for _, s := range a {
set[s] = struct{}{}
}
for _, s := range b {
set[s] = struct{}{}
}
var ss []string
for s := range set {
ss = append(ss, s)
}
return ss
}

View File

@@ -0,0 +1,59 @@
/*
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 util
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestInStringSlice(t *testing.T) {
ss := []string{"ABC", "def", "ghi"}
assert.True(t, InStringSlice(ss, "ABC"))
assert.True(t, InStringSlice(ss, "abc"))
assert.True(t, InStringSlice(ss, "def"))
assert.True(t, InStringSlice(ss, "DEF"))
assert.False(t, InStringSlice(ss, "hij"))
assert.False(t, InStringSlice(ss, "HIJ"))
assert.False(t, InStringSlice(nil, "HIJ"))
}
func TestSubtractStringSlice(t *testing.T) {
ss := []string{"ABC", "def", "ghi"}
assert.Equal(t, []string{"def", "ghi"}, SubtractStringSlice(ss, "abc"))
assert.Equal(t, []string{"def", "ghi"}, SubtractStringSlice(ss, "ABC"))
assert.Equal(t, []string{"ABC", "ghi"}, SubtractStringSlice(ss, "def"))
assert.Equal(t, []string{"ABC", "ghi"}, SubtractStringSlice(ss, "DEF"))
assert.Equal(t, []string{"ABC", "def", "ghi"}, SubtractStringSlice(ss, "hij"))
assert.Equal(t, []string{"ABC", "def", "ghi"}, SubtractStringSlice(ss, "HIJ"))
assert.Empty(t, SubtractStringSlice(nil, "hij"))
assert.Empty(t, SubtractStringSlice([]string{}, "hij"))
}
func TestMergeStringSlices(t *testing.T) {
s1 := []string{"abc", "def", "ghi"}
s2 := []string{"def", "jkl", "mno"}
expect := []string{"abc", "def", "ghi", "jkl", "mno"}
result := MergeStringSlices(s1, s2)
assert.Len(t, result, len(expect))
for _, s := range expect {
assert.Contains(t, result, s)
}
}

46
pkg/cri/util/util.go Normal file
View File

@@ -0,0 +1,46 @@
/*
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 util
import (
"time"
"github.com/containerd/containerd/namespaces"
"golang.org/x/net/context"
"github.com/containerd/containerd/pkg/cri/constants"
)
// deferCleanupTimeout is the default timeout for containerd cleanup operations
// in defer.
const deferCleanupTimeout = 1 * time.Minute
// DeferContext returns a context for containerd cleanup operations in defer.
// A default timeout is applied to avoid cleanup operation pending forever.
func DeferContext() (context.Context, context.CancelFunc) {
return context.WithTimeout(NamespacedContext(), deferCleanupTimeout)
}
// NamespacedContext returns a context with kubernetes namespace set.
func NamespacedContext() context.Context {
return WithNamespace(context.Background())
}
// WithNamespace adds kubernetes namespace to the context.
func WithNamespace(ctx context.Context) context.Context {
return namespaces.WithNamespace(ctx, constants.K8sContainerdNamespace)
}