github.com/operator-framework/operator-lifecycle-manager@v0.30.0/pkg/lib/catalogsource/catalogsource_update.go (about)

     1  package catalogsource
     2  
     3  import (
     4  	"context"
     5  	"reflect"
     6  
     7  	"github.com/sirupsen/logrus"
     8  	"k8s.io/apimachinery/pkg/api/meta"
     9  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    10  
    11  	"github.com/operator-framework/api/pkg/operators/v1alpha1"
    12  	"github.com/operator-framework/operator-lifecycle-manager/pkg/api/client/clientset/versioned"
    13  )
    14  
    15  /*
    16  UpdateStatus can be used to update the status of the provided catalog source. Note that
    17  the caller is responsible for ensuring accurate status values in the catsrc argument (i.e.
    18  the status is used as-is).
    19  
    20  • logger: used to log errors only
    21  
    22  • client: used to fetch / update catalog source status
    23  
    24  • catsrc: the CatalogSource to use as a source for status updates. Callers are
    25  responsible for updating the catalog source status values as necessary.
    26  */
    27  func UpdateStatus(logger *logrus.Entry, client versioned.Interface, catsrc *v1alpha1.CatalogSource) error {
    28  
    29  	// make the status update if possible
    30  	if _, err := client.OperatorsV1alpha1().CatalogSources(catsrc.GetNamespace()).UpdateStatus(context.TODO(), catsrc, metav1.UpdateOptions{}); err != nil {
    31  		logger.WithError(err).Error("UpdateStatus - error while setting CatalogSource status")
    32  		return err
    33  	}
    34  
    35  	return nil
    36  }
    37  
    38  /*
    39  UpdateStatusWithConditions can be used to update the status conditions for the provided catalog source.
    40  This function will make no changes to the other status fields (those fields will be used as-is).
    41  If the provided conditions do not result in any status condition changes, then the API server will not be updated.
    42  Note that the caller is responsible for ensuring accurate status values for all other fields.
    43  
    44  • logger: used to log errors only
    45  
    46  • client: used to fetch / update catalog source status
    47  
    48  • catsrc: the CatalogSource to use as a source for status updates.
    49  
    50  • conditions: condition values to be updated
    51  */
    52  func UpdateStatusWithConditions(logger *logrus.Entry, client versioned.Interface, catsrc *v1alpha1.CatalogSource, conditions ...metav1.Condition) error {
    53  
    54  	// make a copy of the status before we make the change
    55  	statusBefore := catsrc.Status.DeepCopy()
    56  
    57  	// update the conditions
    58  	for _, condition := range conditions {
    59  		meta.SetStatusCondition(&catsrc.Status.Conditions, condition)
    60  	}
    61  
    62  	// don't bother updating if no changes were made
    63  	if reflect.DeepEqual(catsrc.Status.Conditions, statusBefore.Conditions) {
    64  		logger.Debug("UpdateStatusWithConditions - request to update status conditions did not result in any changes, so updates were not made")
    65  		return nil
    66  	}
    67  
    68  	// make the update if possible
    69  	if _, err := client.OperatorsV1alpha1().CatalogSources(catsrc.GetNamespace()).UpdateStatus(context.TODO(), catsrc, metav1.UpdateOptions{}); err != nil {
    70  		logger.WithError(err).Error("UpdateStatusWithConditions - unable to update CatalogSource image reference")
    71  		return err
    72  	}
    73  	return nil
    74  }
    75  
    76  /*
    77  UpdateSpecAndStatusConditions can be used to update the catalog source with the provided status conditions.
    78  This will update the spec and status portions of the catalog source. Calls to the API server will occur
    79  even if the provided conditions result in no changes.
    80  
    81  • logger: used to log errors only
    82  
    83  • client: used to fetch / update catalog source
    84  
    85  • catsrc: the CatalogSource to use as a source for image and status condition updates.
    86  
    87  • conditions: condition values to be updated
    88  */
    89  func UpdateSpecAndStatusConditions(logger *logrus.Entry, client versioned.Interface, catsrc *v1alpha1.CatalogSource, conditions ...metav1.Condition) error {
    90  
    91  	// update the conditions
    92  	for _, condition := range conditions {
    93  		meta.SetStatusCondition(&catsrc.Status.Conditions, condition)
    94  	}
    95  
    96  	// make the update if possible
    97  	if _, err := client.OperatorsV1alpha1().CatalogSources(catsrc.GetNamespace()).Update(context.TODO(), catsrc, metav1.UpdateOptions{}); err != nil {
    98  		logger.WithError(err).Error("UpdateSpecAndStatusConditions - unable to update CatalogSource image reference")
    99  		return err
   100  	}
   101  	return nil
   102  }
   103  
   104  /*
   105  RemoveStatusConditions can be used to remove the status conditions for the provided catalog source.
   106  This function will make no changes to the other status fields (those fields will be used as-is).
   107  If the provided conditions do not result in any status condition changes, then the API server will not be updated.
   108  Note that the caller is responsible for ensuring accurate status values for all other fields.
   109  
   110  • logger: used to log errors only
   111  
   112  • client: used to fetch / update catalog source status
   113  
   114  • catsrc: the CatalogSource to use as a source for status condition removal.
   115  
   116  • conditionTypes: condition types to be removed
   117  */
   118  func RemoveStatusConditions(logger *logrus.Entry, client versioned.Interface, catsrc *v1alpha1.CatalogSource, conditionTypes ...string) error {
   119  
   120  	// make a copy of the status before we make the change
   121  	statusBefore := catsrc.Status.DeepCopy()
   122  
   123  	// remove the conditions
   124  	for _, conditionType := range conditionTypes {
   125  		meta.RemoveStatusCondition(&catsrc.Status.Conditions, conditionType)
   126  	}
   127  
   128  	// don't bother updating if no changes were made
   129  	if reflect.DeepEqual(catsrc.Status.Conditions, statusBefore.Conditions) {
   130  		logger.Debug("RemoveStatusConditions - request to remove status conditions did not result in any changes, so updates were not made")
   131  		return nil
   132  	}
   133  
   134  	// make the update if possible
   135  	if _, err := client.OperatorsV1alpha1().CatalogSources(catsrc.GetNamespace()).UpdateStatus(context.TODO(), catsrc, metav1.UpdateOptions{}); err != nil {
   136  		logger.WithError(err).Error("RemoveStatusConditions - unable to update CatalogSource image reference")
   137  		return err
   138  	}
   139  	return nil
   140  }