Skip to content

Commit 638debe

Browse files
committed
set service ports when containerPort is set
Signed-off-by: bin <[email protected]>
1 parent e74a50e commit 638debe

File tree

2 files changed

+186
-0
lines changed

2 files changed

+186
-0
lines changed

controllers/sandbox_controller.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import (
2929
"k8s.io/apimachinery/pkg/labels"
3030
"k8s.io/apimachinery/pkg/runtime"
3131
"k8s.io/apimachinery/pkg/types"
32+
"k8s.io/apimachinery/pkg/util/intstr"
3233
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
3334
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
3435
ctrl "sigs.k8s.io/controller-runtime"
@@ -272,6 +273,12 @@ func (r *SandboxReconciler) reconcileService(ctx context.Context, sandbox *sandb
272273
},
273274
},
274275
}
276+
277+
ports := buildServicePorts(&sandbox.Spec.PodTemplate.Spec)
278+
if len(ports) > 0 {
279+
service.Spec.Ports = ports
280+
}
281+
275282
service.SetGroupVersionKind(corev1.SchemeGroupVersion.WithKind("Service"))
276283
if err := ctrl.SetControllerReference(sandbox, service, r.Scheme); err != nil {
277284
log.Error(err, "Failed to set controller reference")
@@ -290,6 +297,48 @@ func (r *SandboxReconciler) reconcileService(ctx context.Context, sandbox *sandb
290297
return service, nil
291298
}
292299

300+
func buildServicePorts(podSpec *corev1.PodSpec) []corev1.ServicePort {
301+
if podSpec == nil {
302+
return nil
303+
}
304+
305+
seen := map[string]struct{}{}
306+
var ports []corev1.ServicePort
307+
308+
for _, container := range podSpec.Containers {
309+
for _, containerPort := range container.Ports {
310+
if containerPort.ContainerPort <= 0 {
311+
continue
312+
}
313+
314+
protocol := containerPort.Protocol
315+
if protocol == "" {
316+
protocol = corev1.ProtocolTCP
317+
}
318+
319+
key := fmt.Sprintf("%d/%s", containerPort.ContainerPort, protocol)
320+
if _, exists := seen[key]; exists {
321+
continue
322+
}
323+
324+
port := corev1.ServicePort{
325+
Port: containerPort.ContainerPort,
326+
Protocol: protocol,
327+
TargetPort: intstr.FromInt32(containerPort.ContainerPort),
328+
}
329+
330+
if containerPort.Name != "" {
331+
port.Name = containerPort.Name
332+
}
333+
334+
ports = append(ports, port)
335+
seen[key] = struct{}{}
336+
}
337+
}
338+
339+
return ports
340+
}
341+
293342
func (r *SandboxReconciler) reconcilePod(ctx context.Context, sandbox *sandboxv1alpha1.Sandbox, nameHash string) (*corev1.Pod, error) {
294343
log := log.FromContext(ctx)
295344

controllers/sandbox_controller_test.go

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import (
2828
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2929
"k8s.io/apimachinery/pkg/runtime"
3030
"k8s.io/apimachinery/pkg/types"
31+
"k8s.io/apimachinery/pkg/util/intstr"
3132
"k8s.io/utils/ptr"
3233
sandboxv1alpha1 "sigs.k8s.io/agent-sandbox/api/v1alpha1"
3334
ctrl "sigs.k8s.io/controller-runtime"
@@ -406,6 +407,142 @@ func TestReconcile(t *testing.T) {
406407
},
407408
},
408409
},
410+
{
411+
name: "sandbox spec with container ports creates service ports",
412+
sandboxSpec: sandboxv1alpha1.SandboxSpec{
413+
PodTemplate: sandboxv1alpha1.PodTemplate{
414+
Spec: corev1.PodSpec{
415+
Containers: []corev1.Container{
416+
{
417+
Name: "main",
418+
Ports: []corev1.ContainerPort{
419+
{
420+
Name: "http",
421+
ContainerPort: 8080,
422+
},
423+
{
424+
Name: "metrics",
425+
ContainerPort: 9090,
426+
Protocol: corev1.ProtocolTCP,
427+
},
428+
{
429+
Name: "duplicate",
430+
ContainerPort: 8080,
431+
},
432+
},
433+
},
434+
{
435+
Name: "sidecar",
436+
Ports: []corev1.ContainerPort{
437+
{
438+
Name: "admin",
439+
ContainerPort: 9000,
440+
Protocol: corev1.ProtocolTCP,
441+
},
442+
},
443+
},
444+
},
445+
},
446+
},
447+
},
448+
wantStatus: sandboxv1alpha1.SandboxStatus{
449+
Service: sandboxName,
450+
ServiceFQDN: "sandbox-name.sandbox-ns.svc.cluster.local",
451+
Replicas: 1,
452+
LabelSelector: "agents.x-k8s.io/sandbox-name-hash=ab179450",
453+
Conditions: []metav1.Condition{
454+
{
455+
Type: "Ready",
456+
Status: "False",
457+
ObservedGeneration: 1,
458+
Reason: "DependenciesNotReady",
459+
Message: "Pod exists with phase: ; Service Exists",
460+
},
461+
},
462+
},
463+
wantObjs: []client.Object{
464+
&corev1.Pod{
465+
ObjectMeta: metav1.ObjectMeta{
466+
Name: sandboxName,
467+
Namespace: sandboxNs,
468+
ResourceVersion: "1",
469+
Labels: map[string]string{
470+
"agents.x-k8s.io/sandbox-name-hash": "ab179450",
471+
},
472+
OwnerReferences: []metav1.OwnerReference{sandboxControllerRef(sandboxName)},
473+
},
474+
Spec: corev1.PodSpec{
475+
Containers: []corev1.Container{
476+
{
477+
Name: "main",
478+
Ports: []corev1.ContainerPort{
479+
{
480+
Name: "http",
481+
ContainerPort: 8080,
482+
},
483+
{
484+
Name: "metrics",
485+
ContainerPort: 9090,
486+
Protocol: corev1.ProtocolTCP,
487+
},
488+
{
489+
Name: "duplicate",
490+
ContainerPort: 8080,
491+
},
492+
},
493+
},
494+
{
495+
Name: "sidecar",
496+
Ports: []corev1.ContainerPort{
497+
{
498+
Name: "admin",
499+
ContainerPort: 9000,
500+
Protocol: corev1.ProtocolTCP,
501+
},
502+
},
503+
},
504+
},
505+
},
506+
},
507+
&corev1.Service{
508+
ObjectMeta: metav1.ObjectMeta{
509+
Name: sandboxName,
510+
Namespace: sandboxNs,
511+
ResourceVersion: "1",
512+
Labels: map[string]string{
513+
"agents.x-k8s.io/sandbox-name-hash": "ab179450",
514+
},
515+
OwnerReferences: []metav1.OwnerReference{sandboxControllerRef(sandboxName)},
516+
},
517+
Spec: corev1.ServiceSpec{
518+
Selector: map[string]string{
519+
"agents.x-k8s.io/sandbox-name-hash": "ab179450",
520+
},
521+
ClusterIP: "None",
522+
Ports: []corev1.ServicePort{
523+
{
524+
Name: "http",
525+
Port: 8080,
526+
Protocol: corev1.ProtocolTCP,
527+
TargetPort: intstr.FromInt32(8080),
528+
},
529+
{
530+
Name: "metrics",
531+
Port: 9090,
532+
Protocol: corev1.ProtocolTCP,
533+
TargetPort: intstr.FromInt32(9090),
534+
},
535+
{
536+
Name: "admin",
537+
Port: 9000,
538+
Protocol: corev1.ProtocolTCP,
539+
TargetPort: intstr.FromInt32(9000),
540+
},
541+
},
542+
},
543+
},
544+
},
545+
},
409546
}
410547

411548
for _, tc := range testCases {

0 commit comments

Comments
 (0)