Skip to content

Commit cf0b325

Browse files
committed
fix: harden v2 controller initialization
Signed-off-by: Eitan Yarmush <[email protected]>
1 parent 06f81be commit cf0b325

4 files changed

Lines changed: 40 additions & 3 deletions

File tree

‎go/core/cmd/controller-v2/main.go‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -153,8 +153,11 @@ func envBool(name string) bool {
153153
}
154154

155155
func namespaces(value string) []string {
156-
if value == "" {
157-
return nil
156+
var result []string
157+
for _, namespace := range strings.Split(value, ",") {
158+
if namespace = strings.TrimSpace(namespace); namespace != "" {
159+
result = append(result, namespace)
160+
}
158161
}
159-
return strings.Split(value, ",")
162+
return result
160163
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package main
2+
3+
import (
4+
"reflect"
5+
"testing"
6+
)
7+
8+
func TestNamespaces(t *testing.T) {
9+
want := []string{"one", "two"}
10+
if got := namespaces(" one, ,two,"); !reflect.DeepEqual(got, want) {
11+
t.Fatalf("namespaces() = %q, want %q", got, want)
12+
}
13+
}

‎go/core/v2/controller/apiclient/types.go‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package apiclient
22

33
import (
44
"context"
5+
"sync"
56

67
atev1alpha1 "github.com/agent-substrate/substrate/pkg/api/v1alpha1"
78
kagentv1alpha3 "github.com/kagent-dev/kagent/go/api/v1alpha3"
@@ -12,9 +13,15 @@ import (
1213
"k8s.io/apimachinery/pkg/watch"
1314
)
1415

16+
var registerTypesOnce sync.Once
17+
1518
// RegisterTypes connects KRT's type registry to the generated Kubernetes
1619
// clients. This is the same registration boundary used by agentgateway.
1720
func RegisterTypes() {
21+
registerTypesOnce.Do(registerTypes)
22+
}
23+
24+
func registerTypes() {
1825
kubeclient.Register(
1926
kagentv1alpha3.GroupVersion.WithResource("agenttemplates"),
2027
kagentv1alpha3.GroupVersion.WithKind("AgentTemplate"),
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package apiclient
2+
3+
import (
4+
"sync"
5+
"testing"
6+
)
7+
8+
func TestRegisterTypesConcurrent(t *testing.T) {
9+
var group sync.WaitGroup
10+
for range 10 {
11+
group.Go(RegisterTypes)
12+
}
13+
group.Wait()
14+
}

0 commit comments

Comments
 (0)