fix: create response header middleware only when configuration ask for it

This commit is contained in:
RouxAntoine 2023-10-05 01:08:00 +02:00
parent 2e7da70a48
commit 1adb18e186
Signed by: antoine
GPG Key ID: 098FB66FC0475E70
2 changed files with 26 additions and 16 deletions

View File

@ -17,7 +17,7 @@ func main() {
}, },
{ {
Image: "nginx", Image: "nginx",
Path: "/api", Path: "/api",
}, },
}, },
Env: map[string]string{ Env: map[string]string{

View File

@ -212,27 +212,37 @@ func createMiddlewareAddResponseHeader(
namespace *corev1.Namespace, namespace *corev1.Namespace,
application *Application, application *Application,
) (*traefikv1alpha1.Middleware, error) { ) (*traefikv1alpha1.Middleware, error) {
middlewareName := fmt.Sprintf("%s-response-header-middleware", configuration.Name) if configuration.ResponseHeaders != nil {
return traefikv1alpha1.NewMiddleware(ctx, middlewareName, &traefikv1alpha1.MiddlewareArgs{ middlewareName := fmt.Sprintf("%s-response-header-middleware", configuration.Name)
Metadata: &metav1.ObjectMetaArgs{ return traefikv1alpha1.NewMiddleware(ctx, middlewareName, &traefikv1alpha1.MiddlewareArgs{
Namespace: namespace.Metadata.Name(), Metadata: &metav1.ObjectMetaArgs{
Labels: pulumi.StringMap{ Namespace: namespace.Metadata.Name(),
"app.kubernetes.io/part-of": pulumi.String(configuration.Name), Labels: pulumi.StringMap{
"app.kubernetes.io/managed-by": pulumi.String("pulumi"), "app.kubernetes.io/part-of": pulumi.String(configuration.Name),
"app.kubernetes.io/managed-by": pulumi.String("pulumi"),
},
}, },
}, Spec: &traefikv1alpha1.MiddlewareSpecArgs{
Spec: &traefikv1alpha1.MiddlewareSpecArgs{ Headers: configuration.ResponseHeaders,
Headers: configuration.ResponseHeaders, },
}, }, pulumi.Parent(application))
}, pulumi.Parent(application)) }
return nil, nil
} }
func createIngress(ctx *pulumi.Context, configuration *configuration, namespace *corev1.Namespace, certificate *v1.Certificate, servicesConfiguration []ingressConfiguration, application *Application, responseHeaderMiddleware *traefikv1alpha1.Middleware) (*netv1.Ingress, error) { func createIngress(ctx *pulumi.Context, configuration *configuration, namespace *corev1.Namespace, certificate *v1.Certificate, servicesConfiguration []ingressConfiguration, application *Application, responseHeaderMiddleware *traefikv1alpha1.Middleware) (*netv1.Ingress, error) {
if configuration.ShouldCreateIngress { if configuration.ShouldCreateIngress {
host := pulumi.String(configuration.Dns) host := pulumi.String(configuration.Dns)
middlewares := pulumi.All(namespace.Metadata.Name().Elem(), responseHeaderMiddleware.Metadata.Name().Elem()).ApplyT(func(args []interface{}) string {
return fmt.Sprintf("kube-ingress-gzip-compress@kubernetescrd,%s-%s@kubernetescrd", args[0], args[1]) var middlewares pulumi.StringInput
}).(pulumi.StringOutput) if configuration.ResponseHeaders != nil {
middlewares = pulumi.All(namespace.Metadata.Name().Elem(), responseHeaderMiddleware.Metadata.Name().Elem()).ApplyT(func(args []interface{}) string {
return fmt.Sprintf("kube-ingress-gzip-compress@kubernetescrd,%s-%s@kubernetescrd", args[0], args[1])
}).(pulumi.StringOutput)
} else {
middlewares = pulumi.String("kube-ingress-gzip-compress@kubernetescrd")
}
ingressAnnotations := pulumi.StringMap{ ingressAnnotations := pulumi.StringMap{
"traefik.ingress.kubernetes.io/router.middlewares": middlewares, "traefik.ingress.kubernetes.io/router.middlewares": middlewares,
"traefik.ingress.kubernetes.io/router.entrypoints": pulumi.String("websecure"), "traefik.ingress.kubernetes.io/router.entrypoints": pulumi.String("websecure"),