|
15 | 15 | package v1
|
16 | 16 |
|
17 | 17 | import (
|
| 18 | + "bufio" |
18 | 19 | "errors"
|
19 | 20 | "fmt"
|
| 21 | + "os" |
| 22 | + "path/filepath" |
| 23 | + "strings" |
20 | 24 |
|
| 25 | + "github.com/operator-framework/java-operator-plugins/pkg/quarkus/v1alpha/scaffolds" |
| 26 | + log "github.com/sirupsen/logrus" |
| 27 | + "github.com/spf13/afero" |
21 | 28 | "github.com/spf13/pflag"
|
| 29 | + |
22 | 30 | "sigs.k8s.io/kubebuilder/v3/pkg/config"
|
23 | 31 | "sigs.k8s.io/kubebuilder/v3/pkg/machinery"
|
24 | 32 | "sigs.k8s.io/kubebuilder/v3/pkg/model/resource"
|
25 | 33 | "sigs.k8s.io/kubebuilder/v3/pkg/plugin"
|
| 34 | + "sigs.k8s.io/kubebuilder/v3/pkg/plugin/util" |
26 | 35 | pluginutil "sigs.k8s.io/kubebuilder/v3/pkg/plugin/util"
|
27 |
| - |
28 |
| - "github.com/operator-framework/java-operator-plugins/pkg/quarkus/v1alpha/scaffolds" |
29 | 36 | )
|
30 | 37 |
|
| 38 | +const filePath = "Makefile" |
| 39 | + |
31 | 40 | type createAPIOptions struct {
|
32 | 41 | CRDVersion string
|
33 | 42 | Namespaced bool
|
@@ -81,7 +90,40 @@ func (p *createAPISubcommand) PostScaffold() error {
|
81 | 90 |
|
82 | 91 | func (p *createAPISubcommand) Scaffold(fs machinery.Filesystem) error {
|
83 | 92 | 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 | + |
84 | 125 | scaffolder.InjectFS(fs)
|
| 126 | + |
85 | 127 | if err := scaffolder.Scaffold(); err != nil {
|
86 | 128 | return err
|
87 | 129 | }
|
@@ -116,3 +158,82 @@ func (p *createAPISubcommand) InjectResource(res *resource.Resource) error {
|
116 | 158 |
|
117 | 159 | return nil
|
118 | 160 | }
|
| 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