@@ -23,10 +23,6 @@ import (
2323 "github.com/jetstack/preflight/pkg/version"
2424)
2525
26- const (
27- inClusterNamespacePath = "/var/run/secrets/kubernetes.io/serviceaccount/namespace"
28- )
29-
3026// Config wraps the options for a run of the agent.
3127type Config struct {
3228 // Deprecated: Schedule doesn't do anything. Use `period` instead.
@@ -154,9 +150,8 @@ type AgentCmdFlags struct {
154150 // InstallNS (--install-namespace) is the namespace in which the agent is
155151 // running in. Only needed when running the agent outside of Kubernetes.
156152 //
157- // May be left empty when running in Kubernetes. In this case, the namespace
158- // is read from the file
159- // /var/run/secrets/kubernetes.io/serviceaccount/namespace.
153+ // May be left empty when running in Kubernetes. In Kubernetes, the
154+ // namespace is read from the environment variable `POD_NAMESPACE`.
160155 InstallNS string
161156
162157 // Profiling (--enable-pprof) enables the pprof server.
@@ -273,8 +268,7 @@ func InitAgentCmdFlags(c *cobra.Command, cfg *AgentCmdFlags) {
273268 "install-namespace" ,
274269 "" ,
275270 "For testing purposes. Namespace in which the agent is running. " +
276- "Only needed with the " + string (VenafiCloudVenafiConnection )+ " mode" +
277- "when running the agent outside of Kubernetes." ,
271+ "Only needed when running the agent outside of Kubernetes." ,
278272 )
279273 c .PersistentFlags ().BoolVarP (
280274 & cfg .Profiling ,
@@ -314,6 +308,7 @@ type CombinedConfig struct {
314308 BackoffMaxTime time.Duration
315309 StrictMode bool
316310 OneShot bool
311+ InstallNS string
317312
318313 // Used by JetstackSecureOAuth, JetstackSecureAPIToken, and
319314 // VenafiCloudKeypair. Ignored in VenafiCloudVenafiConnection mode.
@@ -330,7 +325,6 @@ type CombinedConfig struct {
330325 // VenafiCloudVenafiConnection mode only.
331326 VenConnName string
332327 VenConnNS string
333- InstallNS string
334328
335329 // Only used for testing purposes.
336330 OutputPath string
@@ -530,20 +524,20 @@ func ValidateAndCombineConfig(log *log.Logger, cfg Config, flags AgentCmdFlags)
530524 res .StrictMode = flags .StrictMode
531525 }
532526
533- // Validation of --venafi-connection, --venafi-connection-namespace, and
534- // --install-namespace.
535- if res .AuthMode == VenafiCloudVenafiConnection {
536- var installNS string = flags .InstallNS
537- if flags .InstallNS == "" {
538- var err error
539- installNS , err = getInClusterNamespace ()
540- if err != nil {
541- errs = multierror .Append (errs , fmt .Errorf ("could not guess which namespace the agent is running in: %w" , err ))
542- }
527+ // Validation of --install-namespace.
528+ var installNS string = flags .InstallNS
529+ if flags .InstallNS == "" {
530+ var err error
531+ installNS , err = getInClusterNamespace ()
532+ if err != nil {
533+ errs = multierror .Append (errs , fmt .Errorf ("could not guess which namespace the agent is running in: %w" , err ))
543534 }
544- res . InstallNS = installNS
545- res .VenConnName = flags . VenConnName
535+ }
536+ res .InstallNS = installNS
546537
538+ // Validation of --venafi-connection and --venafi-connection-namespace.
539+ if res .AuthMode == VenafiCloudVenafiConnection {
540+ res .VenConnName = flags .VenConnName
547541 var venConnNS string = flags .VenConnNS
548542 if flags .VenConnNS == "" {
549543 venConnNS = installNS
@@ -727,21 +721,12 @@ func createCredentialClient(log *log.Logger, credentials client.Credentials, cfg
727721
728722// Inspired by the controller-runtime project.
729723func getInClusterNamespace () (string , error ) {
730- // Check whether the namespace file exists.
731- // If not, we are not running in cluster so can't guess the namespace.
732- _ , err := os .Stat (inClusterNamespacePath )
733- if os .IsNotExist (err ) {
734- return "" , fmt .Errorf ("not running in cluster, please use --install-namespace to specify the namespace in which the agent is running" )
735- }
736- if err != nil {
737- return "" , fmt .Errorf ("error checking namespace file: %w" , err )
724+ ns := os .Getenv ("POD_NAMESPACE" )
725+ if ns != "" {
726+ return ns , nil
738727 }
739728
740- namespace , err := os .ReadFile (inClusterNamespacePath )
741- if err != nil {
742- return "" , fmt .Errorf ("error reading namespace file: %w" , err )
743- }
744- return string (namespace ), nil
729+ return "" , fmt .Errorf ("POD_NAMESPACE env var not set, meaning that you are probably not running in cluster. Please use --install-namespace or POD_NAMESPACE to specify the namespace in which the agent is running." )
745730}
746731
747732func reMarshal (rawConfig interface {}, config datagatherer.Config ) error {
0 commit comments