Revision c2b8f48b5326d48a977bb770e3f6d83623610204 authored by Jonny on 27 May 2024, 12:59:44 UTC, committed by André Martins on 17 June 2024, 07:30:25 UTC
Previously, when a cluster ran with native-networking and had multiple
zones, it wasn't possible to enable auto direct routes. This caused a
bottleneck for same-zone traffic as it always had to be routed through
the gw.

With this new flag, any direct routes for nodes on different L2 networks
will be skipped. Cilium will add routes for nodes on the same L2 and not
exit.

Fixes: #31124
Signed-off-by: Jonny <jonny@linkpool.io>
1 parent 72ffe0f
Raw File
kube_proxy_healthz_test.go
// SPDX-License-Identifier: Apache-2.0
// Copyright Authors of Cilium

package cmd

import (
	"encoding/json"
	"net/http"
	"net/http/httptest"
	"testing"
	"time"

	"github.com/stretchr/testify/require"

	"github.com/cilium/cilium/api/v1/models"
)

type KubeProxyHealthzTestSuite struct{}

// Injected fake service.
type FakeService struct {
	injectedCurrentTs     time.Time
	injectedLastUpdatedTs time.Time
}

func (s *FakeService) GetCurrentTs() time.Time {
	return s.injectedCurrentTs
}

func (s *FakeService) GetLastUpdatedTs() time.Time {
	return s.injectedLastUpdatedTs
}

// Injected fake daemon.
type FakeDaemon struct {
	injectedStatusResponse models.StatusResponse
}

func (d *FakeDaemon) getStatus(blah bool) models.StatusResponse {
	return d.injectedStatusResponse
}

type healthzPayload struct {
	LastUpdated string
	CurrentTime string
}

func TestKubeProxyHealth(t *testing.T) {
	s := KubeProxyHealthzTestSuite{}
	s.healthTestHelper(t, models.StatusStateOk, http.StatusOK, true)
	s.healthTestHelper(t, models.StatusStateWarning,
		http.StatusServiceUnavailable, false)
	s.healthTestHelper(t, models.StatusStateFailure,
		http.StatusServiceUnavailable, false)
}

func (s *KubeProxyHealthzTestSuite) healthTestHelper(t *testing.T, ciliumStatus string,
	expectedHttpStatus int, testcasepositive bool) {
	var lastUpdateTs, currentTs, expectedTs time.Time
	lastUpdateTs = time.Unix(100, 0) // Fake 100 seconds after Unix.
	currentTs = time.Unix(200, 0)    // Fake 200 seconds after Unix.
	expectedTs = lastUpdateTs
	if testcasepositive {
		expectedTs = currentTs
	}
	// Create handler with injected behavior.
	h := kubeproxyHealthzHandler{
		d: &FakeDaemon{injectedStatusResponse: models.StatusResponse{
			Cilium: &models.Status{State: ciliumStatus}}},
		svc: &FakeService{
			injectedCurrentTs:     currentTs,
			injectedLastUpdatedTs: lastUpdateTs}}

	// Create a new request.
	req, err := http.NewRequest(http.MethodGet, "/healthz", nil)
	require.Nil(t, err)
	w := httptest.NewRecorder()

	// Serve.
	h.ServeHTTP(w, req)

	// Main return code meets expectations.
	require.Equalf(t, expectedHttpStatus, w.Code, "expected status code %v, got %v", expectedHttpStatus, w.Code)

	// Timestamps meet expectations.
	var payload healthzPayload
	require.Nil(t, json.Unmarshal(w.Body.Bytes(), &payload))
	layout := "2006-01-02 15:04:05 -0700 MST"
	lastUpdateTs, err = time.Parse(layout, payload.LastUpdated)
	require.Nil(t, err)

	_, err = time.Parse(layout, payload.CurrentTime)
	require.Nil(t, err)
	require.Equal(t, true, lastUpdateTs.Equal(expectedTs))
}
back to top