-
Notifications
You must be signed in to change notification settings - Fork 260
/
storage.go
138 lines (123 loc) · 3.95 KB
/
storage.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
/*
Copyright The CloudNativePG Contributors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package utils
import (
"fmt"
"os"
volumesnapshot "github.com/kubernetes-csi/external-snapshotter/client/v7/apis/volumesnapshot/v1"
corev1 "k8s.io/api/core/v1"
storagev1 "k8s.io/api/storage/v1"
"sigs.k8s.io/controller-runtime/pkg/client"
apiv1 "github.com/cloudnative-pg/cloudnative-pg/api/v1"
"github.com/cloudnative-pg/cloudnative-pg/pkg/utils"
)
// GetStorageAllowExpansion returns the boolean value of the 'AllowVolumeExpansion' value of the storage class
func GetStorageAllowExpansion(defaultStorageClass string, env *TestingEnvironment) (*bool, error) {
storageClass := &storagev1.StorageClass{}
err := GetObject(env, client.ObjectKey{Name: defaultStorageClass}, storageClass)
return storageClass.AllowVolumeExpansion, err
}
// IsWalStorageEnabled returns true if 'WalStorage' is being used
func IsWalStorageEnabled(namespace, clusterName string, env *TestingEnvironment) (bool, error) {
cluster, err := env.GetCluster(namespace, clusterName)
if cluster.Spec.WalStorage == nil {
return false, err
}
return true, err
}
// PvcHasLabels returns true if a PVC contains a given map of labels
func PvcHasLabels(
pvc corev1.PersistentVolumeClaim,
labels map[string]string,
) bool {
pvcLabels := pvc.Labels
for k, v := range labels {
val, ok := pvcLabels[k]
if !ok || (v != val) {
return false
}
}
return true
}
// ObjectHasAnnotations returns true if the object has the passed annotations
func ObjectHasAnnotations(
object client.Object,
annotations []string,
) bool {
objectAnnotations := object.GetAnnotations()
for _, v := range annotations {
_, ok := objectAnnotations[v]
if !ok {
return false
}
}
return true
}
// ObjectMatchesAnnotations returns true if the object has the passed annotations key/value
func ObjectMatchesAnnotations(
object client.Object,
annotations map[string]string,
) bool {
objectAnnotations := object.GetAnnotations()
for k, v := range annotations {
value, ok := objectAnnotations[k]
if !ok && (v != value) {
return false
}
}
return true
}
// EnvVarsForSnapshots represents the environment variables to use to track snapshots
// and apply them in test fixture templates
type EnvVarsForSnapshots struct {
DataSnapshot string
WalSnapshot string
TablespaceSnapshotPrefix string
}
// SetSnapshotNameAsEnv sets the names of a PG_DATA, a PG_WAL and a list of PG_TABLESPACE snapshots from a
// given snapshotList as env variables
func SetSnapshotNameAsEnv(
snapshotList *volumesnapshot.VolumeSnapshotList,
backup *apiv1.Backup,
envVars EnvVarsForSnapshots,
) error {
if len(snapshotList.Items) != len(backup.Status.BackupSnapshotStatus.Elements) {
return fmt.Errorf("could not find the expected number of snapshots")
}
for _, item := range snapshotList.Items {
switch utils.PVCRole(item.Annotations[utils.PvcRoleLabelName]) {
case utils.PVCRolePgData:
err := os.Setenv(envVars.DataSnapshot, item.Name)
if err != nil {
return err
}
case utils.PVCRolePgWal:
err := os.Setenv(envVars.WalSnapshot, item.Name)
if err != nil {
return err
}
case utils.PVCRolePgTablespace:
tbsName := item.Labels[utils.TablespaceNameLabelName]
err := os.Setenv(envVars.TablespaceSnapshotPrefix+"_"+tbsName, item.Name)
if err != nil {
return err
}
default:
return fmt.Errorf("unrecognized PVC snapshot role: %s, name: %s",
item.Annotations[utils.PvcRoleLabelName],
item.Name,
)
}
}
return nil
}