forked from kubevirt/containerized-data-importer
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhost-clone.go
153 lines (126 loc) · 4.21 KB
/
host-clone.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package clone
import (
"context"
"fmt"
"net/http"
"time"
"github.com/go-logr/logr"
corev1 "k8s.io/api/core/v1"
"k8s.io/client-go/tools/record"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
cdiv1 "kubevirt.io/containerized-data-importer-api/pkg/apis/core/v1beta1"
cc "kubevirt.io/containerized-data-importer/pkg/controller/common"
)
// HostClonePhaseName is the name of the host clone phase
const HostClonePhaseName = "HostClone"
// HostClonePhase creates and monitors a dumb clone operation
type HostClonePhase struct {
Owner client.Object
Namespace string
SourceName string
DesiredClaim *corev1.PersistentVolumeClaim
ImmediateBind bool
OwnershipLabel string
Preallocation bool
PriorityClassName string
Client client.Client
Log logr.Logger
Recorder record.EventRecorder
}
var _ Phase = &HostClonePhase{}
var _ StatusReporter = &HostClonePhase{}
var httpClient *http.Client
func init() {
httpClient = cc.BuildHTTPClient(httpClient)
}
// Name returns the name of the phase
func (p *HostClonePhase) Name() string {
return HostClonePhaseName
}
// Status returns the phase status
func (p *HostClonePhase) Status(ctx context.Context) (*PhaseStatus, error) {
result := &PhaseStatus{}
pvc := &corev1.PersistentVolumeClaim{}
exists, err := getResource(ctx, p.Client, p.Namespace, p.DesiredClaim.Name, pvc)
if err != nil {
return nil, err
}
if !exists {
return result, nil
}
result.Annotations = pvc.Annotations
podName := pvc.Annotations[cc.AnnCloneSourcePod]
if podName == "" {
return result, nil
}
args := &cc.ProgressFromClaimArgs{
Client: p.Client,
HTTPClient: httpClient,
Claim: pvc,
PodNamespace: p.Namespace,
PodName: podName,
OwnerUID: string(p.Owner.GetUID()),
}
progress, err := cc.ProgressFromClaim(ctx, args)
if err != nil {
return nil, err
}
result.Progress = progress
return result, nil
}
// Reconcile creates the desired pvc and waits for the operation to complete
func (p *HostClonePhase) Reconcile(ctx context.Context) (*reconcile.Result, error) {
actualClaim := &corev1.PersistentVolumeClaim{}
exists, err := getResource(ctx, p.Client, p.Namespace, p.DesiredClaim.Name, actualClaim)
if err != nil {
return nil, err
}
if !exists {
actualClaim, err = p.createClaim(ctx)
if err != nil {
return nil, err
}
}
if !p.hostCloneComplete(actualClaim) {
// requeue to update status
return &reconcile.Result{RequeueAfter: 3 * time.Second}, nil
}
return nil, nil
}
func (p *HostClonePhase) createClaim(ctx context.Context) (*corev1.PersistentVolumeClaim, error) {
claim := p.DesiredClaim.DeepCopy()
claim.Namespace = p.Namespace
cc.AddAnnotation(claim, cc.AnnPreallocationRequested, fmt.Sprintf("%t", p.Preallocation))
cc.AddAnnotation(claim, cc.AnnOwnerUID, string(p.Owner.GetUID()))
cc.AddAnnotation(claim, cc.AnnPodRestarts, "0")
cc.AddAnnotation(claim, cc.AnnCloneRequest, fmt.Sprintf("%s/%s", p.Namespace, p.SourceName))
cc.AddAnnotation(claim, cc.AnnPopulatorKind, cdiv1.VolumeCloneSourceRef)
cc.AddAnnotation(claim, cc.AnnEventSourceKind, p.Owner.GetObjectKind().GroupVersionKind().Kind)
cc.AddAnnotation(claim, cc.AnnEventSource, fmt.Sprintf("%s/%s", p.Owner.GetNamespace(), p.Owner.GetName()))
if p.OwnershipLabel != "" {
AddOwnershipLabel(p.OwnershipLabel, claim, p.Owner)
}
if p.ImmediateBind {
cc.AddAnnotation(claim, cc.AnnImmediateBinding, "")
}
if p.PriorityClassName != "" {
cc.AddAnnotation(claim, cc.AnnPriorityClassName, p.PriorityClassName)
}
cc.AddLabel(claim, cc.LabelExcludeFromVeleroBackup, "true")
if err := p.Client.Create(ctx, claim); err != nil {
checkQuotaExceeded(p.Recorder, p.Owner, err)
return nil, err
}
return claim, nil
}
func (p *HostClonePhase) hostCloneComplete(pvc *corev1.PersistentVolumeClaim) bool {
// this is awfully lame
// both the upload controller and clone controller update the PVC status to succeeded
// but only the clone controller will set the preallocation annotation
// so we have to wait for that
if p.Preallocation && pvc.Annotations[cc.AnnPreallocationApplied] != "true" {
return false
}
return pvc.Annotations[cc.AnnPodPhase] == string(cdiv1.Succeeded)
}