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

Display information about the running API Server and web UI in odo describe component output #6964

15 changes: 12 additions & 3 deletions pkg/api/component.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ package api

// Component describes the state of a devfile component
type Component struct {
DevfilePath string `json:"devfilePath,omitempty"`
DevfileData *DevfileData `json:"devfileData,omitempty"`
DevForwardedPorts []ForwardedPort `json:"devForwardedPorts,omitempty"`
DevfilePath string `json:"devfilePath,omitempty"`
DevfileData *DevfileData `json:"devfileData,omitempty"`
DevControlPlane []DevControlPlane `json:"devControlPlane,omitempty"`
DevForwardedPorts []ForwardedPort `json:"devForwardedPorts,omitempty"`
// RunningIn is the overall running mode map of the component;
// this is computing as a merge of RunningOn (all the different running modes
// for each platform the component is running on).
Expand All @@ -29,13 +30,21 @@ type ForwardedPort struct {
Protocol string `json:"protocol,omitempty"`
}

func (o ForwardedPort) GetPlatform() string {
return o.Platform
}

type DevControlPlane struct {
Platform string `json:"platform,omitempty"`
LocalPort int `json:"localPort"`
APIServerPath string `json:"apiServerPath"`
WebInterfacePath string `json:"webInterfacePath"`
}

func (o DevControlPlane) GetPlatform() string {
return o.Platform
}

type ConnectionData struct {
Name string `json:"name"`
Rules []Rules `json:"rules,omitempty"`
Expand Down
84 changes: 57 additions & 27 deletions pkg/component/describe/describe.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ import (
"github.com/redhat-developer/odo/pkg/state"
)

type platformDependent interface {
GetPlatform() string
}

// DescribeDevfileComponent describes the component defined by the devfile in the current directory
func DescribeDevfileComponent(
ctx context.Context,
Expand Down Expand Up @@ -64,6 +68,22 @@ func DescribeDevfileComponent(
kubeClient = nil
}

isApiServerFeatureEnabled := feature.IsEnabled(ctx, feature.APIServerFlag)
// TODO(feloy) Pass PID with `--pid` flag
allControlPlaneData, err := stateClient.GetAPIServerPorts(ctx)
if err != nil {
return api.Component{}, nil, err
}
if isApiServerFeatureEnabled {
for i := range allControlPlaneData {
if allControlPlaneData[i].Platform == "" {
allControlPlaneData[i].Platform = commonflags.PlatformCluster
}
}
}

devControlPlaneData := filterByPlatform(ctx, isApiServerFeatureEnabled, allControlPlaneData, false)

// TODO(feloy) Pass PID with `--pid` flag
allFwdPorts, err := stateClient.GetForwardedPorts(ctx)
if err != nil {
Expand All @@ -76,33 +96,7 @@ func DescribeDevfileComponent(
}
}
}
var forwardedPorts []api.ForwardedPort
switch platform {
case "":
if isPlatformFeatureEnabled {
// Read ports from all platforms
forwardedPorts = allFwdPorts
} else {
// Limit to cluster ports only
for _, p := range allFwdPorts {
if p.Platform == "" || p.Platform == commonflags.PlatformCluster {
forwardedPorts = append(forwardedPorts, p)
}
}
}
case commonflags.PlatformCluster:
for _, p := range allFwdPorts {
if p.Platform == "" || p.Platform == commonflags.PlatformCluster {
forwardedPorts = append(forwardedPorts, p)
}
}
case commonflags.PlatformPodman:
for _, p := range allFwdPorts {
if p.Platform == commonflags.PlatformPodman {
forwardedPorts = append(forwardedPorts, p)
}
}
}
forwardedPorts := filterByPlatform(ctx, isPlatformFeatureEnabled, allFwdPorts, true)

runningOn, err := GetRunningOn(ctx, componentName, kubeClient, podmanClient)
if err != nil {
Expand All @@ -122,6 +116,7 @@ func DescribeDevfileComponent(
cmp := api.Component{
DevfilePath: devfilePath,
DevfileData: devfileData,
DevControlPlane: devControlPlaneData,
DevForwardedPorts: forwardedPorts,
RunningIn: api.MergeRunningModes(runningOn),
RunningOn: runningOn,
Expand Down Expand Up @@ -234,6 +229,41 @@ func GetRunningOn(ctx context.Context, n string, kubeClient kclient.ClientInterf
return runningOn, nil
}

func filterByPlatform[T platformDependent](ctx context.Context, isFeatEnabled bool, all []T, includeIfFeatDisabled bool) (result []T) {
plt := fcontext.GetPlatform(ctx, "")
switch plt {
case "":
if isFeatEnabled {
// Read from all platforms
result = all
} else if includeIfFeatDisabled {
// Limit to cluster only
for _, p := range all {
if p.GetPlatform() == "" || p.GetPlatform() == commonflags.PlatformCluster {
result = append(result, p)
}
}
}
case commonflags.PlatformCluster:
if isFeatEnabled || includeIfFeatDisabled {
for _, p := range all {
if p.GetPlatform() == "" || p.GetPlatform() == commonflags.PlatformCluster {
result = append(result, p)
}
}
}
case commonflags.PlatformPodman:
if isFeatEnabled || includeIfFeatDisabled {
for _, p := range all {
if p.GetPlatform() == commonflags.PlatformPodman {
result = append(result, p)
}
}
}
}
return result
}

func updateWithRemoteSourceLocation(cmp *api.Component) {
components, err := cmp.DevfileData.Devfile.GetComponents(common.DevfileOptions{
ComponentOptions: common.ComponentOptions{ComponentType: v1alpha2.ContainerComponentType},
Expand Down
13 changes: 13 additions & 0 deletions pkg/odo/cli/describe/component.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,19 @@ func printHumanReadableOutput(ctx context.Context, cmp api.Component, devfileObj
fmt.Println()
}

if feature.IsEnabled(ctx, feature.APIServerFlag) && len(cmp.DevControlPlane) != 0 {
const ctrlPlaneHost = "localhost"
log.Info("Dev Control Plane:")
for _, dcp := range cmp.DevControlPlane {
log.Printf(`%[1]s
API: http://%[2]s:%[3]d/%[4]s
Web UI: http://%[2]s:%[3]d/`,
log.Sbold(dcp.Platform),
ctrlPlaneHost, dcp.LocalPort, strings.TrimPrefix(dcp.APIServerPath, "/"))
}
fmt.Println()
}

if len(cmp.DevForwardedPorts) > 0 {
log.Info("Forwarded ports:")
for _, port := range cmp.DevForwardedPorts {
Expand Down