1 package clientutils
2
3 import (
4 "context"
5
6 containerApi "github.com/GoogleCloudPlatform/k8s-config-connector/pkg/clients/generated/apis/container/v1beta1"
7 iamAPI "github.com/GoogleCloudPlatform/k8s-config-connector/pkg/clients/generated/apis/iam/v1beta1"
8
9 persistenceapi "edge-infra.dev/pkg/edge/apis/persistence/v1alpha1"
10 syncedobjectApi "edge-infra.dev/pkg/edge/apis/syncedobject/apis/v1alpha1"
11 dsapi "edge-infra.dev/pkg/edge/datasync/apis/v1alpha1"
12
13 corev1 "k8s.io/api/core/v1"
14 "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
15
16 "sigs.k8s.io/controller-runtime/pkg/client"
17 "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
18 )
19
20 func CreateOrUpdateContainerCluster(ctx context.Context, c client.Client, cc *containerApi.ContainerCluster) error {
21 cp := cc.DeepCopy()
22 _, err := controllerutil.CreateOrUpdate(ctx, c, cp, func() error {
23 cp.Spec = cc.Spec
24 return nil
25 })
26 return err
27 }
28
29 func CreateOrUpdateContainerNodePool(ctx context.Context, c client.Client, nodePool *containerApi.ContainerNodePool) error {
30 cp := nodePool.DeepCopy()
31 _, err := controllerutil.CreateOrUpdate(ctx, c, cp, func() error {
32 cp.Spec = nodePool.Spec
33 return nil
34 })
35 return err
36 }
37
38 func CreateOrUpdatePolicyMember(ctx context.Context, c client.Client, pm *iamAPI.IAMPolicyMember) error {
39 cp := pm.DeepCopy()
40 _, err := controllerutil.CreateOrUpdate(ctx, c, cp, func() error {
41 cp.Spec = pm.Spec
42 return nil
43 })
44 return err
45 }
46
47 func CreateOrUpdateSyncedObject(ctx context.Context, c client.Client, so *syncedobjectApi.SyncedObject) error {
48 cp := so.DeepCopy()
49 _, err := controllerutil.CreateOrUpdate(ctx, c, cp, func() error {
50 cp.Spec = so.Spec
51 return nil
52 })
53 return err
54 }
55
56 func CreateOrUpdateConfigmap(ctx context.Context, c client.Client, configMap *corev1.ConfigMap) error {
57 cfg := configMap.DeepCopy()
58 _, err := controllerutil.CreateOrUpdate(ctx, c, cfg, func() error {
59 updateNonEmptyField(cfg.Data, configMap.Data)
60 return nil
61 })
62 return err
63 }
64
65 func CreateOrUpdateSecret(ctx context.Context, c client.Client, secret *corev1.Secret) error {
66 cp := secret.DeepCopy()
67 _, err := controllerutil.CreateOrUpdate(ctx, c, cp, func() error {
68 cp.OwnerReferences = secret.OwnerReferences
69 cp.Data = secret.Data
70 cp.StringData = secret.StringData
71 return nil
72 })
73 return err
74 }
75
76 func CreateOrUpdateCouchDBUser(ctx context.Context, c client.Client, user *dsapi.CouchDBUser) error {
77 cp := user.DeepCopy()
78 _, err := controllerutil.CreateOrUpdate(ctx, c, cp, func() error {
79 cp.OwnerReferences = user.OwnerReferences
80 cp.Spec = user.Spec
81 return nil
82 })
83 return err
84 }
85
86 func CreateOrUpdateCouchDBServer(ctx context.Context, c client.Client, server *dsapi.CouchDBServer) error {
87 cp := server.DeepCopy()
88 _, err := controllerutil.CreateOrUpdate(ctx, c, cp, func() error {
89 cp.OwnerReferences = server.OwnerReferences
90 cp.Spec = server.Spec
91 cp.Annotations = server.Annotations
92 return nil
93 })
94 return err
95 }
96
97 func CreateOrUpdatePersistence(ctx context.Context, c client.Client, p *persistenceapi.Persistence) error {
98 cp := p.DeepCopy()
99 _, err := controllerutil.CreateOrUpdate(ctx, c, cp, func() error {
100 cp.OwnerReferences = p.OwnerReferences
101 cp.Spec = p.Spec
102 return nil
103 })
104 return err
105 }
106
107 func CreateOrUpdateUnstructured(ctx context.Context, c client.Client, manifest *unstructured.Unstructured) error {
108 cp := manifest.DeepCopy()
109 _, err := controllerutil.CreateOrUpdate(ctx, c, cp, func() error {
110 rv := cp.GetResourceVersion()
111 cp.Object = manifest.Object
112 cp.SetResourceVersion(rv)
113 return nil
114 })
115 return err
116 }
117
118 func CreateOrUpdateCouchDBDatabase(ctx context.Context, c client.Client, db *dsapi.CouchDBDatabase) error {
119 cp := db.DeepCopy()
120 _, err := controllerutil.CreateOrUpdate(ctx, c, cp, func() error {
121 cp.OwnerReferences = db.OwnerReferences
122 cp.Spec = db.Spec
123 return nil
124 })
125 return err
126 }
127
128 func updateNonEmptyField(old, new map[string]string) {
129 for key, value := range new {
130 if value == "" {
131 continue
132 }
133 old[key] = value
134 }
135 }
136
View as plain text