github.com/oam-dev/cluster-gateway@v1.9.0/pkg/apis/cluster/v1alpha1/clustergateway_health.go (about)

     1  package v1alpha1
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"strconv"
     7  
     8  	"github.com/oam-dev/cluster-gateway/pkg/config"
     9  	"github.com/oam-dev/cluster-gateway/pkg/util/singleton"
    10  
    11  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    12  	"k8s.io/apimachinery/pkg/runtime"
    13  	"k8s.io/apiserver/pkg/registry/rest"
    14  	"sigs.k8s.io/apiserver-runtime/pkg/builder/resource"
    15  	contextutil "sigs.k8s.io/apiserver-runtime/pkg/util/context"
    16  )
    17  
    18  var _ resource.ArbitrarySubResource = &ClusterGatewayHealth{}
    19  var _ rest.Getter = &ClusterGatewayHealth{}
    20  var _ rest.Updater = &ClusterGatewayHealth{}
    21  
    22  type ClusterGatewayHealth ClusterGateway
    23  
    24  func (in *ClusterGatewayHealth) New() runtime.Object {
    25  	return &ClusterGateway{}
    26  }
    27  
    28  func (in *ClusterGatewayHealth) SubResourceName() string {
    29  	return "health"
    30  }
    31  
    32  func (in *ClusterGatewayHealth) Destroy() {}
    33  
    34  func (in *ClusterGatewayHealth) Get(ctx context.Context, name string, options *metav1.GetOptions) (runtime.Object, error) {
    35  	parentStorage, ok := contextutil.GetParentStorageGetter(ctx)
    36  	if !ok {
    37  		return nil, fmt.Errorf("no parent storage found")
    38  	}
    39  	parentObj, err := parentStorage.Get(ctx, name, options)
    40  	if err != nil {
    41  		return nil, fmt.Errorf("no such cluster %v", name)
    42  	}
    43  	clusterGateway := parentObj.(*ClusterGateway)
    44  	return clusterGateway, nil
    45  }
    46  
    47  func (in *ClusterGatewayHealth) Update(ctx context.Context, name string, objInfo rest.UpdatedObjectInfo, createValidation rest.ValidateObjectFunc, updateValidation rest.ValidateObjectUpdateFunc, forceAllowCreate bool, options *metav1.UpdateOptions) (runtime.Object, bool, error) {
    48  	if singleton.GetSecretControl() == nil {
    49  		return nil, false, fmt.Errorf("loopback clients are not inited")
    50  	}
    51  
    52  	latestSecret, err := singleton.GetSecretControl().Get(ctx, name)
    53  	if err != nil {
    54  		return nil, false, err
    55  	}
    56  	updating, err := objInfo.UpdatedObject(ctx, nil)
    57  	if err != nil {
    58  		return nil, false, err
    59  	}
    60  	updatingClusterGateway := updating.(*ClusterGateway)
    61  	if latestSecret.Annotations == nil {
    62  		latestSecret.Annotations = make(map[string]string)
    63  	}
    64  	latestSecret.Annotations[AnnotationKeyClusterGatewayStatusHealthy] = strconv.FormatBool(updatingClusterGateway.Status.Healthy)
    65  	latestSecret.Annotations[AnnotationKeyClusterGatewayStatusHealthyReason] = string(updatingClusterGateway.Status.HealthyReason)
    66  	updated, err := singleton.GetKubeClient().
    67  		CoreV1().
    68  		Secrets(config.SecretNamespace).
    69  		Update(ctx, latestSecret, metav1.UpdateOptions{})
    70  	if err != nil {
    71  		return nil, false, err
    72  	}
    73  	clusterGateway, err := convertFromSecret(updated)
    74  	if err != nil {
    75  		return nil, false, err
    76  	}
    77  	return clusterGateway, false, nil
    78  }