/
util.go
58 lines (51 loc) · 1.46 KB
/
util.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
package admission
import (
"fmt"
"strings"
api "github.com/kubedb/apimachinery/apis/kubedb/v1alpha1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/util/mergepatch"
meta_util "kmodules.xyz/client-go/meta"
)
func ValidateUpdate(obj, oldObj runtime.Object, kind string) error {
preconditions := getPreconditionFunc(kind)
_, err := meta_util.CreateStrategicPatch(oldObj, obj, preconditions...)
if err != nil {
if mergepatch.IsPreconditionFailed(err) {
return fmt.Errorf("%v.%v", err, preconditionFailedError(kind))
}
return err
}
return nil
}
func getPreconditionFunc(kind string) []mergepatch.PreconditionFunc {
preconditions := []mergepatch.PreconditionFunc{
mergepatch.RequireKeyUnchanged("apiVersion"),
mergepatch.RequireKeyUnchanged("kind"),
mergepatch.RequireMetadataKeyUnchanged("name"),
mergepatch.RequireMetadataKeyUnchanged("namespace"),
}
if fields, found := preconditionSpecField[kind]; found {
for _, field := range fields {
preconditions = append(preconditions,
meta_util.RequireChainKeyUnchanged(field),
)
}
}
return preconditions
}
var preconditionSpecField = map[string][]string{
api.ResourceKindDormantDatabase: {
"spec.origin",
},
}
func preconditionFailedError(kind string) error {
str := preconditionSpecField[kind]
strList := strings.Join(str, "\n\t")
return fmt.Errorf(strings.Join([]string{`At least one of the following was changed:
apiVersion
kind
name
namespace
status`, strList}, "\n\t"))
}