-
Notifications
You must be signed in to change notification settings - Fork 218
/
create.go
91 lines (79 loc) · 2.37 KB
/
create.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package kubernetes
import (
"context"
"fmt"
"os"
"github.com/spf13/cobra"
"github.com/superfly/flyctl/gql"
"github.com/superfly/flyctl/internal/appconfig"
"github.com/superfly/flyctl/internal/command"
extensions_core "github.com/superfly/flyctl/internal/command/extensions/core"
"github.com/superfly/flyctl/internal/command/orgs"
"github.com/superfly/flyctl/internal/flag"
"github.com/superfly/flyctl/internal/flyutil"
"github.com/superfly/flyctl/iostreams"
)
const (
betaMsg = "Fly Kubernetes is in beta, it is not recommended for critical production use cases. For help or feedback, email us at beta@fly.io"
)
func create() (cmd *cobra.Command) {
const (
short = "Provision a Kubernetes cluster for an organization"
long = short + "\n"
usage = "create [flags]"
)
cmd = command.New(usage, short, long, runK8sCreate, command.RequireSession)
flag.Add(cmd,
flag.String{
Name: "name",
Shorthand: "n",
Description: "The name of your cluster",
},
flag.Org(),
flag.Region(),
flag.String{
Name: "output",
Description: "The output path to save the kubeconfig file",
},
)
return cmd
}
func runK8sCreate(ctx context.Context) (err error) {
io := iostreams.FromContext(ctx)
colorize := io.ColorScheme()
fmt.Fprintln(io.Out, colorize.Yellow(betaMsg))
client := flyutil.ClientFromContext(ctx).GenqClient()
appName := appconfig.NameFromContext(ctx)
targetOrg, err := orgs.OrgFromFlagOrSelect(ctx)
if err != nil {
return err
}
extension, err := extensions_core.ProvisionExtension(ctx, extensions_core.ExtensionParams{
AppName: appName,
Provider: "kubernetes",
Organization: targetOrg,
})
if err != nil {
return err
}
resp, err := gql.GetAddOn(ctx, client, extension.Data.Name, string(gql.AddOnTypeKubernetes))
if err != nil {
return err
}
outFilename := flag.GetString(ctx, "output")
if outFilename == "" {
outFilename = fmt.Sprintf("%s.kubeconfig.yml", resp.AddOn.Name)
}
f, err := os.Create(outFilename)
if err != nil {
return err
}
defer f.Close()
metadata := resp.AddOn.Metadata.(map[string]interface{})
kubeconfig := metadata["kubeconfig"].(string)
if _, err := f.Write([]byte(kubeconfig)); err != nil {
return fmt.Errorf("failed to write kubeconfig to file %s, error: %w", outFilename, err)
}
fmt.Fprintf(io.Out, "Wrote kubeconfig to file %s. Use it to connect to your cluster", outFilename)
return
}