Skip to content

Commit 9a105d9

Browse files
authored
Fix/default rev (#58123)
* fix * stop and test * wait for caches to sync * Fix tests * lint
1 parent 3f99a50 commit 9a105d9

File tree

6 files changed

+38
-16
lines changed

6 files changed

+38
-16
lines changed

istioctl/pkg/cli/context.go

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626
"istio.io/istio/istioctl/pkg/util/handlers"
2727
"istio.io/istio/pkg/cluster"
2828
"istio.io/istio/pkg/kube"
29+
"istio.io/istio/pkg/log"
2930
"istio.io/istio/pkg/maps"
3031
"istio.io/istio/pkg/ptr"
3132
"istio.io/istio/pkg/revisions"
@@ -215,21 +216,27 @@ func (i *instance) RevisionOrDefault(rev string) string {
215216

216217
// Try to get the default revision from the default watcher
217218
// We create a simple approach that doesn't cause circular dependencies
219+
220+
stop := make(chan struct{})
221+
defer close(stop)
218222
if i.defaultWatcher == nil {
219223
// Try to initialize the default watcher using a basic client
220224
if basicClient := i.getBasicClientForDefaultWatcher(); basicClient != nil {
221225
i.defaultWatcher = revisions.NewDefaultWatcher(basicClient, "")
226+
basicClient.RunAndWait(stop)
227+
go i.defaultWatcher.Run(stop)
228+
kube.WaitForCacheSync("default revision watcher", stop, i.defaultWatcher.HasSynced)
222229
}
223230
}
224231

225232
if i.defaultWatcher != nil {
226233
if defaultRev := i.defaultWatcher.GetDefault(); defaultRev != "" {
227234
return defaultRev
228235
}
236+
log.Warnf("default revision watcher not synced, falling back to \"default\"")
229237
}
230238

231-
// If no default revision found, return empty string (which means default behavior)
232-
return ""
239+
return "default"
233240
}
234241

235242
// getBasicClientForDefaultWatcher creates a basic kube client just for watching default revisions
@@ -318,7 +325,10 @@ func (f *fakeInstance) IstioNamespace() string {
318325
}
319326

320327
func (f *fakeInstance) RevisionOrDefault(rev string) string {
321-
// For fake instance, just return the revision as-is (no default resolution)
328+
// For fake instance, return "default" if empty (consistent with real implementation fallback)
329+
if rev == "" {
330+
return "default"
331+
}
322332
return rev
323333
}
324334

istioctl/pkg/cli/context_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,9 @@ func TestRevisionOrDefault(t *testing.T) {
6363
expectedResult: "test-revision",
6464
},
6565
{
66-
description: "return empty string when no revision provided and no default available",
66+
description: "return default when no revision provided and no default watcher available",
6767
inputRevision: "",
68-
expectedResult: "",
68+
expectedResult: "default",
6969
},
7070
}
7171

istioctl/pkg/cli/revision_or_default_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,9 @@ func TestRevisionOrDefaultWithMockDefaultRevision(t *testing.T) {
3030
expectedResult: "test-revision",
3131
},
3232
{
33-
description: "return empty string when no revision provided (fake context has no default detection)",
33+
description: "return default when no revision provided (fake context has no default detection)",
3434
inputRevision: "",
35-
expectedResult: "",
35+
expectedResult: "default",
3636
},
3737
}
3838

@@ -59,10 +59,10 @@ func TestRevisionOrDefaultCaching(t *testing.T) {
5959
t.Fatalf("expected stable-revision, got %v", result1)
6060
}
6161

62-
// Test that empty revisions return empty (since fake context has no default detection)
62+
// Test that empty revisions return "default" (since fake context has no default detection)
6363
result2 := ctx.RevisionOrDefault("")
64-
if result2 != "" {
65-
t.Fatalf("expected empty string, got %v", result2)
64+
if result2 != "default" {
65+
t.Fatalf("expected default, got %v", result2)
6666
}
6767

6868
// Test consistency

istioctl/pkg/internaldebug/internal-debug_test.go

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@ import (
4444
)
4545

4646
type execTestCase struct {
47-
args []string
47+
args []string
48+
// revision should default to "default" if not set
4849
revision string
4950
noIstiod bool
5051

@@ -83,6 +84,9 @@ func TestInternalDebug(t *testing.T) {
8384
})
8485

8586
for i, c := range cases {
87+
if c.revision == "" {
88+
c.revision = "default"
89+
}
8690
t.Run(fmt.Sprintf("case %d %s", i, strings.Join(c.args, " ")), func(t *testing.T) {
8791
ctx := cli.NewFakeContext(&cli.NewFakeContextOption{
8892
IstioNamespace: "istio-system",
@@ -192,6 +196,9 @@ func TestInternalDebugWithMultiIstiod(t *testing.T) {
192196
}
193197

194198
for _, c := range cases {
199+
if c.testcase.revision == "" {
200+
c.testcase.revision = "default"
201+
}
195202
t.Run(c.name, func(t *testing.T) {
196203
defer func() {
197204
getXdsResponseCall = 0
@@ -201,15 +208,16 @@ func TestInternalDebugWithMultiIstiod(t *testing.T) {
201208
ctx := cli.NewFakeContext(&cli.NewFakeContextOption{
202209
IstioNamespace: "istio-system",
203210
})
204-
client, err := ctx.CLIClient()
211+
client, err := ctx.CLIClientWithRevision(c.testcase.revision)
205212
require.NoError(t, err)
206213

207214
_, err = client.Kube().CoreV1().Pods("istio-system").Create(context.TODO(), &corev1.Pod{
208215
ObjectMeta: metav1.ObjectMeta{
209216
Name: "istiod-test-1",
210217
Namespace: "istio-system",
211218
Labels: map[string]string{
212-
"app": "istiod",
219+
"app": "istiod",
220+
label.IoIstioRev.Name: c.testcase.revision,
213221
},
214222
},
215223
Status: corev1.PodStatus{
@@ -223,7 +231,8 @@ func TestInternalDebugWithMultiIstiod(t *testing.T) {
223231
Name: "istiod-test-2",
224232
Namespace: "istio-system",
225233
Labels: map[string]string{
226-
"app": "istiod",
234+
"app": "istiod",
235+
label.IoIstioRev.Name: c.testcase.revision,
227236
},
228237
},
229238
Status: corev1.PodStatus{

istioctl/pkg/proxystatus/proxystatus_test.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ type execTestCase struct {
5858

5959
func TestProxyStatus(t *testing.T) {
6060
cases := []execTestCase{
61-
{ // case 0, with no Isitod instance
61+
{ // case 0, with no Istiod instance
6262
args: []string{},
6363
noIstiod: true,
6464
expectedOutput: "Error: no running Istio pods in \"istio-system\"\n",
@@ -106,6 +106,9 @@ func TestProxyStatus(t *testing.T) {
106106

107107
for i, c := range cases {
108108
t.Run(fmt.Sprintf("case %d %s", i, strings.Join(c.args, " ")), func(t *testing.T) {
109+
if c.revision == "" {
110+
c.revision = "default"
111+
}
109112
ctx := cli.NewFakeContext(&cli.NewFakeContextOption{
110113
IstioNamespace: "istio-system",
111114
})

istioctl/pkg/workload/workload_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -400,7 +400,7 @@ func runTestCmd(t *testing.T, createResourceFunc func(client kube.CLIClient), re
400400
})
401401
rootCmd := Cmd(ctx)
402402
rootCmd.SetArgs(args)
403-
client, err := ctx.CLIClientWithRevision(rev)
403+
client, err := ctx.CLIClientWithRevision(ctx.RevisionOrDefault(rev))
404404
if err != nil {
405405
return "", err
406406
}

0 commit comments

Comments
 (0)