@@ -23,10 +23,6 @@ import (
23
23
"github.com/jetstack/preflight/pkg/version"
24
24
)
25
25
26
- const (
27
- inClusterNamespacePath = "/var/run/secrets/kubernetes.io/serviceaccount/namespace"
28
- )
29
-
30
26
// Config wraps the options for a run of the agent.
31
27
type Config struct {
32
28
// Deprecated: Schedule doesn't do anything. Use `period` instead.
@@ -154,9 +150,8 @@ type AgentCmdFlags struct {
154
150
// InstallNS (--install-namespace) is the namespace in which the agent is
155
151
// running in. Only needed when running the agent outside of Kubernetes.
156
152
//
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`.
160
155
InstallNS string
161
156
162
157
// Profiling (--enable-pprof) enables the pprof server.
@@ -273,8 +268,7 @@ func InitAgentCmdFlags(c *cobra.Command, cfg *AgentCmdFlags) {
273
268
"install-namespace" ,
274
269
"" ,
275
270
"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." ,
278
272
)
279
273
c .PersistentFlags ().BoolVarP (
280
274
& cfg .Profiling ,
@@ -314,6 +308,7 @@ type CombinedConfig struct {
314
308
BackoffMaxTime time.Duration
315
309
StrictMode bool
316
310
OneShot bool
311
+ InstallNS string
317
312
318
313
// Used by JetstackSecureOAuth, JetstackSecureAPIToken, and
319
314
// VenafiCloudKeypair. Ignored in VenafiCloudVenafiConnection mode.
@@ -330,7 +325,6 @@ type CombinedConfig struct {
330
325
// VenafiCloudVenafiConnection mode only.
331
326
VenConnName string
332
327
VenConnNS string
333
- InstallNS string
334
328
335
329
// Only used for testing purposes.
336
330
OutputPath string
@@ -530,20 +524,20 @@ func ValidateAndCombineConfig(log *log.Logger, cfg Config, flags AgentCmdFlags)
530
524
res .StrictMode = flags .StrictMode
531
525
}
532
526
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 ))
543
534
}
544
- res . InstallNS = installNS
545
- res .VenConnName = flags . VenConnName
535
+ }
536
+ res .InstallNS = installNS
546
537
538
+ // Validation of --venafi-connection and --venafi-connection-namespace.
539
+ if res .AuthMode == VenafiCloudVenafiConnection {
540
+ res .VenConnName = flags .VenConnName
547
541
var venConnNS string = flags .VenConnNS
548
542
if flags .VenConnNS == "" {
549
543
venConnNS = installNS
@@ -727,21 +721,12 @@ func createCredentialClient(log *log.Logger, credentials client.Credentials, cfg
727
721
728
722
// Inspired by the controller-runtime project.
729
723
func 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
738
727
}
739
728
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." )
745
730
}
746
731
747
732
func reMarshal (rawConfig interface {}, config datagatherer.Config ) error {
0 commit comments