-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpatch.go
More file actions
62 lines (49 loc) · 1.58 KB
/
Copy pathpatch.go
File metadata and controls
62 lines (49 loc) · 1.58 KB
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
package objectpatch
import (
"fmt"
"strings"
"github.com/deckhouse/module-sdk/pkg"
)
// Compile-time interface compliance check
var _ pkg.PatchCollectorOptionApplier = (*Patch)(nil)
// Patch represents a single patch operation with its parameters stored as a map.
// The patchValues map is serialized to JSON when sent to the shell-operator.
type Patch struct {
patchValues map[string]any
}
// Description returns a human-readable description of the patch operation.
// Returns "unknown" if operation type is missing or invalid.
func (p *Patch) Description() string {
op, ok := p.patchValues["operation"]
if !ok {
return "unknown"
}
// Handle both string and typed operation enums (CreateOperation, etc.)
return fmt.Sprintf("%v", op)
}
// SetObjectPrefix sets prefix for object name.
func (p *Patch) SetObjectPrefix(prefix string) {
if p.patchValues == nil {
return
}
name, ok := p.patchValues["name"]
if !ok {
return
}
if strings.HasPrefix(name.(string), prefix+"-") {
return
}
p.patchValues["name"] = fmt.Sprintf("%s-%s", prefix, name.(string))
}
// WithSubresource sets the subresource to patch (e.g., "status", "scale").
func (p *Patch) WithSubresource(subresource string) {
p.patchValues["subresource"] = subresource
}
// WithIgnoreMissingObject prevents errors when the target object doesn't exist.
func (p *Patch) WithIgnoreMissingObject(ignore bool) {
p.patchValues["ignoreMissingObjects"] = ignore
}
// WithIgnoreHookError continues execution even if this patch fails.
func (p *Patch) WithIgnoreHookError(ignore bool) {
p.patchValues["ignoreHookError"] = ignore
}