1
2
3
4 package invconfig
5
6 import (
7 "context"
8 "strings"
9
10 "github.com/onsi/gomega"
11 v1 "k8s.io/api/core/v1"
12 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
13 "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
14 "k8s.io/apimachinery/pkg/runtime"
15 "k8s.io/apimachinery/pkg/types"
16 "k8s.io/client-go/rest"
17 "sigs.k8s.io/cli-utils/pkg/apply"
18 "sigs.k8s.io/cli-utils/pkg/common"
19 "sigs.k8s.io/cli-utils/pkg/inventory"
20 "sigs.k8s.io/cli-utils/test/e2e/customprovider"
21 "sigs.k8s.io/cli-utils/test/e2e/e2eutil"
22 "sigs.k8s.io/controller-runtime/pkg/client"
23 )
24
25 func NewCustomTypeInvConfig(cfg *rest.Config) InventoryConfig {
26 return InventoryConfig{
27 ClientConfig: cfg,
28 Strategy: inventory.NameStrategy,
29 FactoryFunc: customInventoryManifest,
30 InvWrapperFunc: customprovider.WrapInventoryInfoObj,
31 ApplierFactoryFunc: newCustomInvApplierFactory(cfg),
32 DestroyerFactoryFunc: newCustomInvDestroyerFactory(cfg),
33 InvSizeVerifyFunc: customInvSizeVerifyFunc,
34 InvCountVerifyFunc: customInvCountVerifyFunc,
35 InvNotExistsFunc: customInvNotExistsFunc,
36 }
37 }
38
39 func newCustomInvApplierFactory(cfg *rest.Config) applierFactoryFunc {
40 cfgPtrCopy := cfg
41 return func() *apply.Applier {
42 return newApplier(customprovider.CustomClientFactory{}, cfgPtrCopy)
43 }
44 }
45
46 func newCustomInvDestroyerFactory(cfg *rest.Config) destroyerFactoryFunc {
47 cfgPtrCopy := cfg
48 return func() *apply.Destroyer {
49 return newDestroyer(customprovider.CustomClientFactory{}, cfgPtrCopy)
50 }
51 }
52
53 func customInvNotExistsFunc(ctx context.Context, c client.Client, name, namespace, id string) {
54 var u unstructured.Unstructured
55 u.SetGroupVersionKind(customprovider.InventoryGVK)
56 u.SetName(name)
57 u.SetNamespace(namespace)
58 e2eutil.AssertUnstructuredDoesNotExist(ctx, c, &u)
59 }
60
61 func customInvSizeVerifyFunc(ctx context.Context, c client.Client, name, namespace, _ string, specCount, statusCount int) {
62 var u unstructured.Unstructured
63 u.SetGroupVersionKind(customprovider.InventoryGVK)
64 err := c.Get(ctx, types.NamespacedName{
65 Name: name,
66 Namespace: namespace,
67 }, &u)
68 gomega.Expect(err).WithOffset(1).ToNot(gomega.HaveOccurred(), "getting custom inventory from cluster")
69
70 s, found, err := unstructured.NestedSlice(u.Object, "spec", "objects")
71 gomega.Expect(err).WithOffset(1).ToNot(gomega.HaveOccurred(), "reading inventory spec.objects")
72 if found {
73 gomega.Expect(len(s)).WithOffset(1).To(gomega.Equal(specCount), "inventory status.objects length")
74 } else {
75 gomega.Expect(specCount).WithOffset(1).To(gomega.Equal(0), "inventory spec.objects not found")
76 }
77
78 s, found, err = unstructured.NestedSlice(u.Object, "status", "objects")
79 gomega.Expect(err).WithOffset(1).ToNot(gomega.HaveOccurred(), "reading inventory status.objects")
80 if found {
81 gomega.Expect(len(s)).WithOffset(1).To(gomega.Equal(statusCount), "inventory status.objects length")
82 } else {
83 gomega.Expect(statusCount).WithOffset(1).To(gomega.Equal(0), "inventory status.objects not found")
84 }
85 }
86
87 func customInvCountVerifyFunc(ctx context.Context, c client.Client, namespace string, count int) {
88 var u unstructured.UnstructuredList
89 u.SetGroupVersionKind(customprovider.InventoryGVK)
90 err := c.List(ctx, &u, client.InNamespace(namespace))
91 gomega.Expect(err).NotTo(gomega.HaveOccurred())
92 gomega.Expect(len(u.Items)).To(gomega.Equal(count))
93 }
94
95 func cmInventoryManifest(name, namespace, id string) *unstructured.Unstructured {
96 cm := &v1.ConfigMap{
97 TypeMeta: metav1.TypeMeta{
98 APIVersion: v1.SchemeGroupVersion.String(),
99 Kind: "ConfigMap",
100 },
101 ObjectMeta: metav1.ObjectMeta{
102 Name: name,
103 Namespace: namespace,
104 Labels: map[string]string{
105 common.InventoryLabel: id,
106 },
107 },
108 }
109 u, err := runtime.DefaultUnstructuredConverter.ToUnstructured(cm)
110 if err != nil {
111 panic(err)
112 }
113 return &unstructured.Unstructured{
114 Object: u,
115 }
116 }
117
118 func customInventoryManifest(name, namespace, id string) *unstructured.Unstructured {
119 u := e2eutil.ManifestToUnstructured([]byte(strings.TrimSpace(`
120 apiVersion: cli-utils.example.io/v1alpha1
121 kind: Inventory
122 metadata:
123 name: PLACEHOLDER
124 `)))
125 u.SetName(name)
126 u.SetNamespace(namespace)
127 u.SetLabels(map[string]string{
128 common.InventoryLabel: id,
129 })
130 return u
131 }
132
View as plain text