Skip to content

Commit 23d22aa

Browse files
committed
tests: add unit tests to the code that loads config/flags
The config is also no longer dumped at startup. It used to do something like: dump, err := config.Dump() I don't think there is a point in dumping the whole content of a file... it's super verbose and not very useful. It should at least be gated behind a --debug flag. So I propose to remove it for now.
1 parent 1459adf commit 23d22aa

File tree

10 files changed

+1122
-865
lines changed

10 files changed

+1122
-865
lines changed

cmd/agent.go

Lines changed: 5 additions & 120 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,9 @@ package cmd
33
import (
44
"fmt"
55
"io/ioutil"
6-
"os"
7-
"time"
86

97
"github.com/jetstack/preflight/pkg/agent"
8+
"github.com/jetstack/preflight/pkg/agent/config"
109
"github.com/jetstack/preflight/pkg/logs"
1110
"github.com/jetstack/preflight/pkg/permissions"
1211
"github.com/spf13/cobra"
@@ -37,16 +36,16 @@ var agentRBACCmd = &cobra.Command{
3736
Long: `Print RBAC string by reading GVRs`,
3837
Run: func(cmd *cobra.Command, args []string) {
3938

40-
b, err := ioutil.ReadFile(agent.ConfigFilePath)
39+
b, err := ioutil.ReadFile(agent.AgentCmdFlags.ConfigFilePath)
4140
if err != nil {
4241
logs.Log.Fatalf("Failed to read config file: %s", err)
4342
}
44-
config, err := agent.ParseConfig(b, false)
43+
cfg, err := config.ParseConfig(b, false)
4544
if err != nil {
4645
logs.Log.Fatalf("Failed to parse config file: %s", err)
4746
}
4847

49-
out := permissions.GenerateFullManifest(config.DataGatherers)
48+
out := permissions.GenerateFullManifest(cfg.DataGatherers)
5049
fmt.Print(out)
5150
},
5251
}
@@ -55,119 +54,5 @@ func init() {
5554
rootCmd.AddCommand(agentCmd)
5655
agentCmd.AddCommand(agentInfoCmd)
5756
agentCmd.AddCommand(agentRBACCmd)
58-
agentCmd.PersistentFlags().StringVarP(
59-
&agent.ConfigFilePath,
60-
"agent-config-file",
61-
"c",
62-
"./agent.yaml",
63-
"Config file location, default is `agent.yaml` in the current working directory.",
64-
)
65-
agentCmd.PersistentFlags().DurationVarP(
66-
&agent.Period,
67-
"period",
68-
"p",
69-
0,
70-
"Override time between scans in the configuration file (given as XhYmZs).",
71-
)
72-
agentCmd.PersistentFlags().StringVarP(
73-
&agent.CredentialsPath,
74-
"credentials-file",
75-
"k",
76-
"",
77-
"Location of the credentials file. For OAuth2 based authentication.",
78-
)
79-
agentCmd.PersistentFlags().BoolVarP(
80-
&agent.VenafiCloudMode,
81-
"venafi-cloud",
82-
"",
83-
false,
84-
"Runs agent with parsing config (and credentials file if provided) in Venafi Cloud format if true.",
85-
)
86-
agentCmd.PersistentFlags().StringVarP(
87-
&agent.ClientID,
88-
"client-id",
89-
"",
90-
"",
91-
"Venafi Cloud Service Account client ID. If you use this flag you don't need to use --venafi-cloud as it will assume you are authenticating against Venafi Cloud. Using this removes the need to use a credentials file with Venafi Cloud mode.",
92-
)
93-
agentCmd.PersistentFlags().StringVarP(
94-
&agent.PrivateKeyPath,
95-
"private-key-path",
96-
"",
97-
"/etc/venafi/agent/key/privatekey.pem",
98-
"Venafi Cloud Service Account private key path.",
99-
)
100-
agentCmd.PersistentFlags().BoolVarP(
101-
&agent.OneShot,
102-
"one-shot",
103-
"",
104-
false,
105-
"Runs agent a single time if true, or continously if false",
106-
)
107-
agentCmd.PersistentFlags().StringVarP(
108-
&agent.OutputPath,
109-
"output-path",
110-
"",
111-
"",
112-
"Output file path, if used, it will write data to a local file instead of uploading to the preflight server",
113-
)
114-
agentCmd.PersistentFlags().StringVarP(
115-
&agent.InputPath,
116-
"input-path",
117-
"",
118-
"",
119-
"Input file path, if used, it will read data from a local file instead of gathering data from clusters",
120-
)
121-
agentCmd.PersistentFlags().DurationVarP(
122-
&agent.BackoffMaxTime,
123-
"backoff-max-time",
124-
"",
125-
10*time.Minute,
126-
"Max time for retrying failed data gatherers (given as XhYmZs).",
127-
)
128-
agentCmd.PersistentFlags().BoolVarP(
129-
&agent.StrictMode,
130-
"strict",
131-
"",
132-
false,
133-
"Runs agent in strict mode. No retry attempts will be made for a missing data gatherer's data.",
134-
)
135-
agentCmd.PersistentFlags().StringVar(
136-
&agent.APIToken,
137-
"api-token",
138-
os.Getenv("API_TOKEN"),
139-
"Token used for authentication when API tokens are in use on the backend",
140-
)
141-
agentCmd.PersistentFlags().StringVar(
142-
&agent.VenConnName,
143-
"venafi-connection",
144-
"",
145-
"Name of the VenafiConnection to be used. Using this flag will enable the VenafiConnection mode.",
146-
)
147-
agentCmd.PersistentFlags().StringVar(
148-
&agent.VenConnNS,
149-
"venafi-connection-namespace",
150-
"",
151-
"Namespace of the VenafiConnection to be used. It is only useful when the VenafiConnection isn't in the same namespace as the agent. The field `allowReferencesFrom` must be present on the cross-namespace VenafiConnection for the agent to use it.",
152-
)
153-
agentCmd.PersistentFlags().StringVar(
154-
&agent.InstallNS,
155-
"install-namespace",
156-
"",
157-
"Namespace in which the agent is running. Only needed when running the agent outside of Kubernetes. Used for testing purposes.",
158-
)
159-
agentCmd.PersistentFlags().BoolVarP(
160-
&agent.Profiling,
161-
"enable-pprof",
162-
"",
163-
false,
164-
"Enables the pprof profiling server on the agent (port: 6060).",
165-
)
166-
agentCmd.PersistentFlags().BoolVarP(
167-
&agent.Prometheus,
168-
"enable-metrics",
169-
"",
170-
false,
171-
"Enables Prometheus metrics server on the agent (port: 8081).",
172-
)
57+
config.InitAgentCmdFlags(agentCmd, &agent.AgentCmdFlags)
17358
}

pkg/agent/config.go

Lines changed: 0 additions & 196 deletions
This file was deleted.

0 commit comments

Comments
 (0)