forked from goharbor/harbor-operator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
50 lines (39 loc) · 1.36 KB
/
utils.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package test
import (
"context"
"github.com/plotly/harbor-operator/pkg/version"
"github.com/onsi/gomega"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/types"
)
func AddVersionAnnotations(annotations map[string]string) map[string]string {
return version.SetVersion(annotations, GetVersion())
}
func GetVersion() string {
return "2.6.0"
}
func GetGVK(ctx context.Context, object runtime.Object) schema.GroupVersionKind {
gvks, _, err := GetScheme(ctx).ObjectKinds(object)
gomega.Expect(err).ToNot(gomega.HaveOccurred())
return gvks[0]
}
func GetNamespacedName(object runtime.Object) types.NamespacedName {
data, err := runtime.DefaultUnstructuredConverter.ToUnstructured(object)
gomega.Expect(err).ToNot(gomega.HaveOccurred())
name, ok, err := unstructured.NestedString(data, "metadata", "name")
gomega.Expect(err).ToNot(gomega.HaveOccurred())
gomega.Expect(ok).To(gomega.BeTrue())
namespace, _, err := unstructured.NestedString(data, "metadata", "namespace")
gomega.Expect(err).ToNot(gomega.HaveOccurred())
return types.NamespacedName{
Name: name,
Namespace: namespace,
}
}
var SuccessOrExists = gomega.SatisfyAny(
gomega.Succeed(),
gomega.WithTransform(apierrors.IsAlreadyExists, gomega.BeTrue()),
)