Skip to content
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

cmd: Support a Configuration File #2831

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft

Conversation

mqasimsarfraz
Copy link
Member

@mqasimsarfraz mqasimsarfraz commented May 10, 2024

Support a Configuration File for kubectl-gadget, gadgetctl and ig. It will also allow to specify kubectl gadget deploy flags as config file (should it be done) and defines following priority order:

  1. Flags
  2. Config file [/etc/ig/config.yaml, $HOME/.ig/config.yaml, ./config.yaml]
  3. Defaults

Also, it is print a debug log for unused keys to avoid surprises.

Related: #2493

Testing Done

$ echo 'verbose: true' > ~/.ig/config.yaml 
$ go run ./cmd/kubectl-gadget snapshot process
INFO[0000] Experimental features enabled                
DEBU[0000] Using config file: /home/qasim/.ig/config.yaml 
K8S.NODE                               K8S.NAMESPACE                         K8S.POD                               K8S.CONTAINER                         COMM             PID                 UID        GID       
DEBU[0000] Params                                       
DEBU[0000] - operator.KubeManager.podname: ""           
DEBU[0000] - operator.KubeManager.namespace: "default"  


$ echo 'broken: key' >> ~/.ig/config.yaml
$ go run ./cmd/kubectl-gadget snapshot process
INFO[0000] Experimental features enabled                
DEBU[0000] Using config file: /home/qasim/.ig/config.yaml 
DEBU[0000] Ignoring unused config key: broken 

TODO:

  • Add documentation
  • Handle flags for run command

  • Handle config for ig-k8s, I want to keep the scope of current PR to CLIs only.

Signed-off-by: Qasim Sarfraz <qasimsarfraz@microsoft.com>
Signed-off-by: Qasim Sarfraz <qasimsarfraz@microsoft.com>
Signed-off-by: Qasim Sarfraz <qasimsarfraz@microsoft.com>
Copy link
Member

@eiffel-fl eiffel-fl left a comment

Choose a reason for hiding this comment

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

Hi!

This is a great addition as it will ease Inspektor Gadget usage.
I have some comments, I will test everything later.

Best regards.

Comment on lines +46 to +47
// we don't care about the error here, as the config file is optional
v.ReadInConfig()
Copy link
Member

Choose a reason for hiding this comment

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

Hum...
I would still print a warning, I would like to avoid a bad situation where not printing this message could lead to really bad stuff occurring.

}

func bindFlags(command *cobra.Command, v *viper.Viper) error {
command.Flags().VisitAll(func(f *pflag.Flag) {
Copy link
Member

Choose a reason for hiding this comment

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

Does this return an error? If so, catch it and return it.
Otherwise, this function never returns an error, and the above test will never be true.

Comment on lines +34 to +36
// 1. Flags
// 2. Config file [/etc/ig/config.yaml, $HOME/.ig/config.yaml, ./config.yaml]
// 3. Defaults
Copy link
Member

Choose a reason for hiding this comment

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

Do we also need to deal with environment variables here?

Comment on lines +41 to +53
{
name: "flags precedence over defaults",
args: []string{"--flag", "fvalue", "--flag-with-default", "fvalue"},
wantFlag: "fvalue",
wantFlagDefault: "fvalue",
},
{
name: "flag precedence over config",
args: []string{"--flag", "fvalue", "--flag-with-default", "fvalue"},
config: testConfig,
wantFlag: "fvalue",
wantFlagDefault: "fvalue",
},
Copy link
Member

Choose a reason for hiding this comment

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

Currently, these tests are the same.
Indeed, you create the config in all cases.
So, in order, you will get:

  1. The default values.
  2. The config ones.
  3. The flag ones.

For the first test, you will need a test without any config or an empty config.
To ease everything, you can add a member config to the test case and handle it in the execution of the test only if present.

Comment on lines +65 to +66
if cmd.Flags().Lookup(key) == nil {
log.Debugf("Ignoring unused config key: %s", key)
Copy link
Member

Choose a reason for hiding this comment

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

If I understand correctly, this is equivalent to giving an unknown flag on the CLI?
If that so, we should rather explode, as it is the case with unknown flag.

@mqasimsarfraz
Copy link
Member Author

This is a great addition as it will ease Inspektor Gadget usage.

Thanks for the feedback!

I have some comments, I will test everything later.

Just fyi, I am planning to make some adjustments (like introducing a config package) so perhaps wait a bit till I push those changes, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants