/
volumes.go
133 lines (112 loc) · 3.9 KB
/
volumes.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
/*
Copyright 2020,2021 Avi Zimmerman
This file is part of kvdi.
kvdi is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
kvdi is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with kvdi. If not, see <https://www.gnu.org/licenses/>.
*/
package desktop
import (
"context"
appv1 "github.com/kvdi/kvdi/apis/app/v1"
desktopsv1 "github.com/kvdi/kvdi/apis/desktops/v1"
"github.com/kvdi/kvdi/pkg/util/errors"
"github.com/kvdi/kvdi/pkg/util/k8sutil"
"github.com/kvdi/kvdi/pkg/util/reconcile"
"github.com/go-logr/logr"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/client"
)
func (f *Reconciler) reconcileVolumes(ctx context.Context, reqLogger logr.Logger, cluster *appv1.VDICluster, instance *desktopsv1.Session) error {
volMapCM, err := f.getVolMapForCluster(cluster)
if err != nil {
return err
}
var existingVol string
var ok bool
if existingVol, ok = volMapCM.Data[instance.GetUser()]; ok {
reqLogger.Info("Fetching existing volume for user")
if err := f.client.Get(ctx, types.NamespacedName{Name: existingVol, Namespace: metav1.NamespaceAll}, &corev1.PersistentVolume{}); err != nil {
if client.IgnoreNotFound(err) != nil {
return err
}
reqLogger.Info("The volume referenced in the userdata configmap no longer exists, creating a new one")
existingVol = ""
}
}
pvc := newPVCForUser(cluster, instance, existingVol)
return reconcile.PersistentVolumeClaim(ctx, reqLogger, f.client, pvc)
}
func (f *Reconciler) reconcileUserdataMapping(ctx context.Context, reqLogger logr.Logger, cluster *appv1.VDICluster, instance *desktopsv1.Session) error {
pvc, err := f.getPVCForInstance(cluster, instance)
if err != nil {
return err
}
if pvc.Spec.VolumeName == "" {
return errors.NewRequeueError("PVC has not had its volume provisioned yet", 3)
}
pvName := pvc.Spec.VolumeName
pv, err := f.getPV(pvName)
if err != nil {
return err
}
// it won't harm the running instance and the storage class provider may
// leave us alone
if _, err := f.freePV(pv); err != nil {
return err
}
volMapCM, err := f.getVolMapForCluster(cluster)
if err != nil {
return err
}
if volMapCM.Data == nil {
volMapCM.Data = make(map[string]string)
}
if pv, ok := volMapCM.Data[instance.GetUser()]; !ok || pv != pvName {
volMapCM.Data[instance.GetUser()] = pvName
if err := f.client.Update(ctx, volMapCM); err != nil {
return err
}
}
return nil
}
func newConfigMapForCluster(cluster *appv1.VDICluster) *corev1.ConfigMap {
nn := cluster.GetUserdataVolumeMapName()
return &corev1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Name: nn.Name,
Namespace: nn.Namespace,
Labels: cluster.GetComponentLabels("userdata-map"),
OwnerReferences: cluster.OwnerReferences(),
},
Data: make(map[string]string),
}
}
func newPVCForUser(cluster *appv1.VDICluster, instance *desktopsv1.Session, existingPVName string) *corev1.PersistentVolumeClaim {
spec := cluster.GetUserdataVolumeSpec()
if existingPVName != "" {
spec.VolumeName = existingPVName
}
var ownerReferences []metav1.OwnerReference
if !cluster.RetainPVCs() {
ownerReferences = instance.OwnerReferences()
}
return &corev1.PersistentVolumeClaim{
ObjectMeta: metav1.ObjectMeta{
Name: cluster.GetUserdataVolumeName(instance.GetUser()),
Namespace: instance.GetNamespace(),
Labels: k8sutil.GetDesktopLabels(cluster, instance),
OwnerReferences: ownerReferences,
},
Spec: *spec,
}
}