Skip to content

Commit

Permalink
Merge 974d25c into 9fd65cd
Browse files Browse the repository at this point in the history
  • Loading branch information
rm3l authored Jul 18, 2023
2 parents 9fd65cd + 974d25c commit f253a5b
Show file tree
Hide file tree
Showing 9 changed files with 378 additions and 40 deletions.
22 changes: 19 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,6 +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
184 changes: 184 additions & 0 deletions pkg/component/describe/describe_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,184 @@
package describe

import (
"context"
"testing"

"github.com/google/go-cmp/cmp"

fcontext "github.com/redhat-developer/odo/pkg/odo/commonflags/context"
)

type testType struct {
value string
platform string
}

var _ platformDependent = testType{}

func (c testType) GetPlatform() string {
return c.platform
}

func Test_filterByPlatform(t *testing.T) {
type args struct {
ctx context.Context
isFeatEnabled bool
includeIfFeatDisabled bool
}
type testCase struct {
name string
args args
wantResult []testType
}
allValues := []testType{
{value: "value without platform"},
{value: "value11 (cluster)", platform: "cluster"},
{value: "value12 (cluster)", platform: "cluster"},
{value: "value21 (podman)", platform: "podman"},
{value: "value22 (podman)", platform: "podman"},
}
tests := []testCase{
{
name: "platform unset in context, isFeatEnabled=true, includeIfFeatDisabled=false",
args: args{
ctx: context.Background(),
isFeatEnabled: true,
includeIfFeatDisabled: false,
},
wantResult: allValues,
},
{
name: "platform unset in context, isFeatEnabled=true, includeIfFeatDisabled=true",
args: args{
ctx: context.Background(),
isFeatEnabled: true,
includeIfFeatDisabled: true,
},
wantResult: allValues,
},
{
name: "platform unset in context, isFeatEnabled=false, includeIfFeatDisabled=true",
args: args{
ctx: context.Background(),
isFeatEnabled: false,
includeIfFeatDisabled: true,
},
wantResult: []testType{
{"value without platform", ""},
{"value11 (cluster)", "cluster"},
{"value12 (cluster)", "cluster"},
},
},
{
name: "platform unset in context, isFeatEnabled=false, includeIfFeatDisabled=false",
args: args{
ctx: context.Background(),
isFeatEnabled: false,
includeIfFeatDisabled: false,
},
wantResult: nil,
},
{
name: "platform set to cluster in context, isFeatEnabled=true, includeIfFeatDisabled=false",
args: args{
ctx: fcontext.WithPlatform(context.Background(), "cluster"),
isFeatEnabled: true,
includeIfFeatDisabled: false,
},
wantResult: []testType{
{"value without platform", ""},
{"value11 (cluster)", "cluster"},
{"value12 (cluster)", "cluster"},
},
},
{
name: "platform set to cluster in context, isFeatEnabled=true, includeIfFeatDisabled=true",
args: args{
ctx: fcontext.WithPlatform(context.Background(), "cluster"),
isFeatEnabled: true,
includeIfFeatDisabled: true,
},
wantResult: []testType{
{"value without platform", ""},
{"value11 (cluster)", "cluster"},
{"value12 (cluster)", "cluster"},
},
},
{
name: "platform set to cluster in context, isFeatEnabled=false, includeIfFeatDisabled=false",
args: args{
ctx: fcontext.WithPlatform(context.Background(), "cluster"),
isFeatEnabled: false,
includeIfFeatDisabled: false,
},
wantResult: nil,
},
{
name: "platform set to cluster in context, isFeatEnabled=false, includeIfFeatDisabled=true",
args: args{
ctx: fcontext.WithPlatform(context.Background(), "cluster"),
isFeatEnabled: false,
includeIfFeatDisabled: true,
},
wantResult: []testType{
{"value without platform", ""},
{"value11 (cluster)", "cluster"},
{"value12 (cluster)", "cluster"},
},
},
{
name: "platform set to podman in context, isFeatEnabled=true, includeIfFeatDisabled=false",
args: args{
ctx: fcontext.WithPlatform(context.Background(), "podman"),
isFeatEnabled: true,
includeIfFeatDisabled: false,
},
wantResult: []testType{
{"value21 (podman)", "podman"},
{"value22 (podman)", "podman"},
},
},
{
name: "platform set to podman in context, isFeatEnabled=true, includeIfFeatDisabled=true",
args: args{
ctx: fcontext.WithPlatform(context.Background(), "podman"),
isFeatEnabled: true,
includeIfFeatDisabled: true,
},
wantResult: []testType{
{"value21 (podman)", "podman"},
{"value22 (podman)", "podman"},
},
},
{
name: "platform set to podman in context, isFeatEnabled=false, includeIfFeatDisabled=false",
args: args{
ctx: fcontext.WithPlatform(context.Background(), "podman"),
isFeatEnabled: false,
includeIfFeatDisabled: false,
},
wantResult: nil,
},
{
name: "platform set to podman in context, isFeatEnabled=false, includeIfFeatDisabled=true",
args: args{
ctx: fcontext.WithPlatform(context.Background(), "podman"),
isFeatEnabled: false,
includeIfFeatDisabled: true,
},
wantResult: []testType{
{"value21 (podman)", "podman"},
{"value22 (podman)", "podman"},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
gotResult := filterByPlatform(tt.args.ctx, tt.args.isFeatEnabled, allValues, tt.args.includeIfFeatDisabled)
if diff := cmp.Diff(tt.wantResult, gotResult, cmp.AllowUnexported(testType{})); diff != "" {
t.Errorf("filterByPlatform() mismatch (-want +got):\n%s", diff)
}
})
}
}
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
3 changes: 3 additions & 0 deletions pkg/state/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,7 @@ type Client interface {

// SetAPIServerPort sets the port where API server is listening in the state file and saves it to the file, updating the metadata
SetAPIServerPort(ctx context.Context, port int) error

// GetAPIServerPorts returns the port where the API servers are listening, possibly per platform.
GetAPIServerPorts(ctx context.Context) ([]api.DevControlPlane, error)
}
Loading

0 comments on commit f253a5b

Please sign in to comment.