|
| 1 | +package openslosdk |
| 2 | + |
| 3 | +import ( |
| 4 | + "sync" |
| 5 | + |
| 6 | + "github.com/OpenSLO/go-sdk/pkg/openslo" |
| 7 | + v1 "github.com/OpenSLO/go-sdk/pkg/openslo/v1" |
| 8 | +) |
| 9 | + |
| 10 | +func NewReferenceExporter(objects ...openslo.Object) *ReferenceExporter { |
| 11 | + return &ReferenceExporter{ |
| 12 | + objects: objects, |
| 13 | + exported: make([]openslo.Object, 0, len(objects)), |
| 14 | + } |
| 15 | +} |
| 16 | + |
| 17 | +type ReferenceExporter struct { |
| 18 | + objects []openslo.Object |
| 19 | + exported []openslo.Object |
| 20 | + once sync.Once |
| 21 | +} |
| 22 | + |
| 23 | +// Export replaces all the inlined objects with references and returns |
| 24 | +// the original objects along with the exported, previously inlined, objects. |
| 25 | +func (r *ReferenceExporter) Export() []openslo.Object { |
| 26 | + r.once.Do(func() { |
| 27 | + r.exported = r.exportObjects() |
| 28 | + }) |
| 29 | + return r.exported |
| 30 | +} |
| 31 | + |
| 32 | +func (r *ReferenceExporter) exportObjects() []openslo.Object { |
| 33 | + for _, object := range r.objects { |
| 34 | + r.exportObject(object) |
| 35 | + } |
| 36 | + return r.exported |
| 37 | +} |
| 38 | + |
| 39 | +func (r *ReferenceExporter) exportObject(object openslo.Object) { |
| 40 | + version := object.GetVersion() |
| 41 | + switch version { |
| 42 | + case openslo.VersionV1: |
| 43 | + r.addResult(r.exportV1Object(object)...) |
| 44 | + default: |
| 45 | + r.addResult(object) |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +func (r *ReferenceExporter) exportV1Object(object openslo.Object) []openslo.Object { |
| 50 | + switch v := object.(type) { |
| 51 | + case v1.AlertPolicy: |
| 52 | + return r.exportV1AlertPolicy(v) |
| 53 | + case v1.SLO: |
| 54 | + return r.exportV1SLO(v) |
| 55 | + default: |
| 56 | + return []openslo.Object{object} |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +func (r *ReferenceExporter) exportV1AlertPolicy(alertPolicy v1.AlertPolicy) []openslo.Object { |
| 61 | + exported := make([]openslo.Object, 0) |
| 62 | + for i, target := range alertPolicy.Spec.NotificationTargets { |
| 63 | + if target.AlertPolicyNotificationTargetInline == nil { |
| 64 | + continue |
| 65 | + } |
| 66 | + exported = append(exported, v1.NewAlertNotificationTarget(target.Metadata, target.Spec)) |
| 67 | + target.AlertPolicyNotificationTargetRef = &v1.AlertPolicyNotificationTargetRef{ |
| 68 | + TargetRef: target.Metadata.Name, |
| 69 | + } |
| 70 | + target.AlertPolicyNotificationTargetInline = nil |
| 71 | + alertPolicy.Spec.NotificationTargets[i] = target |
| 72 | + } |
| 73 | + for i, condition := range alertPolicy.Spec.Conditions { |
| 74 | + if condition.AlertPolicyConditionInline == nil { |
| 75 | + continue |
| 76 | + } |
| 77 | + exported = append(exported, v1.NewAlertCondition(condition.Metadata, condition.Spec)) |
| 78 | + condition.AlertPolicyConditionRef = &v1.AlertPolicyConditionRef{ |
| 79 | + ConditionRef: condition.Metadata.Name, |
| 80 | + } |
| 81 | + condition.AlertPolicyConditionInline = nil |
| 82 | + alertPolicy.Spec.Conditions[i] = condition |
| 83 | + } |
| 84 | + return append([]openslo.Object{alertPolicy}, exported...) |
| 85 | +} |
| 86 | + |
| 87 | +func (r *ReferenceExporter) exportV1SLO(slo v1.SLO) []openslo.Object { |
| 88 | + exported := make([]openslo.Object, 0) |
| 89 | + for i, ap := range slo.Spec.AlertPolicies { |
| 90 | + if ap.SLOAlertPolicyInline == nil { |
| 91 | + continue |
| 92 | + } |
| 93 | + alertPolicy := v1.NewAlertPolicy(ap.Metadata, ap.Spec) |
| 94 | + exported = append(exported, r.exportV1AlertPolicy(alertPolicy)...) |
| 95 | + ap.SLOAlertPolicyRef = &v1.SLOAlertPolicyRef{ |
| 96 | + AlertPolicyRef: alertPolicy.Metadata.Name, |
| 97 | + } |
| 98 | + ap.SLOAlertPolicyInline = nil |
| 99 | + slo.Spec.AlertPolicies[i] = ap |
| 100 | + } |
| 101 | + if slo.Spec.Indicator != nil { |
| 102 | + exported = append(exported, v1.NewSLI(slo.Spec.Indicator.Metadata, slo.Spec.Indicator.Spec)) |
| 103 | + slo.Spec.IndicatorRef = &slo.Spec.Indicator.Metadata.Name |
| 104 | + slo.Spec.Indicator = nil |
| 105 | + } |
| 106 | + return append([]openslo.Object{slo}, exported...) |
| 107 | +} |
| 108 | + |
| 109 | +func (r *ReferenceExporter) addResult(objects ...openslo.Object) { |
| 110 | + r.exported = append(r.exported, objects...) |
| 111 | +} |
0 commit comments