-
Notifications
You must be signed in to change notification settings - Fork 474
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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]>
- Loading branch information
1 parent
7e281bd
commit 4f42a4a
Showing
2 changed files
with
33 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package k8s | ||
|
||
import ( | ||
"io/ioutil" | ||
"os" | ||
"strings" | ||
) | ||
|
||
// CurrentNamespace attempts to return the current namespace from the environment | ||
// or from the service account file. If it cannot find the namespace, it returns | ||
// an empty string. This will be empty when the not running in-cluster. | ||
// | ||
// This implementation is based on the clientcmd.inClusterClientConfig.Namespace method. | ||
// This is not exported and not accessible via other methods, so we have to copy it. | ||
func CurrentNamespace() (namespace string, found bool) { | ||
if ns := os.Getenv("POD_NAMESPACE"); ns != "" { | ||
return ns, true | ||
} | ||
|
||
if data, err := ioutil.ReadFile("/var/run/secrets/kubernetes.io/serviceaccount/namespace"); err == nil { | ||
if ns := strings.TrimSpace(string(data)); len(ns) > 0 { | ||
return ns, true | ||
} | ||
} | ||
|
||
return "", false | ||
} |