github.com/banzaicloud/operator-tools@v0.28.10/pkg/reconciler/state.go (about) 1 // Copyright © 2020 Banzai Cloud 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package reconciler 16 17 import ( 18 "k8s.io/apimachinery/pkg/runtime" 19 runtimeClient "sigs.k8s.io/controller-runtime/pkg/client" 20 ) 21 22 type DynamicDesiredState struct { 23 DesiredState DesiredState 24 BeforeCreateFunc func(desired runtime.Object) error 25 BeforeUpdateFunc func(current, desired runtime.Object) error 26 BeforeDeleteFunc func(current runtime.Object) error 27 CreateOptions []runtimeClient.CreateOption 28 UpdateOptions []runtimeClient.UpdateOption 29 DeleteOptions []runtimeClient.DeleteOption 30 ShouldCreateFunc func(desired runtime.Object) (bool, error) 31 ShouldUpdateFunc func(current, desired runtime.Object) (bool, error) 32 ShouldDeleteFunc func(desired runtime.Object) (bool, error) 33 } 34 35 func (s DynamicDesiredState) GetDesiredState() DesiredState { 36 return s.DesiredState 37 } 38 39 func (s DynamicDesiredState) ShouldCreate(desired runtime.Object) (bool, error) { 40 if s.ShouldCreateFunc != nil { 41 return s.ShouldCreateFunc(desired) 42 } 43 44 return true, nil 45 } 46 47 func (s DynamicDesiredState) ShouldUpdate(current, desired runtime.Object) (bool, error) { 48 if s.ShouldUpdateFunc != nil { 49 return s.ShouldUpdateFunc(current, desired) 50 } 51 52 return true, nil 53 } 54 55 func (s DynamicDesiredState) ShouldDelete(desired runtime.Object) (bool, error) { 56 if s.ShouldDeleteFunc != nil { 57 return s.ShouldDeleteFunc(desired) 58 } 59 60 return true, nil 61 } 62 63 func (s DynamicDesiredState) BeforeCreate(desired runtime.Object) error { 64 if s.BeforeCreateFunc != nil { 65 return s.BeforeCreateFunc(desired) 66 } 67 68 return nil 69 } 70 71 func (s DynamicDesiredState) BeforeUpdate(current, desired runtime.Object) error { 72 if s.BeforeUpdateFunc != nil { 73 return s.BeforeUpdateFunc(current, desired) 74 } 75 76 return nil 77 } 78 79 func (s DynamicDesiredState) BeforeDelete(current runtime.Object) error { 80 if s.BeforeDeleteFunc != nil { 81 return s.BeforeDeleteFunc(current) 82 } 83 84 return nil 85 } 86 87 func (s DynamicDesiredState) GetCreateptions() []runtimeClient.CreateOption { 88 return s.CreateOptions 89 } 90 91 func (s DynamicDesiredState) GetUpdateOptions() []runtimeClient.UpdateOption { 92 return s.UpdateOptions 93 } 94 95 func (s DynamicDesiredState) GetDeleteOptions() []runtimeClient.DeleteOption { 96 return s.DeleteOptions 97 } 98 99 type MultipleDesiredStates []DesiredState 100 101 func (s MultipleDesiredStates) GetDesiredState() DesiredState { 102 ds := s[len(s)-1] 103 if ds, ok := ds.(DesiredStateWithStaticState); ok { 104 return ds.DesiredState() 105 } 106 if s, ok := ds.(interface { 107 GetDesiredState() DesiredState 108 }); ok { 109 return s.GetDesiredState() 110 } 111 112 return ds 113 } 114 115 func (s MultipleDesiredStates) BeforeCreate(desired runtime.Object) error { 116 for _, ds := range s { 117 err := ds.BeforeCreate(desired) 118 if err != nil { 119 return err 120 } 121 } 122 123 return nil 124 } 125 126 func (s MultipleDesiredStates) BeforeUpdate(current, desired runtime.Object) error { 127 for _, ds := range s { 128 err := ds.BeforeUpdate(current, desired) 129 if err != nil { 130 return err 131 } 132 } 133 134 return nil 135 } 136 137 func (s MultipleDesiredStates) BeforeDelete(desired runtime.Object) error { 138 for _, ds := range s { 139 err := ds.BeforeDelete(desired) 140 if err != nil { 141 return err 142 } 143 } 144 145 return nil 146 } 147 148 func (s MultipleDesiredStates) GetCreateptions() []runtimeClient.CreateOption { 149 var createOptions []runtimeClient.CreateOption 150 for _, ds := range s { 151 if ds, ok := ds.(DesiredStateWithCreateOptions); ok { 152 createOptions = append(createOptions, ds.GetCreateOptions()...) 153 } 154 } 155 156 return createOptions 157 } 158 159 func (s MultipleDesiredStates) GetUpdateOptions() []runtimeClient.UpdateOption { 160 var updateOptions []runtimeClient.UpdateOption 161 for _, ds := range s { 162 if ds, ok := ds.(DesiredStateWithUpdateOptions); ok { 163 updateOptions = append(updateOptions, ds.GetUpdateOptions()...) 164 } 165 } 166 167 return updateOptions 168 } 169 170 func (s MultipleDesiredStates) GetDeleteOptions() []runtimeClient.DeleteOption { 171 var deleteOptions []runtimeClient.DeleteOption 172 for _, ds := range s { 173 if ds, ok := ds.(DesiredStateWithDeleteOptions); ok { 174 deleteOptions = append(deleteOptions, ds.GetDeleteOptions()...) 175 } 176 } 177 178 return deleteOptions 179 } 180 181 func (s MultipleDesiredStates) ShouldCreate(desired runtime.Object) (bool, error) { 182 for _, ds := range s { 183 if s, ok := ds.(DesiredStateShouldCreate); ok { 184 should, err := s.ShouldCreate(desired) 185 if err != nil { 186 return should, err 187 } 188 if !should { 189 return should, nil 190 } 191 } 192 } 193 194 return true, nil 195 } 196 197 func (s MultipleDesiredStates) ShouldUpdate(current, desired runtime.Object) (bool, error) { 198 for _, ds := range s { 199 if s, ok := ds.(DesiredStateShouldUpdate); ok { 200 should, err := s.ShouldUpdate(current, desired) 201 if err != nil { 202 return should, err 203 } 204 if !should { 205 return should, nil 206 } 207 } 208 } 209 210 return true, nil 211 } 212 213 func (s MultipleDesiredStates) ShouldDelete(desired runtime.Object) (bool, error) { 214 for _, ds := range s { 215 if s, ok := ds.(DesiredStateShouldDelete); ok { 216 should, err := s.ShouldDelete(desired) 217 if err != nil { 218 return should, err 219 } 220 if !should { 221 return should, nil 222 } 223 } 224 } 225 226 return true, nil 227 }