...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package main
16
17 import (
18 "context"
19 "fmt"
20 "log"
21 "time"
22
23 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
24 "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
25 "k8s.io/apimachinery/pkg/runtime/schema"
26 "k8s.io/apimachinery/pkg/util/wait"
27 "k8s.io/client-go/dynamic"
28 "k8s.io/client-go/util/retry"
29 ctrl "sigs.k8s.io/controller-runtime"
30 "sigs.k8s.io/yaml"
31 )
32
33 const defaultConfigConnector = `
34 apiVersion: core.cnrm.cloud.google.com/v1beta1
35 kind: ConfigConnector
36 metadata:
37 name: configconnector.core.cnrm.cloud.google.com
38 spec:
39 mode: namespaced
40 `
41
42 var configConnectorResource = schema.GroupVersionResource{
43 Group: "core.cnrm.cloud.google.com",
44 Version: "v1beta1",
45 Resource: "configconnectors",
46 }
47
48
49
50
51 func main() {
52 ctx := context.Background()
53 dynamicClient, err := dynamic.NewForConfig(ctrl.GetConfigOrDie())
54 if err != nil {
55 log.Fatalf("error creating dynamic client: %v", err)
56 }
57 if err := createDefaultConfigConnector(ctx, dynamicClient); err != nil {
58 log.Fatalf("error creating default ConfigConnector object: %v", err)
59 }
60 }
61
62
63
64
65 func createDefaultConfigConnector(ctx context.Context, dynamicClient dynamic.Interface) error {
66 u := &unstructured.Unstructured{}
67 b := []byte(defaultConfigConnector)
68 if err := yaml.Unmarshal(b, u); err != nil {
69 return fmt.Errorf("error unmarshalling bytes to unstruct: %v", err)
70 }
71
72
73
74 backoff := wait.Backoff{
75
76 Steps: 10,
77 Duration: 1 * time.Second,
78 Factor: 1.0,
79 }
80 retriable := func(_ error) bool {
81
82 return true
83 }
84 return retry.OnError(backoff, retriable, func() error {
85
86 _, err := dynamicClient.Resource(configConnectorResource).Get(ctx, u.GetName(), metav1.GetOptions{})
87 if err == nil {
88 return nil
89 }
90
91 _, err = dynamicClient.Resource(configConnectorResource).Create(ctx, u, metav1.CreateOptions{})
92 return err
93 })
94 }
95
View as plain text