|
| 1 | +/* |
| 2 | +Copyright © 2021 The VolSync authors |
| 3 | +
|
| 4 | +This program is free software: you can redistribute it and/or modify |
| 5 | +it under the terms of the GNU Affero General Public License as published by |
| 6 | +the Free Software Foundation, either version 3 of the License, or |
| 7 | +(at your option) any later version. |
| 8 | +
|
| 9 | +This program is distributed in the hope that it will be useful, |
| 10 | +but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | +GNU Affero General Public License for more details. |
| 13 | +
|
| 14 | +You should have received a copy of the GNU Affero General Public License |
| 15 | +along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 16 | +*/ |
| 17 | +package cmd |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "fmt" |
| 22 | + |
| 23 | + "github.com/spf13/cobra" |
| 24 | + corev1 "k8s.io/api/core/v1" |
| 25 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 26 | + errorsutil "k8s.io/apimachinery/pkg/util/errors" |
| 27 | + "k8s.io/klog/v2" |
| 28 | + "k8s.io/kubectl/pkg/util/i18n" |
| 29 | + "k8s.io/kubectl/pkg/util/templates" |
| 30 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 31 | + |
| 32 | + volsyncv1alpha1 "github.com/backube/volsync/api/v1alpha1" |
| 33 | +) |
| 34 | + |
| 35 | +const ReplicationRelationshipType RelationshipType = "replication" |
| 36 | + |
| 37 | +// replicationRelationship holds the config state for replication-type |
| 38 | +// relationships |
| 39 | +type replicationRelationship struct { |
| 40 | + Relationship |
| 41 | + data replicationRelationshipData |
| 42 | +} |
| 43 | + |
| 44 | +// replicationRelationshipData is the state that will be saved to the |
| 45 | +// relationship config file |
| 46 | +type replicationRelationshipData struct { |
| 47 | + // Config file/struct version used so we know how to decode when parsing |
| 48 | + // from disk |
| 49 | + Version int |
| 50 | + // Config info for the source side of the relationship |
| 51 | + Source *replicationRelationshipSource |
| 52 | + // Config info for the destination side of the relationship |
| 53 | + Destination *replicationRelationshipDestination |
| 54 | +} |
| 55 | + |
| 56 | +type replicationRelationshipSource struct { |
| 57 | + // Cluster context name |
| 58 | + Cluster string |
| 59 | + // Namespace on source cluster |
| 60 | + Namespace string |
| 61 | + // Name of PVC being replicated |
| 62 | + PVCName string |
| 63 | + // Name of ReplicationSource object |
| 64 | + RSName string |
| 65 | + // Parameters for the ReplicationSource |
| 66 | + Source volsyncv1alpha1.ReplicationSourceRsyncSpec |
| 67 | +} |
| 68 | + |
| 69 | +type replicationRelationshipDestination struct { |
| 70 | + // Cluster context name |
| 71 | + Cluster string |
| 72 | + // Namespace on destination cluster |
| 73 | + Namespace string |
| 74 | + // Name of the ReplicationDestination object |
| 75 | + RDName string |
| 76 | + // Parameters for the ReplicationDestination |
| 77 | + Destination volsyncv1alpha1.ReplicationDestinationRsyncSpec |
| 78 | +} |
| 79 | + |
| 80 | +// replicationCmd represents the replication command |
| 81 | +var replicationCmd = &cobra.Command{ |
| 82 | + Use: "replication", |
| 83 | + Short: i18n.T("Replicate data between two PersistentVolumes"), |
| 84 | + Long: templates.LongDesc(i18n.T(` |
| 85 | + Replicate the contents of one PersistentVolume to another. |
| 86 | +
|
| 87 | + This set of commands is designed to set up and manage a replication |
| 88 | + relationship between two different PVCs in the same Namespace, across |
| 89 | + Namespaces, or in different clusters. The contents of the volume can be |
| 90 | + replicated either on-demand or based on a provided schedule. |
| 91 | + `)), |
| 92 | +} |
| 93 | + |
| 94 | +func init() { |
| 95 | + rootCmd.AddCommand(replicationCmd) |
| 96 | + |
| 97 | + replicationCmd.PersistentFlags().StringP("relationship", "r", "", "relationship name") |
| 98 | + cobra.CheckErr(replicationCmd.MarkPersistentFlagRequired("relationship")) |
| 99 | +} |
| 100 | + |
| 101 | +func newReplicationRelationship(cmd *cobra.Command) (*replicationRelationship, error) { |
| 102 | + r, err := CreateRelationshipFromCommand(cmd, ReplicationRelationshipType) |
| 103 | + if err != nil { |
| 104 | + return nil, err |
| 105 | + } |
| 106 | + |
| 107 | + return &replicationRelationship{ |
| 108 | + Relationship: *r, |
| 109 | + data: replicationRelationshipData{ |
| 110 | + Version: 1, |
| 111 | + }, |
| 112 | + }, nil |
| 113 | +} |
| 114 | + |
| 115 | +func loadReplicationRelationship(cmd *cobra.Command) (*replicationRelationship, error) { |
| 116 | + r, err := LoadRelationshipFromCommand(cmd, ReplicationRelationshipType) |
| 117 | + if err != nil { |
| 118 | + return nil, err |
| 119 | + } |
| 120 | + |
| 121 | + rr := &replicationRelationship{ |
| 122 | + Relationship: *r, |
| 123 | + } |
| 124 | + // Decode according to the file version |
| 125 | + version := rr.GetInt("data.version") |
| 126 | + switch version { |
| 127 | + case 1: |
| 128 | + if err := rr.GetData(&rr.data); err != nil { |
| 129 | + return nil, err |
| 130 | + } |
| 131 | + default: |
| 132 | + return nil, fmt.Errorf("unsupported config file version %d", version) |
| 133 | + } |
| 134 | + return rr, nil |
| 135 | +} |
| 136 | + |
| 137 | +func (rr *replicationRelationship) Save() error { |
| 138 | + if err := rr.SetData(rr.data); err != nil { |
| 139 | + return err |
| 140 | + } |
| 141 | + // resource.Quantity doesn't properly encode, so we need to do it manually |
| 142 | + if rr.data.Source != nil && rr.data.Source.Source.Capacity != nil { |
| 143 | + rr.Set("data.source.source.replicationsourcevolumeoptions.capacity", |
| 144 | + rr.data.Source.Source.Capacity.String()) |
| 145 | + } |
| 146 | + if rr.data.Destination != nil && rr.data.Destination.Destination.Capacity != nil { |
| 147 | + rr.Set("data.destination.destination.replicationdestinationvolumeoptions.capacity", |
| 148 | + rr.data.Destination.Destination.Capacity.String()) |
| 149 | + } |
| 150 | + return rr.Relationship.Save() |
| 151 | +} |
| 152 | + |
| 153 | +// GetClients returns clients to access the src & dst clusters (srcClient, |
| 154 | +// dstClient, error) |
| 155 | +func (rr *replicationRelationship) GetClients() (client.Client, client.Client, error) { |
| 156 | + var srcClient, dstClient client.Client |
| 157 | + var err error |
| 158 | + errList := []error{} |
| 159 | + if rr.data.Source != nil { |
| 160 | + if srcClient, err = newClient(rr.data.Source.Cluster); err != nil { |
| 161 | + klog.Errorf("unable to create client for source cluster: %w", err) |
| 162 | + errList = append(errList, err) |
| 163 | + } |
| 164 | + } |
| 165 | + if rr.data.Destination != nil { |
| 166 | + if dstClient, err = newClient(rr.data.Destination.Cluster); err != nil { |
| 167 | + klog.Errorf("unable to create client for destination cluster: %w", err) |
| 168 | + errList = append(errList, err) |
| 169 | + } |
| 170 | + } |
| 171 | + return srcClient, dstClient, errorsutil.NewAggregate(errList) |
| 172 | +} |
| 173 | + |
| 174 | +// DeleteSource removes the resources we've created on the source cluster |
| 175 | +func (rr *replicationRelationship) DeleteSource(ctx context.Context, |
| 176 | + srcClient client.Client) error { |
| 177 | + src := rr.data.Source |
| 178 | + if srcClient == nil || src == nil { |
| 179 | + // Nothing to do because we don't have a client or the source isn't |
| 180 | + // defined |
| 181 | + return nil |
| 182 | + } |
| 183 | + |
| 184 | + errList := []error{} |
| 185 | + for _, o := range []client.Object{ |
| 186 | + // cleaning up requires deleting both RS and the Secret we copied |
| 187 | + &volsyncv1alpha1.ReplicationSource{}, |
| 188 | + &corev1.Secret{}, |
| 189 | + } { |
| 190 | + err := srcClient.DeleteAllOf(ctx, o, |
| 191 | + client.InNamespace(src.Namespace), |
| 192 | + client.MatchingLabels{RelationshipLabelKey: rr.ID().String()}, |
| 193 | + client.PropagationPolicy(metav1.DeletePropagationBackground)) |
| 194 | + if client.IgnoreNotFound(err) != nil { |
| 195 | + klog.Errorf("unable to remove previous Source objects: %w", err) |
| 196 | + errList = append(errList, err) |
| 197 | + } |
| 198 | + } |
| 199 | + return errorsutil.NewAggregate(errList) |
| 200 | +} |
| 201 | + |
| 202 | +// DeleteDestination removes the resources we've created on the destination |
| 203 | +// cluster |
| 204 | +func (rr *replicationRelationship) DeleteDestination(ctx context.Context, |
| 205 | + dstClient client.Client) error { |
| 206 | + dst := rr.data.Destination |
| 207 | + if dstClient == nil || dst == nil { |
| 208 | + // Nothing to do because we don't have a client or the destination isn't |
| 209 | + // defined |
| 210 | + return nil |
| 211 | + } |
| 212 | + |
| 213 | + err := dstClient.DeleteAllOf(ctx, &volsyncv1alpha1.ReplicationDestination{}, |
| 214 | + client.InNamespace(dst.Namespace), |
| 215 | + client.MatchingLabels{RelationshipLabelKey: rr.ID().String()}, |
| 216 | + client.PropagationPolicy(metav1.DeletePropagationBackground)) |
| 217 | + err = client.IgnoreNotFound(err) |
| 218 | + if err != nil { |
| 219 | + klog.Errorf("unable to remove previous Destination objects: %w", err) |
| 220 | + } |
| 221 | + return err |
| 222 | +} |
0 commit comments