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",
Path: "/api",
Path: "/api",
},
},
Env: map[string]string{

View File

@ -212,27 +212,37 @@ func createMiddlewareAddResponseHeader(
namespace *corev1.Namespace,
application *Application,
) (*traefikv1alpha1.Middleware, error) {
middlewareName := fmt.Sprintf("%s-response-header-middleware", configuration.Name)
return traefikv1alpha1.NewMiddleware(ctx, middlewareName, &traefikv1alpha1.MiddlewareArgs{
Metadata: &metav1.ObjectMetaArgs{
Namespace: namespace.Metadata.Name(),
Labels: pulumi.StringMap{
"app.kubernetes.io/part-of": pulumi.String(configuration.Name),
"app.kubernetes.io/managed-by": pulumi.String("pulumi"),
if configuration.ResponseHeaders != nil {
middlewareName := fmt.Sprintf("%s-response-header-middleware", configuration.Name)
return traefikv1alpha1.NewMiddleware(ctx, middlewareName, &traefikv1alpha1.MiddlewareArgs{
Metadata: &metav1.ObjectMetaArgs{
Namespace: namespace.Metadata.Name(),
Labels: pulumi.StringMap{
"app.kubernetes.io/part-of": pulumi.String(configuration.Name),
"app.kubernetes.io/managed-by": pulumi.String("pulumi"),
},
},
},
Spec: &traefikv1alpha1.MiddlewareSpecArgs{
Headers: configuration.ResponseHeaders,
},
}, pulumi.Parent(application))
Spec: &traefikv1alpha1.MiddlewareSpecArgs{
Headers: configuration.ResponseHeaders,
},
}, 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) {
if configuration.ShouldCreateIngress {
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])
}).(pulumi.StringOutput)
var middlewares pulumi.StringInput
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{
"traefik.ingress.kubernetes.io/router.middlewares": middlewares,
"traefik.ingress.kubernetes.io/router.entrypoints": pulumi.String("websecure"),