Skip to content

Commit 1aa19ec

Browse files
committed
feat: add startup error when running in the kube-system namespace
To avoid any accedential security issues, we block running anything in the kube-system namespace. We already have this explicitly blocked in the rest of the code that deals with namespaces and it causes hard to debug errors for users that try to deploy to the kube-system namespace. This adds an explicit check so that this mis-configuration is easier to detect and debug for end users. Signed-off-by: Lucas Roesler <[email protected]>
1 parent 85d425b commit 1aa19ec

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

main.go

+6
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,12 @@ func main() {
9898

9999
config.Fprint(verbose)
100100

101+
// use kubeclient to check the current namespace
102+
namespace, _ := k8s.CurrentNamespace()
103+
if namespace == "kube-system" {
104+
log.Fatal("You cannot run the OpenFaaS provider in the kube-system namespace, please try another namespace.")
105+
}
106+
101107
deployConfig := k8s.DeploymentConfig{
102108
RuntimeHTTPPort: 8080,
103109
HTTPProbe: config.HTTPProbe,

pkg/k8s/namespaces.go

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package k8s
2+
3+
import (
4+
"io/ioutil"
5+
"os"
6+
"strings"
7+
)
8+
9+
// CurrentNamespace attempts to return the current namespace from the environment
10+
// or from the service account file. If it cannot find the namespace, it returns
11+
// an empty string. This will be empty when the not running in-cluster.
12+
//
13+
// This implementation is based on the clientcmd.inClusterClientConfig.Namespace method.
14+
// This is not exported and not accessible via other methods, so we have to copy it.
15+
func CurrentNamespace() (namespace string, found bool) {
16+
if ns := os.Getenv("POD_NAMESPACE"); ns != "" {
17+
return ns, true
18+
}
19+
20+
if data, err := ioutil.ReadFile("/var/run/secrets/kubernetes.io/serviceaccount/namespace"); err == nil {
21+
if ns := strings.TrimSpace(string(data)); len(ns) > 0 {
22+
return ns, true
23+
}
24+
}
25+
26+
return "", false
27+
}

0 commit comments

Comments
 (0)