Skip to content

Commit 311b704

Browse files
leogrpoiana
authored andcommitted
chore(cmd): init kube flags only when needed
Signed-off-by: Leonardo Grasso <[email protected]>
1 parent f9b9f8d commit 311b704

File tree

5 files changed

+23
-10
lines changed

5 files changed

+23
-10
lines changed

cmd/delete.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ limitations under the License.
1717
package cmd
1818

1919
import (
20-
"github.com/falcosecurity/falcoctl/pkg/kubernetes/factory"
2120
"github.com/spf13/cobra"
2221
"k8s.io/cli-runtime/pkg/genericclioptions"
2322
)
@@ -40,7 +39,7 @@ func NewDeleteOptions(streams genericclioptions.IOStreams) CommandOptions {
4039
}
4140

4241
// Delete creates the `delete` command
43-
func Delete(streams genericclioptions.IOStreams, f factory.Factory) *cobra.Command {
42+
func Delete(streams genericclioptions.IOStreams) *cobra.Command {
4443
// o := NewDeleteOptions(streams).(*DeleteOptions)
4544

4645
cmd := &cobra.Command{
@@ -50,7 +49,7 @@ func Delete(streams genericclioptions.IOStreams, f factory.Factory) *cobra.Comma
5049
Long: `Delete a component with falcoctl`,
5150
}
5251

53-
cmd.AddCommand(DeleteFalco(streams, f))
52+
cmd.AddCommand(DeleteFalco(streams))
5453

5554
return cmd
5655
}

cmd/delete_falco.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,17 +47,18 @@ func NewFalcoDeleteOptions(streams genericclioptions.IOStreams) CommandOptions {
4747
}
4848

4949
// DeleteFalco creates the `delete falco` command
50-
func DeleteFalco(streams genericclioptions.IOStreams, f factory.Factory) *cobra.Command {
50+
func DeleteFalco(streams genericclioptions.IOStreams) *cobra.Command {
5151
o := NewFalcoDeleteOptions(streams).(*FalcoDeleteOptions)
5252

53+
var clientGetter genericclioptions.RESTClientGetter
5354
cmd := &cobra.Command{
5455
Use: "falco",
5556
DisableFlagsInUseLine: true,
5657
Short: "Delete Falco from Kubernetes",
5758
Long: `Delete Falco from Kubernetes`,
5859
Run: func(cmd *cobra.Command, args []string) {
5960
// todo > pass daemonset name using o.daemonSetName
60-
installer, err := kubernetesfalc.NewFalcoInstaller(f)
61+
installer, err := kubernetesfalc.NewFalcoInstaller(factory.New(clientGetter))
6162
if err != nil {
6263
logger.Critical("Fatal error: %v", err)
6364
os.Exit(1)
@@ -70,6 +71,7 @@ func DeleteFalco(streams genericclioptions.IOStreams, f factory.Factory) *cobra.
7071
},
7172
}
7273

74+
clientGetter = initKubeFlags(cmd.PersistentFlags())
7375
cmd.Flags().StringVarP(&o.daemonSetName, "daemonset-name", "D", o.daemonSetName, "Set the name to use with the Falco DaemonSet")
7476

7577
return cmd

cmd/install.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ limitations under the License.
1717
package cmd
1818

1919
import (
20-
"github.com/falcosecurity/falcoctl/pkg/kubernetes/factory"
2120
"github.com/spf13/cobra"
2221
"k8s.io/cli-runtime/pkg/genericclioptions"
2322
)
@@ -40,7 +39,7 @@ func NewInstallOptions(streams genericclioptions.IOStreams) CommandOptions {
4039
}
4140

4241
// Install creates the `install` command
43-
func Install(streams genericclioptions.IOStreams, f factory.Factory) *cobra.Command {
42+
func Install(streams genericclioptions.IOStreams) *cobra.Command {
4443
// o := NewInstallOptions(streams).(*InstallOptions)
4544

4645
cmd := &cobra.Command{
@@ -51,7 +50,7 @@ func Install(streams genericclioptions.IOStreams, f factory.Factory) *cobra.Comm
5150
Long: `Install a component with falcoctl`,
5251
}
5352

54-
cmd.AddCommand(InstallFalco(streams, f))
53+
cmd.AddCommand(InstallFalco(streams))
5554
cmd.AddCommand(InstallModule(streams))
5655
cmd.AddCommand(InstallTLS(streams))
5756
cmd.AddCommand(InstallRule(streams))

cmd/install_falco.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,10 @@ func NewFalcoOptions(streams genericclioptions.IOStreams) CommandOptions {
4646
}
4747

4848
// InstallFalco creates the `install falco` command
49-
func InstallFalco(streams genericclioptions.IOStreams, f factory.Factory) *cobra.Command {
49+
func InstallFalco(streams genericclioptions.IOStreams) *cobra.Command {
5050
o := NewFalcoOptions(streams).(*FalcoOptions)
5151

52+
var clientGetter genericclioptions.RESTClientGetter
5253
cmd := &cobra.Command{
5354
Use: "falco",
5455
TraverseChildren: true,
@@ -57,7 +58,7 @@ func InstallFalco(streams genericclioptions.IOStreams, f factory.Factory) *cobra
5758
Long: `Deploy Falco to Kubernetes`,
5859
Run: func(cmd *cobra.Command, args []string) {
5960
// todo > pass daemonset name using o.daemonSetName
60-
installer, err := kubernetesfalc.NewFalcoInstaller(f)
61+
installer, err := kubernetesfalc.NewFalcoInstaller(factory.New(clientGetter))
6162
if err != nil {
6263
logger.Critical("Fatal error: %v", err)
6364
os.Exit(1)
@@ -70,6 +71,7 @@ func InstallFalco(streams genericclioptions.IOStreams, f factory.Factory) *cobra
7071
},
7172
}
7273

74+
clientGetter = initKubeFlags(cmd.PersistentFlags())
7375
cmd.Flags().StringVarP(&o.daemonSetName, "daemonset-name", "D", o.daemonSetName, "Set the name to use with the Falco DaemonSet")
7476

7577
return cmd

cmd/root.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,11 @@ import (
2222

2323
"k8s.io/cli-runtime/pkg/genericclioptions"
2424

25+
"github.com/falcosecurity/falcoctl/pkg/kubernetes/factory"
2526
"github.com/kris-nova/logger"
2627
"github.com/mitchellh/go-homedir"
2728
"github.com/spf13/cobra"
29+
"github.com/spf13/pflag"
2830
"github.com/spf13/viper"
2931
)
3032

@@ -106,3 +108,12 @@ func initConfig(configOptions *ConfigOptions) {
106108
}
107109
}
108110
}
111+
112+
func initKubeFlags(flags *pflag.FlagSet) genericclioptions.RESTClientGetter {
113+
configFlags := genericclioptions.NewConfigFlags(false)
114+
configFlags.AddFlags(flags)
115+
116+
matchVersionFlags := factory.MatchVersion(configFlags)
117+
matchVersionFlags.AddFlags(flags)
118+
return matchVersionFlags
119+
}

0 commit comments

Comments
 (0)