-
Notifications
You must be signed in to change notification settings - Fork 50
/
deploy_volsync.go
80 lines (66 loc) · 2.51 KB
/
deploy_volsync.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
// SPDX-FileCopyrightText: The RamenDR authors
// SPDX-License-Identifier: Apache-2.0
package volsync
import (
"context"
"fmt"
"github.com/go-logr/logr"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime/schema"
"sigs.k8s.io/controller-runtime/pkg/client"
ctrlutil "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
)
const (
ManagedClusterAddOnKind string = "ManagedClusterAddOn"
ManagedClusterAddOnGroup string = "addon.open-cluster-management.io"
ManagedClusterAddOnVersion string = "v1alpha1"
VolsyncManagedClusterAddOnName string = "volsync" // Needs to have this name
)
// Function to deploy Volsync from ACM to managed cluster via a ManagedClusterAddOn
//
// Calling this function requires a clusterrole that can create/update ManagedClusterAddOns
//
// Should be called from the Hub
func DeployVolSyncToCluster(ctx context.Context, k8sClient client.Client,
managedClusterName string, log logr.Logger,
) error {
err := reconcileVolSyncManagedClusterAddOn(ctx, k8sClient, managedClusterName,
log.WithValues("managedClusterName", managedClusterName))
if err != nil {
return err
}
return nil
}
func reconcileVolSyncManagedClusterAddOn(ctx context.Context, k8sClient client.Client,
managedClusterName string, log logr.Logger,
) error {
log.Info("Reconciling VolSync ManagedClusterAddOn")
// Using unstructured to avoid needing to require ManagedClusterAddOn in client scheme
vsMCAO := &unstructured.Unstructured{}
vsMCAO.Object = map[string]interface{}{
"metadata": map[string]interface{}{
"name": VolsyncManagedClusterAddOnName,
"namespace": managedClusterName, // Needs to be deployed to managedcluster ns on hub
},
}
vsMCAO.SetGroupVersionKind(schema.GroupVersionKind{
Group: ManagedClusterAddOnGroup,
Version: ManagedClusterAddOnVersion,
Kind: ManagedClusterAddOnKind,
})
op, err := ctrlutil.CreateOrUpdate(ctx, k8sClient, vsMCAO, func() error {
// Do not update the ManagedClusterAddOn if it already exists - let users update settings if required
creationTimeStamp := vsMCAO.GetCreationTimestamp()
if creationTimeStamp.IsZero() {
// Create with empty spec - no spec settings required
vsMCAO.Object["spec"] = map[string]interface{}{}
}
return nil
})
if err != nil {
log.Error(err, "error creating or updating VolSync ManagedClusterAddOn")
return fmt.Errorf("error creating or updating VolSync ManagedClusterAddOn (%w)", err)
}
log.Info("VolSync ManagedClusterAddOn createOrUpdate Complete", "op", op)
return nil
}