...
1
2
3
4 package invconfig
5
6 import (
7 "context"
8 "fmt"
9
10 "github.com/onsi/gomega"
11 "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
12 "k8s.io/cli-runtime/pkg/genericclioptions"
13 "k8s.io/client-go/rest"
14 "k8s.io/kubectl/pkg/cmd/util"
15 "sigs.k8s.io/cli-utils/pkg/apply"
16 "sigs.k8s.io/cli-utils/pkg/inventory"
17 "sigs.k8s.io/cli-utils/test/e2e/e2eutil"
18 "sigs.k8s.io/controller-runtime/pkg/client"
19 )
20
21 type inventoryFactoryFunc func(name, namespace, id string) *unstructured.Unstructured
22 type invWrapperFunc func(*unstructured.Unstructured) inventory.Info
23 type applierFactoryFunc func() *apply.Applier
24 type destroyerFactoryFunc func() *apply.Destroyer
25 type invSizeVerifyFunc func(ctx context.Context, c client.Client, name, namespace, id string, specCount, statusCount int)
26 type invCountVerifyFunc func(ctx context.Context, c client.Client, namespace string, count int)
27 type invNotExistsFunc func(ctx context.Context, c client.Client, name, namespace, id string)
28
29 type InventoryConfig struct {
30 ClientConfig *rest.Config
31 Strategy inventory.Strategy
32 FactoryFunc inventoryFactoryFunc
33 InvWrapperFunc invWrapperFunc
34 ApplierFactoryFunc applierFactoryFunc
35 DestroyerFactoryFunc destroyerFactoryFunc
36 InvSizeVerifyFunc invSizeVerifyFunc
37 InvCountVerifyFunc invCountVerifyFunc
38 InvNotExistsFunc invNotExistsFunc
39 }
40
41 func CreateInventoryInfo(invConfig InventoryConfig, inventoryName, namespaceName, inventoryID string) inventory.Info {
42 switch invConfig.Strategy {
43 case inventory.NameStrategy:
44 return invConfig.InvWrapperFunc(invConfig.FactoryFunc(inventoryName, namespaceName, e2eutil.RandomString("inventory-")))
45 case inventory.LabelStrategy:
46 return invConfig.InvWrapperFunc(invConfig.FactoryFunc(e2eutil.RandomString("inventory-"), namespaceName, inventoryID))
47 default:
48 panic(fmt.Errorf("unknown inventory strategy %q", invConfig.Strategy))
49 }
50 }
51
52 func newFactory(cfg *rest.Config) util.Factory {
53 kubeConfigFlags := genericclioptions.NewConfigFlags(true).WithDeprecatedPasswordFlag()
54 cfgPtrCopy := cfg
55 kubeConfigFlags.WrapConfigFn = func(c *rest.Config) *rest.Config {
56
57 deepCopyRESTConfig(cfgPtrCopy, c)
58 return c
59 }
60 matchVersionKubeConfigFlags := util.NewMatchVersionFlags(kubeConfigFlags)
61 return util.NewFactory(matchVersionKubeConfigFlags)
62 }
63
64 func newApplier(invFactory inventory.ClientFactory, cfg *rest.Config) *apply.Applier {
65 f := newFactory(cfg)
66 invClient, err := invFactory.NewClient(f)
67 gomega.Expect(err).NotTo(gomega.HaveOccurred())
68
69 a, err := apply.NewApplierBuilder().
70 WithFactory(f).
71 WithInventoryClient(invClient).
72 Build()
73 gomega.Expect(err).NotTo(gomega.HaveOccurred())
74 return a
75 }
76
77 func newDestroyer(invFactory inventory.ClientFactory, cfg *rest.Config) *apply.Destroyer {
78 f := newFactory(cfg)
79 invClient, err := invFactory.NewClient(f)
80 gomega.Expect(err).NotTo(gomega.HaveOccurred())
81
82 d, err := apply.NewDestroyerBuilder().
83 WithFactory(f).
84 WithInventoryClient(invClient).
85 Build()
86 gomega.Expect(err).NotTo(gomega.HaveOccurred())
87 return d
88 }
89
90 func deepCopyRESTConfig(from, to *rest.Config) {
91 to.Host = from.Host
92 to.APIPath = from.APIPath
93 to.ContentConfig = from.ContentConfig
94 to.Username = from.Username
95 to.Password = from.Password
96 to.BearerToken = from.BearerToken
97 to.BearerTokenFile = from.BearerTokenFile
98 to.Impersonate = rest.ImpersonationConfig{
99 UserName: from.Impersonate.UserName,
100 UID: from.Impersonate.UID,
101 Groups: from.Impersonate.Groups,
102 Extra: from.Impersonate.Extra,
103 }
104 to.AuthProvider = from.AuthProvider
105 to.AuthConfigPersister = from.AuthConfigPersister
106 to.ExecProvider = from.ExecProvider
107 if from.ExecProvider != nil && from.ExecProvider.Config != nil {
108 to.ExecProvider.Config = from.ExecProvider.Config.DeepCopyObject()
109 }
110 to.TLSClientConfig = rest.TLSClientConfig{
111 Insecure: from.TLSClientConfig.Insecure,
112 ServerName: from.TLSClientConfig.ServerName,
113 CertFile: from.TLSClientConfig.CertFile,
114 KeyFile: from.TLSClientConfig.KeyFile,
115 CAFile: from.TLSClientConfig.CAFile,
116 CertData: from.TLSClientConfig.CertData,
117 KeyData: from.TLSClientConfig.KeyData,
118 CAData: from.TLSClientConfig.CAData,
119 NextProtos: from.TLSClientConfig.NextProtos,
120 }
121 to.UserAgent = from.UserAgent
122 to.DisableCompression = from.DisableCompression
123 to.Transport = from.Transport
124 to.WrapTransport = from.WrapTransport
125 to.QPS = from.QPS
126 to.Burst = from.Burst
127 to.RateLimiter = from.RateLimiter
128 to.WarningHandler = from.WarningHandler
129 to.Timeout = from.Timeout
130 to.Dial = from.Dial
131 to.Proxy = from.Proxy
132 }
133
View as plain text