-
Notifications
You must be signed in to change notification settings - Fork 244
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
Implement "odo list" #6043
Merged
openshift-merge-robot
merged 6 commits into
redhat-developer:main
from
feloy:feature-5991/list-all
Aug 29, 2022
Merged
Implement "odo list" #6043
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
cf9a6c1
Move "odo list" to "odo list component"
feloy f6fd6d8
Refactor odo list component
feloy a162db4
Add --namespace flag to "odo list binding"
feloy 863e107
odo list implementation
feloy a2de738
Doc
feloy 7315268
Apply suggestions from code review
feloy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
--- | ||
title: odo list component | ||
--- | ||
|
||
`odo list component` command is useful for getting information about components running on a specific namespace. | ||
|
||
If the command is executed from a directory containing a Devfile, it also displays the component | ||
defined in the Devfile as part of the list, prefixed with a star(*). | ||
|
||
For each component, the command displays: | ||
- its name, | ||
- its project type, | ||
- on which mode it is running (None, Dev, Deploy, or both), note that None is only applicable to the component | ||
defined in the local Devfile, | ||
- by which application the component has been deployed. | ||
|
||
## Available flags | ||
|
||
* `--namespace` - Namespace to list the components from (optional). By default, the current namespace defined in kubeconfig is used | ||
* `-o json` - Outputs the list in JSON format. See [JSON output](json-output.md) for more information | ||
|
||
:::tip use of cache | ||
|
||
`odo list component` makes use of cache for performance reasons. This is the same cache that is referred by `kubectl` command | ||
when you do `kubectl api-resources --cached=true`. As a result, if you were to install an Operator/CRD on the | ||
Kubernetes cluster, and create a resource from it using odo, you might not see it in the `odo list component` output. This | ||
would be the case for 10 minutes timeframe for which the cache is considered valid. Beyond this 10 minutes, the | ||
cache is updated anyway. | ||
|
||
If you would like to invalidate the cache before the 10 minutes timeframe, you could manually delete it by doing: | ||
```shell | ||
rm -rf ~/.kube/cache/discovery/api.crc.testing_6443/ | ||
``` | ||
Above example shows how to invalidate the cache for a CRC cluster. Note that you will have to modify the `api.crc. | ||
testing_6443` part based on the cluster you are working against. |
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,212 @@ | ||
package component | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"fmt" | ||
|
||
"github.com/jedib0t/go-pretty/v6/table" | ||
"github.com/jedib0t/go-pretty/v6/text" | ||
"github.com/spf13/cobra" | ||
|
||
"github.com/redhat-developer/odo/pkg/api" | ||
"github.com/redhat-developer/odo/pkg/machineoutput" | ||
"github.com/redhat-developer/odo/pkg/odo/cli/ui" | ||
|
||
dfutil "github.com/devfile/library/pkg/util" | ||
|
||
"github.com/redhat-developer/odo/pkg/component" | ||
|
||
"github.com/redhat-developer/odo/pkg/log" | ||
"github.com/redhat-developer/odo/pkg/odo/cmdline" | ||
"github.com/redhat-developer/odo/pkg/odo/genericclioptions" | ||
"github.com/redhat-developer/odo/pkg/odo/genericclioptions/clientset" | ||
"github.com/redhat-developer/odo/pkg/odo/util/completion" | ||
|
||
ktemplates "k8s.io/kubectl/pkg/util/templates" | ||
) | ||
|
||
// RecommendedCommandName is the recommended list name | ||
const RecommendedCommandName = "component" | ||
|
||
var listExample = ktemplates.Examples(` # List all components in the application | ||
%[1]s | ||
`) | ||
|
||
// ListOptions ... | ||
type ListOptions struct { | ||
// Context | ||
*genericclioptions.Context | ||
|
||
// Clients | ||
clientset *clientset.Clientset | ||
|
||
// Local variables | ||
namespaceFilter string | ||
|
||
// Flags | ||
namespaceFlag string | ||
} | ||
|
||
var _ genericclioptions.Runnable = (*ListOptions)(nil) | ||
var _ genericclioptions.JsonOutputter = (*ListOptions)(nil) | ||
|
||
// NewListOptions ... | ||
func NewListOptions() *ListOptions { | ||
return &ListOptions{} | ||
} | ||
|
||
func (o *ListOptions) SetClientset(clientset *clientset.Clientset) { | ||
o.clientset = clientset | ||
} | ||
|
||
// Complete ... | ||
func (lo *ListOptions) Complete(cmdline cmdline.Cmdline, args []string) (err error) { | ||
|
||
// Check to see if KUBECONFIG exists, and if not, error the user that we would not be able to get cluster information | ||
// Do this before anything else, or else we will just error out with the: | ||
// invalid configuration: no configuration has been provided, try setting KUBERNETES_MASTER environment variable | ||
// instead | ||
if !dfutil.CheckKubeConfigExist() { | ||
return errors.New("KUBECONFIG not found. Unable to retrieve cluster information. Please set your Kubernetes configuration via KUBECONFIG env variable or ~/.kube/config") | ||
} | ||
|
||
// Create the local context and initial Kubernetes client configuration | ||
lo.Context, err = genericclioptions.New(genericclioptions.NewCreateParameters(cmdline).NeedDevfile("")) | ||
// The command must work without Devfile | ||
if err != nil && !genericclioptions.IsNoDevfileError(err) { | ||
return err | ||
} | ||
|
||
// If the namespace flag has been passed, we will search there. | ||
// if it hasn't, we will search from the default project / namespace. | ||
if lo.namespaceFlag != "" { | ||
lo.namespaceFilter = lo.namespaceFlag | ||
} else { | ||
lo.namespaceFilter = lo.GetProject() | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// Validate ... | ||
func (lo *ListOptions) Validate() (err error) { | ||
return nil | ||
} | ||
|
||
// Run has the logic to perform the required actions as part of command | ||
func (lo *ListOptions) Run(ctx context.Context) error { | ||
listSpinner := log.Spinnerf("Listing components from namespace '%s'", lo.namespaceFilter) | ||
defer listSpinner.End(false) | ||
|
||
list, err := lo.run(ctx) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
listSpinner.End(true) | ||
|
||
HumanReadableOutput(list) | ||
return nil | ||
} | ||
|
||
// Run contains the logic for the odo command | ||
func (lo *ListOptions) RunForJsonOutput(ctx context.Context) (out interface{}, err error) { | ||
return lo.run(ctx) | ||
} | ||
|
||
func (lo *ListOptions) run(ctx context.Context) (api.ResourcesList, error) { | ||
devfileComponents, componentInDevfile, err := component.ListAllComponents(lo.clientset.KubernetesClient, lo.namespaceFilter, lo.EnvSpecificInfo.GetDevfileObj()) | ||
if err != nil { | ||
return api.ResourcesList{}, err | ||
} | ||
return api.ResourcesList{ | ||
ComponentInDevfile: componentInDevfile, | ||
Components: devfileComponents, | ||
}, nil | ||
} | ||
|
||
// NewCmdList implements the list odo command | ||
func NewCmdComponentList(name, fullName string) *cobra.Command { | ||
o := NewListOptions() | ||
|
||
var listCmd = &cobra.Command{ | ||
Use: name, | ||
Short: "List all components in the current namespace", | ||
Long: "List all components in the current namespace.", | ||
Example: fmt.Sprintf(listExample, fullName), | ||
Args: cobra.NoArgs, | ||
Annotations: map[string]string{"command": "management"}, | ||
Run: func(cmd *cobra.Command, args []string) { | ||
genericclioptions.GenericRun(o, cmd, args) | ||
}, | ||
} | ||
clientset.Add(listCmd, clientset.KUBERNETES) | ||
|
||
listCmd.Flags().StringVar(&o.namespaceFlag, "namespace", "", "Namespace for odo to scan for components") | ||
|
||
completion.RegisterCommandFlagHandler(listCmd, "path", completion.FileCompletionHandler) | ||
machineoutput.UsedByCommand(listCmd) | ||
|
||
return listCmd | ||
} | ||
|
||
func HumanReadableOutput(list api.ResourcesList) { | ||
components := list.Components | ||
if len(components) == 0 { | ||
log.Error("There are no components deployed.") | ||
return | ||
} | ||
|
||
t := ui.NewTable() | ||
|
||
// Create the header and then sort accordingly | ||
t.AppendHeader(table.Row{"NAME", "PROJECT TYPE", "RUNNING IN", "MANAGED"}) | ||
t.SortBy([]table.SortBy{ | ||
{Name: "MANAGED", Mode: table.Asc}, | ||
{Name: "NAME", Mode: table.Dsc}, | ||
}) | ||
|
||
// Go through each component and add it to the table | ||
for _, comp := range components { | ||
|
||
// Mark the name as yellow in the index to it's easier to see. | ||
name := text.Colors{text.FgHiYellow}.Sprint(comp.Name) | ||
|
||
// Get the managed by label | ||
managedBy := comp.ManagedBy | ||
if managedBy == "" { | ||
managedBy = api.TypeUnknown | ||
} | ||
|
||
// Get the mode (dev or deploy) | ||
mode := comp.RunningIn.String() | ||
|
||
// Get the type of the component | ||
componentType := comp.Type | ||
if componentType == "" { | ||
componentType = api.TypeUnknown | ||
} | ||
|
||
// If we find our local unpushed component, let's change the output appropriately. | ||
if list.ComponentInDevfile == comp.Name { | ||
name = fmt.Sprintf("* %s", name) | ||
|
||
if comp.ManagedBy == "" { | ||
managedBy = "odo" | ||
} | ||
} | ||
|
||
// If we are managing that component, output it as blue (our logo colour) to indicate it's used by odo | ||
if managedBy == "odo" { | ||
managedBy = text.Colors{text.FgBlue}.Sprintf("odo (%s)", comp.ManagedByVersion) | ||
} else if managedBy != "" && comp.ManagedByVersion != "" { | ||
// this is done to maintain the color of the output | ||
managedBy += fmt.Sprintf("(%s)", comp.ManagedByVersion) | ||
} | ||
|
||
t.AppendRow(table.Row{name, componentType, mode, managedBy}) | ||
} | ||
t.Render() | ||
|
||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we want to print the local component/binding if --namespace is passed? I find it a little confusing to display the binding/component running in some other namespace to be displayed when I explicitly ask for some other namespace.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@kadel what's your opinion?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we decide to, I would prefer to make this change as part as another PR
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree it seems a bit confusing to display the local component/binding when requesting some other namespace (especially since the first line printed is
Listing {ServiceBindings,resources} from the namespace "$ns"
).But as discussed, it is okay to change this in a separate PR.