diff --git a/pkg/component/component.go b/pkg/component/component.go index f1940632aa0..06b2d33a5b8 100644 --- a/pkg/component/component.go +++ b/pkg/component/component.go @@ -4,7 +4,6 @@ import ( "bufio" "bytes" "fmt" - devfileParser "github.com/openshift/odo/pkg/devfile/parser" "io" "os" "path/filepath" @@ -12,7 +11,10 @@ import ( "strings" "time" + devfileParser "github.com/openshift/odo/pkg/devfile/parser" + "github.com/openshift/odo/pkg/devfile/adapters/common" + "github.com/openshift/odo/pkg/devfile/parser" "github.com/openshift/odo/pkg/kclient" "github.com/openshift/odo/pkg/envinfo" @@ -986,7 +988,7 @@ func getComponentFrom(info envinfo.LocalConfigProvider, componentType string) Co } // ListIfPathGiven lists all available component in given path directory -func ListIfPathGiven(client *occlient.Client, paths []string) (ComponentList, error) { +func ListIfPathGiven(client *occlient.Client, paths []string) ([]Component, error) { var components []Component var err error for _, path := range paths { @@ -1025,7 +1027,62 @@ func ListIfPathGiven(client *occlient.Client, paths []string) (ComponentList, er }) } - return GetMachineReadableFormatForList(components), err + return components, err +} + +func ListDevfileComponentsInPath(client *kclient.Client, paths []string) ([]DevfileComponent, error) { + var components []DevfileComponent + var err error + for _, path := range paths { + err = filepath.Walk(path, func(path string, f os.FileInfo, err error) error { + // we check for .odo/env/env.yaml folder first and then find devfile.yaml, this could be changed + // TODO: optimise this + if f != nil && strings.Contains(f.Name(), ".odo") { + // lets find if there is a devfile and an env.yaml + dir := filepath.Dir(path) + data, err := envinfo.NewEnvSpecificInfo(dir) + if err != nil { + return err + } + + // if the .odo folder doesn't contain a proper env file + if data.GetName() == "" || data.GetApplication() == "" || data.GetNamespace() == "" { + return nil + } + + // we just want to confirm if the devfile is correct + _, err = parser.Parse(filepath.Join(dir, "devfile.yaml")) + if err != nil { + return err + } + con, _ := filepath.Abs(filepath.Dir(path)) + + comp := NewDevfileComponent(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 + if client != nil { + client.Namespace = data.GetNamespace() + deployment, err := client.GetDeploymentByName(data.GetName()) + if err != nil { + comp.Status.State = StateTypeNotPushed + } else if deployment != nil { + comp.Status.State = StateTypePushed + } + } + + components = append(components, comp) + } + + return nil + }) + + } + return components, err } // GetComponentSource what source type given component uses @@ -1337,9 +1394,8 @@ func GetComponentState(client *occlient.Client, componentName, applicationName s } if c != nil { return StateTypePushed - } else { - return StateTypeNotPushed } + return StateTypeNotPushed } // GetComponent provides component definition @@ -1476,20 +1532,40 @@ func getMachineReadableFormat(componentName, componentType string) Component { } -// GetMachineReadableFormatForList returns list of components in machine readable format -func GetMachineReadableFormatForList(components []Component) ComponentList { - if len(components) == 0 { - components = []Component{} +// GetMachineReadableFormatForList returns list of devfile and s2i components in machine readable format +func GetMachineReadableFormatForList(s2iComps []Component) ComponentList { + if len(s2iComps) == 0 { + s2iComps = []Component{} } + return ComponentList{ TypeMeta: metav1.TypeMeta{ Kind: "List", APIVersion: apiVersion, }, ListMeta: metav1.ListMeta{}, - Items: components, + Items: s2iComps, + } +} + +// GetMachineReadableFormatForCombinedCompList returns list of devfile and s2i components in machine readable format +func GetMachineReadableFormatForCombinedCompList(s2iComps []Component, devfileComps []DevfileComponent) CombinedComponentList { + if len(s2iComps) == 0 { + s2iComps = []Component{} + } + if len(devfileComps) == 0 { + devfileComps = []DevfileComponent{} } + return CombinedComponentList{ + TypeMeta: metav1.TypeMeta{ + Kind: "List", + APIVersion: apiVersion, + }, + ListMeta: metav1.ListMeta{}, + S2IComponents: s2iComps, + DevfileComponents: devfileComps, + } } // getStorageFromConfig gets all the storage from the config diff --git a/pkg/component/devfile_component.go b/pkg/component/devfile_component.go new file mode 100644 index 00000000000..de437f3eff5 --- /dev/null +++ b/pkg/component/devfile_component.go @@ -0,0 +1,41 @@ +package component + +import ( + applabels "github.com/openshift/odo/pkg/application/labels" + componentlabels "github.com/openshift/odo/pkg/component/labels" + "github.com/openshift/odo/pkg/config" + appsv1 "k8s.io/api/apps/v1" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" +) + +func NewDevfileComponent(componentName string) DevfileComponent { + return DevfileComponent{ + TypeMeta: metav1.TypeMeta{ + Kind: "DevfileComponent", + APIVersion: apiVersion, + }, + ObjectMeta: metav1.ObjectMeta{ + Name: componentName, + }, + Spec: DevfileComponentSpec{ + SourceType: string(config.LOCAL), + }, + } +} + +func DevfileComponentsFromDeployments(deployList *appsv1.DeploymentList) []DevfileComponent { + compList := []DevfileComponent{} + for _, deployment := range deployList.Items { + app := deployment.Labels[applabels.ApplicationLabel] + cmpType := deployment.Labels[componentlabels.ComponentTypeLabel] + + comp := NewDevfileComponent(deployment.Name) + comp.Status.State = StateTypePushed + comp.Namespace = deployment.Namespace + comp.Spec.App = app + comp.Spec.Type = cmpType + comp.Spec.Name = deployment.Name + compList = append(compList, comp) + } + return compList +} diff --git a/pkg/devfile/parser/representation.go b/pkg/component/devfile_repr.go similarity index 88% rename from pkg/devfile/parser/representation.go rename to pkg/component/devfile_repr.go index c7e612004ac..6ac1bca4feb 100644 --- a/pkg/devfile/parser/representation.go +++ b/pkg/component/devfile_repr.go @@ -1,14 +1,15 @@ -package parser +package component import ( "github.com/openshift/odo/pkg/config" + "github.com/openshift/odo/pkg/devfile/parser" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" ) -func (d DevfileObj) ToRepresentation() ConfigurableRepr { +func ToDevfileRepresentation(d parser.DevfileObj) ConfigurableRepr { confRepr := ConfigurableRepr{ - Name: d.getMetadataName(), - Memory: d.getMemory(), + Name: d.GetMetadataName(), + Memory: d.GetMemory(), } var contReprs []ContainerRepr components := d.Data.GetComponents() @@ -38,7 +39,7 @@ func (d DevfileObj) ToRepresentation() ConfigurableRepr { return confRepr } -func (d DevfileObj) WrapFromJSONOutput(confRepr ConfigurableRepr) JSONConfigRepr { +func WrapFromJSONOutput(confRepr ConfigurableRepr) JSONConfigRepr { return JSONConfigRepr{ TypeMeta: metav1.TypeMeta{ Kind: "DevfileConfiguration", diff --git a/pkg/component/pushed_component.go b/pkg/component/pushed_component.go index 15eecb8825b..c09597a30d2 100644 --- a/pkg/component/pushed_component.go +++ b/pkg/component/pushed_component.go @@ -2,6 +2,7 @@ package component import ( "fmt" + appsv1 "github.com/openshift/api/apps/v1" applabels "github.com/openshift/odo/pkg/application/labels" componentlabels "github.com/openshift/odo/pkg/component/labels" diff --git a/pkg/component/types.go b/pkg/component/types.go index adaaff90dc3..95199c22ce5 100644 --- a/pkg/component/types.go +++ b/pkg/component/types.go @@ -40,7 +40,29 @@ type ComponentStatus struct { LinkedServices []string `json:"linkedServices,omitempty"` } -// State represents component state +// CombinedComponentList is list of s2i and devfile components +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"` +} + +// State reperesents component state type State string const ( diff --git a/pkg/devfile/parser/configurables.go b/pkg/devfile/parser/configurables.go index 10755b18b34..48665847984 100644 --- a/pkg/devfile/parser/configurables.go +++ b/pkg/devfile/parser/configurables.go @@ -75,11 +75,11 @@ func (d DevfileObj) IsSet(parameter string) bool { if parameter, ok := AsDevfileSupportedParameter(parameter); ok { switch parameter { case "name": - return d.getMetadataName() != "" + return d.GetMetadataName() != "" case "ports": return d.hasPorts() case "memory": - return d.getMemory() != "" + return d.GetMemory() != "" } } return false @@ -171,7 +171,7 @@ func (d DevfileObj) setMemory(memory string) error { } return d.WriteYamlDevfile() } -func (d DevfileObj) getMemory() string { +func (d DevfileObj) GetMemory() string { components := d.Data.GetComponents() for _, component := range components { if component.Container != nil { @@ -184,7 +184,7 @@ func (d DevfileObj) getMemory() string { return "" } -func (d DevfileObj) getMetadataName() string { +func (d DevfileObj) GetMetadataName() string { return d.Data.GetMetadata().Name } diff --git a/pkg/envinfo/envinfo.go b/pkg/envinfo/envinfo.go index 7a36dc533e7..3b36075a8c6 100644 --- a/pkg/envinfo/envinfo.go +++ b/pkg/envinfo/envinfo.go @@ -432,6 +432,11 @@ func (ei *EnvInfo) GetApplication() string { return ei.componentSettings.AppName } +// MatchComponent matches a component information provided by a devfile compoment with the local env info +func (ei *EnvInfo) MatchComponent(name, app, namespace string) bool { + return name == ei.GetName() && app == ei.GetApplication() && namespace == ei.GetNamespace() +} + // GetLink returns the EnvInfoLink, returns default if nil func (ei *EnvInfo) GetLink() []EnvInfoLink { if ei.componentSettings.Link == nil { diff --git a/pkg/odo/cli/component/list.go b/pkg/odo/cli/component/list.go index 11a3daa568a..dc0773d1a2d 100644 --- a/pkg/odo/cli/component/list.go +++ b/pkg/odo/cli/component/list.go @@ -18,7 +18,6 @@ import ( "k8s.io/klog" applabels "github.com/openshift/odo/pkg/application/labels" - componentlabels "github.com/openshift/odo/pkg/component/labels" "github.com/openshift/odo/pkg/component" "github.com/openshift/odo/pkg/log" @@ -34,9 +33,6 @@ import ( // ListRecommendedCommandName is the recommended watch command name const ListRecommendedCommandName = "list" -const UnpushedCompState = "Unpushed" -const PushedCompState = "Pushed" - var listExample = ktemplates.Examples(` # List all components in the application %[1]s `) @@ -84,7 +80,7 @@ func (lo *ListOptions) Complete(name string, cmd *cobra.Command, args []string) if util.CheckKubeConfigExist() { klog.V(4).Infof("New Context") - lo.Context = genericclioptions.NewContext(cmd) + lo.Context = genericclioptions.NewContext(cmd, false, true) lo.hasDCSupport, err = lo.Client.IsDeploymentConfigSupported() if err != nil { return err @@ -95,7 +91,6 @@ func (lo *ListOptions) Complete(name string, cmd *cobra.Command, args []string) lo.Context = genericclioptions.NewConfigContext(cmd) // for disconnected situation we just assume we have DC support lo.hasDCSupport = true - } } @@ -111,7 +106,7 @@ func (lo *ListOptions) Validate() (err error) { } if util.CheckPathExists(lo.devfilePath) { - if lo.Context.Application == "" && lo.Context.KClient.Namespace == "" { + if lo.Application == "" && lo.KClient.Namespace == "" { return odoutil.ThrowContextError() } return nil @@ -126,7 +121,6 @@ func (lo *ListOptions) Validate() (err error) { project = lo.Context.Project app = lo.Application } - if !lo.allAppsFlag && lo.pathFlag == "" && (project == "" || app == "") { return odoutil.ThrowContextError() } @@ -141,22 +135,50 @@ func (lo *ListOptions) Run() (err error) { if len(lo.pathFlag) != 0 { - if util.CheckPathExists(lo.devfilePath) { - log.Experimental("--path flag is not supported for devfile components") + devfileComps, err := component.ListDevfileComponentsInPath(lo.KClient, filepath.SplitList(lo.pathFlag)) + if err != nil { + return err } - components, err := component.ListIfPathGiven(lo.Context.Client, filepath.SplitList(lo.pathFlag)) + s2iComps, err := component.ListIfPathGiven(lo.Context.Client, filepath.SplitList(lo.pathFlag)) if err != nil { return err } + + combinedComponents := component.GetMachineReadableFormatForCombinedCompList(s2iComps, devfileComps) + if log.IsJSON() { - machineoutput.OutputSuccess(components) + machineoutput.OutputSuccess(combinedComponents) } else { + w := tabwriter.NewWriter(os.Stdout, 5, 2, 3, ' ', tabwriter.TabIndent) - fmt.Fprintln(w, "APP", "\t", "NAME", "\t", "PROJECT", "\t", "TYPE", "\t", "SOURCETYPE", "\t", "STATE", "\t", "CONTEXT") - for _, comp := range components.Items { - fmt.Fprintln(w, comp.Spec.App, "\t", comp.Name, "\t", comp.Namespace, "\t", comp.Spec.Type, "\t", comp.Spec.SourceType, "\t", comp.Status.State, "\t", comp.Status.Context) + if len(devfileComps) != 0 { + lo.hasDevfileComponents = true + fmt.Fprintln(w, "Devfile Components: ") + fmt.Fprintln(w, "APP", "\t", "NAME", "\t", "PROJECT", "\t", "STATE", "\t", "CONTEXT") + for _, comp := range devfileComps { + fmt.Fprintln(w, comp.Spec.App, "\t", comp.Name, "\t", comp.Namespace, "\t", comp.Status.State, "\t", comp.Status.Context) + } + } + if lo.hasDevfileComponents { + fmt.Fprintln(w) + } + + if len(s2iComps) != 0 { + lo.hasS2IComponents = true + fmt.Fprintln(w, "S2I Components: ") + fmt.Fprintln(w, "APP", "\t", "NAME", "\t", "PROJECT", "\t", "TYPE", "\t", "SOURCETYPE", "\t", "STATE", "\t", "CONTEXT") + for _, comp := range s2iComps { + fmt.Fprintln(w, comp.Spec.App, "\t", comp.Name, "\t", comp.Namespace, "\t", comp.Spec.Type, "\t", comp.Spec.SourceType, "\t", comp.Status.State, "\t", comp.Status.Context) + + } + } + + // if we dont have any then + if !lo.hasDevfileComponents && !lo.hasS2IComponents { + fmt.Fprintln(w, "No components found") } + w.Flush() } return nil @@ -173,66 +195,56 @@ func (lo *ListOptions) Run() (err error) { // experimental workflow - if util.CheckPathExists(lo.devfilePath) { - - var deploymentList *appsv1.DeploymentList - var err error + var deploymentList *appsv1.DeploymentList - var selector string - // TODO: wrap this into a component list for docker support - if lo.allAppsFlag { - selector = project.GetSelector() + var selector string + // TODO: wrap this into a component list for docker support + if lo.allAppsFlag { + selector = project.GetSelector() + } else { + selector = applabels.GetSelector(lo.Application) + } - } else { - selector = applabels.GetSelector(lo.Application) - } + var devfileComponents []component.DevfileComponent + currentComponentState := component.StateTypeNotPushed + if lo.KClient != nil { deploymentList, err = lo.KClient.ListDeployments(selector) - if err != nil { return err } - - // Json output is not implemented yet for devfile - if !log.IsJSON() { - envinfo := lo.EnvSpecificInfo.EnvInfo - if len(deploymentList.Items) != 0 || envinfo.GetApplication() == lo.Application { - - currentComponentState := UnpushedCompState - currentComponentName := envinfo.GetName() - lo.hasDevfileComponents = true - w := tabwriter.NewWriter(os.Stdout, 5, 2, 3, ' ', tabwriter.TabIndent) - fmt.Fprintln(w, "Devfile Components: ") - fmt.Fprintln(w, "APP", "\t", "NAME", "\t", "PROJECT", "\t", "TYPE", "\t", "STATE") - for _, comp := range deploymentList.Items { - app := comp.Labels[applabels.ApplicationLabel] - cmpType := comp.Labels[componentlabels.ComponentTypeLabel] - if comp.Name == currentComponentName && app == envinfo.GetApplication() && comp.Namespace == envinfo.GetNamespace() { - currentComponentState = PushedCompState - } - fmt.Fprintln(w, app, "\t", comp.Name, "\t", comp.Namespace, "\t", cmpType, "\t", "Pushed") - } - - // 1st condition - only if we are using the same application or all-apps are provided should we show the current component - // 2nd condition - if the currentComponentState is unpushed that means it didn't show up in the list above - if (envinfo.GetApplication() == lo.Application || lo.allAppsFlag) && currentComponentState == UnpushedCompState { - fmt.Fprintln(w, envinfo.GetApplication(), "\t", currentComponentName, "\t", envinfo.GetNamespace(), "\t", lo.componentType, "\t", currentComponentState) + devfileComponents = append(devfileComponents, component.DevfileComponentsFromDeployments(deploymentList)...) + for _, comp := range devfileComponents { + 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) { + currentComponentState = component.StateTypePushed } - - w.Flush() } - } + } + // 1st condition - only if we are using the same application or all-apps are provided should we show the current component + // 2nd condition - if the currentComponentState is unpushed that means it didn't show up in the list above + if lo.EnvSpecificInfo != nil { + envinfo := lo.EnvSpecificInfo.EnvInfo + if (envinfo.GetApplication() == lo.Application || lo.allAppsFlag) && currentComponentState == component.StateTypeNotPushed { + comp := component.NewDevfileComponent(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) + } } // non-experimental workflow + var components []component.Component // we now check if DC is supported if lo.hasDCSupport { - var components component.ComponentList - if lo.allAppsFlag { // retrieve list of application apps, err := application.List(lo.Client) @@ -240,14 +252,12 @@ func (lo *ListOptions) Run() (err error) { return err } - var componentList []component.Component - if len(apps) == 0 && lo.LocalConfigInfo.Exists() { comps, err := component.List(lo.Client, lo.LocalConfigInfo.GetApplication(), lo.LocalConfigInfo) if err != nil { return err } - componentList = append(componentList, comps.Items...) + components = append(components, comps.Items...) } // interating over list of application and get list of all components @@ -256,43 +266,60 @@ func (lo *ListOptions) Run() (err error) { if err != nil { return err } - componentList = append(componentList, comps.Items...) + components = append(components, comps.Items...) } - // Get machine readable component list format - components = component.GetMachineReadableFormatForList(componentList) } else { - components, err = component.List(lo.Client, lo.Application, lo.LocalConfigInfo) + componentList, err := component.List(lo.Client, lo.Application, lo.LocalConfigInfo) + // compat + components = componentList.Items if err != nil { return errors.Wrapf(err, "failed to fetch component list") } } + } - if log.IsJSON() { - machineoutput.OutputSuccess(components) - } else { - if len(components.Items) != 0 { - if lo.hasDevfileComponents { - fmt.Println() - } - lo.hasS2IComponents = true - w := tabwriter.NewWriter(os.Stdout, 5, 2, 3, ' ', tabwriter.TabIndent) - fmt.Fprintln(w, "Openshift Components: ") - fmt.Fprintln(w, "APP", "\t", "NAME", "\t", "PROJECT", "\t", "TYPE", "\t", "SOURCETYPE", "\t", "STATE") - for _, comp := range components.Items { - fmt.Fprintln(w, comp.Spec.App, "\t", comp.Name, "\t", comp.Namespace, "\t", comp.Spec.Type, "\t", comp.Spec.SourceType, "\t", comp.Status.State) - } - w.Flush() + w := tabwriter.NewWriter(os.Stdout, 5, 2, 3, ' ', tabwriter.TabIndent) + + if !log.IsJSON() { + + if len(devfileComponents) != 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) } + w.Flush() + + } + if lo.hasDevfileComponents { + fmt.Fprintln(w) + } + + if len(components) != 0 { + if lo.hasDevfileComponents { + fmt.Println() + } + lo.hasS2IComponents = true + w := tabwriter.NewWriter(os.Stdout, 5, 2, 3, ' ', tabwriter.TabIndent) + fmt.Fprintln(w, "Openshift Components: ") + fmt.Fprintln(w, "APP", "\t", "NAME", "\t", "PROJECT", "\t", "TYPE", "\t", "SOURCETYPE", "\t", "STATE") + for _, comp := range components { + fmt.Fprintln(w, comp.Spec.App, "\t", comp.Name, "\t", comp.Namespace, "\t", comp.Spec.Type, "\t", comp.Spec.SourceType, "\t", comp.Status.State) + } + w.Flush() } - // if we dont have any of the components if !lo.hasDevfileComponents && !lo.hasS2IComponents { log.Error("There are no components deployed.") return } - + } else { + combinedComponents := component.GetMachineReadableFormatForCombinedCompList(components, devfileComponents) + machineoutput.OutputSuccess(combinedComponents) } + return } diff --git a/pkg/odo/cli/config/view.go b/pkg/odo/cli/config/view.go index ec0f02885e2..10092fee6e5 100644 --- a/pkg/odo/cli/config/view.go +++ b/pkg/odo/cli/config/view.go @@ -8,6 +8,7 @@ import ( "strings" "text/tabwriter" + "github.com/openshift/odo/pkg/component" "github.com/openshift/odo/pkg/config" "github.com/openshift/odo/pkg/devfile/parser" "github.com/openshift/odo/pkg/log" @@ -78,12 +79,12 @@ func (o *ViewOptions) Validate() (err error) { // DevfileRun is ran when the context detects a devfile locally func (o *ViewOptions) DevfileRun() (err error) { w := tabwriter.NewWriter(os.Stdout, 5, 2, 2, ' ', tabwriter.TabIndent) - repr := o.devfileObj.ToRepresentation() + repr := component.ToDevfileRepresentation(o.devfileObj) if log.IsJSON() { - machineoutput.OutputSuccess(o.devfileObj.WrapFromJSONOutput(repr)) + machineoutput.OutputSuccess(component.WrapFromJSONOutput(repr)) return } - representation, err := yaml.Marshal(o.devfileObj.ToRepresentation()) + representation, err := yaml.Marshal(repr) if err != nil { return err } diff --git a/pkg/odo/genericclioptions/context.go b/pkg/odo/genericclioptions/context.go index 1d21de29813..415f7cbb408 100644 --- a/pkg/odo/genericclioptions/context.go +++ b/pkg/odo/genericclioptions/context.go @@ -28,12 +28,16 @@ const ( ) // NewContext creates a new Context struct populated with the current state based on flags specified for the provided command -func NewContext(command *cobra.Command, ignoreMissingConfiguration ...bool) *Context { +func NewContext(command *cobra.Command, toggles ...bool) *Context { ignoreMissingConfig := false - if len(ignoreMissingConfiguration) == 1 { - ignoreMissingConfig = ignoreMissingConfiguration[0] + createApp := false + if len(toggles) == 1 { + ignoreMissingConfig = toggles[0] } - return newContext(command, false, ignoreMissingConfig) + if len(toggles) == 2 { + createApp = toggles[1] + } + return newContext(command, createApp, ignoreMissingConfig) } // NewDevfileContext creates a new Context struct populated with the current state based on flags specified for the provided command @@ -271,6 +275,9 @@ func (o *internalCxt) resolveProject(localConfiguration envinfo.LocalConfigProvi } o.Client.Namespace = namespace o.Project = namespace + if o.KClient != nil { + o.KClient.Namespace = namespace + } } // resolveNamespace resolves namespace for devfile component diff --git a/tests/integration/cmd_app_test.go b/tests/integration/cmd_app_test.go index 8bc5083f94b..c53e3881bb9 100644 --- a/tests/integration/cmd_app_test.go +++ b/tests/integration/cmd_app_test.go @@ -78,7 +78,7 @@ var _ = Describe("odo app command tests", func() { Expect(appListOutput).To(ContainSubstring(appName)) actualCompListJSON := helper.CmdShouldPass("odo", "list", "-o", "json") - desiredCompListJSON := fmt.Sprintf(`{"kind":"List","apiVersion":"odo.dev/v1alpha1","metadata":{},"items":[{"kind":"Component","apiVersion":"odo.dev/v1alpha1","metadata":{"name":"nodejs","creationTimestamp":null, "namespace":"%s"},"spec":{"type":"nodejs","app":"app","sourceType": "local","env":[{"name":"DEBUG_PORT","value":"5858"}]},"status":{"state":"Pushed"}}]}`, project) + desiredCompListJSON := fmt.Sprintf(`{"kind":"List","apiVersion":"odo.dev/v1alpha1","metadata":{},"s2iComponents":[{"kind":"Component","apiVersion":"odo.dev/v1alpha1","metadata":{"name":"nodejs","namespace":"%s","creationTimestamp":null},"spec":{"app":"app","type":"nodejs","sourceType":"local","env":[{"name":"DEBUG_PORT","value":"5858"}]},"status":{"state":"Pushed"}}],"devfileComponents":[]}`, project) Expect(desiredCompListJSON).Should(MatchJSON(actualCompListJSON)) helper.CmdShouldPass("odo", "app", "describe") diff --git a/tests/integration/component.go b/tests/integration/component.go index 0541c71fade..e11544a5a6f 100644 --- a/tests/integration/component.go +++ b/tests/integration/component.go @@ -193,7 +193,7 @@ func componentTests(args ...string) { cmpList := helper.CmdShouldPass("odo", append(args, "list", "--project", project)...) Expect(cmpList).To(ContainSubstring("cmp-git")) actualCompListJSON := helper.CmdShouldPass("odo", append(args, "list", "--project", project, "-o", "json")...) - desiredCompListJSON := fmt.Sprintf(`{"kind":"List","apiVersion":"odo.dev/v1alpha1","metadata":{},"items":[{"kind":"Component","apiVersion":"odo.dev/v1alpha1","metadata":{"name":"cmp-git","namespace":"%s","creationTimestamp":null},"spec":{"app":"testing","type":"nodejs","source":"https://github.com/openshift/nodejs-ex","sourceType": "git","env":[{"name":"DEBUG_PORT","value":"5858"}]},"status":{"state":"Pushed"}}]}`, project) + desiredCompListJSON := fmt.Sprintf(`{"kind":"List","apiVersion":"odo.dev/v1alpha1","metadata":{},"s2iComponents":[{"kind":"Component","apiVersion":"odo.dev/v1alpha1","metadata":{"name":"cmp-git","namespace":"%s","creationTimestamp":null},"spec":{"app":"testing","type":"nodejs","source":"https://github.com/openshift/nodejs-ex","sourceType":"git","env":[{"name":"DEBUG_PORT","value":"5858"}]},"status":{"state":"Pushed"}}],"devfileComponents":[]}`, project) Expect(desiredCompListJSON).Should(MatchJSON(actualCompListJSON)) cmpAllList := helper.CmdShouldPass("odo", append(args, "list", "--all-apps")...) Expect(cmpAllList).To(ContainSubstring("cmp-git")) @@ -559,7 +559,6 @@ func componentTests(args ...string) { componentList := helper.CmdShouldPass("odo", append(args, "list", "--app", appName, "--project", project)...) Expect(componentList).NotTo(ContainSubstring(componentName)) files := helper.ListFilesInDir(context) - fmt.Println(files) Expect(files).NotTo(ContainElement(".odo")) }) @@ -661,8 +660,6 @@ func componentTests(args ...string) { helper.ValidateLocalCmpExist(context, "Type,nodejs", "Name,"+cmpName, "Application,"+appName) helper.CmdShouldPass("odo", append(args, "push", "--context", context)...) - // list command should fail as no app flag is given - helper.CmdShouldFail("odo", append(args, "list", "--project", project)...) // commands should fail as the component name is missing helper.CmdShouldFail("odo", append(args, "describe", "--app", appName, "--project", project)...) helper.CmdShouldFail("odo", append(args, "delete", "-f", "--app", appName, "--project", project)...) diff --git a/tests/integration/devfile/cmd_devfile_push_test.go b/tests/integration/devfile/cmd_devfile_push_test.go index bfb59ba963b..37aab2f5f75 100644 --- a/tests/integration/devfile/cmd_devfile_push_test.go +++ b/tests/integration/devfile/cmd_devfile_push_test.go @@ -928,7 +928,7 @@ var _ = Describe("odo devfile push command tests", func() { helper.CopyExample(filepath.Join("source", "devfiles", "nodejs", "project"), context) helper.CopyExampleDevFile(filepath.Join("source", "devfiles", "nodejs", "devfile.yaml"), filepath.Join(context, "devfile.yaml")) output := helper.CmdShouldPass("odo", "list", "--context", context) - Expect(helper.Suffocate(output)).To(ContainSubstring(helper.Suffocate(fmt.Sprintf("%s%s%s%sUnpushed", "app", cmpName, namespace, "nodejs")))) + Expect(helper.Suffocate(output)).To(ContainSubstring(helper.Suffocate(fmt.Sprintf("%s%s%s%sNotPushed", "app", cmpName, namespace, "nodejs")))) output = helper.CmdShouldPass("odo", "push", "--context", context) Expect(output).To(ContainSubstring("Changes successfully pushed to component")) @@ -944,7 +944,7 @@ var _ = Describe("odo devfile push command tests", func() { helper.CopyExampleDevFile(filepath.Join("source", "devfiles", "nodejs", "devfile.yaml"), filepath.Join(context2, "devfile.yaml")) output = helper.CmdShouldPass("odo", "list", "--context", context2) - Expect(helper.Suffocate(output)).To(ContainSubstring(helper.Suffocate(fmt.Sprintf("%s%s%s%sUnpushed", appName, cmpName2, namespace, "nodejs")))) + Expect(helper.Suffocate(output)).To(ContainSubstring(helper.Suffocate(fmt.Sprintf("%s%s%s%sNotPushed", appName, cmpName2, namespace, "nodejs")))) output2 := helper.CmdShouldPass("odo", "push", "--context", context2) Expect(output2).To(ContainSubstring("Changes successfully pushed to component")) @@ -973,7 +973,7 @@ var _ = Describe("odo devfile push command tests", func() { helper.CopyExampleDevFile(filepath.Join("source", "devfiles", "nodejs", "devfile.yaml"), filepath.Join(context, "devfile.yaml")) output := helper.CmdShouldPass("odo", "list", "--context", context) - Expect(helper.Suffocate(output)).To(ContainSubstring(helper.Suffocate(fmt.Sprintf("%s%s%s%sUnpushed", "app", cmpName, namespace, "nodejs")))) + Expect(helper.Suffocate(output)).To(ContainSubstring(helper.Suffocate(fmt.Sprintf("%s%s%s%sNotPushed", "app", cmpName, namespace, "nodejs")))) output = helper.CmdShouldPass("odo", "push", "--context", context) Expect(output).To(ContainSubstring("Changes successfully pushed to component"))