Skip to content

Commit

Permalink
Dont save name of component to env.yaml
Browse files Browse the repository at this point in the history
<!--
Thank you for opening a PR! Here are some things you need to know before submitting:

1. Please read our developer guideline: https://github.com/redhat-developer/odo/wiki/Dev:-odo-Dev-Guidelines
2. Label this PR accordingly with the '/kind' line
3. Ensure you have written and ran the appropriate tests: https://github.com/redhat-developer/odo/wiki/Dev:-Writing-and-running-tests
4. Read how we approve and LGTM each PR: https://github.com/redhat-developer/odo/wiki/Pull-Requests:-Review-guideline

Documentation:

If you are pushing a change to documentation, please read: https://github.com/redhat-developer/odo/wiki/Documentation:-Contributing
-->

**What type of PR is this:**

<!--
Add one of the following kinds:
/kind feature
/kind cleanup
/kind tests
/kind documentation

Feel free to use other [labels](https://github.com/redhat-developer/odo/labels) as needed. However one of the above labels must be present or the PR will not be reviewed. This instruction is for reviewers as well.
-->
/kind bug

**What does this PR do / why we need it:**

By default we should be saving the namespace / project name, not the app name /
component name.

**Which issue(s) this PR fixes:**
<!--
Specifying the issue will automatically close it when this PR is merged
-->

Fixes #5780
Fixes #5886

**PR acceptance criteria:**

- [X] Unit test

- [X] Integration test

- [X] Documentation

**How to test changes / Special notes to the reviewer:**

Signed-off-by: Charlie Drage <charlie@charliedrage.com>
  • Loading branch information
cdrage committed Jul 23, 2022
1 parent 8ba58d9 commit fb14d31
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 34 deletions.
2 changes: 1 addition & 1 deletion pkg/component/pushed_component.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion pkg/devfile/adapters/kubernetes/component/adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
})
Expand Down
29 changes: 18 additions & 11 deletions pkg/odo/cli/deploy/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"path/filepath"

"github.com/spf13/cobra"
"k8s.io/klog"
"k8s.io/kubectl/pkg/util/templates"
)

Expand Down Expand Up @@ -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
Expand Down
29 changes: 16 additions & 13 deletions pkg/odo/cli/dev/dev.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down
11 changes: 3 additions & 8 deletions pkg/storage/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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,
Expand Down
7 changes: 7 additions & 0 deletions tests/helper/helper_filesystem.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
7 changes: 7 additions & 0 deletions tests/integration/cmd_dev_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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() {
Expand Down

0 comments on commit fb14d31

Please sign in to comment.