istio.io/istio@v0.0.0-20240520182934-d79c90f27776/pilot/pkg/model/status/helper.go (about)

     1  // Copyright Istio Authors
     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 status
    16  
    17  import (
    18  	"istio.io/api/meta/v1alpha1"
    19  	"istio.io/istio/pkg/config"
    20  	"istio.io/istio/pkg/slices"
    21  )
    22  
    23  const (
    24  	StatusTrue  = "True"
    25  	StatusFalse = "False"
    26  )
    27  
    28  func GetConditionFromSpec(cfg config.Config, condition string) *v1alpha1.IstioCondition {
    29  	c, ok := cfg.Status.(*v1alpha1.IstioStatus)
    30  	if !ok {
    31  		return nil
    32  	}
    33  	return GetCondition(c.Conditions, condition)
    34  }
    35  
    36  func GetBoolConditionFromSpec(cfg config.Config, condition string, defaultValue bool) bool {
    37  	c, ok := cfg.Status.(*v1alpha1.IstioStatus)
    38  	if !ok {
    39  		return defaultValue
    40  	}
    41  	return GetBoolCondition(c.Conditions, condition, defaultValue)
    42  }
    43  
    44  func GetBoolCondition(conditions []*v1alpha1.IstioCondition, condition string, defaultValue bool) bool {
    45  	got := GetCondition(conditions, condition)
    46  	if got == nil {
    47  		return defaultValue
    48  	}
    49  	if got.Status == StatusTrue {
    50  		return true
    51  	}
    52  	if got.Status == StatusFalse {
    53  		return false
    54  	}
    55  	return defaultValue
    56  }
    57  
    58  func GetCondition(conditions []*v1alpha1.IstioCondition, condition string) *v1alpha1.IstioCondition {
    59  	for _, cond := range conditions {
    60  		if cond.Type == condition {
    61  			return cond
    62  		}
    63  	}
    64  	return nil
    65  }
    66  
    67  func UpdateConfigCondition(cfg config.Config, condition *v1alpha1.IstioCondition) config.Config {
    68  	cfg = cfg.DeepCopy()
    69  	var status *v1alpha1.IstioStatus
    70  	if cfg.Status == nil {
    71  		cfg.Status = &v1alpha1.IstioStatus{}
    72  	}
    73  	status = cfg.Status.(*v1alpha1.IstioStatus)
    74  	status.Conditions = updateCondition(status.Conditions, condition)
    75  	return cfg
    76  }
    77  
    78  func updateCondition(conditions []*v1alpha1.IstioCondition, condition *v1alpha1.IstioCondition) []*v1alpha1.IstioCondition {
    79  	for i, cond := range conditions {
    80  		if cond.Type == condition.Type {
    81  			conditions[i] = condition
    82  			return conditions
    83  		}
    84  	}
    85  
    86  	return append(conditions, condition)
    87  }
    88  
    89  func DeleteConfigCondition(cfg config.Config, condition string) config.Config {
    90  	c, ok := cfg.Status.(*v1alpha1.IstioStatus)
    91  	if !ok {
    92  		return cfg
    93  	}
    94  	if GetCondition(c.Conditions, condition) == nil {
    95  		return cfg
    96  	}
    97  	cfg = cfg.DeepCopy()
    98  	status := cfg.Status.(*v1alpha1.IstioStatus)
    99  	status.Conditions = deleteCondition(status.Conditions, condition)
   100  	return cfg
   101  }
   102  
   103  func deleteCondition(conditions []*v1alpha1.IstioCondition, condition string) []*v1alpha1.IstioCondition {
   104  	conditions = slices.FilterInPlace(conditions, func(c *v1alpha1.IstioCondition) bool {
   105  		return c.Type != condition
   106  	})
   107  
   108  	return conditions
   109  }