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

refactor: Move Hooks to the api package #2400

Merged
merged 1 commit into from Mar 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
34 changes: 34 additions & 0 deletions api/api.go
Expand Up @@ -6,7 +6,13 @@ package api
import (
"context"

"github.com/spf13/cobra"
"github.com/spf13/pflag"

"github.com/cilium/cilium-cli/connectivity"
"github.com/cilium/cilium-cli/connectivity/check"
"github.com/cilium/cilium-cli/k8s"
"github.com/cilium/cilium-cli/sysdump"
)

// key is a context.Context value key type. It's unexported to avoid key collisions.
Expand Down Expand Up @@ -44,3 +50,31 @@ func GetK8sClientContextValue(ctx context.Context) (*k8s.Client, bool) {
client, ok := ctx.Value(k8sClientKey).(*k8s.Client)
return client, ok
}

// Hooks to extend the default cilium-cli command with additional functionality.
type Hooks interface {
ConnectivityTestHooks
sysdump.Hooks
// InitializeCommand gets called with the root command before returning it
// from cli.NewCiliumCommand.
InitializeCommand(rootCmd *cobra.Command)
}

// ConnectivityTestHooks to extend cilium-cli with additional connectivity tests and related flags.
type ConnectivityTestHooks interface {
connectivity.Hooks
// AddConnectivityTestFlags is an hook to register additional connectivity test flags.
AddConnectivityTestFlags(flags *pflag.FlagSet)
}

type NopHooks struct{}

var _ Hooks = &NopHooks{}

func (*NopHooks) AddSysdumpFlags(*pflag.FlagSet) {}
func (*NopHooks) AddSysdumpTasks(*sysdump.Collector) error { return nil }
func (*NopHooks) AddConnectivityTestFlags(*pflag.FlagSet) {}
func (*NopHooks) AddConnectivityTests(*check.ConnectivityTest) error { return nil }
func (*NopHooks) DetectFeatures(context.Context, *check.ConnectivityTest) error { return nil }
func (*NopHooks) SetupAndValidate(context.Context, *check.ConnectivityTest) error { return nil }
func (*NopHooks) InitializeCommand(*cobra.Command) {}
23 changes: 2 additions & 21 deletions cli/cmd.go
Expand Up @@ -9,12 +9,9 @@ import (
"os"

"github.com/spf13/cobra"
"github.com/spf13/pflag"

"github.com/cilium/cilium-cli/api"
"github.com/cilium/cilium-cli/connectivity/check"
"github.com/cilium/cilium-cli/k8s"
"github.com/cilium/cilium-cli/sysdump"
)

var (
Expand All @@ -26,11 +23,11 @@ var (

// NewDefaultCiliumCommand returns a new "cilium" cli cobra command without any additional hooks.
func NewDefaultCiliumCommand() *cobra.Command {
return NewCiliumCommand(&NopHooks{})
return NewCiliumCommand(&api.NopHooks{})
}

// NewCiliumCommand returns a new "cilium" cli cobra command registering all the additional input hooks.
func NewCiliumCommand(hooks Hooks) *cobra.Command {
func NewCiliumCommand(hooks api.Hooks) *cobra.Command {
cmd := &cobra.Command{
PersistentPreRunE: func(cmd *cobra.Command, _ []string) error {
// return early for commands that don't require the kubernetes client
Expand Down Expand Up @@ -104,19 +101,3 @@ cilium connectivity test`,
hooks.InitializeCommand(cmd)
return cmd
}

type (
SysdumpHooks = sysdump.Hooks
)

type NopHooks struct{}

var _ Hooks = &NopHooks{}

func (*NopHooks) AddSysdumpFlags(*pflag.FlagSet) {}
func (*NopHooks) AddSysdumpTasks(*sysdump.Collector) error { return nil }
func (*NopHooks) AddConnectivityTestFlags(*pflag.FlagSet) {}
func (*NopHooks) AddConnectivityTests(*check.ConnectivityTest) error { return nil }
func (*NopHooks) DetectFeatures(context.Context, *check.ConnectivityTest) error { return nil }
func (*NopHooks) SetupAndValidate(context.Context, *check.ConnectivityTest) error { return nil }
func (*NopHooks) InitializeCommand(*cobra.Command) {}
4 changes: 3 additions & 1 deletion cli/cmd_test.go
Expand Up @@ -8,12 +8,14 @@ import (

"github.com/spf13/cobra"
"github.com/stretchr/testify/assert"

"github.com/cilium/cilium-cli/api"
)

const usage = "override usage"

type testHooks struct {
NopHooks
api.NopHooks
}

func (th *testHooks) InitializeCommand(rootCmd *cobra.Command) {
Expand Down
9 changes: 5 additions & 4 deletions cli/connectivity.go
Expand Up @@ -17,6 +17,7 @@ import (
"github.com/spf13/cobra"
"github.com/spf13/pflag"

"github.com/cilium/cilium-cli/api"
"github.com/cilium/cilium-cli/connectivity"
"github.com/cilium/cilium-cli/connectivity/check"
"github.com/cilium/cilium-cli/defaults"
Expand All @@ -25,7 +26,7 @@ import (

var errInternal = errors.New("encountered internal error, exiting")

func newCmdConnectivity(hooks Hooks) *cobra.Command {
func newCmdConnectivity(hooks api.Hooks) *cobra.Command {
cmd := &cobra.Command{
Use: "connectivity",
Short: "Connectivity troubleshooting",
Expand All @@ -49,7 +50,7 @@ var params = check.Parameters{

var tests []string

func RunE(hooks Hooks) func(cmd *cobra.Command, args []string) error {
func RunE(hooks api.Hooks) func(cmd *cobra.Command, args []string) error {
return func(cmd *cobra.Command, _ []string) error {
params.CiliumNamespace = namespace

Expand Down Expand Up @@ -115,7 +116,7 @@ func RunE(hooks Hooks) func(cmd *cobra.Command, args []string) error {
}
}

func newCmdConnectivityTest(hooks Hooks) *cobra.Command {
func newCmdConnectivityTest(hooks api.Hooks) *cobra.Command {
cmd := &cobra.Command{
Use: "test",
Short: "Validate connectivity in cluster",
Expand Down Expand Up @@ -202,7 +203,7 @@ func newCmdConnectivityTest(hooks Hooks) *cobra.Command {
return cmd
}

func newCmdConnectivityPerf(hooks Hooks) *cobra.Command {
func newCmdConnectivityPerf(hooks api.Hooks) *cobra.Command {
cmd := &cobra.Command{
Use: "perf",
Short: "Test network performance",
Expand Down
28 changes: 0 additions & 28 deletions cli/hooks.go

This file was deleted.