mirror of
https://github.com/agent-substrate/substrate.git
synced 2026-10-02 03:24:42 +08:00
Fixes #1001 `TestK8sResolverEndpointSliceUpdates` intermittently fails at `resolver_test.go:215` — 8 of the 30 most recent failed `pr-workflow` runs (~27%): ``` updated state.Addresses = [{Addr: "10.0.0.1:443", ServerName: "", }], want [{Addr: "10.0.0.1:443", ServerName: "", } {Addr: "10.0.0.2:443", ServerName: "", }] ``` `Build` starts a goroutine that calls `updateState` once `WaitForCacheSync` returns. That report is deliberate: a service with no EndpointSlices never fires `AddFunc`, so without it the resolver would stay silent instead of telling gRPC the answer is an empty set. But `WaitForCacheSync` polls at `syncedPollPeriod = 100ms`, so it fires roughly 100ms after Build — and if the test has not yet created the second slice by then, that update still carries only `10.0.0.1` and sits in the channel ahead of the real one. The second `select` took whatever came next, so it asserted against the stale update. Locally Build-to-Create is 0.6ms, well ahead of the timer, which is why this only shows up on loaded runners. The resolver is not at fault — it promises eventual convergence, not that the first update after a change is final, and a duplicate update costs gRPC nothing. So both waits now go through one `waitForAddrs` helper that consumes updates until the set matches, with a timeout so a genuinely broken resolver still fails and reports the last set it saw. Note this changes the first wait as well: it asserted the *first* update equals `[10.0.0.1]`, and now waits for that set instead. The same argument applies there — nothing promises the first update is final. ## Verification `-count=N` proves nothing here: the unfixed test passes locally at any count because the window is never hit. A/B with the Build→Create delay as the only variable: | delay | old assertion | new assertion | |---|---|---| | 0ms | 5/5 pass | 5/5 pass | | 150ms | **0/5 pass** | **5/5 pass** | Measured timeline with the 150ms stall in place: ``` [ 0.5ms] update #1: [10.0.0.1] <- AddFunc for slice1 first select takes it ... 150ms stall ... [101.1ms] update #2: [10.0.0.1] <- the WaitForCacheSync goroutine test creates slice2 [151.4ms] update #3: [10.0.0.1, 10.0.0.2] ``` The probe tests used for this are not included. Rebased over #1013. That fixes a different bug — concurrent `updateState` calls letting an older address set win — and does not close this one: the two updates here are ~100ms apart, so the queue has nothing to coalesce. Re-measured on top of it, unchanged: at a 150ms delay the old assertion is 0/5 and the new one 5/5.
258 lines
6.4 KiB
Go
258 lines
6.4 KiB
Go
// Copyright 2026 Google LLC
|
|
//
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
// you may not use this file except in compliance with the License.
|
|
// You may obtain a copy of the License at
|
|
//
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
//
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
// See the License for the specific language governing permissions and
|
|
// limitations under the License.
|
|
|
|
package k8sresolver
|
|
|
|
import (
|
|
"context"
|
|
"net/url"
|
|
"reflect"
|
|
"testing"
|
|
"time"
|
|
|
|
"google.golang.org/grpc/resolver"
|
|
"google.golang.org/grpc/serviceconfig"
|
|
discoveryv1 "k8s.io/api/discovery/v1"
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
|
"k8s.io/client-go/kubernetes/fake"
|
|
)
|
|
|
|
type testClientConn struct {
|
|
resolver.ClientConn
|
|
stateChan chan resolver.State
|
|
errChan chan error
|
|
}
|
|
|
|
func newTestClientConn() *testClientConn {
|
|
return &testClientConn{
|
|
stateChan: make(chan resolver.State, 10),
|
|
errChan: make(chan error, 10),
|
|
}
|
|
}
|
|
|
|
func (c *testClientConn) UpdateState(state resolver.State) error {
|
|
c.stateChan <- state
|
|
return nil
|
|
}
|
|
|
|
func (c *testClientConn) ReportError(err error) {
|
|
c.errChan <- err
|
|
}
|
|
|
|
func (c *testClientConn) ParseServiceConfig(serviceConfigJSON string) *serviceconfig.ParseResult {
|
|
return nil
|
|
}
|
|
|
|
// waitForAddrs consumes updates until the set matches want: the resolver promises
|
|
// eventual convergence, not that the first update after a change is final.
|
|
func waitForAddrs(t *testing.T, cc *testClientConn, want []resolver.Address) {
|
|
t.Helper()
|
|
deadline := time.After(2 * time.Second)
|
|
var last []resolver.Address
|
|
for {
|
|
select {
|
|
case state := <-cc.stateChan:
|
|
last = state.Addresses
|
|
if reflect.DeepEqual(last, want) {
|
|
return
|
|
}
|
|
case <-deadline:
|
|
t.Fatalf("state.Addresses never converged: last = %v, want %v", last, want)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestParseTarget(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
target resolver.Target
|
|
wantNamespace string
|
|
wantService string
|
|
wantPort string
|
|
wantErr bool
|
|
}{
|
|
{
|
|
name: "slash format namespace/service:port",
|
|
target: resolver.Target{
|
|
URL: url.URL{Scheme: "k8s", Path: "/ate-system/api:443"},
|
|
},
|
|
wantNamespace: "ate-system",
|
|
wantService: "api",
|
|
wantPort: "443",
|
|
},
|
|
{
|
|
name: "host format host=ate-system path=/api:443",
|
|
target: resolver.Target{
|
|
URL: url.URL{Scheme: "k8s", Host: "ate-system", Path: "/api:443"},
|
|
},
|
|
wantNamespace: "ate-system",
|
|
wantService: "api",
|
|
wantPort: "443",
|
|
},
|
|
{
|
|
name: "fqdn format api.ate-system.svc:443",
|
|
target: resolver.Target{
|
|
URL: url.URL{Scheme: "k8s", Path: "/api.ate-system.svc:443"},
|
|
},
|
|
wantNamespace: "ate-system",
|
|
wantService: "api",
|
|
wantPort: "443",
|
|
},
|
|
{
|
|
name: "full cluster fqdn format api.ate-system.svc.cluster.local:443",
|
|
target: resolver.Target{
|
|
URL: url.URL{Scheme: "k8s", Path: "/api.ate-system.svc.cluster.local:443"},
|
|
},
|
|
wantNamespace: "ate-system",
|
|
wantService: "api",
|
|
wantPort: "443",
|
|
},
|
|
{
|
|
name: "simple service without port",
|
|
target: resolver.Target{
|
|
URL: url.URL{Scheme: "k8s", Path: "/api"},
|
|
},
|
|
wantNamespace: "default",
|
|
wantService: "api",
|
|
wantPort: "443",
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
gotNs, gotSvc, gotPort, err := ParseTarget(tt.target)
|
|
if (err != nil) != tt.wantErr {
|
|
t.Fatalf("ParseTarget() error = %v, wantErr %v", err, tt.wantErr)
|
|
}
|
|
if gotNs != tt.wantNamespace {
|
|
t.Errorf("ParseTarget() gotNs = %v, want %v", gotNs, tt.wantNamespace)
|
|
}
|
|
if gotSvc != tt.wantService {
|
|
t.Errorf("ParseTarget() gotSvc = %v, want %v", gotSvc, tt.wantService)
|
|
}
|
|
if gotPort != tt.wantPort {
|
|
t.Errorf("ParseTarget() gotPort = %v, want %v", gotPort, tt.wantPort)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestK8sResolverEndpointSliceUpdates(t *testing.T) {
|
|
readyTrue := true
|
|
port443 := int32(443)
|
|
|
|
slice1 := &discoveryv1.EndpointSlice{
|
|
ObjectMeta: metav1.ObjectMeta{
|
|
Name: "api-slice-1",
|
|
Namespace: "ate-system",
|
|
Labels: map[string]string{
|
|
"kubernetes.io/service-name": "api",
|
|
},
|
|
},
|
|
Ports: []discoveryv1.EndpointPort{
|
|
{Port: &port443},
|
|
},
|
|
Endpoints: []discoveryv1.Endpoint{
|
|
{
|
|
Addresses: []string{"10.0.0.1"},
|
|
Conditions: discoveryv1.EndpointConditions{
|
|
Ready: &readyTrue,
|
|
},
|
|
},
|
|
},
|
|
}
|
|
|
|
fakeClient := fake.NewSimpleClientset(slice1)
|
|
builder := NewBuilder(fakeClient)
|
|
cc := newTestClientConn()
|
|
|
|
target := resolver.Target{
|
|
URL: url.URL{Scheme: "k8s", Path: "/ate-system/api:443"},
|
|
}
|
|
|
|
res, err := builder.Build(target, cc, resolver.BuildOptions{})
|
|
if err != nil {
|
|
t.Fatalf("builder.Build() error = %v", err)
|
|
}
|
|
defer res.Close()
|
|
|
|
waitForAddrs(t, cc, []resolver.Address{{Addr: "10.0.0.1:443"}})
|
|
|
|
// Add a new EndpointSlice
|
|
slice2 := &discoveryv1.EndpointSlice{
|
|
ObjectMeta: metav1.ObjectMeta{
|
|
Name: "api-slice-2",
|
|
Namespace: "ate-system",
|
|
Labels: map[string]string{
|
|
"kubernetes.io/service-name": "api",
|
|
},
|
|
},
|
|
Ports: []discoveryv1.EndpointPort{
|
|
{Port: &port443},
|
|
},
|
|
Endpoints: []discoveryv1.Endpoint{
|
|
{
|
|
Addresses: []string{"10.0.0.2"},
|
|
Conditions: discoveryv1.EndpointConditions{
|
|
Ready: &readyTrue,
|
|
},
|
|
},
|
|
},
|
|
}
|
|
|
|
_, err = fakeClient.DiscoveryV1().EndpointSlices("ate-system").Create(context.Background(), slice2, metav1.CreateOptions{})
|
|
if err != nil {
|
|
t.Fatalf("failed to create slice2: %v", err)
|
|
}
|
|
|
|
waitForAddrs(t, cc, []resolver.Address{
|
|
{Addr: "10.0.0.1:443"},
|
|
{Addr: "10.0.0.2:443"},
|
|
})
|
|
}
|
|
|
|
func TestK8sResolverClose(t *testing.T) {
|
|
fakeClient := fake.NewSimpleClientset()
|
|
builder := NewBuilder(fakeClient)
|
|
cc := newTestClientConn()
|
|
|
|
target := resolver.Target{
|
|
URL: url.URL{Scheme: "k8s", Path: "/ate-system/api:443"},
|
|
}
|
|
|
|
res, err := builder.Build(target, cc, resolver.BuildOptions{})
|
|
if err != nil {
|
|
t.Fatalf("builder.Build() error = %v", err)
|
|
}
|
|
|
|
res.Close()
|
|
|
|
// Drain any initial state updates that happened before Close
|
|
for len(cc.stateChan) > 0 {
|
|
<-cc.stateChan
|
|
}
|
|
|
|
// Trigger a resolution after Close
|
|
res.ResolveNow(resolver.ResolveNowOptions{})
|
|
|
|
select {
|
|
case state := <-cc.stateChan:
|
|
t.Fatalf("unexpected state update after Close(): %v", state)
|
|
case err := <-cc.errChan:
|
|
t.Fatalf("unexpected error report after Close(): %v", err)
|
|
case <-time.After(100 * time.Millisecond):
|
|
// Expected: no calls to ClientConn after Close
|
|
}
|
|
}
|