Skip to content

Commit

Permalink
remove duplicate list and refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
girishramnani committed Nov 4, 2020
1 parent aa28203 commit 4b9e481
Show file tree
Hide file tree
Showing 7 changed files with 86 additions and 108 deletions.
90 changes: 61 additions & 29 deletions pkg/component/component.go
Original file line number Diff line number Diff line change
Expand Up @@ -856,8 +856,33 @@ func GetComponentNames(client *occlient.Client, applicationName string) ([]strin
return names, nil
}

// List lists components in active application
func List(client *occlient.Client, applicationName string, localConfigInfo *config.LocalConfigInfo) (ComponentList, error) {
func ListDevfileComponents(client *occlient.Client, selector string) (ComponentList, error) {

var deploymentList []v1.Deployment
var components []Component

// retrieve all the deployments that are associated with this application
deploymentList, err := client.GetKubeClient().GetDeploymentFromSelector(selector)
if err != nil {
return ComponentList{}, errors.Wrapf(err, "unable to list components")
}

// extract the labels we care about from each component
for _, elem := range deploymentList {
fmt.Println("HERE2")
component, err := GetComponent(client, elem.Labels[componentlabels.ComponentLabel], selector, client.Namespace)
if err != nil {
return ComponentList{}, errors.Wrap(err, "Unable to get component")
}
components = append(components, component)
}

compoList := GetMachineReadableFormatForList(components)
return compoList, nil
}

// ListS2IComponents lists s2i components in active application
func ListS2IComponents(client *occlient.Client, applicationName string, localConfigInfo *config.LocalConfigInfo) (ComponentList, error) {

var applicationSelector string
if applicationName != "" {
Expand All @@ -866,7 +891,6 @@ func List(client *occlient.Client, applicationName string, localConfigInfo *conf

deploymentConfigSupported := false
var err error
var deploymentList []v1.Deployment

var components []Component
componentNamesMap := make(map[string]bool)
Expand All @@ -876,22 +900,6 @@ func List(client *occlient.Client, applicationName string, localConfigInfo *conf
if err != nil {
return ComponentList{}, err
}

// retrieve all the deployments that are associated with this application
deploymentList, err = client.GetKubeClient().GetDeploymentFromSelector(applicationSelector)
if err != nil {
return ComponentList{}, errors.Wrapf(err, "unable to list components")
}
}

// extract the labels we care about from each component
for _, elem := range deploymentList {
component, err := GetComponent(client, elem.Labels[componentlabels.ComponentLabel], applicationName, client.Namespace)
if err != nil {
return ComponentList{}, errors.Wrap(err, "Unable to get component")
}
components = append(components, component)
componentNamesMap[component.Name] = true
}

if deploymentConfigSupported && client != nil {
Expand All @@ -912,6 +920,7 @@ func List(client *occlient.Client, applicationName string, localConfigInfo *conf
}
}

// this adds the local s2i component if there is one
if localConfigInfo != nil {
component, err := GetComponentFromConfig(localConfigInfo)

Expand All @@ -937,6 +946,28 @@ func List(client *occlient.Client, applicationName string, localConfigInfo *conf
return compoList, nil
}

// List lists all components in active application
func List(client *occlient.Client, applicationName string, localConfigInfo *config.LocalConfigInfo) (ComponentList, error) {
var applicationSelector string
if applicationName != "" {
applicationSelector = fmt.Sprintf("%s=%s", applabels.ApplicationLabel, applicationName)
}
var components []Component
s2iList, err := ListS2IComponents(client, applicationName, localConfigInfo)
if err != nil {
return ComponentList{}, err
}
components = append(components, s2iList.Items...)

devfileList, err := ListDevfileComponents(client, applicationSelector)
if err != nil {
return ComponentList{}, nil
}

components = append(components, devfileList.Items...)
return GetMachineReadableFormatForList(components), nil
}

// GetComponentFromConfig returns the component on the config if it exists
func GetComponentFromConfig(localConfig *config.LocalConfigInfo) (Component, error) {
component := getComponentFrom(localConfig, localConfig.GetType())
Expand Down Expand Up @@ -1052,8 +1083,8 @@ func ListIfPathGiven(client *occlient.Client, paths []string) ([]Component, erro
return components, err
}

func ListDevfileComponentsInPath(client *kclient.Client, paths []string) ([]DevfileComponent, error) {
var components []DevfileComponent
func ListDevfileComponentsInPath(client *kclient.Client, paths []string) ([]Component, error) {
var components []Component
var err error
for _, path := range paths {
err = filepath.Walk(path, func(path string, f os.FileInfo, err error) error {
Expand All @@ -1079,11 +1110,10 @@ func ListDevfileComponentsInPath(client *kclient.Client, paths []string) ([]Devf
}
con, _ := filepath.Abs(filepath.Dir(path))

comp := NewDevfileComponent(data.GetName())
comp := NewComponent(data.GetName())
comp.Status.State = StateTypeUnknown
comp.Spec.App = data.GetApplication()
comp.Namespace = data.GetNamespace()
comp.Spec.Name = data.GetName()
comp.Status.Context = con

// since the config file maybe belong to a component of a different project
Expand Down Expand Up @@ -1541,6 +1571,12 @@ func GetLogs(client *occlient.Client, componentName string, applicationName stri
}

func getMachineReadableFormat(componentName, componentType string) Component {
cmp := NewComponent(componentName)
cmp.Spec.Type = componentType
return cmp
}

func NewComponent(componentName string) Component {
return Component{
TypeMeta: metav1.TypeMeta{
Kind: "Component",
Expand All @@ -1549,12 +1585,8 @@ func getMachineReadableFormat(componentName, componentType string) Component {
ObjectMeta: metav1.ObjectMeta{
Name: componentName,
},
Spec: ComponentSpec{
Type: componentType,
},
Status: ComponentStatus{},
}

}

// GetMachineReadableFormatForList returns list of devfile and s2i components in machine readable format
Expand All @@ -1574,12 +1606,12 @@ func GetMachineReadableFormatForList(s2iComps []Component) ComponentList {
}

// GetMachineReadableFormatForCombinedCompList returns list of devfile and s2i components in machine readable format
func GetMachineReadableFormatForCombinedCompList(s2iComps []Component, devfileComps []DevfileComponent) CombinedComponentList {
func GetMachineReadableFormatForCombinedCompList(s2iComps []Component, devfileComps []Component) CombinedComponentList {
if len(s2iComps) == 0 {
s2iComps = []Component{}
}
if len(devfileComps) == 0 {
devfileComps = []DevfileComponent{}
devfileComps = []Component{}
}

return CombinedComponentList{
Expand Down
41 changes: 0 additions & 41 deletions pkg/component/devfile_component.go

This file was deleted.

5 changes: 4 additions & 1 deletion pkg/component/pushed_component.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func (d defaultPushedComponent) GetURLs() ([]url.URL, error) {
if d.urls == nil {
name := d.GetName()
var routes url.URLList

fmt.Println("HERE5")
if routeAvailable, err := d.client.IsRouteSupported(); routeAvailable && err == nil {
routes, err = url.ListPushed(d.client, name, d.GetApplication())
if err != nil && !isIgnorableError(err) {
Expand All @@ -89,6 +89,7 @@ func (d defaultPushedComponent) GetURLs() ([]url.URL, error) {
}
d.urls = append(routes.Items, ingresses.Items...)
}
fmt.Println("HERE6")
return d.urls, nil
}

Expand Down Expand Up @@ -269,7 +270,9 @@ func newPushedComponent(applicationName string, p provider, c *occlient.Client)

// GetPushedComponent returns an abstraction over the cluster representation of the component
func GetPushedComponent(c *occlient.Client, componentName, applicationName string) (PushedComponent, error) {
fmt.Println("HERE3")
d, err := c.GetKubeClient().GetDeploymentByName(componentName)
fmt.Println("HERE4")
if err != nil {
if isIgnorableError(err) {
// if it's not found, check if there's a deploymentconfig
Expand Down
18 changes: 2 additions & 16 deletions pkg/component/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,22 +48,8 @@ type ComponentStatus struct {
type CombinedComponentList struct {
metav1.TypeMeta `json:",inline"`
metav1.ListMeta `json:"metadata,omitempty"`
S2IComponents []Component `json:"s2iComponents"`
DevfileComponents []DevfileComponent `json:"devfileComponents"`
}

type DevfileComponent struct {
metav1.TypeMeta `json:",inline"`
metav1.ObjectMeta `json:"metadata,omitempty"`
Spec DevfileComponentSpec `json:"spec,omitempty"`
Status ComponentStatus `json:"status,omitempty"`
}

type DevfileComponentSpec struct {
Name string `json:"componentName,omitempty"`
App string `json:"app,omitempty"`
Type string `json:"type,omitempty"`
SourceType string `json:"sourceType,omitempty"`
S2IComponents []Component `json:"s2iComponents"`
DevfileComponents []Component `json:"devfileComponents"`
}

// State represents the component state
Expand Down
33 changes: 14 additions & 19 deletions pkg/odo/cli/component/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ import (
"path/filepath"
"text/tabwriter"

appsv1 "k8s.io/api/apps/v1"

"github.com/openshift/odo/pkg/application"
"github.com/openshift/odo/pkg/devfile"
"github.com/openshift/odo/pkg/machineoutput"
Expand Down Expand Up @@ -194,8 +192,7 @@ func (lo *ListOptions) Run() (err error) {

// experimental workflow

var deploymentList *appsv1.DeploymentList

devfileComponents := component.ComponentList{}
var selector string
// TODO: wrap this into a component list for docker support
if lo.allAppsFlag {
Expand All @@ -204,19 +201,18 @@ func (lo *ListOptions) Run() (err error) {
selector = applabels.GetSelector(lo.Application)
}

var devfileComponents []component.DevfileComponent
currentComponentState := component.StateTypeNotPushed

if lo.KClient != nil {
deploymentList, err = lo.KClient.ListDeployments(selector)
fmt.Println("HERE")
devfileComponents, err = component.ListDevfileComponents(lo.Client, selector)
if err != nil {
return err
}
devfileComponents = append(devfileComponents, component.DevfileComponentsFromDeployments(deploymentList)...)
for _, comp := range devfileComponents {
for _, comp := range devfileComponents.Items {
if lo.EnvSpecificInfo != nil {
// if we can find a component from the listing from server then the local state is pushed
if lo.EnvSpecificInfo.EnvInfo.MatchComponent(comp.Spec.Name, comp.Spec.App, comp.Namespace) {
if lo.EnvSpecificInfo.EnvInfo.MatchComponent(comp.Name, comp.Spec.App, comp.Namespace) {
currentComponentState = component.StateTypePushed
}
}
Expand All @@ -228,13 +224,12 @@ func (lo *ListOptions) Run() (err error) {
if lo.EnvSpecificInfo != nil {
envinfo := lo.EnvSpecificInfo.EnvInfo
if (envinfo.GetApplication() == lo.Application || lo.allAppsFlag) && currentComponentState == component.StateTypeNotPushed {
comp := component.NewDevfileComponent(envinfo.GetName())
comp := component.NewComponent(envinfo.GetName())
comp.Status.State = component.StateTypeNotPushed
comp.Namespace = envinfo.GetNamespace()
comp.Spec.App = envinfo.GetApplication()
comp.Spec.Type = lo.componentType
comp.Spec.Name = envinfo.GetName()
devfileComponents = append(devfileComponents, comp)
devfileComponents.Items = append(devfileComponents.Items, comp)
}
}

Expand All @@ -250,7 +245,7 @@ func (lo *ListOptions) Run() (err error) {
}

if len(apps) == 0 && lo.LocalConfigInfo.Exists() {
comps, err := component.List(lo.Client, lo.LocalConfigInfo.GetApplication(), lo.LocalConfigInfo)
comps, err := component.ListS2IComponents(lo.Client, lo.LocalConfigInfo.GetApplication(), lo.LocalConfigInfo)
if err != nil {
return err
}
Expand All @@ -259,15 +254,15 @@ func (lo *ListOptions) Run() (err error) {

// iterating over list of application and get list of all components
for _, app := range apps {
comps, err := component.List(lo.Client, app, lo.LocalConfigInfo)
comps, err := component.ListS2IComponents(lo.Client, app, lo.LocalConfigInfo)
if err != nil {
return err
}
components = append(components, comps.Items...)
}
} else {

componentList, err := component.List(lo.Client, lo.Application, lo.LocalConfigInfo)
componentList, err := component.ListS2IComponents(lo.Client, lo.Application, lo.LocalConfigInfo)
// compat
components = componentList.Items
if err != nil {
Expand All @@ -280,12 +275,12 @@ func (lo *ListOptions) Run() (err error) {

if !log.IsJSON() {

if len(devfileComponents) != 0 {
if len(devfileComponents.Items) != 0 {
lo.hasDevfileComponents = true
fmt.Fprintln(w, "Devfile Components: ")
fmt.Fprintln(w, "APP", "\t", "NAME", "\t", "PROJECT", "\t", "TYPE", "\t", "STATE")
for _, comp := range devfileComponents {
fmt.Fprintln(w, comp.Spec.App, "\t", comp.Spec.Name, "\t", comp.Namespace, "\t", comp.Spec.Type, "\t", comp.Status.State)
for _, comp := range devfileComponents.Items {
fmt.Fprintln(w, comp.Spec.App, "\t", comp.Name, "\t", comp.Namespace, "\t", comp.Spec.Type, "\t", comp.Status.State)
}
w.Flush()

Expand All @@ -310,7 +305,7 @@ func (lo *ListOptions) Run() (err error) {
return
}
} else {
combinedComponents := component.GetMachineReadableFormatForCombinedCompList(components, devfileComponents)
combinedComponents := component.GetMachineReadableFormatForCombinedCompList(components, devfileComponents.Items)
machineoutput.OutputSuccess(combinedComponents)
}

Expand Down
3 changes: 2 additions & 1 deletion pkg/odo/util/completion/completionhandlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package completion

import (
"fmt"
"strings"

"github.com/openshift/odo/pkg/application"
"github.com/openshift/odo/pkg/catalog"
"github.com/openshift/odo/pkg/component"
Expand All @@ -13,7 +15,6 @@ import (
"github.com/openshift/odo/pkg/util"
"github.com/posener/complete"
"github.com/spf13/cobra"
"strings"
)

// ServiceCompletionHandler provides service name completion for the current project and application
Expand Down
Loading

0 comments on commit 4b9e481

Please sign in to comment.