Skip to content

Commit

Permalink
Added timeout option
Browse files Browse the repository at this point in the history
  • Loading branch information
yutachaos committed Oct 19, 2024
1 parent a49927c commit 99c05ec
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 4 deletions.
3 changes: 3 additions & 0 deletions cmd/kubefwd/services/services.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ var mappings []string
var isAllNs bool
var fwdConfigurationPath string
var fwdReservations []string
var timeout int

func init() {
// override error output from k8s.io/apimachinery/pkg/util/runtime
Expand All @@ -78,6 +79,7 @@ func init() {
Cmd.Flags().BoolVarP(&isAllNs, "all-namespaces", "A", false, "Enable --all-namespaces option like kubectl.")
Cmd.Flags().StringSliceVarP(&fwdReservations, "reserve", "r", []string{}, "Specify an IP reservation. Specify multiple reservations by duplicating this argument.")
Cmd.Flags().StringVarP(&fwdConfigurationPath, "fwd-conf", "z", "", "Define an IP reservation configuration")
Cmd.Flags().IntVarP(&timeout, "timeout", "t", 300, "Specify a timeout seconds for the port forwarding.")

}

Expand Down Expand Up @@ -432,6 +434,7 @@ func (opts *NamespaceOpts) AddServiceHandler(obj interface{}) {
ClientSet: opts.ClientSet,
Context: opts.Context,
Namespace: opts.Namespace,
Timeout: timeout,
Hostfile: opts.HostFile,
ClientConfig: opts.ClientConfig,
RESTClient: opts.RESTClient,
Expand Down
7 changes: 4 additions & 3 deletions pkg/fwdport/fwdport.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ type PortForwardOpts struct {
PodPort string
LocalIp net.IP
LocalPort string
// Timeout for the port-forwarding process
Timeout int
HostFile *HostFileWithLock

// Context is a unique key (string) in kubectl config representing
Expand Down Expand Up @@ -407,14 +409,13 @@ func (pfo *PortForwardOpts) WaitUntilPodRunning(stopChannel <-chan struct{}) (*v
// if the os.signal (we enter the Ctrl+C)
// or ManualStop (service delete or some thing wrong)
// or RunningChannel channel (the watch for pod runnings is done)
// or timeout after 300s
// or timeout after 300s(default)
// we'll stop the watcher
// TODO: change the 300s timeout to custom settings.
go func() {
defer watcher.Stop()
select {
case <-stopChannel:
case <-time.After(time.Second * 300):
case <-time.After(time.Duration(pfo.Timeout) * time.Second):
}
}()

Expand Down
6 changes: 5 additions & 1 deletion pkg/fwdservice/fwdservice.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ type ServiceFWD struct {
// and the pods that back them for port-forwarding
Namespace string

// Timeout is specify a timeout seconds for the port forwarding.
Timeout int

// ClusterN is the ordinal index of the cluster (from configuration)
// cluster 0 is considered local while > 0 is remote
ClusterN int
Expand Down Expand Up @@ -76,7 +79,8 @@ type ServiceFWD struct {
ForwardIPReservations []string // cli passed IP reservations
}

/**
/*
*
add port map
@url https://github.com/txn2/kubefwd/issues/121
*/
Expand Down

0 comments on commit 99c05ec

Please sign in to comment.