Skip to content
This repository was archived by the owner on Dec 15, 2021. It is now read-only.

Commit 4064c41

Browse files
imishravmwandresmgot
authored andcommitted
Add CORS support (#1008)
1 parent 56772bd commit 4064c41

File tree

5 files changed

+55
-10
lines changed

5 files changed

+55
-10
lines changed

cmd/kubeless/trigger/http/create.go

+10-5
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,12 @@ package http
1818

1919
import (
2020
"fmt"
21-
22-
"github.com/sirupsen/logrus"
23-
"github.com/spf13/cobra"
24-
2521
httpApi "github.com/kubeless/http-trigger/pkg/apis/kubeless/v1beta1"
2622
httpUtils "github.com/kubeless/http-trigger/pkg/utils"
2723
kubelessUtils "github.com/kubeless/kubeless/pkg/utils"
24+
"github.com/sirupsen/logrus"
25+
"github.com/spf13/cobra"
26+
2827
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2928
)
3029

@@ -38,7 +37,6 @@ var createCmd = &cobra.Command{
3837
logrus.Fatal("Need exactly one argument - http trigger name")
3938
}
4039
triggerName := args[0]
41-
4240
ns, err := cmd.Flags().GetString("namespace")
4341
if err != nil {
4442
logrus.Fatal(err)
@@ -101,6 +99,12 @@ var createCmd = &cobra.Command{
10199
}
102100
httpTrigger.Spec.TLSAcme = enableTLSAcme
103101

102+
corsEnabled, err := cmd.Flags().GetBool("cors-enable")
103+
if err != nil {
104+
logrus.Fatal(err)
105+
}
106+
httpTrigger.Spec.CorsEnable = corsEnabled
107+
104108
tlsSecret, err := cmd.Flags().GetString("tls-secret")
105109
if err != nil {
106110
logrus.Fatal(err)
@@ -179,5 +183,6 @@ func init() {
179183
createCmd.Flags().StringP("tls-secret", "", "", "Specify an existing secret that contains a TLS private key and certificate to secure ingress")
180184
createCmd.Flags().Bool("dryrun", false, "Output JSON manifest of the function without creating it")
181185
createCmd.Flags().StringP("output", "o", "yaml", "Output format")
186+
createCmd.Flags().BoolP("cors-enable", "", false, "If true then cors will be enabled on Http Trigger")
182187
createCmd.MarkFlagRequired("function-name")
183188
}

docs/http-triggers.md

+27
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ Usage:
8383

8484
Flags:
8585
--basic-auth-secret string Specify an existing secret name for basic authentication
86+
--cors-enable If true then cors will be enabled on Http Trigger
8687
--enableTLSAcme If true, routing rule will be configured for use with kube-lego
8788
--function-name string Name of the function to be associated with trigger
8889
--gateway string Specify a valid gateway for the Ingress. Supported: nginx, traefik, kong (default "nginx")
@@ -228,6 +229,32 @@ hello world
228229

229230
It is not yet supported to create an HTTP trigger with basic authentication using Kong as backend but the steps to do it manually are pretty simple. It is possible to do so using Kong plugins. In the [next section](#enable-kong-security-plugins) we explain how to enable any of the available Kong plugins and in particular we explain how to enable the basic-auth plugin.
230231

232+
## Enable CORS
233+
234+
It's possible to enable CORS requests at the HTTPTrigger level. To do so use the --cors-enable flag when deploying
235+
the HTTPTrigger or add the field cors-enable: true to the YAML manifest.
236+
237+
## Add arbitrary annotations
238+
239+
It is also possible to add any annotation to the resulting Ingress object if you add those to the HTTPTrigger. For example:
240+
241+
```
242+
apiVersion: kubeless.io/v1beta1
243+
kind: HTTPTrigger
244+
metadata:
245+
name: cors-trigger
246+
annotations:
247+
nginx.ingress.kubernetes.io/enable-cors: "true"
248+
nginx.ingress.kubernetes.io/cors-allow-methods: "GET"
249+
spec:
250+
function-name: get-python
251+
host-name: example.com
252+
path: echo
253+
```
254+
255+
The above will create an Ingress object with the annotations nginx.ingress.kubernetes.io/enable-cors: "true"
256+
and nginx.ingress.kubernetes.io/cors-allow-methods: "GET".
257+
231258
## Enable Kong Security plugins
232259

233260
Kong has available several free [plugins](https://konghq.com/plugins/) that can be used along with the Kong Ingress controller for securing the access to Kubeless functions. In particular, the list of security plugins that can be used is:

script/start-test-environment.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ if ! minikube status | grep -q "minikube: $"; then
1515
exit 1
1616
fi
1717

18-
minikube start --extra-config=apiserver.Authorization.Mode=RBAC --insecure-registry 0.0.0.0/0
18+
minikube start --extra-config=apiserver.authorization-mode=RBAC --insecure-registry 0.0.0.0/0
1919
eval $(minikube docker-env)
2020

2121
CONTEXT=$(kubectl config current-context)

vendor/github.com/kubeless/http-trigger/pkg/apis/kubeless/v1beta1/http_trigger.go

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/kubeless/http-trigger/pkg/utils/k8sutil.go

+16-4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)