Skip to content
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

Do not save component name in env.yaml #5852

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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