package clientutils import ( "context" containerApi "github.com/GoogleCloudPlatform/k8s-config-connector/pkg/clients/generated/apis/container/v1beta1" iamAPI "github.com/GoogleCloudPlatform/k8s-config-connector/pkg/clients/generated/apis/iam/v1beta1" persistenceapi "edge-infra.dev/pkg/edge/apis/persistence/v1alpha1" syncedobjectApi "edge-infra.dev/pkg/edge/apis/syncedobject/apis/v1alpha1" dsapi "edge-infra.dev/pkg/edge/datasync/apis/v1alpha1" corev1 "k8s.io/api/core/v1" "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" "sigs.k8s.io/controller-runtime/pkg/client" "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil" ) func CreateOrUpdateContainerCluster(ctx context.Context, c client.Client, cc *containerApi.ContainerCluster) error { cp := cc.DeepCopy() _, err := controllerutil.CreateOrUpdate(ctx, c, cp, func() error { cp.Spec = cc.Spec return nil }) return err } func CreateOrUpdateContainerNodePool(ctx context.Context, c client.Client, nodePool *containerApi.ContainerNodePool) error { cp := nodePool.DeepCopy() _, err := controllerutil.CreateOrUpdate(ctx, c, cp, func() error { cp.Spec = nodePool.Spec return nil }) return err } func CreateOrUpdatePolicyMember(ctx context.Context, c client.Client, pm *iamAPI.IAMPolicyMember) error { cp := pm.DeepCopy() _, err := controllerutil.CreateOrUpdate(ctx, c, cp, func() error { cp.Spec = pm.Spec return nil }) return err } func CreateOrUpdateSyncedObject(ctx context.Context, c client.Client, so *syncedobjectApi.SyncedObject) error { cp := so.DeepCopy() _, err := controllerutil.CreateOrUpdate(ctx, c, cp, func() error { cp.Spec = so.Spec return nil }) return err } func CreateOrUpdateConfigmap(ctx context.Context, c client.Client, configMap *corev1.ConfigMap) error { cfg := configMap.DeepCopy() _, err := controllerutil.CreateOrUpdate(ctx, c, cfg, func() error { updateNonEmptyField(cfg.Data, configMap.Data) return nil }) return err } func CreateOrUpdateSecret(ctx context.Context, c client.Client, secret *corev1.Secret) error { cp := secret.DeepCopy() _, err := controllerutil.CreateOrUpdate(ctx, c, cp, func() error { cp.OwnerReferences = secret.OwnerReferences cp.Data = secret.Data cp.StringData = secret.StringData return nil }) return err } func CreateOrUpdateCouchDBUser(ctx context.Context, c client.Client, user *dsapi.CouchDBUser) error { cp := user.DeepCopy() _, err := controllerutil.CreateOrUpdate(ctx, c, cp, func() error { cp.OwnerReferences = user.OwnerReferences cp.Spec = user.Spec return nil }) return err } func CreateOrUpdateCouchDBServer(ctx context.Context, c client.Client, server *dsapi.CouchDBServer) error { cp := server.DeepCopy() _, err := controllerutil.CreateOrUpdate(ctx, c, cp, func() error { cp.OwnerReferences = server.OwnerReferences cp.Spec = server.Spec cp.Annotations = server.Annotations return nil }) return err } func CreateOrUpdatePersistence(ctx context.Context, c client.Client, p *persistenceapi.Persistence) error { cp := p.DeepCopy() _, err := controllerutil.CreateOrUpdate(ctx, c, cp, func() error { cp.OwnerReferences = p.OwnerReferences cp.Spec = p.Spec return nil }) return err } func CreateOrUpdateUnstructured(ctx context.Context, c client.Client, manifest *unstructured.Unstructured) error { cp := manifest.DeepCopy() _, err := controllerutil.CreateOrUpdate(ctx, c, cp, func() error { rv := cp.GetResourceVersion() cp.Object = manifest.Object cp.SetResourceVersion(rv) return nil }) return err } func CreateOrUpdateCouchDBDatabase(ctx context.Context, c client.Client, db *dsapi.CouchDBDatabase) error { cp := db.DeepCopy() _, err := controllerutil.CreateOrUpdate(ctx, c, cp, func() error { cp.OwnerReferences = db.OwnerReferences cp.Spec = db.Spec return nil }) return err } func updateNonEmptyField(old, new map[string]string) { for key, value := range new { if value == "" { continue } old[key] = value } }