Skip to content

Commit 4c7d095

Browse files
committed
fixes tests
1 parent 3319a92 commit 4c7d095

File tree

2 files changed

+306
-3
lines changed

2 files changed

+306
-3
lines changed

pkg/resources/istioingress/meshgateway.go

Lines changed: 119 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,13 +63,34 @@ func (r *Reconciler) meshgateway(log logr.Logger, externalListenerConfig v1beta1
6363
Spec: corev1.PodSpec{
6464
Containers: []corev1.Container{
6565
{
66-
Name: "istio-proxy",
67-
Image: v1beta1.DefaultIstioProxyImage, // Use a standard Istio proxy image
68-
Env: convertEnvVars(ingressConfig.IstioIngressConfig.Envs),
66+
Name: "istio-proxy",
67+
Image: v1beta1.DefaultIstioProxyImage, // Use a standard Istio proxy image
68+
Command: []string{"/usr/local/bin/pilot-agent"},
69+
Args: []string{
70+
"proxy",
71+
"router",
72+
"--domain", fmt.Sprintf("%s.svc.cluster.local", r.KafkaCluster.Namespace),
73+
"--proxyLogLevel=warning",
74+
"--proxyComponentLogLevel=misc:error",
75+
"--log_output_level=default:info",
76+
},
77+
Env: append(convertEnvVars(ingressConfig.IstioIngressConfig.Envs), getIstioProxyEnvVars(meshgatewayName, r.KafkaCluster.Namespace)...),
6978
Resources: *ingressConfig.IstioIngressConfig.GetResources(),
7079
SecurityContext: &corev1.SecurityContext{
7180
RunAsNonRoot: util.BoolPointer(false),
7281
},
82+
Ports: []corev1.ContainerPort{
83+
{
84+
ContainerPort: 15090,
85+
Protocol: corev1.ProtocolTCP,
86+
Name: "http-envoy-prom",
87+
},
88+
{
89+
ContainerPort: 15021,
90+
Protocol: corev1.ProtocolTCP,
91+
Name: "status-port",
92+
},
93+
},
7394
},
7495
},
7596
NodeSelector: ingressConfig.IstioIngressConfig.NodeSelector,
@@ -190,3 +211,98 @@ func convertTolerations(tolerations []*corev1.Toleration) []corev1.Toleration {
190211
}
191212
return result
192213
}
214+
215+
// getIstioProxyEnvVars returns the required environment variables for Istio proxy
216+
func getIstioProxyEnvVars(gatewayName, namespace string) []corev1.EnvVar {
217+
return []corev1.EnvVar{
218+
{
219+
Name: "PILOT_CERT_PROVIDER",
220+
Value: "istiod",
221+
},
222+
{
223+
Name: "CA_ADDR",
224+
Value: "istiod.istio-system.svc:15012",
225+
},
226+
{
227+
Name: "POD_NAME",
228+
ValueFrom: &corev1.EnvVarSource{
229+
FieldRef: &corev1.ObjectFieldSelector{
230+
APIVersion: "v1",
231+
FieldPath: "metadata.name",
232+
},
233+
},
234+
},
235+
{
236+
Name: "POD_NAMESPACE",
237+
ValueFrom: &corev1.EnvVarSource{
238+
FieldRef: &corev1.ObjectFieldSelector{
239+
APIVersion: "v1",
240+
FieldPath: "metadata.namespace",
241+
},
242+
},
243+
},
244+
{
245+
Name: "INSTANCE_IP",
246+
ValueFrom: &corev1.EnvVarSource{
247+
FieldRef: &corev1.ObjectFieldSelector{
248+
APIVersion: "v1",
249+
FieldPath: "status.podIP",
250+
},
251+
},
252+
},
253+
{
254+
Name: "SERVICE_ACCOUNT",
255+
ValueFrom: &corev1.EnvVarSource{
256+
FieldRef: &corev1.ObjectFieldSelector{
257+
APIVersion: "v1",
258+
FieldPath: "spec.serviceAccountName",
259+
},
260+
},
261+
},
262+
{
263+
Name: "HOST_IP",
264+
ValueFrom: &corev1.EnvVarSource{
265+
FieldRef: &corev1.ObjectFieldSelector{
266+
APIVersion: "v1",
267+
FieldPath: "status.hostIP",
268+
},
269+
},
270+
},
271+
{
272+
Name: "PROXY_CONFIG",
273+
Value: "{}",
274+
},
275+
{
276+
Name: "ISTIO_META_POD_PORTS",
277+
Value: `[{"containerPort":15090,"protocol":"TCP","name":"http-envoy-prom"},{"containerPort":15021,"protocol":"TCP","name":"status-port"}]`,
278+
},
279+
{
280+
Name: "ISTIO_META_APP_CONTAINERS",
281+
Value: "istio-proxy",
282+
},
283+
{
284+
Name: "ISTIO_META_CLUSTER_ID",
285+
Value: "Kubernetes",
286+
},
287+
{
288+
Name: "ISTIO_META_INTERCEPTION_MODE",
289+
Value: "REDIRECT",
290+
},
291+
{
292+
Name: "ISTIO_META_WORKLOAD_NAME",
293+
Value: gatewayName,
294+
},
295+
{
296+
Name: "ISTIO_META_OWNER",
297+
Value: fmt.Sprintf("kubernetes://apis/apps/v1/namespaces/%s/deployments/%s", namespace, gatewayName),
298+
},
299+
{
300+
Name: "ISTIO_META_MESH_ID",
301+
Value: "cluster.local",
302+
},
303+
{
304+
Name: "TRUST_DOMAIN",
305+
Value: "cluster.local",
306+
},
307+
}
308+
}
Lines changed: 187 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,187 @@
1+
package istioingress
2+
3+
import (
4+
"testing"
5+
6+
"github.com/go-logr/logr"
7+
appsv1 "k8s.io/api/apps/v1"
8+
corev1 "k8s.io/api/core/v1"
9+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
10+
11+
"github.com/banzaicloud/koperator/api/v1beta1"
12+
"github.com/banzaicloud/koperator/pkg/resources"
13+
)
14+
15+
func TestMeshgatewayContainerConfiguration(t *testing.T) {
16+
// Create a minimal KafkaCluster for testing
17+
kafkaCluster := &v1beta1.KafkaCluster{
18+
ObjectMeta: metav1.ObjectMeta{
19+
Name: "test-kafka",
20+
Namespace: "test-namespace",
21+
},
22+
Spec: v1beta1.KafkaClusterSpec{
23+
Brokers: []v1beta1.Broker{
24+
{Id: 0},
25+
},
26+
},
27+
}
28+
29+
// Create reconciler
30+
reconciler := &Reconciler{
31+
Reconciler: resources.Reconciler{
32+
KafkaCluster: kafkaCluster,
33+
},
34+
}
35+
36+
// Create test external listener config
37+
externalListenerConfig := v1beta1.ExternalListenerConfig{
38+
CommonListenerSpec: v1beta1.CommonListenerSpec{
39+
Type: "plaintext",
40+
Name: "external",
41+
ContainerPort: 9094,
42+
},
43+
ExternalStartingPort: 19090,
44+
}
45+
46+
// Create test ingress config
47+
ingressConfig := v1beta1.IngressConfig{
48+
IstioIngressConfig: &v1beta1.IstioIngressConfig{},
49+
}
50+
51+
// Call the meshgateway function
52+
result := reconciler.meshgateway(
53+
logr.Discard(),
54+
externalListenerConfig,
55+
ingressConfig,
56+
"test-config",
57+
"default-config",
58+
"",
59+
)
60+
61+
// Cast to Deployment
62+
deployment, ok := result.(*appsv1.Deployment)
63+
if !ok {
64+
t.Fatalf("Expected *appsv1.Deployment, got %T", result)
65+
}
66+
67+
// Verify deployment has one container
68+
if len(deployment.Spec.Template.Spec.Containers) != 1 {
69+
t.Fatalf("Expected 1 container, got %d", len(deployment.Spec.Template.Spec.Containers))
70+
}
71+
72+
container := deployment.Spec.Template.Spec.Containers[0]
73+
74+
// Verify container name
75+
if container.Name != "istio-proxy" {
76+
t.Errorf("Expected container name 'istio-proxy', got '%s'", container.Name)
77+
}
78+
79+
// Verify container image
80+
if container.Image != v1beta1.DefaultIstioProxyImage {
81+
t.Errorf("Expected image '%s', got '%s'", v1beta1.DefaultIstioProxyImage, container.Image)
82+
}
83+
84+
// Verify container has command
85+
if len(container.Command) == 0 {
86+
t.Error("Expected container to have command, but it's empty")
87+
} else if container.Command[0] != "/usr/local/bin/pilot-agent" {
88+
t.Errorf("Expected command '/usr/local/bin/pilot-agent', got '%s'", container.Command[0])
89+
}
90+
91+
// Verify container has args
92+
if len(container.Args) == 0 {
93+
t.Error("Expected container to have args, but it's empty")
94+
} else if container.Args[0] != "proxy" {
95+
t.Errorf("Expected first arg 'proxy', got '%s'", container.Args[0])
96+
}
97+
98+
// Verify container has required environment variables
99+
envVarMap := make(map[string]string)
100+
for _, env := range container.Env {
101+
envVarMap[env.Name] = env.Value
102+
}
103+
104+
requiredEnvVars := []string{
105+
"PILOT_CERT_PROVIDER",
106+
"CA_ADDR",
107+
"PROXY_CONFIG",
108+
"ISTIO_META_CLUSTER_ID",
109+
"ISTIO_META_INTERCEPTION_MODE",
110+
"TRUST_DOMAIN",
111+
}
112+
113+
for _, envVar := range requiredEnvVars {
114+
if _, exists := envVarMap[envVar]; !exists {
115+
t.Errorf("Expected environment variable '%s' to be set", envVar)
116+
}
117+
}
118+
119+
// Verify container has required ports
120+
if len(container.Ports) != 2 {
121+
t.Errorf("Expected 2 container ports, got %d", len(container.Ports))
122+
}
123+
124+
portMap := make(map[string]int32)
125+
for _, port := range container.Ports {
126+
portMap[port.Name] = port.ContainerPort
127+
}
128+
129+
if port, exists := portMap["http-envoy-prom"]; !exists || port != 15090 {
130+
t.Errorf("Expected port 'http-envoy-prom' on 15090, got %d", port)
131+
}
132+
133+
if port, exists := portMap["status-port"]; !exists || port != 15021 {
134+
t.Errorf("Expected port 'status-port' on 15021, got %d", port)
135+
}
136+
}
137+
138+
func TestGetIstioProxyEnvVars(t *testing.T) {
139+
gatewayName := "test-gateway"
140+
namespace := "test-namespace"
141+
142+
envVars := getIstioProxyEnvVars(gatewayName, namespace)
143+
144+
// Verify we have environment variables
145+
if len(envVars) == 0 {
146+
t.Fatal("Expected environment variables, got none")
147+
}
148+
149+
// Create a map for easier testing
150+
envVarMap := make(map[string]corev1.EnvVar)
151+
for _, env := range envVars {
152+
envVarMap[env.Name] = env
153+
}
154+
155+
// Test specific environment variables
156+
testCases := []struct {
157+
name string
158+
expectedValue string
159+
}{
160+
{"PILOT_CERT_PROVIDER", "istiod"},
161+
{"CA_ADDR", "istiod.istio-system.svc:15012"},
162+
{"PROXY_CONFIG", "{}"},
163+
{"ISTIO_META_CLUSTER_ID", "Kubernetes"},
164+
{"ISTIO_META_INTERCEPTION_MODE", "REDIRECT"},
165+
{"ISTIO_META_WORKLOAD_NAME", gatewayName},
166+
{"ISTIO_META_MESH_ID", "cluster.local"},
167+
{"TRUST_DOMAIN", "cluster.local"},
168+
}
169+
170+
for _, tc := range testCases {
171+
if env, exists := envVarMap[tc.name]; !exists {
172+
t.Errorf("Expected environment variable '%s' to exist", tc.name)
173+
} else if env.Value != tc.expectedValue {
174+
t.Errorf("Expected '%s' to have value '%s', got '%s'", tc.name, tc.expectedValue, env.Value)
175+
}
176+
}
177+
178+
// Test environment variables with field references
179+
fieldRefVars := []string{"POD_NAME", "POD_NAMESPACE", "INSTANCE_IP", "SERVICE_ACCOUNT", "HOST_IP"}
180+
for _, varName := range fieldRefVars {
181+
if env, exists := envVarMap[varName]; !exists {
182+
t.Errorf("Expected environment variable '%s' to exist", varName)
183+
} else if env.ValueFrom == nil || env.ValueFrom.FieldRef == nil {
184+
t.Errorf("Expected '%s' to have FieldRef, but it doesn't", varName)
185+
}
186+
}
187+
}

0 commit comments

Comments
 (0)