1
0
mirror of https://github.com/go-micro/go-micro.git synced 2024-12-30 10:10:44 +02:00

Add generate kubernetes command (#2261)

This commit is contained in:
Niek den Breeje 2021-09-10 19:56:51 +02:00 committed by GitHub
parent ac21bb5b19
commit fa27250605
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -17,23 +17,60 @@ func init() {
Name: "generate",
Usage: "Generate project template files after the fact",
Subcommands: []*cli.Command{
{
Name: "kubernetes",
Usage: "Generate Kubernetes resource template files",
Action: Kubernetes,
},
{
Name: "skaffold",
Usage: "Generate Skaffold project template files",
Usage: "Generate Skaffold template files",
Action: Skaffold,
},
},
})
}
// Skaffold generates Skaffold project template files in the current directory.
// Exits on error.
func Skaffold(ctx *cli.Context) error {
dir, err := os.Getwd()
// Kubernetes generates Kubernetes resource template files in the current
// working directory. Exits on error.
func Kubernetes(ctx *cli.Context) error {
service, err := getService()
if err != nil {
return err
}
vendor, err := getServiceVendor(service)
if err != nil {
return err
}
g := generator.New(
generator.Service(service),
generator.Vendor(vendor),
generator.Directory("."),
generator.Client(strings.HasSuffix(service, "-client")),
)
files := []generator.File{
{"plugins.go", tmpl.Plugins},
{"resources/clusterrole.yaml", tmpl.KubernetesClusterRole},
{"resources/configmap.yaml", tmpl.KubernetesEnv},
{"resources/deployment.yaml", tmpl.KubernetesDeployment},
{"resources/rolebinding.yaml", tmpl.KubernetesRoleBinding},
}
g.Generate(files)
return nil
}
// Skaffold generates Skaffold template files in the current working directory.
// Exits on error.
func Skaffold(ctx *cli.Context) error {
service, err := getService()
if err != nil {
return err
}
service := dir[strings.LastIndex(dir, "/")+1:]
vendor, err := getServiceVendor(service)
if err != nil {
@ -68,6 +105,14 @@ func Skaffold(ctx *cli.Context) error {
return nil
}
func getService() (string, error) {
dir, err := os.Getwd()
if err != nil {
return "", err
}
return dir[strings.LastIndex(dir, "/")+1:], nil
}
func getServiceVendor(s string) (string, error) {
f, err := os.Open("go.mod")
if err != nil {