-
Notifications
You must be signed in to change notification settings - Fork 244
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
odo experimental debug port-forward command (#2043)
* add port-forward code from oc * added command for debug and experimental * integrating with odo - in progress * add build portfoward request in occlient * resolved all references * extract the port forwarder into a different package * some cleanup * integrating the portforward command in odo cli * removed i18n * add debug port support in the config * use the debug port in portforward cli * resolved failing unit test * added debug_port in the env var list * remove the pod timeout option * resolved long desc for port forward * sending corrupt debug port * better logging * add tests for odo debug * minor cleanup * removed focus * resolved failing tests * added comments * small error in tests * removed experimental, changed the tests and add warning for tech preview * added logging for debugging * added more comments * removed the port forward interface * use init:0.12.0 bootstrap image * made port forwarder more independent and added comment * major refactor to resolve mrinal's comments * moved logging * doc typo * doc typo
- Loading branch information
1 parent
567ad81
commit 8b253d0
Showing
20 changed files
with
476 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
package debug | ||
|
||
import ( | ||
"github.com/openshift/odo/pkg/occlient" | ||
|
||
componentlabels "github.com/openshift/odo/pkg/component/labels" | ||
|
||
"fmt" | ||
"net/http" | ||
|
||
"github.com/openshift/odo/pkg/log" | ||
"github.com/openshift/odo/pkg/util" | ||
"github.com/pkg/errors" | ||
corev1 "k8s.io/api/core/v1" | ||
|
||
"k8s.io/client-go/tools/portforward" | ||
"k8s.io/client-go/transport/spdy" | ||
k8sgenclioptions "k8s.io/kubernetes/pkg/kubectl/genericclioptions" | ||
) | ||
|
||
// DefaultPortForwarder implements the SPDY based port forwarder | ||
type DefaultPortForwarder struct { | ||
client *occlient.Client | ||
k8sgenclioptions.IOStreams | ||
componentName string | ||
appName string | ||
} | ||
|
||
func NewDefaultPortForwarder(componentName, appName string, client *occlient.Client, streams k8sgenclioptions.IOStreams) *DefaultPortForwarder { | ||
return &DefaultPortForwarder{ | ||
client: client, | ||
IOStreams: streams, | ||
componentName: componentName, | ||
appName: appName, | ||
} | ||
} | ||
|
||
// ForwardPorts forwards the port using the url for the remote pod. | ||
// portPair is a pair of port in format "localPort:RemotePort" that is to be forwarded | ||
// stop Chan is used to stop port forwarding | ||
// ready Chan is used to signal failure to the channel receiver | ||
func (f *DefaultPortForwarder) ForwardPorts(portPair string, stopChan, readyChan chan struct{}) error { | ||
conf, err := f.client.KubeConfig.ClientConfig() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
pod, err := f.getPodUsingComponentName() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if pod.Status.Phase != corev1.PodRunning { | ||
return fmt.Errorf("unable to forward port because pod is not running. Current status=%v", pod.Status.Phase) | ||
} | ||
|
||
transport, upgrader, err := spdy.RoundTripperFor(conf) | ||
if err != nil { | ||
return err | ||
} | ||
req := f.client.BuildPortForwardReq(pod.Name) | ||
dialer := spdy.NewDialer(upgrader, &http.Client{Transport: transport}, "POST", req.URL()) | ||
fw, err := portforward.New(dialer, []string{portPair}, stopChan, readyChan, f.Out, f.ErrOut) | ||
if err != nil { | ||
return err | ||
} | ||
log.Info("Started port forwarding at ports -", portPair) | ||
return fw.ForwardPorts() | ||
} | ||
|
||
func (f *DefaultPortForwarder) getPodUsingComponentName() (*corev1.Pod, error) { | ||
componentLabels := componentlabels.GetLabels(f.componentName, f.appName, false) | ||
componentSelector := util.ConvertLabelsToSelector(componentLabels) | ||
dc, err := f.client.GetOneDeploymentConfigFromSelector(componentSelector) | ||
if err != nil { | ||
return nil, errors.Wrap(err, "unable to get deployment for component") | ||
} | ||
// Find Pod for component | ||
podSelector := fmt.Sprintf("deploymentconfig=%s", dc.Name) | ||
|
||
return f.client.GetOnePodFromSelector(podSelector) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.