Repository: istio/istio
File: pilot/pkg/config/kube/file/controller_test.go (Go)

Code before:
package file

import (
	"fmt"
	"path/filepath"
	"testing"
	"time"

	networking "istio.io/api/networking/v1alpha3"
	"istio.io/istio/pilot/pkg/model"
	kubecontroller "istio.io/istio/pilot/pkg/serviceregistry/kube/controller"
	"istio.io/istio/pkg/config"
	"istio.io/istio/pkg/config/schema/collections"
	"istio.io/istio/pkg/config/schema/gvk"
	"istio.io/istio/pkg/config/schema/kind"
	"istio.io/istio/pkg/kube/krt"
	"istio.io/istio/pkg/test"
	"istio.io/istio/pkg/test/util/assert"
	"istio.io/istio/pkg/test/util/file"
	"istio.io/istio/pkg/test/util/retry"
)

// fileEventOpts uses a longer timeout than the default 1s for the event tracker,
// since file watcher events pass through fsnotify debouncing (50ms) and multiple
// levels of async krt event processing before reaching the handler.
var fileEventOpts = []retry.Option{retry.Timeout(5 * time.Second), retry.BackoffDelay(time.Millisecond)}

func TestController(t *testing.T) {
	stop := test.NewStop(t)
	root := t.TempDir()

	controller, err := NewController(root, "example.com", collections.Pilot, kubecontroller.Options{
		KrtDebugger: krt.GlobalDebugHandler,
	})
	assert.NoError(t, err)
	go controller.Run(stop)
	tt := assert.NewTracker[string](t, fileEventOpts...)
	controller.RegisterEventHandler(gvk.Gateway, TrackerHandler(tt, kind.Gateway))
	controller.RegisterEventHandler(gvk.VirtualService, TrackerHandler(tt, kind.VirtualService))

	file.WriteOrFail(t, filepath.Join(root, "gw.yaml"), []byte(`
apiVersion: networking.istio.io/v1
kind: Gateway
metadata:
  name: foo
  namespace: default
spec:
  servers:
  - port:
      number: 80
      protocol: HTTP2

Review comment (performance):
Given the slowness of this file events we're trying to handle, should our backoff be less aggressive too? maybe 50-100ms or something?

Code after:
package file

import (
	"fmt"
	"path/filepath"
	"testing"
	"time"

	networking "istio.io/api/networking/v1alpha3"
	"istio.io/istio/pilot/pkg/model"
	kubecontroller "istio.io/istio/pilot/pkg/serviceregistry/kube/controller"
	"istio.io/istio/pkg/config"
	"istio.io/istio/pkg/config/schema/collections"
	"istio.io/istio/pkg/config/schema/gvk"
	"istio.io/istio/pkg/config/schema/kind"
	"istio.io/istio/pkg/kube/krt"
	"istio.io/istio/pkg/test"
	"istio.io/istio/pkg/test/util/assert"
	"istio.io/istio/pkg/test/util/file"
	"istio.io/istio/pkg/test/util/retry"
)

// fileEventOpts uses a longer timeout than the default 1s for the event tracker,
// since file watcher events pass through fsnotify debouncing (50ms) and multiple
// levels of async krt event processing before reaching the handler.
var fileEventOpts = []retry.Option{retry.Timeout(5 * time.Second), retry.BackoffDelay(50 * time.Millisecond)}

func TestController(t *testing.T) {
	stop := test.NewStop(t)
	root := t.TempDir()

	controller, err := NewController(root, "example.com", collections.Pilot, kubecontroller.Options{
		KrtDebugger: krt.GlobalDebugHandler,
	})
	assert.NoError(t, err)
	go controller.Run(stop)
	tt := assert.NewTracker[string](t, fileEventOpts...)
	controller.RegisterEventHandler(gvk.Gateway, TrackerHandler(tt, kind.Gateway))
	controller.RegisterEventHandler(gvk.VirtualService, TrackerHandler(tt, kind.VirtualService))

	file.WriteOrFail(t, filepath.Join(root, "gw.yaml"), []byte(`
apiVersion: networking.istio.io/v1
kind: Gateway
metadata:
  name: foo
  namespace: default
spec:
  servers:
  - port:
      number: 80
      protocol: HTTP2