...
1
2
3
4 package apply
5
6 import (
7 "errors"
8 "fmt"
9
10 "k8s.io/apimachinery/pkg/api/meta"
11 "k8s.io/cli-runtime/pkg/resource"
12 "k8s.io/client-go/discovery"
13 "k8s.io/client-go/dynamic"
14 "k8s.io/client-go/rest"
15 "k8s.io/kubectl/pkg/cmd/util"
16 "sigs.k8s.io/cli-utils/pkg/inventory"
17 "sigs.k8s.io/cli-utils/pkg/kstatus/watcher"
18 )
19
20 type commonBuilder struct {
21
22 factory util.Factory
23 invClient inventory.Client
24 client dynamic.Interface
25 discoClient discovery.CachedDiscoveryInterface
26 mapper meta.RESTMapper
27 restConfig *rest.Config
28 unstructuredClientForMapping func(*meta.RESTMapping) (resource.RESTClient, error)
29 statusWatcher watcher.StatusWatcher
30 }
31
32 func (cb *commonBuilder) finalize() (*commonBuilder, error) {
33 cx := *cb
34 var err error
35 if cx.invClient == nil {
36 return nil, errors.New("inventory client must be provided")
37 }
38 if cx.client == nil {
39 if cx.factory == nil {
40 return nil, fmt.Errorf("a factory must be provided or all other options: %v", err)
41 }
42 cx.client, err = cx.factory.DynamicClient()
43 if err != nil {
44 return nil, fmt.Errorf("error getting dynamic client: %v", err)
45 }
46 }
47 if cx.discoClient == nil {
48 if cx.factory == nil {
49 return nil, fmt.Errorf("a factory must be provided or all other options: %v", err)
50 }
51 cx.discoClient, err = cx.factory.ToDiscoveryClient()
52 if err != nil {
53 return nil, fmt.Errorf("error getting discovery client: %v", err)
54 }
55 }
56 if cx.mapper == nil {
57 if cx.factory == nil {
58 return nil, fmt.Errorf("a factory must be provided or all other options: %v", err)
59 }
60 cx.mapper, err = cx.factory.ToRESTMapper()
61 if err != nil {
62 return nil, fmt.Errorf("error getting rest mapper: %v", err)
63 }
64 }
65 if cx.restConfig == nil {
66 if cx.factory == nil {
67 return nil, fmt.Errorf("a factory must be provided or all other options: %v", err)
68 }
69 cx.restConfig, err = cx.factory.ToRESTConfig()
70 if err != nil {
71 return nil, fmt.Errorf("error getting rest config: %v", err)
72 }
73 }
74 if cx.unstructuredClientForMapping == nil {
75 if cx.factory == nil {
76 return nil, fmt.Errorf("a factory must be provided or all other options: %v", err)
77 }
78 cx.unstructuredClientForMapping = cx.factory.UnstructuredClientForMapping
79 }
80 if cx.statusWatcher == nil {
81 cx.statusWatcher = watcher.NewDefaultStatusWatcher(cx.client, cx.mapper)
82 }
83 return &cx, nil
84 }
85
View as plain text