/
validation.go
177 lines (149 loc) · 5.15 KB
/
validation.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/*
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 persistentvolumeclaim
import (
"context"
"fmt"
volumesnapshot "github.com/kubernetes-csi/external-snapshotter/client/v7/apis/volumesnapshot/v1"
apierrs "k8s.io/apimachinery/pkg/api/errors"
"sigs.k8s.io/controller-runtime/pkg/client"
apiv1 "github.com/cloudnative-pg/cloudnative-pg/api/v1"
"github.com/cloudnative-pg/cloudnative-pg/pkg/utils"
)
// ValidationStatus is the result of the validation of a cluster
// datasource
type ValidationStatus struct {
// Errors is the list of blocking errors
Errors []ValidationMessage `json:"errors"`
// Warnings is the list of warnings that are not blocking
Warnings []ValidationMessage `json:"warnings"`
}
// ValidationMessage is a message about a snapshot
type ValidationMessage struct {
ObjectName string `json:"objectName"`
Message string `json:"message"`
}
func newValidationMessage(objectName string, message string) ValidationMessage {
return ValidationMessage{ObjectName: objectName, Message: message}
}
// ContainsErrors returns true if the validation result
// has any blocking errors.
func (status *ValidationStatus) ContainsErrors() bool {
return len(status.Errors) > 0
}
// ContainsWarnings returns true if there are any validation warnings.
func (status *ValidationStatus) ContainsWarnings() bool {
return len(status.Warnings) > 0
}
// AddError adds an error message to the validation status
func (status *ValidationStatus) addErrorf(name string, format string, args ...interface{}) {
status.Errors = append(status.Errors, newValidationMessage(name, fmt.Sprintf(format, args...)))
}
// AddWarning adds an error message to the validation status
func (status *ValidationStatus) addWarningf(name string, format string, args ...interface{}) {
status.Warnings = append(status.Warnings, newValidationMessage(name, fmt.Sprintf(format, args...)))
}
// validateVolumeSnapshot validates the label of a volume snapshot,
// adding the result to the status
func (status *ValidationStatus) validateVolumeSnapshot(
name string,
snapshot *volumesnapshot.VolumeSnapshot,
expectedMeta Meta,
) {
if snapshot == nil {
status.addErrorf(name, "VolumeSnapshot doesn't exist")
return
}
pvcRoleLabel := snapshot.GetAnnotations()[utils.PvcRoleLabelName]
if len(pvcRoleLabel) == 0 {
status.addWarningf(name, "Empty PVC role annotation")
} else if pvcRoleLabel != expectedMeta.GetRoleName() {
status.addErrorf(
name,
"Expected role '%s', found '%s'",
utils.PVCRolePgData,
pvcRoleLabel)
}
backupNameLabel := snapshot.GetLabels()[utils.BackupNameLabelName]
if len(backupNameLabel) == 0 {
status.addWarningf(
name,
"Empty backup name label",
)
}
}
// VerifyDataSourceCoherence verifies if th specified data source that we should
// use when creating a new cluster is coherent. We check for:
//
// - role of the volume snapshot is coherent with the requested section
// (being storage or walStorage)
//
// - the specified snapshots all belong to the same cluster and backupName
func VerifyDataSourceCoherence(
ctx context.Context,
c client.Client,
namespace string,
source *apiv1.DataSource,
) (ValidationStatus, error) {
var result ValidationStatus
if source == nil {
return result, nil
}
pgDataSnapshot, err := getVolumeShapshotOrNil(
ctx,
c,
client.ObjectKey{Namespace: namespace, Name: source.Storage.Name})
if err != nil {
return result, err
}
result.validateVolumeSnapshot(source.Storage.Name, pgDataSnapshot, NewPgDataCalculator())
var pgWalSnapshot *volumesnapshot.VolumeSnapshot
if source.WalStorage != nil {
pgWalSnapshot, err = getVolumeShapshotOrNil(
ctx,
c,
client.ObjectKey{Namespace: namespace, Name: source.WalStorage.Name})
if err != nil {
return result, err
}
result.validateVolumeSnapshot(source.WalStorage.Name, pgWalSnapshot, NewPgWalCalculator())
}
if pgDataSnapshot != nil && pgWalSnapshot != nil {
pgDataBackupName := pgDataSnapshot.GetLabels()[utils.BackupNameLabelName]
pgWalBackupName := pgWalSnapshot.GetLabels()[utils.BackupNameLabelName]
if pgDataBackupName != pgWalBackupName {
result.addErrorf(
source.Storage.Name,
"Non coherent backup names: '%s' and '%s'",
pgDataBackupName,
pgWalBackupName)
}
}
return result, nil
}
// getVolumeShapshotOrNil gets a volume snapshot with a specified name.
// If the volume snapshot don't exist, returns nil
func getVolumeShapshotOrNil(
ctx context.Context,
c client.Client,
name client.ObjectKey,
) (*volumesnapshot.VolumeSnapshot, error) {
var result volumesnapshot.VolumeSnapshot
if err := c.Get(ctx, name, &result); err != nil {
if apierrs.IsNotFound(err) {
return nil, nil
}
return nil, err
}
return &result, nil
}