-
Notifications
You must be signed in to change notification settings - Fork 591
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: use the same tag in Makefile as in code when installing Gatewa…
…y API CRDs
- Loading branch information
Showing
7 changed files
with
157 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package consts | ||
|
||
const ( | ||
GatewayCRDsKustomizeURL = "github.com/kubernetes-sigs/gateway-api/config/crd?sha=v0.4.1-0.20220306235253-71fee1c2808f" | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 5 additions & 0 deletions
5
test/internal/cmd/generate-gateway-api-crds-url/gateway_consts.tmpl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package consts | ||
|
||
const ( | ||
GatewayCRDsKustomizeURL = "{{.CRDsKustomizeURL}}" | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package main | ||
|
||
import ( | ||
"bufio" | ||
"bytes" | ||
"flag" | ||
"fmt" | ||
"go/format" | ||
"log" | ||
"os" | ||
"path/filepath" | ||
"text/template" | ||
) | ||
|
||
//go:generate go run . -url $URL -in $INPUT -out $OUTPUT | ||
|
||
var ( | ||
urlFlag = flag.String("url", "", "The URL of Gateway API CRDs to be consumed by kustomize") | ||
inFlag = flag.String("in", "", "Template file path") | ||
outFlag = flag.String("out", "", "Output file path where the generate file will be placed") | ||
) | ||
|
||
type Data struct { | ||
CRDsKustomizeURL string | ||
} | ||
|
||
func main() { | ||
flagParse() | ||
|
||
data := Data{ | ||
CRDsKustomizeURL: *urlFlag, | ||
} | ||
processTemplate(*inFlag, *outFlag, data) | ||
} | ||
|
||
func must(err error, errMsg string) { | ||
if err != nil { | ||
log.Fatalf("%s: %v", errMsg, err) | ||
} | ||
} | ||
|
||
func flagParse() { | ||
flag.Parse() | ||
if *urlFlag == "" { | ||
log.Print("Please provide the 'url' flag") | ||
os.Exit(0) | ||
} | ||
if *inFlag == "" { | ||
log.Print("Please provide the 'in' flag") | ||
os.Exit(0) | ||
} | ||
if *outFlag == "" { | ||
log.Print("Please provide the 'out' flag") | ||
os.Exit(0) | ||
} | ||
} | ||
|
||
func processTemplate(fileName string, outputFile string, data Data) { | ||
base := filepath.Base(fileName) | ||
tmpl, err := template.New(base).ParseFiles(fileName) | ||
must(err, "Unable to parse template file") | ||
|
||
var processed bytes.Buffer | ||
err = tmpl.Execute(&processed, data) | ||
must(err, "Unable to parse data into template") | ||
|
||
formatted, err := format.Source(processed.Bytes()) | ||
must(err, "Unable to format resulting file") | ||
|
||
outputPath := outputFile | ||
|
||
f, err := os.Create(outputPath) | ||
must(err, fmt.Sprintf("Unable to create file: %v", outputPath)) | ||
|
||
w := bufio.NewWriter(f) | ||
_, err = w.Write(formatted) | ||
must(err, "Unable to underlying buffered writer") | ||
|
||
err = w.Flush() | ||
must(err, "Unable to flush") | ||
} |