github.com/banzaicloud/operator-tools@v0.28.10/pkg/reconciler/conditions.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 "time" 19 20 "github.com/go-logr/logr" 21 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 22 "k8s.io/apimachinery/pkg/runtime" 23 "k8s.io/apimachinery/pkg/runtime/schema" 24 "sigs.k8s.io/controller-runtime/pkg/client" 25 26 "github.com/banzaicloud/operator-tools/pkg/wait" 27 ) 28 29 var DefaultBackoff = wait.Backoff{ 30 Duration: time.Second * 5, 31 Factor: 1, 32 Jitter: 0, 33 Steps: 12, 34 } 35 36 type ConditionType string 37 38 type ObjectKeyWithGVK struct { 39 ObjectKey client.ObjectKey `json:"objectKey,omitempty"` 40 GVK schema.GroupVersionKind `json:"gvk,omitempty"` 41 } 42 43 type ResourceCondition struct { 44 ID string `json:"id,omitempty"` 45 Description string `json:"shortDescription,omitempty"` 46 Checks []wait.ResourceConditionCheck `json:"checks,omitempty"` 47 CustomChecks []wait.CustomResourceConditionCheck `json:"customChecks,omitempty"` 48 Object *ObjectKeyWithGVK `json:"object,omitempty"` 49 } 50 51 type ConditionChecker struct { 52 client client.Client 53 scheme *runtime.Scheme 54 log logr.Logger 55 } 56 57 func NewConditionChecker(client client.Client, scheme *runtime.Scheme, log logr.Logger) *ConditionChecker { 58 return &ConditionChecker{ 59 client: client, 60 scheme: scheme, 61 log: log, 62 } 63 } 64 65 func (c *ConditionChecker) CheckResourceConditions(conditions []ResourceCondition, backoff *wait.Backoff) error { 66 if backoff == nil { 67 backoff = &DefaultBackoff 68 } 69 70 log := c.log.WithName("pre-checks") 71 checks := wait.NewResourceConditionChecks(c.client, *backoff, log, c.scheme) 72 73 log.Info("checking resource pre-conditions") 74 75 for _, condition := range conditions { 76 if condition.Object != nil && len(condition.Checks) > 0 { 77 o, err := c.scheme.New(condition.Object.GVK) 78 if err != nil { 79 return err 80 } 81 if mo, ok := o.(metav1.Object); ok { 82 mo.SetName(condition.Object.ObjectKey.Name) 83 mo.SetNamespace(condition.Object.ObjectKey.Namespace) 84 } 85 err = checks.WaitForResources(condition.ID, []runtime.Object{o}, condition.Checks...) 86 if err != nil { 87 return err 88 } 89 } 90 91 if len(condition.CustomChecks) > 0 { 92 err := checks.WaitForCustomConditionChecks(condition.ID, condition.CustomChecks...) 93 if err != nil { 94 return err 95 } 96 } 97 } 98 99 return nil 100 }