Skip to content

VC-35630: Add unit tests to the code that loads config/flags #563

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Aug 26, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
124 changes: 4 additions & 120 deletions cmd/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ package cmd
import (
"fmt"
"io/ioutil"
"os"
"time"

"github.com/jetstack/preflight/pkg/agent"
"github.com/jetstack/preflight/pkg/logs"
Expand Down Expand Up @@ -37,16 +35,16 @@ var agentRBACCmd = &cobra.Command{
Long: `Print RBAC string by reading GVRs`,
Run: func(cmd *cobra.Command, args []string) {

b, err := ioutil.ReadFile(agent.ConfigFilePath)
b, err := ioutil.ReadFile(agent.Flags.ConfigFilePath)
if err != nil {
logs.Log.Fatalf("Failed to read config file: %s", err)
}
config, err := agent.ParseConfig(b, false)
cfg, err := agent.ParseConfig(b, false)
if err != nil {
logs.Log.Fatalf("Failed to parse config file: %s", err)
}

out := permissions.GenerateFullManifest(config.DataGatherers)
out := permissions.GenerateFullManifest(cfg.DataGatherers)
fmt.Print(out)
},
}
Expand All @@ -55,119 +53,5 @@ func init() {
rootCmd.AddCommand(agentCmd)
agentCmd.AddCommand(agentInfoCmd)
agentCmd.AddCommand(agentRBACCmd)
agentCmd.PersistentFlags().StringVarP(
&agent.ConfigFilePath,
"agent-config-file",
"c",
"./agent.yaml",
"Config file location, default is `agent.yaml` in the current working directory.",
)
agentCmd.PersistentFlags().DurationVarP(
&agent.Period,
"period",
"p",
0,
"Override time between scans in the configuration file (given as XhYmZs).",
)
agentCmd.PersistentFlags().StringVarP(
&agent.CredentialsPath,
"credentials-file",
"k",
"",
"Location of the credentials file. For OAuth2 based authentication.",
)
agentCmd.PersistentFlags().BoolVarP(
&agent.VenafiCloudMode,
"venafi-cloud",
"",
false,
"Runs agent with parsing config (and credentials file if provided) in Venafi Cloud format if true.",
)
agentCmd.PersistentFlags().StringVarP(
&agent.ClientID,
"client-id",
"",
"",
"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.",
)
agentCmd.PersistentFlags().StringVarP(
&agent.PrivateKeyPath,
"private-key-path",
"",
"/etc/venafi/agent/key/privatekey.pem",
"Venafi Cloud Service Account private key path.",
)
agentCmd.PersistentFlags().BoolVarP(
&agent.OneShot,
"one-shot",
"",
false,
"Runs agent a single time if true, or continously if false",
)
agentCmd.PersistentFlags().StringVarP(
&agent.OutputPath,
"output-path",
"",
"",
"Output file path, if used, it will write data to a local file instead of uploading to the preflight server",
)
agentCmd.PersistentFlags().StringVarP(
&agent.InputPath,
"input-path",
"",
"",
"Input file path, if used, it will read data from a local file instead of gathering data from clusters",
)
agentCmd.PersistentFlags().DurationVarP(
&agent.BackoffMaxTime,
"backoff-max-time",
"",
10*time.Minute,
"Max time for retrying failed data gatherers (given as XhYmZs).",
)
agentCmd.PersistentFlags().BoolVarP(
&agent.StrictMode,
"strict",
"",
false,
"Runs agent in strict mode. No retry attempts will be made for a missing data gatherer's data.",
)
agentCmd.PersistentFlags().StringVar(
&agent.APIToken,
"api-token",
os.Getenv("API_TOKEN"),
"Token used for authentication when API tokens are in use on the backend",
)
agentCmd.PersistentFlags().StringVar(
&agent.VenConnName,
"venafi-connection",
"",
"Name of the VenafiConnection to be used. Using this flag will enable the VenafiConnection mode.",
)
agentCmd.PersistentFlags().StringVar(
&agent.VenConnNS,
"venafi-connection-namespace",
"",
"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.",
)
agentCmd.PersistentFlags().StringVar(
&agent.InstallNS,
"install-namespace",
"",
"Namespace in which the agent is running. Only needed when running the agent outside of Kubernetes. Used for testing purposes.",
)
agentCmd.PersistentFlags().BoolVarP(
&agent.Profiling,
"enable-pprof",
"",
false,
"Enables the pprof profiling server on the agent (port: 6060).",
)
agentCmd.PersistentFlags().BoolVarP(
&agent.Prometheus,
"enable-metrics",
"",
false,
"Enables Prometheus metrics server on the agent (port: 8081).",
)
agent.InitAgentCmdFlags(agentCmd, &agent.Flags)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Self-review: I've moved the definition of the flags for the agent command to the config package so that the struct and the flags they relate to are defined in the same file. I find it easier to keep things consistent this way.

}
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ require (
github.com/spf13/cobra v1.8.0
github.com/spf13/pflag v1.0.5
github.com/stretchr/testify v1.9.0
gopkg.in/d4l3k/messagediff.v1 v1.2.1
gopkg.in/yaml.v2 v2.4.0
k8s.io/api v0.30.3
k8s.io/apimachinery v0.30.3
Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -280,8 +280,6 @@ google.golang.org/protobuf v1.34.2/go.mod h1:qYOHts0dSfpeUzUFpOMr/WGzszTmLH+DiWn
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
gopkg.in/d4l3k/messagediff.v1 v1.2.1 h1:70AthpjunwzUiarMHyED52mj9UwtAnE89l1Gmrt3EU0=
gopkg.in/d4l3k/messagediff.v1 v1.2.1/go.mod h1:EUzikiKadqXWcD1AzJLagx0j/BeeWGtn++04Xniyg44=
gopkg.in/inf.v0 v0.9.1 h1:73M5CoZyi3ZLMOyDlQh031Cx6N9NDJ2Vvfl76EDAgDc=
gopkg.in/inf.v0 v0.9.1/go.mod h1:cWUDdTG/fYaXco+Dcufb5Vnc6Gp2YChqWtbxRZE0mXw=
gopkg.in/ini.v1 v1.67.0 h1:Dgnx+6+nfE+IfzjUEISNeydPJh9AXNNsWbGP9KzCsOA=
Expand Down
Loading