generated from SAP/repository-template
-
Notifications
You must be signed in to change notification settings - Fork 1
/
hooks.go
71 lines (61 loc) · 2.02 KB
/
hooks.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
/*
SPDX-FileCopyrightText: 2023 SAP SE or an SAP affiliate company and component-operator-runtime contributors
SPDX-License-Identifier: Apache-2.0
*/
package helm
import (
"fmt"
"strconv"
"strings"
"github.com/pkg/errors"
"sigs.k8s.io/controller-runtime/pkg/client"
)
const (
annotationKeyHook = "helm.sh/hook"
annotationKeyHookWeight = "helm.sh/hook-weight"
annotationKeyHookDeletePolicy = "helm.sh/hook-delete-policy"
)
// parse helm hook properties from object, return nil if annotation helm.sh/hook is not set
func ParseHookMetadata(object client.Object) (*HookMetadata, error) {
metadata := &HookMetadata{}
annotations := object.GetAnnotations()
if value, ok := annotations[annotationKeyHook]; ok {
metadata.Types = strings.Split(value, ",")
for _, t := range metadata.Types {
switch t {
case HookTypePreInstall, HookTypePostInstall, HookTypePreUpgrade, HookTypePostUpgrade,
HookTypePreDelete, HookTypePostDelete, HookTypePreRollback, HookTypePostRollback,
HookTypeTest, HookTypeTestSuccess:
default:
return nil, fmt.Errorf("invalid hook type: %s", t)
}
}
} else {
return nil, nil
}
if value, ok := annotations[annotationKeyHookWeight]; ok {
weight, err := strconv.Atoi(value)
if err != nil {
return nil, errors.Wrapf(err, "invalid hook weight: %s", value)
}
if weight < HookMinWeight || weight > HookMaxWeight {
return nil, fmt.Errorf("invalid hook weight: %d (allowed range: %d..%d)", weight, HookMinWeight, HookMaxWeight)
}
metadata.Weight = weight
} else {
metadata.Weight = 0
}
if value, ok := annotations[annotationKeyHookDeletePolicy]; ok {
metadata.DeletePolicies = strings.Split(value, ",")
for _, p := range metadata.DeletePolicies {
switch p {
case HookDeletePolicyBeforeHookCreation, HookDeletePolicyHookSucceeded, HookDeletePolicyHookFailed:
default:
return nil, fmt.Errorf("invalid hook deletion policy: %s", p)
}
}
} else {
metadata.DeletePolicies = []string{HookDeletePolicyBeforeHookCreation}
}
return metadata, nil
}