This repository has been archived by the owner on May 3, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 41
/
strategy_patches.go
95 lines (76 loc) · 2.68 KB
/
strategy_patches.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
package release
import (
"encoding/json"
"k8s.io/apimachinery/pkg/api/equality"
"k8s.io/apimachinery/pkg/runtime/schema"
shipper "github.com/bookingcom/shipper/pkg/apis/shipper/v1alpha1"
)
type StrategyPatch interface {
PatchSpec() (string, schema.GroupVersionKind, []byte)
Alters(interface{}) bool
IsEmpty() bool
}
type CapacityTargetSpecPatch struct {
Name string
NewSpec *shipper.CapacityTargetSpec
}
var _ StrategyPatch = (*CapacityTargetSpecPatch)(nil)
func (p *CapacityTargetSpecPatch) PatchSpec() (string, schema.GroupVersionKind, []byte) {
patch := make(map[string]interface{})
patch["spec"] = p.NewSpec
b, _ := json.Marshal(patch)
return p.Name, shipper.SchemeGroupVersion.WithKind("CapacityTarget"), b
}
func (p *CapacityTargetSpecPatch) Alters(o interface{}) bool {
// CapacityTargetSpecPatch is an altering one by it's nature: it's only
// being created if a capacity target adjustment is required. Therefore
// we're saving a few peanuts and moving on with always-apply strategy.
return !p.IsEmpty()
}
func (p *CapacityTargetSpecPatch) IsEmpty() bool {
return p == nil || p.NewSpec == nil
}
type TrafficTargetSpecPatch struct {
Name string
NewSpec *shipper.TrafficTargetSpec
}
var _ StrategyPatch = (*TrafficTargetSpecPatch)(nil)
func (p *TrafficTargetSpecPatch) PatchSpec() (string, schema.GroupVersionKind, []byte) {
patch := make(map[string]interface{})
patch["spec"] = p.NewSpec
b, _ := json.Marshal(patch)
return p.Name, shipper.SchemeGroupVersion.WithKind("TrafficTarget"), b
}
func (p *TrafficTargetSpecPatch) Alters(o interface{}) bool {
// TrafficTargetSpecPatch is an altering one by it's nature: it's only
// being created if a capacity target adjustment is required. Therefore
// we're saving a few peanuts and moving on with always-apply strategy.
return !p.IsEmpty()
}
func (p *TrafficTargetSpecPatch) IsEmpty() bool {
return p == nil || p.NewSpec == nil
}
type ReleaseStrategyStatusPatch struct {
Name string
NewStrategyStatus *shipper.ReleaseStrategyStatus
}
var _ StrategyPatch = (*ReleaseStrategyStatusPatch)(nil)
func (p *ReleaseStrategyStatusPatch) PatchSpec() (string, schema.GroupVersionKind, []byte) {
patch := map[string]interface{}{
"status": map[string]interface{}{
"strategy": p.NewStrategyStatus,
},
}
b, _ := json.Marshal(patch)
return p.Name, shipper.SchemeGroupVersion.WithKind("Release"), b
}
func (p *ReleaseStrategyStatusPatch) Alters(o interface{}) bool {
rel, ok := o.(*shipper.Release)
if !ok || p.IsEmpty() {
return false
}
return !equality.Semantic.DeepEqual(rel.Status.Strategy, p.NewStrategyStatus)
}
func (p *ReleaseStrategyStatusPatch) IsEmpty() bool {
return p == nil || p.NewStrategyStatus == nil
}