-
Notifications
You must be signed in to change notification settings - Fork 25
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
Changes from 21 commits
52f055f
7605a9b
b8754b2
c434142
7d5502b
9025bf3
9d99a04
484436f
30e8a12
91a283f
aa82175
d2a0acd
d038ebe
078ea6d
276d8aa
c1c01df
2a0662a
9f3c96e
67c8ed6
5048145
713f109
28a3ba6
13652c9
ebbab67
407c2a1
1e55597
203f444
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,19 +15,28 @@ | |
package v1 | ||
|
||
import ( | ||
"bufio" | ||
"errors" | ||
"fmt" | ||
"log" | ||
"os" | ||
"path/filepath" | ||
"strings" | ||
|
||
"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 | ||
|
@@ -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 | ||
} | ||
|
@@ -116,3 +158,83 @@ 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] | ||
// fmt.Printf("merge : %s\n", merge) | ||
laxmikantbpandhare marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
if err := scanner.Err(); err != nil { | ||
log.Fatal(err, "Failed to scan the line from Makefile") | ||
laxmikantbpandhare marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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.Fatal(err, "Failed to Replace in Makefile") | ||
laxmikantbpandhare marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return false | ||
} | ||
} | ||
|
||
return foundMarker | ||
} | ||
|
||
const ( | ||
makefileBundleCRDFile = `target/kubernetes/%[1]s.%[2]s-%[3]s.yml` | ||
) | ||
|
||
const ( | ||
makefileBundleVarFragment = ` | ||
laxmikantbpandhare marked this conversation as resolved.
Show resolved
Hide resolved
|
||
##@Bundle | ||
.PHONY: bundle | ||
bundle: ## Generate bundle manifests and metadata, then validate generated files. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes agree, we must add There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
Do you still think we still need to add |
||
## 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) | ||
laxmikantbpandhare marked this conversation as resolved.
Show resolved
Hide resolved
|
||
` | ||
) |
Uh oh!
There was an error while loading. Please reload this page.