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

hooks: Add new hook SetupAndValidate() #1816

Merged
merged 3 commits into from
Jul 12, 2023
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions cli/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
package cli

import (
"context"

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

Expand Down Expand Up @@ -39,7 +41,8 @@ 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) 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) SetupAndValidate(context.Context, *check.ConnectivityTest) error { return nil }
2 changes: 1 addition & 1 deletion connectivity/check/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -1054,7 +1054,7 @@ func (a *Action) validateMetric(ctx context.Context, node string, result Metrics
select {
case <-ctx.Done():
// Context timeout is reached, let's exit.
a.Failf("failed to collect metrics on node %s, context timeout: %w", node, ctx.Err().Error())
a.Failf("failed to collect metrics on node %s, context timeout: %s\n", node, ctx.Err())
return
case <-ticker.C:
// Ticker is delivered, let's retry.
Expand Down
6 changes: 5 additions & 1 deletion connectivity/check/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ func (ct *ConnectivityTest) GetTest(name string) (*Test, error) {
// SetupAndValidate sets up and validates the connectivity test infrastructure
// such as the client pods and validates the deployment of them along with
// Cilium. This must be run before Run() is called.
func (ct *ConnectivityTest) SetupAndValidate(ctx context.Context) error {
func (ct *ConnectivityTest) SetupAndValidate(ctx context.Context, setupAndValidateExtras func(ctx context.Context, ct *ConnectivityTest) error) error {
if err := ctx.Err(); err != nil {
return err
}
Expand All @@ -278,6 +278,10 @@ func (ct *ConnectivityTest) SetupAndValidate(ctx context.Context) error {
if err := ct.detectFeatures(ctx); err != nil {
return err
}
// Setup and validate all the extras coming from extended functionalities.
if err := setupAndValidateExtras(ctx, ct); err != nil {
return err
}
if err := ct.getNodes(ctx); err != nil {
return err
}
Expand Down
5 changes: 5 additions & 0 deletions connectivity/check/peer.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,11 @@ func (s Service) Name() string {
return s.Service.Namespace + "/" + s.Service.Name
}

// NameWithoutNamespace returns the name of the service without the namespace.
func (s Service) NameWithoutNamespace() string {
return s.Service.Name
}

// Scheme returns the string 'http'.
func (s Service) Scheme() string {
// We only have http services for now.
Expand Down
5 changes: 3 additions & 2 deletions connectivity/suite.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,8 +169,9 @@ var (
client2Label = map[string]string{"name": "client2"}
)

func Run(ctx context.Context, ct *check.ConnectivityTest, addExtraTests func(*check.ConnectivityTest) error) error {
if err := ct.SetupAndValidate(ctx); err != nil {
func Run(ctx context.Context, ct *check.ConnectivityTest, addExtraTests func(*check.ConnectivityTest) error,
addExtraSetup func(context.Context, *check.ConnectivityTest) error) error {
if err := ct.SetupAndValidate(ctx, addExtraSetup); err != nil {
return err
}

Expand Down
2 changes: 1 addition & 1 deletion internal/cli/cmd/connectivity.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ func newCmdConnectivityTest(hooks Hooks) *cobra.Command {
// and end the goroutine without returning.
go func() {
defer func() { done <- struct{}{} }()
err = connectivity.Run(ctx, cc, hooks.AddConnectivityTests)
err = connectivity.Run(ctx, cc, hooks.AddConnectivityTests, hooks.SetupAndValidate)

// If Fatal() was called in the test suite, the statement below won't fire.
finished = true
Expand Down
3 changes: 3 additions & 0 deletions internal/cli/cmd/hooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
package cmd

import (
"context"

"github.com/spf13/pflag"

"github.com/cilium/cilium-cli/connectivity/check"
Expand All @@ -18,6 +20,7 @@ type Hooks interface {

// ConnectivityTestHooks to extend cilium-cli with additional connectivity tests and related flags.
type ConnectivityTestHooks interface {
SetupAndValidate(ctx context.Context, ct *check.ConnectivityTest) error
AddConnectivityTestFlags(flags *pflag.FlagSet)
AddConnectivityTests(ct *check.ConnectivityTest) error
}
Expand Down