github.com/nmstate/kubernetes-nmstate@v0.82.0/pkg/enactmentstatus/status.go (about)

     1  /*
     2  Copyright The Kubernetes NMState Authors.
     3  
     4  
     5  Licensed under the Apache License, Version 2.0 (the "License");
     6  you may not use this file except in compliance with the License.
     7  You may obtain a copy of the License at
     8  
     9      http://www.apache.org/licenses/LICENSE-2.0
    10  
    11  Unless required by applicable law or agreed to in writing, software
    12  distributed under the License is distributed on an "AS IS" BASIS,
    13  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14  See the License for the specific language governing permissions and
    15  limitations under the License.
    16  */
    17  
    18  package enactmentstatus
    19  
    20  import (
    21  	"context"
    22  	"fmt"
    23  
    24  	"github.com/pkg/errors"
    25  	logf "sigs.k8s.io/controller-runtime/pkg/log"
    26  
    27  	corev1 "k8s.io/api/core/v1"
    28  	"k8s.io/apimachinery/pkg/types"
    29  	"k8s.io/client-go/util/retry"
    30  	"sigs.k8s.io/controller-runtime/pkg/client"
    31  
    32  	nmstate "github.com/nmstate/kubernetes-nmstate/api/shared"
    33  	nmstatev1beta1 "github.com/nmstate/kubernetes-nmstate/api/v1beta1"
    34  )
    35  
    36  var (
    37  	log       = logf.Log.WithName("enactmentstatus")
    38  	allErrors = func(error) bool { return true }
    39  )
    40  
    41  func Update(cli client.Client, key types.NamespacedName, statusSetter func(*nmstate.NodeNetworkConfigurationEnactmentStatus)) error {
    42  	logger := log.WithValues("enactment", key.Name)
    43  
    44  	// Some network configuration can break api server connectivity temporally and that
    45  	// prevents the NNCE to final state so is forever at in progress makeing the NNCP also
    46  	// forever in progress too, this retry allow to overcome that issue.
    47  	return retry.OnError(retry.DefaultRetry, allErrors, func() error {
    48  		instance := &nmstatev1beta1.NodeNetworkConfigurationEnactment{}
    49  		err := cli.Get(context.TODO(), key, instance)
    50  		if err != nil {
    51  			return errors.Wrap(err, "getting enactment failed")
    52  		}
    53  
    54  		statusSetter(&instance.Status)
    55  
    56  		logger.Info(fmt.Sprintf("status: %+v", instance.Status))
    57  
    58  		return cli.Status().Update(context.TODO(), instance)
    59  	})
    60  }
    61  
    62  func IsProgressing(conditions *nmstate.ConditionList) bool {
    63  	progressingCondition := conditions.Find(nmstate.NodeNetworkConfigurationEnactmentConditionProgressing)
    64  	if progressingCondition != nil && progressingCondition.Status == corev1.ConditionTrue {
    65  		return true
    66  	}
    67  	return false
    68  }