Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: use annotation to set target port HTTP on backend for HTTPS svc #56

Merged
merged 1 commit into from
Oct 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions controllers/nginx_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ const (
gcpNetworkTierAnnotationKey = "cloud.google.com/network-tier"
ociLoadBalancerTLSSecret = "service.beta.kubernetes.io/oci-load-balancer-tls-secret"
ociLoadBalancerSSLPorts = "service.beta.kubernetes.io/oci-load-balancer-ssl-ports"
useHTTPSOverHTTPAnnotation = "nginx.tsuru.io/https-over-http"
)

// NginxReconciler reconciles a Nginx object
Expand Down
77 changes: 77 additions & 0 deletions controllers/nginx_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -717,6 +717,83 @@ func TestNginxReconciler_reconcileService(t *testing.T) {
"Normal ServiceUpdated service updated successfully",
},
},
{
name: "when using annotation for HTTPS port over HTTP target port",
nginx: &v1alpha1.Nginx{
TypeMeta: metav1.TypeMeta{
APIVersion: "extensions.tsuru.io/v1alpha1",
Kind: "Nginx",
},
ObjectMeta: metav1.ObjectMeta{
Name: "my-nginx",
Namespace: "default",
},
Spec: v1alpha1.NginxSpec{
Service: &v1alpha1.NginxService{
Type: corev1.ServiceTypeClusterIP,
Annotations: map[string]string{
useHTTPSOverHTTPAnnotation: "true",
},
},
},
},
service: &corev1.Service{
TypeMeta: metav1.TypeMeta{
APIVersion: "v1",
Kind: "Service",
},
ObjectMeta: metav1.ObjectMeta{
Name: "my-nginx-service",
Namespace: "default",
Annotations: map[string]string{},
Labels: map[string]string{},
},
Spec: corev1.ServiceSpec{
Type: corev1.ServiceTypeLoadBalancer,
ExternalTrafficPolicy: corev1.ServiceExternalTrafficPolicyTypeCluster,
ClusterIP: "10.1.1.10",
HealthCheckNodePort: int32(43123),
Ports: []corev1.ServicePort{
{
Name: "https",
TargetPort: intstr.FromString("https"),
Protocol: corev1.ProtocolTCP,
Port: int32(443),
NodePort: int32(30667),
},
{
Name: "http",
Protocol: corev1.ProtocolTCP,
TargetPort: intstr.FromString("http"),
Port: int32(80),
NodePort: int32(30666),
},
},
},
},
assertion: func(t *testing.T, err error, got *corev1.Service) {
assert.NoError(t, err)
assert.NotNil(t, got)
expectedPorts := []corev1.ServicePort{
{
Name: "http",
TargetPort: intstr.FromString("http"),
Protocol: corev1.ProtocolTCP,
Port: int32(80),
},
{
Name: "https",
TargetPort: intstr.FromString("http"),
Protocol: corev1.ProtocolTCP,
Port: int32(443),
},
}
assert.Equal(t, expectedPorts, got.Spec.Ports)
},
expectedEvents: []string{
"Normal ServiceUpdated service updated successfully",
},
},
{
name: "when updating then nginx service, should keep resource finalizers",
nginx: &v1alpha1.Nginx{
Expand Down
11 changes: 10 additions & 1 deletion pkg/k8s/k8s.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ const (

// Annotation key used to stored the nginx that created the deployment
generatedFromAnnotation = "nginx.tsuru.io/generated-from"

useHTTPSOverHTTPAnnotation = "nginx.tsuru.io/https-over-http"
)

var nginxEntrypoint = []string{
Expand Down Expand Up @@ -285,12 +287,19 @@ func fillPorts(n *v1alpha1.Nginx, t corev1.ServiceType) []corev1.ServicePort {
{
Name: defaultHTTPSPortName,
Protocol: corev1.ProtocolTCP,
TargetPort: intstr.FromString(defaultHTTPSPortName),
TargetPort: fillHTTPSTargetPort(n),
Port: int32(443),
},
}
}

func fillHTTPSTargetPort(n *v1alpha1.Nginx) intstr.IntOrString {
if n.Spec.Service != nil && n.Spec.Service.Annotations != nil && n.Spec.Service.Annotations[useHTTPSOverHTTPAnnotation] == "true" {
return intstr.FromString(defaultHTTPPortName)
}
return intstr.FromString(defaultHTTPSPortName)
}

func nginxService(n *v1alpha1.Nginx) corev1.ServiceType {
if n == nil || n.Spec.Service == nil {
return corev1.ServiceTypeClusterIP
Expand Down
Loading