Skip to content

Commit

Permalink
fix: unable to build multigroup projects
Browse files Browse the repository at this point in the history
  • Loading branch information
camilamacedo86 committed May 8, 2020
1 parent 45ade82 commit 548be53
Show file tree
Hide file tree
Showing 6 changed files with 128 additions and 3 deletions.
39 changes: 38 additions & 1 deletion pkg/scaffold/edit.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ limitations under the License.
package scaffold

import (
"fmt"
"io/ioutil"
"strings"

"sigs.k8s.io/kubebuilder/pkg/model/config"
)

Expand All @@ -38,5 +42,38 @@ func NewEditScaffolder(config *config.Config, multigroup bool) Scaffolder {
// Scaffold implements Scaffolder
func (s *editScaffolder) Scaffold() error {
s.config.MultiGroup = s.multigroup
return nil
filename := "Dockerfile"
bs, err := ioutil.ReadFile(filename)
if err != nil {
return err
}
str := string(bs)

// update dockerfile
if s.multigroup {
str, err = ensureExistAndReplace(
str,
"COPY api/ api/",
`COPY apis/ apis/`)
if err != nil {
return err
}
} else {
str, err = ensureExistAndReplace(
str,
"COPY apis/ apis/",
`COPY api/ api/`)
if err != nil {
return err
}
}

return ioutil.WriteFile(filename, []byte(str), 0644)
}

func ensureExistAndReplace(input, match, replace string) (string, error) {
if !strings.Contains(input, match) {
return "", fmt.Errorf("can't find %q", match)
}
return strings.Replace(input, match, replace, -1), nil
}
8 changes: 8 additions & 0 deletions test/e2e/utils/test_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,14 @@ func (kc *KBTestContext) CreateAPI(resourceOptions ...string) error {
return err
}

// Edit is for running `kubebuilder edit`
func (kc *KBTestContext) Edit(editOptions ...string) error {
editOptions = append([]string{"edit"}, editOptions...)
cmd := exec.Command("kubebuilder", editOptions...)
_, err := kc.Run(cmd)
return err
}

// CreateWebhook is for running `kubebuilder create webhook`
func (kc *KBTestContext) CreateWebhook(resourceOptions ...string) error {
resourceOptions = append([]string{"create", "webhook"}, resourceOptions...)
Expand Down
40 changes: 40 additions & 0 deletions test/e2e/v2/e2e_suite.go
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,46 @@ var _ = Describe("kubebuilder", func() {
Expect(err).NotTo(HaveOccurred())
Expect(count).To(BeNumerically("==", 5))
})

It("should build successfully with multi-group layout", func() {
By("init v2 multi-group project")
err := kbc.Init(
"--project-version", "2",
"--domain", kbc.Domain,
"--fetch-deps=false")
Expect(err).Should(Succeed())

By("editing default layout")
err = kbc.Edit(
"--multigroup=true")
Expect(err).Should(Succeed())

By("creating api definition")
err = kbc.CreateAPI(
"--group", kbc.Group,
"--version", kbc.Version,
"--kind", kbc.Kind,
"--namespaced",
"--resource=true",
"--controller=true",
"--make=false")
Expect(err).Should(Succeed())

By("creating second api definition with same kind and version")
err = kbc.CreateAPI(
"--group", kbc.Group+"-example",
"--version", kbc.Version,
"--kind", kbc.Kind,
"--namespaced",
"--resource=true",
"--controller=true",
"--make=false")
Expect(err).Should(Succeed())

By("building image")
err = kbc.Make("docker-build", "IMG="+kbc.ImageName)
Expect(err).Should(Succeed())
})
})
})

Expand Down
40 changes: 40 additions & 0 deletions test/e2e/v3/e2e_suite.go
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,46 @@ var _ = Describe("kubebuilder", func() {
Expect(err).NotTo(HaveOccurred())
Expect(count).To(BeNumerically("==", 5))
})

It("should build successfully with multi-group layout", func() {
By("init v3 multi-group project")
err := kbc.Init(
"--project-version", "3-alpha",
"--domain", kbc.Domain,
"--fetch-deps=false")
Expect(err).Should(Succeed())

By("editing default layout")
err = kbc.Edit(
"--multigroup=true")
Expect(err).Should(Succeed())

By("creating api definition")
err = kbc.CreateAPI(
"--group", kbc.Group,
"--version", kbc.Version,
"--kind", kbc.Kind,
"--namespaced",
"--resource=true",
"--controller=true",
"--make=fale")
Expect(err).Should(Succeed())

By("creating second api definition with same kind and version")
err = kbc.CreateAPI(
"--group", kbc.Group+"-example",
"--version", kbc.Version,
"--kind", kbc.Kind,
"--namespaced",
"--resource=true",
"--controller=true",
"--make=false")
Expect(err).Should(Succeed())

By("building image")
err = kbc.Make("docker-build", "IMG="+kbc.ImageName)
Expect(err).Should(Succeed())
})
})
})

Expand Down
2 changes: 1 addition & 1 deletion testdata/project-v2-multigroup/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ RUN go mod download

# Copy the go source
COPY main.go main.go
COPY api/ api/
COPY apis/ apis/
COPY controllers/ controllers/

# Build
Expand Down
2 changes: 1 addition & 1 deletion testdata/project-v3-multigroup/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ RUN go mod download

# Copy the go source
COPY main.go main.go
COPY api/ api/
COPY apis/ apis/
COPY controllers/ controllers/

# Build
Expand Down

0 comments on commit 548be53

Please sign in to comment.