Skip to content

Makefile changes for addition of run bundle in Java Plugin. #84

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

Merged
merged 27 commits into from
Jun 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
52f055f
updated makefile
laxmikantbpandhare May 19, 2022
7605a9b
updated makefile
laxmikantbpandhare May 19, 2022
b8754b2
updated makefile
laxmikantbpandhare May 19, 2022
c434142
modified content to add changes in Makefile
laxmikantbpandhare May 19, 2022
7d5502b
removed unused variable
laxmikantbpandhare May 19, 2022
9025bf3
modified create api subcommand code and added bundle code in Makefile
laxmikantbpandhare May 24, 2022
9d99a04
updated and added bundle commands
laxmikantbpandhare May 24, 2022
484436f
updated image tag
laxmikantbpandhare May 24, 2022
30e8a12
removed comments
laxmikantbpandhare May 24, 2022
91a283f
removed comments
laxmikantbpandhare May 24, 2022
aa82175
removed comments
laxmikantbpandhare May 24, 2022
d2a0acd
modified projectName if it is emoty according to project path
laxmikantbpandhare May 24, 2022
d038ebe
modified code for handling multiple create api commands
laxmikantbpandhare Jun 1, 2022
078ea6d
modified code for handling multiple create api commands
laxmikantbpandhare Jun 1, 2022
276d8aa
removed comments
laxmikantbpandhare Jun 1, 2022
c1c01df
resolved some minor changes
laxmikantbpandhare Jun 1, 2022
2a0662a
Merge pull request #1 from laxmikantbpandhare/run-bundle-makefile-mul…
laxmikantbpandhare Jun 1, 2022
9f3c96e
uodate space
laxmikantbpandhare Jun 1, 2022
67c8ed6
Merge pull request #2 from laxmikantbpandhare/run-bundle-makefile-mul…
laxmikantbpandhare Jun 1, 2022
5048145
modified minor changes
laxmikantbpandhare Jun 2, 2022
713f109
Merge pull request #3 from laxmikantbpandhare/run-bundle-makefile-mul…
laxmikantbpandhare Jun 2, 2022
28a3ba6
added logs for plugin Makefile
laxmikantbpandhare Jun 2, 2022
13652c9
Merge pull request #4 from laxmikantbpandhare/run-bundle-makefile-mul…
laxmikantbpandhare Jun 2, 2022
ebbab67
remove comments from plugin Makefile
laxmikantbpandhare Jun 2, 2022
407c2a1
Merge pull request #5 from laxmikantbpandhare/run-bundle-makefile-mul…
laxmikantbpandhare Jun 2, 2022
1e55597
remove comments from plugin Makefile
laxmikantbpandhare Jun 2, 2022
203f444
Merge pull request #6 from laxmikantbpandhare/run-bundle-makefile-mul…
laxmikantbpandhare Jun 2, 2022
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
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ require (
github.com/fsnotify/fsnotify v1.5.1 // indirect
github.com/gobuffalo/flect v0.2.3 // indirect
github.com/nxadm/tail v1.4.8 // indirect
github.com/spf13/afero v1.6.0 // indirect
github.com/sirupsen/logrus v1.8.1
github.com/spf13/afero v1.6.0
golang.org/x/mod v0.4.2 // indirect
golang.org/x/net v0.0.0-20210825183410-e898025ed96a // indirect
golang.org/x/sys v0.0.0-20211029165221-6e7872819dc8 // indirect
Expand Down
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -445,6 +445,7 @@ github.com/sirupsen/logrus v1.3.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPx
github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE=
github.com/sirupsen/logrus v1.6.0/go.mod h1:7uNnSEd1DgxDLC74fIahvMZmmYsHGZGEOFrfsX/uA88=
github.com/sirupsen/logrus v1.7.0/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0=
github.com/sirupsen/logrus v1.8.1 h1:dJKuHgqk1NNQlqoA6BTlM1Wf9DOH3NBjQyu0h9+AZZE=
github.com/sirupsen/logrus v1.8.1/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0=
github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d/go.mod h1:OnSkiWE9lh6wB0YB77sQom3nweQdgAjqCqsofrRNTgc=
github.com/smartystreets/goconvey v1.6.4/go.mod h1:syvi0/a8iFYH4r/RixwvyeAJjdLS9QV7WQ/tjFTllLA=
Expand Down
125 changes: 123 additions & 2 deletions pkg/quarkus/v1alpha/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,28 @@
package v1

import (
"bufio"
"errors"
"fmt"
"os"
"path/filepath"
"strings"

"github.com/operator-framework/java-operator-plugins/pkg/quarkus/v1alpha/scaffolds"
log "github.com/sirupsen/logrus"
"github.com/spf13/afero"
"github.com/spf13/pflag"

"sigs.k8s.io/kubebuilder/v3/pkg/config"
"sigs.k8s.io/kubebuilder/v3/pkg/machinery"
"sigs.k8s.io/kubebuilder/v3/pkg/model/resource"
"sigs.k8s.io/kubebuilder/v3/pkg/plugin"
"sigs.k8s.io/kubebuilder/v3/pkg/plugin/util"
pluginutil "sigs.k8s.io/kubebuilder/v3/pkg/plugin/util"

"github.com/operator-framework/java-operator-plugins/pkg/quarkus/v1alpha/scaffolds"
)

const filePath = "Makefile"

type createAPIOptions struct {
CRDVersion string
Namespaced bool
Expand Down Expand Up @@ -81,7 +90,40 @@ func (p *createAPISubcommand) PostScaffold() error {

func (p *createAPISubcommand) Scaffold(fs machinery.Filesystem) error {
scaffolder := scaffolds.NewCreateAPIScaffolder(p.config, *p.resource)

var s = fmt.Sprintf(makefileBundleCRDFile, p.resource.Plural, p.resource.QualifiedGroup(), p.resource.Version)
foundLine := findOldFilesForReplacement(filePath, s)

if !foundLine {
makefileBytes, err := afero.ReadFile(fs.FS, filePath)
if err != nil {
return err
}

projectName := p.config.GetProjectName()
if projectName == "" {
dir, err := os.Getwd()
if err != nil {
return fmt.Errorf("error getting current directory: %w", err)
}
projectName = strings.ToLower(filepath.Base(dir))
}

makefileBytes = append(makefileBytes, []byte(fmt.Sprintf(makefileBundleVarFragment, p.resource.Plural, p.resource.QualifiedGroup(), p.resource.Version, projectName))...)

makefileBytes = append([]byte(fmt.Sprintf(makefileBundleImageFragement, p.config.GetDomain(), projectName)), makefileBytes...)

var mode os.FileMode = 0644
if info, err := fs.FS.Stat(filePath); err == nil {
mode = info.Mode()
}
if err := afero.WriteFile(fs.FS, filePath, makefileBytes, mode); err != nil {
return fmt.Errorf("error updating Makefile: %w", err)
}
}

scaffolder.InjectFS(fs)

if err := scaffolder.Scaffold(); err != nil {
return err
}
Expand Down Expand Up @@ -116,3 +158,82 @@ func (p *createAPISubcommand) InjectResource(res *resource.Resource) error {

return nil
}

// findOldFilesForReplacement verifies marker (## marker) and if it found then merge new api CRD file to the odler logic
func findOldFilesForReplacement(path, newfile string) bool {

f, err := os.Open(path)
if err != nil {
log.Fatal(err)
}

// remember to close the file at the end of the program
defer f.Close()

// read the file line by line using scanner
scanner := bufio.NewScanner(f)
var foundMarker bool
for scanner.Scan() {
// do something with a line
if scanner.Text() == "## marker" {
foundMarker = true
break
}
}

if foundMarker {
scanner.Scan()
catLine := scanner.Text()

splitByPipe := strings.Split(catLine, "|")

finalString := strings.TrimSuffix(strings.TrimPrefix(strings.TrimSpace(splitByPipe[0]), "cat"), "target/kubernetes/kubernetes.yml")

updatedLine := " " + "cat" + finalString + newfile + " target/kubernetes/kubernetes.yml" + " |" + splitByPipe[1]

if err := scanner.Err(); err != nil {
log.Error(err, "Unable to scan existing bundle target command from the Makefile. New bundle target command being created. This may overwrite any existing commands.")
return false
}

// ReplaceInFile replaces all instances of old with new in the file at path.
err = util.ReplaceInFile(path, catLine, updatedLine)
if err != nil {
log.Error(err, "Unable to replace existing bundle target command from the Makefile. New bundle target command being created. This may overwrite any existing commands.")
return false
}
}

return foundMarker
}

const (
makefileBundleCRDFile = `target/kubernetes/%[1]s.%[2]s-%[3]s.yml`
)

const (
makefileBundleVarFragment = `
##@Bundle
.PHONY: bundle
bundle: ## Generate bundle manifests and metadata, then validate generated files.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I won't hold this PR up but I think we need to make this depend on a build target.

.PHONY: build
build:
    mvn install

.PHONY: bundle
bundle: build
    cat target...

Basically in our other projects if we run make bundle it will do a generation and create the manifests required. But in Java, it won't. But that's because we're not calling anything from make bundle.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes agree, we must add mvn install before generating the bundle else it will fail if the User is not compiled the project before generating the bundle manifest. Let me try to add this to the existing logic.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Quick question here, when the User tries the below command at that moment itself it will generate the /target directory.

make docker-build docker-push IMG=quay.io/YOURUSER/memcached-quarkus-operator:0.0.1

Do you still think we still need to add mvn clean install in the bundle structure?

## marker
cat target/kubernetes/%[1]s.%[2]s-%[3]s.yml target/kubernetes/kubernetes.yml | operator-sdk generate bundle -q --overwrite --version 0.1.1 --default-channel=stable --channels=stable --package=%[4]s
operator-sdk bundle validate ./bundle

.PHONY: bundle-build
bundle-build: ## Build the bundle image.
docker build -f bundle.Dockerfile -t $(BUNDLE_IMG) .

.PHONY: bundle-push
bundle-push: ## Push the bundle image.
docker push $(BUNDLE_IMG)
`
)

const (
makefileBundleImageFragement = `
VERSION ?= 0.0.1
IMAGE_TAG_BASE ?= %[1]s/%[2]s
BUNDLE_IMG ?= $(IMAGE_TAG_BASE)-bundle:v$(VERSION)
`
)