-
Notifications
You must be signed in to change notification settings - Fork 423
GCP-181: add infrastructure create and destroy CLI commands for GCP #7290
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
cristianoveiga
wants to merge
10
commits into
openshift:main
Choose a base branch
from
cristianoveiga:GCP-181
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 9 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
a01336d
build(vendor): add google.golang.org/api/compute/v1
cristianoveiga 45c7115
feat(gcp): add `hypershift create infra gcp` command
cristianoveiga 664f78d
test(gcp): add unit tests for create infra command
cristianoveiga 536a198
refactor(gcp): rename create files to match naming convention
cristianoveiga 078dc5f
feat(gcp): add `hypershift destroy infra gcp` command
cristianoveiga 52f93ae
test(gcp): add unit tests for destroy infra command
cristianoveiga bc5e968
fix(gcp): apply gci linting fixes
cristianoveiga a126e0c
refactor(gcp): remove redundant egress firewall rule
cristianoveiga ea037d8
fix(gcp): apply default VPCCidr for programmatic callers
cristianoveiga d23703a
refactor(gcp): extract shared constants to constants.go
cristianoveiga File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,183 @@ | ||
| package gcp | ||
|
|
||
| import ( | ||
| "context" | ||
| "encoding/json" | ||
| "fmt" | ||
| "os" | ||
|
|
||
| "github.com/openshift/hypershift/cmd/log" | ||
|
|
||
| "github.com/go-logr/logr" | ||
| "github.com/spf13/cobra" | ||
| ) | ||
|
|
||
| const ( | ||
| DefaultSubnetCIDR = "10.0.0.0/24" | ||
| ) | ||
|
|
||
| // CreateInfraOptions contains options for creating GCP infrastructure | ||
| type CreateInfraOptions struct { | ||
| // Required flags | ||
| ProjectID string | ||
| Region string | ||
| InfraID string | ||
|
|
||
| // Optional flags | ||
| VPCCidr string | ||
| OutputFile string | ||
| } | ||
|
|
||
| // CreateInfraOutput contains the output from infrastructure creation | ||
| type CreateInfraOutput struct { | ||
| Region string `json:"region"` | ||
| ProjectID string `json:"projectId"` | ||
| InfraID string `json:"infraId"` | ||
| NetworkName string `json:"networkName"` | ||
| NetworkSelfLink string `json:"networkSelfLink"` | ||
| SubnetName string `json:"subnetName"` | ||
| SubnetSelfLink string `json:"subnetSelfLink"` | ||
| SubnetCIDR string `json:"subnetCidr"` | ||
| RouterName string `json:"routerName"` | ||
| NATName string `json:"natName"` | ||
| } | ||
|
|
||
| // NewCreateCommand creates a new cobra command for creating GCP infrastructure | ||
| func NewCreateCommand() *cobra.Command { | ||
| cmd := &cobra.Command{ | ||
| Use: "gcp", | ||
| Short: "Creates GCP infrastructure resources for a cluster", | ||
| SilenceUsage: true, | ||
| } | ||
|
|
||
| opts := CreateInfraOptions{ | ||
| VPCCidr: DefaultSubnetCIDR, | ||
| } | ||
|
|
||
| cmd.Flags().StringVar(&opts.ProjectID, "project-id", opts.ProjectID, "GCP Project ID (required)") | ||
| cmd.Flags().StringVar(&opts.Region, "region", opts.Region, "GCP region where infrastructure will be created") | ||
| cmd.Flags().StringVar(&opts.InfraID, "infra-id", opts.InfraID, "Infrastructure ID for resource naming (required)") | ||
| cmd.Flags().StringVar(&opts.VPCCidr, "vpc-cidr", opts.VPCCidr, "CIDR block for the subnet") | ||
| cmd.Flags().StringVar(&opts.OutputFile, "output-file", opts.OutputFile, "Path to file that will contain output information from infra resources (optional)") | ||
|
|
||
| _ = cmd.MarkFlagRequired("project-id") | ||
| _ = cmd.MarkFlagRequired("region") | ||
| _ = cmd.MarkFlagRequired("infra-id") | ||
|
|
||
| logger := log.Log | ||
| cmd.PreRunE = func(cmd *cobra.Command, args []string) error { | ||
| return opts.Validate() | ||
| } | ||
| cmd.RunE = func(cmd *cobra.Command, args []string) error { | ||
| if err := opts.Run(cmd.Context(), logger); err != nil { | ||
| logger.Error(err, "Failed to create GCP infrastructure") | ||
| return err | ||
| } | ||
| logger.Info("Successfully created GCP infrastructure") | ||
| return nil | ||
| } | ||
|
|
||
| return cmd | ||
| } | ||
|
|
||
| // Validate validates the create infrastructure options | ||
| func (o *CreateInfraOptions) Validate() error { | ||
| if o.ProjectID == "" { | ||
| return fmt.Errorf("--project-id is required") | ||
| } | ||
| if o.InfraID == "" { | ||
| return fmt.Errorf("--infra-id is required") | ||
| } | ||
| if o.Region == "" { | ||
| return fmt.Errorf("--region is required") | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| // Run executes the infrastructure creation | ||
| func (o *CreateInfraOptions) Run(ctx context.Context, logger logr.Logger) error { | ||
| result, err := o.CreateInfra(ctx, logger) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| return o.Output(result) | ||
| } | ||
|
|
||
| // Output writes the infrastructure output to stdout or a file | ||
| func (o *CreateInfraOptions) Output(result *CreateInfraOutput) error { | ||
| out := os.Stdout | ||
| if len(o.OutputFile) > 0 { | ||
| var err error | ||
| out, err = os.Create(o.OutputFile) | ||
| if err != nil { | ||
| return fmt.Errorf("cannot create output file: %w", err) | ||
| } | ||
| defer func(out *os.File) { | ||
| _ = out.Close() | ||
| }(out) | ||
| } | ||
| outputBytes, err := json.MarshalIndent(result, "", " ") | ||
| if err != nil { | ||
| return fmt.Errorf("failed to serialize result: %w", err) | ||
| } | ||
| _, err = out.Write(outputBytes) | ||
| if err != nil { | ||
| return fmt.Errorf("failed to write result: %w", err) | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| // CreateInfra creates the GCP infrastructure resources | ||
| func (o *CreateInfraOptions) CreateInfra(ctx context.Context, logger logr.Logger) (*CreateInfraOutput, error) { | ||
| logger.Info("Creating GCP infrastructure", "projectID", o.ProjectID, "region", o.Region, "infraID", o.InfraID) | ||
|
|
||
| // Ensure a sensible default for programmatic callers that don't go through NewCreateCommand | ||
| if o.VPCCidr == "" { | ||
| o.VPCCidr = DefaultSubnetCIDR | ||
| } | ||
|
|
||
| // Initialize network manager | ||
| networkManager, err := NewNetworkManager(ctx, o.ProjectID, o.InfraID, o.Region, logger) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("failed to initialize network manager: %w", err) | ||
| } | ||
|
|
||
| result := &CreateInfraOutput{ | ||
| Region: o.Region, | ||
| ProjectID: o.ProjectID, | ||
| InfraID: o.InfraID, | ||
| } | ||
|
|
||
| // Create VPC network | ||
| network, err := networkManager.CreateNetwork(ctx) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("failed to create VPC network: %w", err) | ||
| } | ||
| result.NetworkName = network.Name | ||
| result.NetworkSelfLink = network.SelfLink | ||
|
|
||
| // Create subnet | ||
| subnet, err := networkManager.CreateSubnet(ctx, network.SelfLink, o.VPCCidr) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("failed to create subnet: %w", err) | ||
| } | ||
| result.SubnetName = subnet.Name | ||
| result.SubnetSelfLink = subnet.SelfLink | ||
| result.SubnetCIDR = subnet.IpCidrRange | ||
|
|
||
| // Create Cloud Router | ||
| router, err := networkManager.CreateRouter(ctx, network.SelfLink) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("failed to create Cloud Router: %w", err) | ||
| } | ||
| result.RouterName = router.Name | ||
|
|
||
| // Create Cloud NAT | ||
| natName, err := networkManager.CreateNAT(ctx, router.Name, subnet.SelfLink) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("failed to create Cloud NAT: %w", err) | ||
| } | ||
| result.NATName = natName | ||
|
|
||
coderabbitai[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| return result, nil | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.