github.com/caos/orbos@v1.5.14-0.20221103111702-e6cd0cea7ad4/internal/operator/orbiter/kinds/providers/gce/healthchecks.go (about)

     1  package gce
     2  
     3  import (
     4  	"fmt"
     5  
     6  	uuid "github.com/satori/go.uuid"
     7  )
     8  
     9  var _ ensureLBFunc = queryHealthchecks
    10  
    11  func queryHealthchecks(context *context, loadbalancing []*normalizedLoadbalancer) ([]func() error, []func() error, error) {
    12  	gceHealthchecks, err := context.client.HttpHealthChecks.
    13  		List(context.projectID).
    14  		Filter(fmt.Sprintf(`description : "orb=%s;provider=%s*"`, context.orbID, context.providerID)).
    15  		Fields("items(description,name,port,requestPath,selfLink)").
    16  		Do()
    17  	if err != nil {
    18  		return nil, nil, err
    19  	}
    20  
    21  	var ensure []func() error
    22  
    23  createLoop:
    24  	for _, lb := range loadbalancing {
    25  
    26  		for _, gceHC := range gceHealthchecks.Items {
    27  			if gceHC.Description == lb.healthcheck.gce.Description {
    28  				lb.healthcheck.gce.Name = gceHC.Name
    29  				lb.healthcheck.gce.SelfLink = gceHC.SelfLink
    30  				if gceHC.Port != lb.healthcheck.gce.Port || gceHC.RequestPath != lb.healthcheck.gce.RequestPath {
    31  					ensure = append(ensure, operateFunc(
    32  						lb.healthcheck.log("Patching healthcheck", true),
    33  						computeOpCall(context.client.HttpHealthChecks.Patch(context.projectID, gceHC.Name, lb.healthcheck.gce).
    34  							RequestId(uuid.NewV1().String()).
    35  							Do),
    36  						toErrFunc(lb.healthcheck.log("Healthcheck patched", false)),
    37  					))
    38  				}
    39  
    40  				continue createLoop
    41  			}
    42  		}
    43  		lb.healthcheck.gce.Name = newName()
    44  		ensure = append(ensure, operateFunc(
    45  			lb.healthcheck.log("Creating healthcheck", true),
    46  			computeOpCall(context.client.HttpHealthChecks.
    47  				Insert(context.projectID, lb.healthcheck.gce).
    48  				RequestId(uuid.NewV1().String()).
    49  				Do),
    50  			func(hc *healthcheck) func() error {
    51  				return func() error {
    52  					newHC, newHCErr := context.client.HttpHealthChecks.Get(context.projectID, hc.gce.Name).
    53  						Fields("selfLink").
    54  						Do()
    55  					if newHCErr != nil {
    56  						return newHCErr
    57  					}
    58  
    59  					hc.gce.SelfLink = newHC.SelfLink
    60  					hc.log("Healthcheck created", false)()
    61  					return nil
    62  				}
    63  			}(lb.healthcheck),
    64  		))
    65  	}
    66  
    67  	var remove []func() error
    68  removeLoop:
    69  	for _, gceHC := range gceHealthchecks.Items {
    70  		for _, lb := range loadbalancing {
    71  			if gceHC.Description == lb.healthcheck.gce.Description {
    72  				continue removeLoop
    73  			}
    74  		}
    75  		remove = append(remove, removeResourceFunc(context.monitor, "healthcheck", gceHC.Name, context.client.HttpHealthChecks.
    76  			Delete(context.projectID, gceHC.Name).
    77  			RequestId(uuid.NewV1().String()).
    78  			Do))
    79  	}
    80  	return ensure, remove, nil
    81  }