Skip to content

Commit 2441afd

Browse files
committed
add proxy
1 parent a47730b commit 2441afd

File tree

8 files changed

+99
-25
lines changed

8 files changed

+99
-25
lines changed

api/v1alpha1/codeserver_types.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,9 @@ type CodeServerSpec struct {
7070
ImagePullSecrets []corev1.LocalObjectReference `json:"imagePullSecrets,omitempty"`
7171

7272
IngressClassName string `json:"ingressClassName,omitempty"`
73+
74+
// PublicProxyPorts specifies the public proxy ports for code server
75+
PublicProxyPorts []int32 `json:"publicProxyPorts,omitempty"`
7376
}
7477

7578
// CodeServerStatus defines the observed state of CodeServer

api/v1alpha1/zz_generated.deepcopy.go

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

charts/code-server-operator/crds/codeserver-crd.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,13 @@ spec:
216216
type: string
217217
description: Specifies the node selector for scheduling.
218218
type: object
219+
publicProxyPorts:
220+
description: PublicProxyPorts specifies the public proxy ports for
221+
code server
222+
items:
223+
format: int32
224+
type: integer
225+
type: array
219226
resources:
220227
description: Specifies the resource requirements for code server pod.
221228
properties:

charts/code-server-operator/crds/codeserverdeployment-crd.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,13 @@ spec:
208208
type: string
209209
description: Specifies the node selector for scheduling.
210210
type: object
211+
publicProxyPorts:
212+
description: PublicProxyPorts specifies the public proxy ports
213+
for code server
214+
items:
215+
format: int32
216+
type: integer
217+
type: array
211218
resources:
212219
description: Specifies the resource requirements for code
213220
server pod.

config/crd/bases/cs.walnuts.dev_codeserverdeployments.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,13 @@ spec:
208208
type: string
209209
description: Specifies the node selector for scheduling.
210210
type: object
211+
publicProxyPorts:
212+
description: PublicProxyPorts specifies the public proxy ports
213+
for code server
214+
items:
215+
format: int32
216+
type: integer
217+
type: array
211218
resources:
212219
description: Specifies the resource requirements for code
213220
server pod.

config/crd/bases/cs.walnuts.dev_codeservers.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,13 @@ spec:
206206
type: string
207207
description: Specifies the node selector for scheduling.
208208
type: object
209+
publicProxyPorts:
210+
description: PublicProxyPorts specifies the public proxy ports for
211+
code server
212+
items:
213+
format: int32
214+
type: integer
215+
type: array
209216
resources:
210217
description: Specifies the resource requirements for code server pod.
211218
properties:

config/samples/cs_v1alpha1_codeserverdeployment.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,5 @@ spec:
2626
imagePullSecrets:
2727
- name: ghcr-login-secret
2828
domain: "walnuts.dev"
29+
publicProxyPorts:
30+
- 8080

internal/controller/codeserver_controller.go

Lines changed: 61 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,23 @@ func (r *CodeServerReconciler) reconcileService(ctx context.Context, codeServer
395395
return fmt.Errorf("failed to create controller reference: %w", err)
396396
}
397397

398+
ports := []*corev1apply.ServicePortApplyConfiguration{
399+
corev1apply.ServicePort().
400+
WithName("http").
401+
WithProtocol(corev1.ProtocolTCP).
402+
WithPort(codeServer.Spec.ContainerPort).
403+
WithTargetPort(intstr.FromInt32(codeServer.Spec.ContainerPort)),
404+
}
405+
406+
for _, port := range codeServer.Spec.PublicProxyPorts {
407+
ports = append(ports, corev1apply.ServicePort().
408+
WithName(fmt.Sprintf("http-%d", port)).
409+
WithProtocol(corev1.ProtocolTCP).
410+
WithPort(port).
411+
WithTargetPort(intstr.FromInt32(port)),
412+
)
413+
}
414+
398415
service := corev1apply.Service(codeServer.Name, codeServer.Namespace).
399416
WithLabels(map[string]string{
400417
"app.kubernetes.io/name": CodeServer,
@@ -404,11 +421,7 @@ func (r *CodeServerReconciler) reconcileService(ctx context.Context, codeServer
404421
WithOwnerReferences(owner).
405422
WithSpec(corev1apply.ServiceSpec().
406423
WithType(corev1.ServiceTypeClusterIP).
407-
WithPorts(corev1apply.ServicePort().
408-
WithName("http").
409-
WithProtocol(corev1.ProtocolTCP).
410-
WithPort(codeServer.Spec.ContainerPort).
411-
WithTargetPort(intstr.FromInt32(codeServer.Spec.ContainerPort)),
424+
WithPorts(ports...,
412425
).
413426
WithSelector(map[string]string{
414427
"app.kubernetes.io/name": CodeServer,
@@ -464,33 +477,56 @@ func (r *CodeServerReconciler) reconcileIngress(ctx context.Context, codeServer
464477
}
465478
host := fmt.Sprintf("%s.%s", codeServer.Name, url.String())
466479

480+
paths := []*networkingv1apply.HTTPIngressPathApplyConfiguration{networkingv1apply.HTTPIngressPath().
481+
WithPath("/").
482+
WithPathType(networkingv1.PathTypePrefix).
483+
WithBackend(networkingv1apply.IngressBackend().
484+
WithService(networkingv1apply.IngressServiceBackend().
485+
WithName(codeServer.Name).
486+
WithPort(networkingv1apply.ServiceBackendPort().
487+
WithName("http"),
488+
),
489+
),
490+
),
491+
}
492+
493+
for _, port := range codeServer.Spec.PublicProxyPorts {
494+
paths = append(paths, networkingv1apply.HTTPIngressPath().
495+
WithPath(fmt.Sprintf("/proxy/%d", port)).
496+
WithPathType(networkingv1.PathTypePrefix).
497+
WithBackend(networkingv1apply.IngressBackend().
498+
WithService(networkingv1apply.IngressServiceBackend().
499+
WithName(codeServer.Name).
500+
WithPort(networkingv1apply.ServiceBackendPort().
501+
WithName(fmt.Sprintf("http-%d", port)),
502+
),
503+
),
504+
),
505+
)
506+
}
507+
508+
spec := networkingv1apply.IngressSpec().
509+
WithRules(networkingv1apply.IngressRule().
510+
WithHost(host).
511+
WithHTTP(networkingv1apply.HTTPIngressRuleValue().
512+
WithPaths(
513+
paths...,
514+
),
515+
),
516+
)
517+
518+
if codeServer.Spec.IngressClassName != "" {
519+
spec = spec.WithIngressClassName(codeServer.Spec.IngressClassName)
520+
}
521+
467522
ingress := networkingv1apply.Ingress(codeServer.Name, codeServer.Namespace).
468523
WithLabels(map[string]string{
469524
"app.kubernetes.io/name": CodeServer,
470525
"app.kubernetes.io/instance": codeServer.Name,
471526
"app.kubernetes.io/created-by": CodeServerManager,
472527
}).
473528
WithOwnerReferences(owner).
474-
WithSpec(networkingv1apply.IngressSpec().
475-
WithIngressClassName(codeServer.Spec.IngressClassName).
476-
WithRules(networkingv1apply.IngressRule().
477-
WithHost(host).
478-
WithHTTP(networkingv1apply.HTTPIngressRuleValue().
479-
WithPaths(networkingv1apply.HTTPIngressPath().
480-
WithPath("/").
481-
WithPathType(networkingv1.PathTypePrefix).
482-
WithBackend(networkingv1apply.IngressBackend().
483-
WithService(networkingv1apply.IngressServiceBackend().
484-
WithName(codeServer.Name).
485-
WithPort(networkingv1apply.ServiceBackendPort().
486-
WithName("http"),
487-
),
488-
),
489-
),
490-
),
491-
),
492-
),
493-
)
529+
WithSpec(spec)
494530

495531
obj, err := runtime.DefaultUnstructuredConverter.ToUnstructured(ingress)
496532
if err != nil {

0 commit comments

Comments
 (0)