github.com/banzaicloud/operator-tools@v0.28.10/pkg/wait/checks.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 wait
    16  
    17  import (
    18  	appsv1 "k8s.io/api/apps/v1"
    19  	apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
    20  	apiextensionsv1beta1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1beta1"
    21  	k8serrors "k8s.io/apimachinery/pkg/api/errors"
    22  	"k8s.io/apimachinery/pkg/api/meta"
    23  	"k8s.io/apimachinery/pkg/runtime"
    24  )
    25  
    26  type ResourceConditionCheck func(runtime.Object, error) bool
    27  type CustomResourceConditionCheck func() (bool, error)
    28  
    29  func ExistsConditionCheck(obj runtime.Object, k8serror error) bool {
    30  	return k8serror == nil
    31  }
    32  
    33  func NonExistsConditionCheck(obj runtime.Object, k8serror error) bool {
    34  	return k8serrors.IsNotFound(k8serror) || meta.IsNoMatchError(k8serror)
    35  }
    36  
    37  func CRDEstablishedConditionCheck(obj runtime.Object, k8serror error) bool {
    38  	var resource *apiextensionsv1beta1.CustomResourceDefinition
    39  	var ok bool
    40  	if resource, ok = obj.(*apiextensionsv1beta1.CustomResourceDefinition); ok {
    41  		for _, condition := range resource.Status.Conditions {
    42  			if condition.Type == apiextensionsv1beta1.Established {
    43  				if condition.Status == apiextensionsv1beta1.ConditionTrue {
    44  					return true
    45  				}
    46  			}
    47  		}
    48  		return false
    49  	}
    50  
    51  	var resourcev1 *apiextensionsv1.CustomResourceDefinition
    52  	if resourcev1, ok = obj.(*apiextensionsv1.CustomResourceDefinition); ok {
    53  		for _, condition := range resourcev1.Status.Conditions {
    54  			if condition.Type == apiextensionsv1.Established {
    55  				if condition.Status == apiextensionsv1.ConditionTrue {
    56  					return true
    57  				}
    58  			}
    59  		}
    60  		return false
    61  	}
    62  
    63  	return true
    64  }
    65  
    66  func ReadyReplicasConditionCheck(obj runtime.Object, k8serror error) bool {
    67  	switch o := obj.(type) {
    68  	case *appsv1.Deployment:
    69  		return o.Status.ReadyReplicas == o.Status.Replicas
    70  	case *appsv1.StatefulSet:
    71  		return o.Status.ReadyReplicas == o.Status.Replicas
    72  	case *appsv1.DaemonSet:
    73  		return o.Status.DesiredNumberScheduled == o.Status.NumberReady
    74  	default:
    75  		// return true for unconvertable objects
    76  		return true
    77  	}
    78  }