forked from openshift/hypershift
-
Notifications
You must be signed in to change notification settings - Fork 0
/
create.go
173 lines (155 loc) · 6.13 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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package powervs
import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
"os"
"os/signal"
"syscall"
apifixtures "github.com/openshift/hypershift/api/fixtures"
"github.com/openshift/hypershift/cmd/cluster/core"
powervsinfra "github.com/openshift/hypershift/cmd/infra/powervs"
"github.com/openshift/hypershift/cmd/log"
"github.com/openshift/hypershift/support/infraid"
"github.com/spf13/cobra"
)
const (
defaultCIDRBlock = "10.0.0.0/16"
)
func NewCreateCommand(opts *core.CreateOptions) *cobra.Command {
cmd := &cobra.Command{
Use: "powervs",
Short: "Creates basic functional HostedCluster resources on PowerVS PowerVS",
SilenceUsage: true,
}
opts.PowerVSPlatform = core.PowerVSPlatformOptions{
APIKey: os.Getenv("IBMCLOUD_API_KEY"),
Region: "us-south",
Zone: "us-south",
VpcRegion: "us-south",
SysType: "s922",
ProcType: "shared",
Processors: "0.5",
Memory: 32,
}
cmd.Flags().StringVar(&opts.PowerVSPlatform.ResourceGroup, "resource-group", "", "IBM Cloud Resource group")
cmd.Flags().StringVar(&opts.PowerVSPlatform.Region, "region", opts.PowerVSPlatform.Region, "IBM Cloud region. Default is us-south")
cmd.Flags().StringVar(&opts.PowerVSPlatform.Zone, "zone", opts.PowerVSPlatform.Zone, "IBM Cloud zone. Default is us-south")
cmd.Flags().StringVar(&opts.PowerVSPlatform.CloudInstanceID, "cloud-instance-id", "", "IBM Cloud PowerVS Service Instance ID")
cmd.Flags().StringVar(&opts.PowerVSPlatform.CloudConnection, "cloud-connection", "", "Cloud Connection in given zone")
cmd.Flags().StringVar(&opts.PowerVSPlatform.VpcRegion, "vpc-region", opts.PowerVSPlatform.VpcRegion, "IBM Cloud VPC Region for VPC resources. Default is us-south")
cmd.Flags().StringVar(&opts.PowerVSPlatform.Vpc, "vpc", "", "IBM Cloud VPC Name")
cmd.Flags().StringVar(&opts.PowerVSPlatform.SysType, "sys-type", opts.PowerVSPlatform.SysType, "System type used to host the instance(e.g: s922, e980, e880). Default is s922")
cmd.Flags().StringVar(&opts.PowerVSPlatform.ProcType, "proc-type", opts.PowerVSPlatform.ProcType, "Processor type (dedicated, shared, capped). Default is shared")
cmd.Flags().StringVar(&opts.PowerVSPlatform.Processors, "processors", opts.PowerVSPlatform.Processors, "Number of processors allocated. Default is 0.5")
cmd.Flags().Int32Var(&opts.PowerVSPlatform.Memory, "memory", opts.PowerVSPlatform.Memory, "Amount of memory allocated (in GB). Default is 32")
cmd.MarkFlagRequired("resource-group")
// these options are only for development and testing purpose,
// can use these to reuse the existing resources, so hiding it.
cmd.Flags().MarkHidden("cloud-instance-id")
cmd.Flags().MarkHidden("cloud-connection")
cmd.Flags().MarkHidden("vpc")
cmd.PreRunE = func(cmd *cobra.Command, args []string) error {
if opts.BaseDomain == "" {
return fmt.Errorf("--base-domain can't be empty")
}
return nil
}
cmd.Run = func(cmd *cobra.Command, args []string) {
ctx, cancel := context.WithCancel(context.Background())
sigs := make(chan os.Signal, 1)
signal.Notify(sigs, syscall.SIGINT)
go func() {
<-sigs
cancel()
}()
if err := CreateCluster(ctx, opts); err != nil {
log.Log.Error(err, "Failed to create cluster")
os.Exit(1)
}
}
return cmd
}
func CreateCluster(ctx context.Context, opts *core.CreateOptions) error {
if err := validate(opts); err != nil {
return err
}
if err := core.Validate(ctx, opts); err != nil {
return err
}
return core.CreateCluster(ctx, opts, applyPlatformSpecificsValues)
}
func validate(opts *core.CreateOptions) error {
if opts.BaseDomain == "" {
return fmt.Errorf("--base-domain can't be empty")
}
if opts.PowerVSPlatform.APIKey == "" {
return fmt.Errorf("IBMCLOUD_API_KEY not set")
}
return nil
}
func applyPlatformSpecificsValues(ctx context.Context, exampleOptions *apifixtures.ExampleOptions, opts *core.CreateOptions) (err error) {
infraID := opts.InfraID
if len(infraID) == 0 {
infraID = infraid.New(opts.Name)
}
// Load or create infrastructure for the cluster
var infra *powervsinfra.Infra
if len(opts.InfrastructureJSON) > 0 {
rawInfra, err := ioutil.ReadFile(opts.InfrastructureJSON)
if err != nil {
return fmt.Errorf("failed to read infra json file: %w", err)
}
infra = &powervsinfra.Infra{}
if err = json.Unmarshal(rawInfra, infra); err != nil {
return fmt.Errorf("failed to load infra json: %w", err)
}
}
if infra == nil {
if len(infraID) == 0 {
infraID = infraid.New(opts.Name)
}
opt := &powervsinfra.CreateInfraOptions{
BaseDomain: opts.BaseDomain,
ResourceGroup: opts.PowerVSPlatform.ResourceGroup,
InfraID: infraID,
OutputFile: opts.InfrastructureJSON,
PowerVSRegion: opts.PowerVSPlatform.Region,
PowerVSZone: opts.PowerVSPlatform.Zone,
PowerVSCloudInstanceID: opts.PowerVSPlatform.CloudInstanceID,
PowerVSCloudConnection: opts.PowerVSPlatform.CloudConnection,
VpcRegion: opts.PowerVSPlatform.VpcRegion,
Vpc: opts.PowerVSPlatform.Vpc,
}
infra = &powervsinfra.Infra{}
err = infra.SetupInfra(opt)
if err != nil {
return fmt.Errorf("failed to create infra: %w", err)
}
}
exampleOptions.BaseDomain = opts.BaseDomain
exampleOptions.ComputeCIDR = defaultCIDRBlock
exampleOptions.PrivateZoneID = infra.CisDomainID
exampleOptions.PublicZoneID = infra.CisDomainID
exampleOptions.InfraID = infraID
exampleOptions.PowerVS = &apifixtures.ExamplePowerVSOptions{
ApiKey: opts.PowerVSPlatform.APIKey,
AccountID: infra.AccountID,
ResourceGroup: opts.PowerVSPlatform.ResourceGroup,
Region: opts.PowerVSPlatform.Region,
Zone: opts.PowerVSPlatform.Zone,
CISInstanceCRN: infra.CisCrn,
CloudInstanceID: infra.PowerVSCloudInstanceID,
Subnet: infra.PowerVSDhcpSubnet,
SubnetID: infra.PowerVSDhcpSubnetID,
VpcRegion: opts.PowerVSPlatform.VpcRegion,
Vpc: infra.VpcName,
VpcSubnet: infra.VpcSubnetName,
SysType: opts.PowerVSPlatform.SysType,
ProcType: opts.PowerVSPlatform.ProcType,
Processors: opts.PowerVSPlatform.Processors,
Memory: opts.PowerVSPlatform.Memory,
}
return nil
}