/
mod.go
116 lines (101 loc) · 2.92 KB
/
mod.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
/*
* Copyright 2020 SAP SE or an SAP affiliate company. All rights reserved. This file is licensed under the Apache Software License, v. 2 except as noted otherwise in the LICENSE file
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*
*/
package abstract
import (
"reflect"
"github.com/gardener/controller-manager-library/pkg/fieldpath"
"github.com/gardener/controller-manager-library/pkg/resources/conditions"
"github.com/gardener/controller-manager-library/pkg/utils"
)
type ModificationState struct {
utils.ModificationState
object Object
handler conditions.ModificationHandler
}
func NewModificationState(object Object, mod ...bool) *ModificationState {
aggr := false
for _, m := range mod {
aggr = aggr || m
}
s := &ModificationState{utils.ModificationState{aggr}, object, nil}
s.handler = &modhandler{s}
return s
}
func (this *ModificationState) Object() Object {
return this.object
}
func (this *ModificationState) Data() ObjectData {
return this.object.Data()
}
func (this *ModificationState) Condition(t *conditions.ConditionType) *conditions.Condition {
c := t.Get(this.Data())
if c != nil {
c.AddModificationHandler(this.handler)
}
return c
}
func (this *ModificationState) Conditions(layout *conditions.ConditionLayout) (*conditions.Conditions, error) {
c, err := layout.For(this.Data())
if err != nil {
return nil, err
}
if c != nil {
c.AddModificationHandler(this.handler)
}
return c, nil
}
func (this *ModificationState) Apply(f func(obj Object) bool) *ModificationState {
this.Modified = this.Modified || f(this.object)
return this
}
func (this *ModificationState) AssureLabel(name, value string) *ModificationState {
labels := this.object.GetLabels()
if labels[name] != value {
if value == "" {
delete(labels, name)
} else {
labels[name] = value
}
this.Modified = true
}
return this
}
func (this *ModificationState) Get(field fieldpath.Field) (interface{}, error) {
return field.Get(this.object.Data())
}
func (this *ModificationState) Set(field fieldpath.Field, value interface{}) error {
old, err := field.Get(this.object.Data())
if err != nil {
return err
}
if reflect.DeepEqual(old, value) {
return nil
}
err = field.Set(this.object.Data(), value)
if err != nil {
return err
}
this.Modified = true
return nil
}
type modhandler struct {
state *ModificationState
}
func (this *modhandler) Modified(condition *conditions.Condition) {
this.state.Modify(true)
}