-
Notifications
You must be signed in to change notification settings - Fork 0
/
features_patch_fields.go
132 lines (117 loc) · 5.44 KB
/
features_patch_fields.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
package kubernetes_ctx
import (
"fmt"
"k8s.io/apimachinery/pkg/types"
"github.com/xunleii/godog-kubernetes/helpers"
)
// LabelizeResource implements the GoDoc step
// - `Kubernetes labelizes <ApiGroupVersionKind> '<NamespacedName>' with '<LabelName>=<LabelValue>'`
// It adds or modifies a specific resource label with the given value.
func LabelizeResource(ctx *FeatureContext, s ScenarioContext) {
s.Step(
`^Kubernetes labelizes (`+RxGroupVersionKind+`) '(`+RxNamespacedName+`)' with '(`+RxFieldPath+`)=(.*)'$`,
func(groupVersionKindStr, resourceName, labelName, labelValue string) error {
groupVersionKind, err := helpers.GroupVersionKindFrom(groupVersionKindStr)
if err != nil {
return err
}
namespacedName, _ := helpers.NamespacedNameFrom(resourceName)
patch := fmt.Sprintf(`{"metadata":{"labels":{"%s":"%s"}}}`, labelName, labelValue)
return ctx.Patch(groupVersionKind, namespacedName, types.MergePatchType, []byte(patch))
},
)
}
// RemoveResourceLabel implements the GoDoc step
// - `Kubernetes removes label <LabelName> on <ApiGroupVersionKind> '<NamespacedName>'`
// It removes the given label on the specified resource.
func RemoveResourceLabel(ctx *FeatureContext, s ScenarioContext) {
s.Step(
`^Kubernetes removes label '(`+RxFieldPath+`)' on (`+RxGroupVersionKind+`) '(`+RxNamespacedName+`)'$`,
func(label, groupVersionKindStr, resourceName string) error {
groupVersionKind, err := helpers.GroupVersionKindFrom(groupVersionKindStr)
if err != nil {
return err
}
namespacedName, _ := helpers.NamespacedNameFrom(resourceName)
patch := fmt.Sprintf(`[{"op":"remove","path":"/metadata/labels/%s"}]`, helpers.SanitizeJsonPatch(label))
return ctx.Patch(groupVersionKind, namespacedName, types.JSONPatchType, []byte(patch))
},
)
}
// UpdateResourceLabel implements the GoDoc step
// - `Kubernetes updates label <LabelName> on <ApiGroupVersionKind> '<NamespacedName>' with '<LabelValue>'`
// It updates the given label on the specified resource with the given value.
func UpdateResourceLabel(ctx *FeatureContext, s ScenarioContext) {
s.Step(
`^Kubernetes updates label '(`+RxFieldPath+`)' on (`+RxGroupVersionKind+`) '(`+RxNamespacedName+`)' with '(.*)'$`,
func(label, groupVersionKindStr, resourceName, value string) error {
groupVersionKind, err := helpers.GroupVersionKindFrom(groupVersionKindStr)
if err != nil {
return err
}
namespacedName, _ := helpers.NamespacedNameFrom(resourceName)
patch := fmt.Sprintf(`[{"op":"replace","path":"/metadata/labels/%s","value":"%s"}]`, helpers.SanitizeJsonPatch(label), value)
return ctx.Patch(groupVersionKind, namespacedName, types.JSONPatchType, []byte(patch))
},
)
}
// AnnotateResource implements the GoDoc step
// - `Kubernetes annotates <ApiGroupVersionKind> '<NamespacedName>' with '<AnnotationName>=<AnnotationValue>'`
// It adds or modifies a specific resource annotation with the given value.
func AnnotateResource(ctx *FeatureContext, s ScenarioContext) {
s.Step(
`^Kubernetes annotates (`+RxGroupVersionKind+`) '(`+RxNamespacedName+`)' with '(`+RxFieldPath+`)=(.*)'$`,
func(groupVersionKindStr, resourceName, annotationName, annotationValue string) error {
groupVersionKind, err := helpers.GroupVersionKindFrom(groupVersionKindStr)
if err != nil {
return err
}
namespacedName, err := helpers.NamespacedNameFrom(resourceName)
if err != nil {
return err
}
patch := fmt.Sprintf(`{"metadata":{"annotations":{"%s":"%s"}}}`, annotationName, annotationValue)
return ctx.Patch(groupVersionKind, namespacedName, types.MergePatchType, []byte(patch))
},
)
}
// RemoveResourceAnnotation implements the GoDoc step
// - `Kubernetes removes annotation <AnnotationName> on <ApiGroupVersionKind> '<NamespacedName>'`
// It removes the given annotation on the specified resource.
func RemoveResourceAnnotation(ctx *FeatureContext, s ScenarioContext) {
s.Step(
`^Kubernetes removes annotation '(`+RxFieldPath+`)' on (`+RxGroupVersionKind+`) '(`+RxNamespacedName+`)'$`,
func(annotation, groupVersionKindStr, resourceName string) error {
groupVersionKind, err := helpers.GroupVersionKindFrom(groupVersionKindStr)
if err != nil {
return err
}
namespacedName, err := helpers.NamespacedNameFrom(resourceName)
if err != nil {
return err
}
patch := fmt.Sprintf(`[{"op":"remove","path":"/metadata/annotations/%s"}]`, helpers.SanitizeJsonPatch(annotation))
return ctx.Patch(groupVersionKind, namespacedName, types.JSONPatchType, []byte(patch))
},
)
}
// UpdateResourceAnnotation implements the GoDoc step
// - `Kubernetes updates annotation <AnnotationName> on <ApiGroupVersionKind> '<NamespacedName>' with '<AnnotationValue>'`
// It updates the given annotation on the specified resource with the given value.
func UpdateResourceAnnotation(ctx *FeatureContext, s ScenarioContext) {
s.Step(
`^Kubernetes updates annotation '(`+RxFieldPath+`)' on (`+RxGroupVersionKind+`) '(`+RxNamespacedName+`)' with '(.*)'$`,
func(annotation, groupVersionKindStr, resourceName, value string) error {
groupVersionKind, err := helpers.GroupVersionKindFrom(groupVersionKindStr)
if err != nil {
return err
}
namespacedName, err := helpers.NamespacedNameFrom(resourceName)
if err != nil {
return err
}
patch := fmt.Sprintf(`[{"op":"replace","path":"/metadata/annotations/%s","value":"%s"}]`, helpers.SanitizeJsonPatch(annotation), value)
return ctx.Patch(groupVersionKind, namespacedName, types.JSONPatchType, []byte(patch))
},
)
}