Skip to content

Commit

Permalink
Jsonify description of healthchecks from THC
Browse files Browse the repository at this point in the history
  • Loading branch information
DamianSawicki committed May 4, 2023
1 parent 2bf2aa0 commit 878e6d7
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 14 deletions.
10 changes: 3 additions & 7 deletions pkg/healthchecks/healthchecks.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ func (h *HealthChecks) new(sp utils.ServicePort) *translator.HealthCheck {
}
hc.Name = sp.BackendName()
hc.Service = h.getService(sp)
hc.SetHealthcheckInfo(h.generateHealthcheckInfo(sp, hc.ForILB))
hc.SetHealthcheckInfo(h.generateClusterServiceInfo(sp, hc.ForILB))
return hc
}

Expand All @@ -114,17 +114,13 @@ func (h *HealthChecks) mainService(sp utils.ServicePort) types.NamespacedName {
return service
}

func (h *HealthChecks) generateHealthcheckInfo(sp utils.ServicePort, iLB bool) healthcheck.HealthcheckInfo {
func (h *HealthChecks) generateClusterServiceInfo(sp utils.ServicePort, iLB bool) (healthcheck.ClusterInfo, healthcheck.ServiceInfo) {
serviceInfo := healthcheck.ServiceInfo(h.defaultBackendSvc)
if sp.ID.Service.Name != "" {
serviceInfo = healthcheck.ServiceInfo(sp.ID.Service)
}

return healthcheck.HealthcheckInfo{
ClusterInfo: h.clusterInfo,
ServiceInfo: serviceInfo,
HealthcheckConfig: healthcheck.DefaultHC,
}
return h.clusterInfo, serviceInfo
}

// SyncServicePort implements HealthChecker.
Expand Down
31 changes: 28 additions & 3 deletions pkg/healthchecks/healthchecks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1021,7 +1021,7 @@ func (*syncSPFixture) thc() *compute.HealthCheck {
Port: 7877,
RequestPath: "/api/podhealth",
},
Description: translator.DescriptionForTransparentHealthChecks, //TODO(DamianSawicki): JSONify.
Description: translator.DescriptionForTransparentHealthChecks,
}
}

Expand Down Expand Up @@ -1202,6 +1202,18 @@ func TestSyncServicePort(t *testing.T) {
wantComputeHC: chc,
})

// Transparent Health Check (ILB), regional cluster
chc = fixture.thc()
toIlb(chc)
cases = append(cases, &tc{
desc: "create thc ilb regional cluster",
sp: testSPs["HTTP-80-ilb-nil-thc"],
enableTHC: true,
regional: true,
regionalCluster: true,
wantComputeHC: chc,
})

// Probe ignored with THC
chc = fixture.thc()
cases = append(cases, &tc{
Expand Down Expand Up @@ -1435,6 +1447,15 @@ func TestSyncServicePort(t *testing.T) {
wantComputeHC: w,
enableTHC: true,
})
cases = append(cases, &tc{
desc: "update ilb to thc regional cluster",
setup: fixture.setupExistingHCFunc(fixture.ilb()),
sp: testSPs["HTTP-80-ilb-nil-thc"],
regional: true,
regionalCluster: true,
wantComputeHC: w,
enableTHC: true,
})

// Preserve user settings.
chc = fixture.hc()
Expand Down Expand Up @@ -1573,7 +1594,11 @@ func TestSyncServicePort(t *testing.T) {
tc.updateHCDescription = true
tc.desc = tc.desc + " with updateHCDescription"
copyOfWant := *tc.wantComputeHC
if tc.sp.BackendConfig != nil {
if tc.sp.BackendConfig != nil || tc.sp.THCEnabled == true {
config := healthcheck.TransparentHC
if tc.sp.BackendConfig != nil {
config = healthcheck.BackendConfigHC
}
var wantLocation string
if tc.regionalCluster {
wantLocation = gce.DefaultTestClusterValues().Region
Expand All @@ -1583,7 +1608,7 @@ func TestSyncServicePort(t *testing.T) {
wantDesc := healthcheck.HealthcheckDesc{
K8sCluster: fmt.Sprintf("/locations/%s/clusters/%s", wantLocation, gce.DefaultTestClusterValues().ClusterName),
K8sResource: fmt.Sprintf("/namespaces/%s/services/%s", defaultBackendSvc.Namespace, defaultBackendSvc.Name),
Config: "BackendConfig",
Config: config,
}
bytes, err := json.MarshalIndent(wantDesc, "", " ")
if err != nil {
Expand Down
11 changes: 8 additions & 3 deletions pkg/translator/healthchecks.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"time"

"github.com/GoogleCloudPlatform/k8s-cloud-provider/pkg/cloud/meta"
"golang.org/x/exp/slices"
computealpha "google.golang.org/api/compute/v0.alpha"
computebeta "google.golang.org/api/compute/v0.beta"
"google.golang.org/api/compute/v1"
Expand Down Expand Up @@ -106,13 +107,15 @@ type HealthCheck struct {
healthcheckInfo healthcheck.HealthcheckInfo
}

func (hc *HealthCheck) SetHealthcheckInfo(i healthcheck.HealthcheckInfo) {
hc.healthcheckInfo = i
func (hc *HealthCheck) SetHealthcheckInfo(ci healthcheck.ClusterInfo, si healthcheck.ServiceInfo) {
hc.healthcheckInfo.ClusterInfo = ci
hc.healthcheckInfo.ServiceInfo = si
hc.reconcileHCDescription()
}

func (hc *HealthCheck) reconcileHCDescription() {
if flags.F.EnableUpdateCustomHealthCheckDescription && hc.healthcheckInfo.HealthcheckConfig == healthcheck.BackendConfigHC {
if flags.F.EnableUpdateCustomHealthCheckDescription &&
slices.Contains([]healthcheck.HealthcheckConfig{healthcheck.BackendConfigHC, healthcheck.TransparentHC}, hc.healthcheckInfo.HealthcheckConfig) {
hc.Description = hc.healthcheckInfo.GenerateHealthcheckDescription()
}
}
Expand Down Expand Up @@ -236,6 +239,8 @@ func OverwriteWithTHC(hc *HealthCheck) {
hc.PortSpecification = thcPortSpecification
hc.Port = THCPort
hc.RequestPath = thcRequestPath
hc.healthcheckInfo.HealthcheckConfig = healthcheck.TransparentHC
hc.reconcileHCDescription()
}

func (hc *HealthCheck) UpdateFromBackendConfig(c *backendconfigv1.HealthCheckConfig) {
Expand Down
9 changes: 8 additions & 1 deletion pkg/translator/healthchecks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"github.com/kr/pretty"
computealpha "google.golang.org/api/compute/v0.alpha"
"k8s.io/ingress-gce/pkg/annotations"
"k8s.io/ingress-gce/pkg/utils/healthcheck"
)

func TestMerge(t *testing.T) {
Expand Down Expand Up @@ -89,6 +90,7 @@ func TestMerge(t *testing.T) {

func TestOverwriteWithTHC(t *testing.T) {
wantHC := &HealthCheck{
ForNEG: true,
HealthCheck: computealpha.HealthCheck{
CheckIntervalSec: 5,
TimeoutSec: 5,
Expand All @@ -102,9 +104,14 @@ func TestOverwriteWithTHC(t *testing.T) {
PortSpecification: "USE_FIXED_PORT",
RequestPath: "/api/podhealth",
},
healthcheckInfo: healthcheck.HealthcheckInfo{
HealthcheckConfig: healthcheck.TransparentHC,
},
}

hc := &HealthCheck{}
hc := &HealthCheck{
ForNEG: true,
}
OverwriteWithTHC(hc)
if !reflect.DeepEqual(hc, wantHC) {
t.Fatalf("Translate healthcheck is:\n%s, want:\n%s", pretty.Sprint(hc), pretty.Sprint(wantHC))
Expand Down
1 change: 1 addition & 0 deletions pkg/utils/healthcheck/healthcheckdesc.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ const (
DefaultHC HealthcheckConfig = "Default"
ReadinessProbeHC HealthcheckConfig = "ReadinessProbe"
BackendConfigHC HealthcheckConfig = "BackendConfig"
TransparentHC HealthcheckConfig = "TransparentHC"
)

type HealthcheckInfo struct {
Expand Down

0 comments on commit 878e6d7

Please sign in to comment.