Skip to content

Commit 173e51e

Browse files
Makefile changes for addition of run bundle in Java Plugin. (#84)
* updated makefile * updated makefile * updated makefile * modified content to add changes in Makefile * removed unused variable * modified create api subcommand code and added bundle code in Makefile * updated and added bundle commands * updated image tag * removed comments * removed comments * removed comments * modified projectName if it is emoty according to project path * modified code for handling multiple create api commands * modified code for handling multiple create api commands * removed comments * resolved some minor changes * uodate space * modified minor changes * added logs for plugin Makefile * remove comments from plugin Makefile * remove comments from plugin Makefile
1 parent a03b909 commit 173e51e

File tree

3 files changed

+126
-3
lines changed

3 files changed

+126
-3
lines changed

go.mod

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ require (
1515
github.com/fsnotify/fsnotify v1.5.1 // indirect
1616
github.com/gobuffalo/flect v0.2.3 // indirect
1717
github.com/nxadm/tail v1.4.8 // indirect
18-
github.com/spf13/afero v1.6.0 // indirect
18+
github.com/sirupsen/logrus v1.8.1
19+
github.com/spf13/afero v1.6.0
1920
golang.org/x/mod v0.4.2 // indirect
2021
golang.org/x/net v0.0.0-20210825183410-e898025ed96a // indirect
2122
golang.org/x/sys v0.0.0-20211029165221-6e7872819dc8 // indirect

go.sum

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -445,6 +445,7 @@ github.com/sirupsen/logrus v1.3.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPx
445445
github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE=
446446
github.com/sirupsen/logrus v1.6.0/go.mod h1:7uNnSEd1DgxDLC74fIahvMZmmYsHGZGEOFrfsX/uA88=
447447
github.com/sirupsen/logrus v1.7.0/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0=
448+
github.com/sirupsen/logrus v1.8.1 h1:dJKuHgqk1NNQlqoA6BTlM1Wf9DOH3NBjQyu0h9+AZZE=
448449
github.com/sirupsen/logrus v1.8.1/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0=
449450
github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d/go.mod h1:OnSkiWE9lh6wB0YB77sQom3nweQdgAjqCqsofrRNTgc=
450451
github.com/smartystreets/goconvey v1.6.4/go.mod h1:syvi0/a8iFYH4r/RixwvyeAJjdLS9QV7WQ/tjFTllLA=

pkg/quarkus/v1alpha/api.go

Lines changed: 123 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,28 @@
1515
package v1
1616

1717
import (
18+
"bufio"
1819
"errors"
1920
"fmt"
21+
"os"
22+
"path/filepath"
23+
"strings"
2024

25+
"github.com/operator-framework/java-operator-plugins/pkg/quarkus/v1alpha/scaffolds"
26+
log "github.com/sirupsen/logrus"
27+
"github.com/spf13/afero"
2128
"github.com/spf13/pflag"
29+
2230
"sigs.k8s.io/kubebuilder/v3/pkg/config"
2331
"sigs.k8s.io/kubebuilder/v3/pkg/machinery"
2432
"sigs.k8s.io/kubebuilder/v3/pkg/model/resource"
2533
"sigs.k8s.io/kubebuilder/v3/pkg/plugin"
34+
"sigs.k8s.io/kubebuilder/v3/pkg/plugin/util"
2635
pluginutil "sigs.k8s.io/kubebuilder/v3/pkg/plugin/util"
27-
28-
"github.com/operator-framework/java-operator-plugins/pkg/quarkus/v1alpha/scaffolds"
2936
)
3037

38+
const filePath = "Makefile"
39+
3140
type createAPIOptions struct {
3241
CRDVersion string
3342
Namespaced bool
@@ -81,7 +90,40 @@ func (p *createAPISubcommand) PostScaffold() error {
8190

8291
func (p *createAPISubcommand) Scaffold(fs machinery.Filesystem) error {
8392
scaffolder := scaffolds.NewCreateAPIScaffolder(p.config, *p.resource)
93+
94+
var s = fmt.Sprintf(makefileBundleCRDFile, p.resource.Plural, p.resource.QualifiedGroup(), p.resource.Version)
95+
foundLine := findOldFilesForReplacement(filePath, s)
96+
97+
if !foundLine {
98+
makefileBytes, err := afero.ReadFile(fs.FS, filePath)
99+
if err != nil {
100+
return err
101+
}
102+
103+
projectName := p.config.GetProjectName()
104+
if projectName == "" {
105+
dir, err := os.Getwd()
106+
if err != nil {
107+
return fmt.Errorf("error getting current directory: %w", err)
108+
}
109+
projectName = strings.ToLower(filepath.Base(dir))
110+
}
111+
112+
makefileBytes = append(makefileBytes, []byte(fmt.Sprintf(makefileBundleVarFragment, p.resource.Plural, p.resource.QualifiedGroup(), p.resource.Version, projectName))...)
113+
114+
makefileBytes = append([]byte(fmt.Sprintf(makefileBundleImageFragement, p.config.GetDomain(), projectName)), makefileBytes...)
115+
116+
var mode os.FileMode = 0644
117+
if info, err := fs.FS.Stat(filePath); err == nil {
118+
mode = info.Mode()
119+
}
120+
if err := afero.WriteFile(fs.FS, filePath, makefileBytes, mode); err != nil {
121+
return fmt.Errorf("error updating Makefile: %w", err)
122+
}
123+
}
124+
84125
scaffolder.InjectFS(fs)
126+
85127
if err := scaffolder.Scaffold(); err != nil {
86128
return err
87129
}
@@ -116,3 +158,82 @@ func (p *createAPISubcommand) InjectResource(res *resource.Resource) error {
116158

117159
return nil
118160
}
161+
162+
// findOldFilesForReplacement verifies marker (## marker) and if it found then merge new api CRD file to the odler logic
163+
func findOldFilesForReplacement(path, newfile string) bool {
164+
165+
f, err := os.Open(path)
166+
if err != nil {
167+
log.Fatal(err)
168+
}
169+
170+
// remember to close the file at the end of the program
171+
defer f.Close()
172+
173+
// read the file line by line using scanner
174+
scanner := bufio.NewScanner(f)
175+
var foundMarker bool
176+
for scanner.Scan() {
177+
// do something with a line
178+
if scanner.Text() == "## marker" {
179+
foundMarker = true
180+
break
181+
}
182+
}
183+
184+
if foundMarker {
185+
scanner.Scan()
186+
catLine := scanner.Text()
187+
188+
splitByPipe := strings.Split(catLine, "|")
189+
190+
finalString := strings.TrimSuffix(strings.TrimPrefix(strings.TrimSpace(splitByPipe[0]), "cat"), "target/kubernetes/kubernetes.yml")
191+
192+
updatedLine := " " + "cat" + finalString + newfile + " target/kubernetes/kubernetes.yml" + " |" + splitByPipe[1]
193+
194+
if err := scanner.Err(); err != nil {
195+
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.")
196+
return false
197+
}
198+
199+
// ReplaceInFile replaces all instances of old with new in the file at path.
200+
err = util.ReplaceInFile(path, catLine, updatedLine)
201+
if err != nil {
202+
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.")
203+
return false
204+
}
205+
}
206+
207+
return foundMarker
208+
}
209+
210+
const (
211+
makefileBundleCRDFile = `target/kubernetes/%[1]s.%[2]s-%[3]s.yml`
212+
)
213+
214+
const (
215+
makefileBundleVarFragment = `
216+
##@Bundle
217+
.PHONY: bundle
218+
bundle: ## Generate bundle manifests and metadata, then validate generated files.
219+
## marker
220+
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
221+
operator-sdk bundle validate ./bundle
222+
223+
.PHONY: bundle-build
224+
bundle-build: ## Build the bundle image.
225+
docker build -f bundle.Dockerfile -t $(BUNDLE_IMG) .
226+
227+
.PHONY: bundle-push
228+
bundle-push: ## Push the bundle image.
229+
docker push $(BUNDLE_IMG)
230+
`
231+
)
232+
233+
const (
234+
makefileBundleImageFragement = `
235+
VERSION ?= 0.0.1
236+
IMAGE_TAG_BASE ?= %[1]s/%[2]s
237+
BUNDLE_IMG ?= $(IMAGE_TAG_BASE)-bundle:v$(VERSION)
238+
`
239+
)

0 commit comments

Comments
 (0)