Skip to content

Commit

Permalink
✨ warning comment on PROJECT file (kubernetes-sigs#3137)
Browse files Browse the repository at this point in the history
* warning comment on PROJECT file

* make generate

* add link
  • Loading branch information
jason1028kr authored and Sajiyah-Salat committed Jun 11, 2023
1 parent d0a6092 commit 81a724c
Show file tree
Hide file tree
Showing 15 changed files with 74 additions and 13 deletions.
10 changes: 10 additions & 0 deletions pkg/config/store/yaml/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,13 @@ import (
const (
// DefaultPath is the default path for the configuration file
DefaultPath = "PROJECT"

// Comment for 'PROJECT' config file
commentStr = `# Code generated by tool. DO NOT EDIT.
# This file is used to track the info used to scaffold your project
# and allow the plugins properly work.
# More info: https://book.kubebuilder.io/reference/project-config.html
`
)

// yamlStore implements store.Store using a YAML file as the storage backend
Expand Down Expand Up @@ -133,6 +140,9 @@ func (s yamlStore) SaveTo(path string) error {
return store.SaveError{Err: fmt.Errorf("unable to marshal to YAML: %w", err)}
}

// Prepend warning comment for the 'PROJECT' file
content = append([]byte(commentStr), content...)

// Write the marshalled configuration
err = afero.WriteFile(s.fs, path, content, 0600)
if err != nil {
Expand Down
25 changes: 12 additions & 13 deletions pkg/config/store/yaml/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ layout: ""

Context("Load", func() {
It("should load the Config from an existing file at the default path", func() {
Expect(afero.WriteFile(s.fs, DefaultPath, []byte(v2File), os.ModePerm)).To(Succeed())
Expect(afero.WriteFile(s.fs, DefaultPath, []byte(commentStr+v2File), os.ModePerm)).To(Succeed())

Expect(s.Load()).To(Succeed())
Expect(s.fs).NotTo(BeNil())
Expand All @@ -102,23 +102,23 @@ layout: ""
})

It("should fail if unable to identify the version of the file at the default path", func() {
Expect(afero.WriteFile(s.fs, DefaultPath, []byte(unversionedFile), os.ModePerm)).To(Succeed())
Expect(afero.WriteFile(s.fs, DefaultPath, []byte(commentStr+unversionedFile), os.ModePerm)).To(Succeed())

err := s.Load()
Expect(err).To(HaveOccurred())
Expect(errors.As(err, &store.LoadError{})).To(BeTrue())
})

It("should fail if unable to create a Config for the version of the file at the default path", func() {
Expect(afero.WriteFile(s.fs, DefaultPath, []byte(nonexistentVersionFile), os.ModePerm)).To(Succeed())
Expect(afero.WriteFile(s.fs, DefaultPath, []byte(commentStr+nonexistentVersionFile), os.ModePerm)).To(Succeed())

err := s.Load()
Expect(err).To(HaveOccurred())
Expect(errors.As(err, &store.LoadError{})).To(BeTrue())
})

It("should fail if unable to unmarshal the file at the default path", func() {
Expect(afero.WriteFile(s.fs, DefaultPath, []byte(wrongFile), os.ModePerm)).To(Succeed())
Expect(afero.WriteFile(s.fs, DefaultPath, []byte(commentStr+wrongFile), os.ModePerm)).To(Succeed())

err := s.Load()
Expect(err).To(HaveOccurred())
Expand All @@ -128,7 +128,7 @@ layout: ""

Context("LoadFrom", func() {
It("should load the Config from an existing file from the specified path", func() {
Expect(afero.WriteFile(s.fs, path, []byte(v2File), os.ModePerm)).To(Succeed())
Expect(afero.WriteFile(s.fs, path, []byte(commentStr+v2File), os.ModePerm)).To(Succeed())

Expect(s.LoadFrom(path)).To(Succeed())
Expect(s.fs).NotTo(BeNil())
Expand All @@ -144,23 +144,23 @@ layout: ""
})

It("should fail if unable to identify the version of the file at the specified path", func() {
Expect(afero.WriteFile(s.fs, path, []byte(unversionedFile), os.ModePerm)).To(Succeed())
Expect(afero.WriteFile(s.fs, path, []byte(commentStr+unversionedFile), os.ModePerm)).To(Succeed())

err := s.LoadFrom(path)
Expect(err).To(HaveOccurred())
Expect(errors.As(err, &store.LoadError{})).To(BeTrue())
})

It("should fail if unable to create a Config for the version of the file at the specified path", func() {
Expect(afero.WriteFile(s.fs, path, []byte(nonexistentVersionFile), os.ModePerm)).To(Succeed())
Expect(afero.WriteFile(s.fs, path, []byte(commentStr+nonexistentVersionFile), os.ModePerm)).To(Succeed())

err := s.LoadFrom(path)
Expect(err).To(HaveOccurred())
Expect(errors.As(err, &store.LoadError{})).To(BeTrue())
})

It("should fail if unable to unmarshal the file at the specified path", func() {
Expect(afero.WriteFile(s.fs, path, []byte(wrongFile), os.ModePerm)).To(Succeed())
Expect(afero.WriteFile(s.fs, path, []byte(commentStr+wrongFile), os.ModePerm)).To(Succeed())

err := s.LoadFrom(path)
Expect(err).To(HaveOccurred())
Expand All @@ -175,7 +175,7 @@ layout: ""

cfgBytes, err := afero.ReadFile(s.fs, DefaultPath)
Expect(err).NotTo(HaveOccurred())
Expect(string(cfgBytes)).To(Equal(v2File))
Expect(string(cfgBytes)).To(Equal(commentStr + v2File))
})

It("should succeed for a valid config that must not exist", func() {
Expand All @@ -185,7 +185,7 @@ layout: ""

cfgBytes, err := afero.ReadFile(s.fs, DefaultPath)
Expect(err).NotTo(HaveOccurred())
Expect(string(cfgBytes)).To(Equal(v2File))
Expect(string(cfgBytes)).To(Equal(commentStr + v2File))
})

It("should fail for an empty config", func() {
Expand All @@ -212,8 +212,7 @@ layout: ""

cfgBytes, err := afero.ReadFile(s.fs, path)
Expect(err).NotTo(HaveOccurred())
Expect(string(cfgBytes)).To(Equal(`version: "2"
`))
Expect(string(cfgBytes)).To(Equal(commentStr + v2File))
})

It("should succeed for a valid config that must not exist", func() {
Expand All @@ -223,7 +222,7 @@ layout: ""

cfgBytes, err := afero.ReadFile(s.fs, path)
Expect(err).NotTo(HaveOccurred())
Expect(string(cfgBytes)).To(Equal(v2File))
Expect(string(cfgBytes)).To(Equal(commentStr + v2File))
})

It("should fail for an empty config", func() {
Expand Down
4 changes: 4 additions & 0 deletions testdata/project-v2/PROJECT
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# Code generated by tool. DO NOT EDIT.
# This file is used to track the info used to scaffold your project
# and allow the plugins properly work.
# More info: https://book.kubebuilder.io/reference/project-config.html
domain: testproject.org
repo: sigs.k8s.io/kubebuilder/testdata/project-v2
resources:
Expand Down
4 changes: 4 additions & 0 deletions testdata/project-v3-config/PROJECT
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# Code generated by tool. DO NOT EDIT.
# This file is used to track the info used to scaffold your project
# and allow the plugins properly work.
# More info: https://book.kubebuilder.io/reference/project-config.html
componentConfig: true
domain: testproject.org
layout:
Expand Down
4 changes: 4 additions & 0 deletions testdata/project-v3-declarative-v1/PROJECT
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# Code generated by tool. DO NOT EDIT.
# This file is used to track the info used to scaffold your project
# and allow the plugins properly work.
# More info: https://book.kubebuilder.io/reference/project-config.html
domain: testproject.org
layout:
- go.kubebuilder.io/v3
Expand Down
4 changes: 4 additions & 0 deletions testdata/project-v3-multigroup/PROJECT
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# Code generated by tool. DO NOT EDIT.
# This file is used to track the info used to scaffold your project
# and allow the plugins properly work.
# More info: https://book.kubebuilder.io/reference/project-config.html
domain: testproject.org
layout:
- go.kubebuilder.io/v3
Expand Down
4 changes: 4 additions & 0 deletions testdata/project-v3-with-deploy-image/PROJECT
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# Code generated by tool. DO NOT EDIT.
# This file is used to track the info used to scaffold your project
# and allow the plugins properly work.
# More info: https://book.kubebuilder.io/reference/project-config.html
domain: testproject.org
layout:
- go.kubebuilder.io/v3
Expand Down
4 changes: 4 additions & 0 deletions testdata/project-v3-with-grafana/PROJECT
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# Code generated by tool. DO NOT EDIT.
# This file is used to track the info used to scaffold your project
# and allow the plugins properly work.
# More info: https://book.kubebuilder.io/reference/project-config.html
domain: testproject.org
layout:
- go.kubebuilder.io/v3
Expand Down
4 changes: 4 additions & 0 deletions testdata/project-v3/PROJECT
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# Code generated by tool. DO NOT EDIT.
# This file is used to track the info used to scaffold your project
# and allow the plugins properly work.
# More info: https://book.kubebuilder.io/reference/project-config.html
domain: testproject.org
layout:
- go.kubebuilder.io/v3
Expand Down
4 changes: 4 additions & 0 deletions testdata/project-v4-config/PROJECT
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# Code generated by tool. DO NOT EDIT.
# This file is used to track the info used to scaffold your project
# and allow the plugins properly work.
# More info: https://book.kubebuilder.io/reference/project-config.html
componentConfig: true
domain: testproject.org
layout:
Expand Down
4 changes: 4 additions & 0 deletions testdata/project-v4-declarative-v1/PROJECT
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# Code generated by tool. DO NOT EDIT.
# This file is used to track the info used to scaffold your project
# and allow the plugins properly work.
# More info: https://book.kubebuilder.io/reference/project-config.html
domain: testproject.org
layout:
- go.kubebuilder.io/v4-alpha
Expand Down
4 changes: 4 additions & 0 deletions testdata/project-v4-multigroup/PROJECT
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# Code generated by tool. DO NOT EDIT.
# This file is used to track the info used to scaffold your project
# and allow the plugins properly work.
# More info: https://book.kubebuilder.io/reference/project-config.html
domain: testproject.org
layout:
- go.kubebuilder.io/v4-alpha
Expand Down
4 changes: 4 additions & 0 deletions testdata/project-v4-with-deploy-image/PROJECT
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# Code generated by tool. DO NOT EDIT.
# This file is used to track the info used to scaffold your project
# and allow the plugins properly work.
# More info: https://book.kubebuilder.io/reference/project-config.html
domain: testproject.org
layout:
- go.kubebuilder.io/v4-alpha
Expand Down
4 changes: 4 additions & 0 deletions testdata/project-v4-with-grafana/PROJECT
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# Code generated by tool. DO NOT EDIT.
# This file is used to track the info used to scaffold your project
# and allow the plugins properly work.
# More info: https://book.kubebuilder.io/reference/project-config.html
domain: testproject.org
layout:
- go.kubebuilder.io/v3
Expand Down
4 changes: 4 additions & 0 deletions testdata/project-v4/PROJECT
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# Code generated by tool. DO NOT EDIT.
# This file is used to track the info used to scaffold your project
# and allow the plugins properly work.
# More info: https://book.kubebuilder.io/reference/project-config.html
domain: testproject.org
layout:
- go.kubebuilder.io/v4-alpha
Expand Down

0 comments on commit 81a724c

Please sign in to comment.