diff --git a/pkg/component/pushed_component.go b/pkg/component/pushed_component.go index 685f34979de..d6d1d53999a 100644 --- a/pkg/component/pushed_component.go +++ b/pkg/component/pushed_component.go @@ -4,7 +4,7 @@ import ( "fmt" appsv1 "k8s.io/api/apps/v1" - "k8s.io/api/core/v1" + v1 "k8s.io/api/core/v1" odolabels "github.com/redhat-developer/odo/pkg/labels" "github.com/redhat-developer/odo/pkg/storage" diff --git a/pkg/devfile/adapters/kubernetes/component/adapter.go b/pkg/devfile/adapters/kubernetes/component/adapter.go index 3855aad43ee..b641b93f87c 100644 --- a/pkg/devfile/adapters/kubernetes/component/adapter.go +++ b/pkg/devfile/adapters/kubernetes/component/adapter.go @@ -414,7 +414,7 @@ func (a *Adapter) createOrUpdateComponent( ei.SetDevfileObj(a.Devfile) componentName := a.ComponentName - storageClient := storagepkg.NewClient(storagepkg.ClientOptions{ + storageClient := storagepkg.NewClient(componentName, a.AppName, storagepkg.ClientOptions{ Client: a.kubeClient, LocalConfigProvider: &ei, }) diff --git a/pkg/odo/cli/deploy/deploy.go b/pkg/odo/cli/deploy/deploy.go index 01a61373036..616f2fbd611 100644 --- a/pkg/odo/cli/deploy/deploy.go +++ b/pkg/odo/cli/deploy/deploy.go @@ -23,6 +23,7 @@ import ( "path/filepath" "github.com/spf13/cobra" + "k8s.io/klog" "k8s.io/kubectl/pkg/util/templates" ) @@ -105,28 +106,34 @@ func (o *DeployOptions) Complete(cmdline cmdline.Cmdline, args []string) (err er return err } - envFileInfo, err := envinfo.NewEnvSpecificInfo(o.contextDir) + // ENV.YAML + // + // Set the appropriate variables in the env.yaml file + // + // TODO: Eventually refactor with code in dev/dev.go + + envfileinfo, err := envinfo.NewEnvSpecificInfo("") if err != nil { return fmt.Errorf("unable to retrieve configuration information: %w", err) } - if !envFileInfo.Exists() { - var cmpName string - cmpName, err = component.GatherName(o.EnvSpecificInfo.GetDevfileObj(), o.GetDevfilePath()) - if err != nil { - return fmt.Errorf("unable to retrieve component name: %w", err) - } - err = envFileInfo.SetComponentSettings(envinfo.ComponentSettings{Name: cmpName, Project: o.GetProject(), AppName: "app"}) + + // If the env.yaml does not exist, we will save the project name + if !envfileinfo.Exists() { + err = envfileinfo.SetComponentSettings(envinfo.ComponentSettings{Project: o.GetProject()}) if err != nil { return fmt.Errorf("failed to write new env.yaml file: %w", err) } - - } else if envFileInfo.GetComponentSettings().Project != o.GetProject() { - err = envFileInfo.SetConfiguration("project", o.GetProject()) + } else if envfileinfo.Exists() && envfileinfo.GetComponentSettings().Project != o.GetProject() { + // If the env.yaml exists and the project is set incorrectly, we'll override it. + klog.V(4).Info("Overriding project name in env.yaml as it's set incorrectly, new project name: ", o.GetProject()) + err = envfileinfo.SetConfiguration("project", o.GetProject()) if err != nil { return fmt.Errorf("failed to update project in env.yaml file: %w", err) } } + // END ENV.YAML + // this ensures that odo deploy uses the namespace set in env.yaml o.clientset.KubernetesClient.SetNamespace(o.GetProject()) return diff --git a/pkg/odo/cli/dev/dev.go b/pkg/odo/cli/dev/dev.go index d4d26743e06..00105c2559d 100644 --- a/pkg/odo/cli/dev/dev.go +++ b/pkg/odo/cli/dev/dev.go @@ -11,6 +11,7 @@ import ( "github.com/spf13/cobra" "github.com/devfile/library/pkg/devfile/parser" + "k8s.io/klog" ktemplates "k8s.io/kubectl/pkg/util/templates" "github.com/redhat-developer/odo/pkg/component" @@ -148,33 +149,35 @@ func (o *DevOptions) Complete(cmdline cmdline.Cmdline, args []string) error { return fmt.Errorf("unable to create context: %v", err) } + o.initialDevfileObj = o.Context.EnvSpecificInfo.GetDevfileObj() + + // ENV.YAML + // + // Set the appropriate variables in the env.yaml file + // + // TODO: Eventually refactor with code in deploy/deploy.go envfileinfo, err := envinfo.NewEnvSpecificInfo("") if err != nil { return fmt.Errorf("unable to retrieve configuration information: %v", err) } - o.initialDevfileObj = o.Context.EnvSpecificInfo.GetDevfileObj() - + // If the env.yaml does not exist, we will save the project name if !envfileinfo.Exists() { - // if env.yaml doesn't exist, get component name from the devfile.yaml - var cmpName string - cmpName, err = component.GatherName(o.EnvSpecificInfo.GetDevfileObj(), o.GetDevfilePath()) - if err != nil { - return fmt.Errorf("unable to retrieve component name: %w", err) - } - // create env.yaml file with component, project/namespace and application info - // TODO - store only namespace into env.yaml, we don't want to track component or application name via env.yaml - err = envfileinfo.SetComponentSettings(envinfo.ComponentSettings{Name: cmpName, Project: o.GetProject(), AppName: "app"}) + err = envfileinfo.SetComponentSettings(envinfo.ComponentSettings{Project: o.GetProject()}) if err != nil { return fmt.Errorf("failed to write new env.yaml file: %w", err) } - } else if envfileinfo.GetComponentSettings().Project != o.GetProject() { - // set namespace if the evn.yaml exists; that's the only piece we care about in env.yaml + } else if envfileinfo.Exists() && envfileinfo.GetComponentSettings().Project != o.GetProject() { + // If the env.yaml exists and the project is set incorrectly, we'll override it. + klog.V(4).Info("Overriding project name in env.yaml as it's set incorrectly, new project name: ", o.GetProject()) err = envfileinfo.SetConfiguration("project", o.GetProject()) if err != nil { return fmt.Errorf("failed to update project in env.yaml file: %w", err) } } + + // END ENV.YAML + o.clientset.KubernetesClient.SetNamespace(o.GetProject()) // 3 steps to evaluate the paths to be ignored when "watching" the pwd/cwd for changes diff --git a/pkg/storage/storage.go b/pkg/storage/storage.go index 89cd9f3271f..fb8c7a64eb5 100644 --- a/pkg/storage/storage.go +++ b/pkg/storage/storage.go @@ -6,7 +6,6 @@ import ( v1 "k8s.io/api/apps/v1" "github.com/redhat-developer/odo/pkg/kclient" - "github.com/redhat-developer/odo/pkg/labels" "github.com/redhat-developer/odo/pkg/localConfigProvider" "github.com/redhat-developer/odo/pkg/log" ) @@ -45,21 +44,17 @@ type Client interface { } // NewClient gets the appropriate Storage client based on the parameters -func NewClient(options ClientOptions) Client { +func NewClient(componentName string, appName string, options ClientOptions) Client { var genericInfo generic if options.LocalConfigProvider != nil { genericInfo = generic{ - appName: options.LocalConfigProvider.GetApplication(), - componentName: options.LocalConfigProvider.GetName(), localConfigProvider: options.LocalConfigProvider, } } - if options.Deployment != nil { - genericInfo.appName = labels.GetAppName(options.Deployment.Labels) - genericInfo.componentName = labels.GetComponentName(options.Deployment.Labels) - } + genericInfo.componentName = componentName + genericInfo.appName = appName return kubernetesClient{ generic: genericInfo, diff --git a/tests/helper/helper_filesystem.go b/tests/helper/helper_filesystem.go index 1a4bf5ed644..09fb34df2ac 100644 --- a/tests/helper/helper_filesystem.go +++ b/tests/helper/helper_filesystem.go @@ -165,6 +165,13 @@ func FileShouldContainSubstring(file string, subString string) { Expect(string(data)).To(ContainSubstring(subString)) } +// FileShouldNotContainSubstring check if file does not contain subString +func FileShouldNotContainSubstring(file string, subString string) { + data, err := ioutil.ReadFile(file) + Expect(err).NotTo(HaveOccurred()) + Expect(string(data)).NotTo(ContainSubstring(subString)) +} + // ReplaceString replaces oldString with newString in text file func ReplaceString(filename string, oldString string, newString string) { fmt.Fprintf(GinkgoWriter, "Replacing \"%s\" with \"%s\" in %s\n", oldString, newString, filename) diff --git a/tests/integration/cmd_dev_test.go b/tests/integration/cmd_dev_test.go index 4a3538ea75f..d57eb9ef531 100644 --- a/tests/integration/cmd_dev_test.go +++ b/tests/integration/cmd_dev_test.go @@ -273,6 +273,7 @@ var _ = Describe("odo dev command tests", func() { // An ENV file should have been created indicating current namespace Expect(helper.VerifyFileExists(".odo/env/env.yaml")).To(BeTrue()) helper.FileShouldContainSubstring(".odo/env/env.yaml", "Project: "+commonVar.Project) + helper.FileShouldNotContainSubstring(".odo/env/env.yaml", "Name: "+cmpName) }) AfterEach(func() { @@ -303,6 +304,12 @@ var _ = Describe("odo dev command tests", func() { ContainSubstring(fmt.Sprintf("%s-app-", cmpName)), )) }) + + // Returned pvc yaml contains ownerreference + By("creating a pvc with ownerreference", func() { + output := commonVar.CliRunner.Run("get", "pvc", "--namespace", commonVar.Project, "-o", `jsonpath='{range .items[*].metadata.ownerReferences[*]}{@..kind}{"/"}{@..name}{"\n"}{end}'`).Out.Contents() + Expect(string(output)).To(ContainSubstring(fmt.Sprintf("Deployment/%s-app", cmpName))) + }) }) When("killing odo dev and running odo delete component --wait", func() {