Skip to content
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

initial draft for pulumi supporting mulitple providers #26

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
1 change: 1 addition & 0 deletions pulumi/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
out
71 changes: 71 additions & 0 deletions pulumi/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
VERSION ?= 0.0.1
CONTAINER_MANAGER ?= podman
# Image URL to use all building/pushing image targets
IMG ?= quay.io/crcont/crc-cloud:${VERSION}

# Go and compilation related variables
GOPATH ?= $(shell go env GOPATH)
BUILD_DIR ?= out
SOURCE_DIRS = cmd pkg test
# https://golang.org/cmd/link/
LDFLAGS := $(VERSION_VARIABLES) -extldflags='-static' ${GO_EXTRA_LDFLAGS}
GCFLAGS := all=-N -l

# Add default target
.PHONY: default
default: install

# Create and update the vendor directory
.PHONY: vendor
vendor:
go mod tidy
go mod vendor

.PHONY: check
check: build test lint

# Start of the actual build targets

.PHONY: install
install: $(SOURCES)
go install -ldflags="$(LDFLAGS)" $(GO_EXTRA_BUILDFLAGS) ./cmd

$(BUILD_DIR)/crc-cloud: $(SOURCES)
GOOS=linux GOARCH=amd64 go build -gcflags="$(GCFLAGS)" -ldflags="$(LDFLAGS)" -o $(BUILD_DIR)/crc-cloud $(GO_EXTRA_BUILDFLAGS) ./cmd



.PHONY: build
build: clean $(BUILD_DIR)/crc-cloud

.PHONY: test
test:
go test -race --tags build -v -ldflags="$(VERSION_VARIABLES)" ./pkg/... ./cmd/...

.PHONY: clean ## Remove all build artifacts
clean:
rm -rf $(BUILD_DIR)
rm -f $(GOPATH)/bin/crc-cloud

.PHONY: fmt
fmt:
@gofmt -l -w $(SOURCE_DIRS)

$(GOPATH)/bin/golangci-lint:
go install github.com/golangci/golangci-lint/cmd/[email protected]

# Run golangci-lint against code
.PHONY: lint
lint: $(GOPATH)/bin/golangci-lint
$(GOPATH)/bin/golangci-lint run

# Build the container image
.PHONY: oci-build
oci-build:
${CONTAINER_MANAGER} build -t ${IMG} -f oci/Containerfile .

# Push the container image
.PHONY: oci-push
container-push:
${CONTAINER_MANAGER} push ${IMG}

21 changes: 21 additions & 0 deletions pulumi/cmd/cmd/constants/constants.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package constants

const (
ProjectName string = "project-name"
ProjectNameDesc string = "project name to identify the instance of the stack"
BackedURL string = "backed-url"
BackedURLDesc string = "backed for stack state. Can be a local path with format file:///path/subpath or s3 s3://existing-bucket"
OutputFolder string = "output"
OutputFolderDesc string = "path to export assets"
Provider string = "provider"
ProviderDesc string = "target cloud provider"
OcpPullSecretFilePath string = "pullsecret-filepath"
OcpPullSecretFilePathDesc string = "path for pullsecret file"
KeyFilePath string = "key-filepath"
KeyFilePathDesc string = "path to init key obtained when importing the image"

BundleDownloadURL string = "bundle-url"
BundleDownloadURLDesc string = "custom url to download the bundle artifact"
ShasumfileDownloadURL string = "bundle-shasumfile-url"
ShasumfileDownloadURLDesc string = "custom url to download the shasum file to verify the bundle artifact"
)
54 changes: 54 additions & 0 deletions pulumi/cmd/cmd/create/aws.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package create

import (
"fmt"
"github.com/crc/crc-cloud/cmd/cmd/constants"
"github.com/crc/crc-cloud/pkg/manager"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"github.com/spf13/viper"
"os"
)

const (
awsProviderName string = "aws"
awsProviderDescription string = "create crc cloud instance on AWS"
)

func getAWSProviderCmd() *cobra.Command {
awsProviderCmd := &cobra.Command{
Use: awsProviderName,
Short: awsProviderDescription,
RunE: func(cmd *cobra.Command, args []string) error {
if err := viper.BindPFlags(cmd.Flags()); err != nil {
return err
}
// Provider dependent params
providerParams := make(map[string]string)
for name := range manager.CreateParams() {
providerParams[name] = viper.GetString(name)
}
if err := manager.Create(
viper.GetString(constants.ProjectName),
viper.GetString(constants.BackedURL),
viper.GetString(constants.OutputFolder),
manager.AWS,
providerParams,
viper.GetString(constants.OcpPullSecretFilePath),
viper.GetString(constants.KeyFilePath)); err != nil {
fmt.Printf("error creating the cluster with %s provider: %s\n", manager.AWS, err)
os.Exit(1)
}
return nil
},
}

flagSet := pflag.NewFlagSet(awsProviderName, pflag.ExitOnError)
// Provider dependent params
for name, description := range manager.CreateParams() {
flagSet.StringP(name, "", "", description)
}

awsProviderCmd.Flags().AddFlagSet(flagSet)
return awsProviderCmd
}
38 changes: 38 additions & 0 deletions pulumi/cmd/cmd/create/create.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package create

import (
"github.com/crc/crc-cloud/cmd/cmd/constants"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"github.com/spf13/viper"
)

const (
createCmdName string = "create"
createCmdDescription string = "create crc cloud instance"
)

func GetCreateCmd() *cobra.Command {
createCmd := &cobra.Command{
Use: createCmdName,
Short: createCmdDescription,
RunE: func(cmd *cobra.Command, args []string) error {
if err := viper.BindPFlags(cmd.Flags()); err != nil {
return err
}
return nil
},
}
flagSet := pflag.NewFlagSet(createCmdName, pflag.ExitOnError)
// Fixed params
flagSet.StringP(constants.ProjectName, "", "", constants.ProjectNameDesc)
flagSet.StringP(constants.BackedURL, "", "", constants.BackedURLDesc)
flagSet.StringP(constants.OutputFolder, "", "", constants.OutputFolderDesc)
flagSet.StringP(constants.OcpPullSecretFilePath, "", "", constants.OcpPullSecretFilePathDesc)
flagSet.StringP(constants.KeyFilePath, "", "", constants.KeyFilePathDesc)
createCmd.PersistentFlags().AddFlagSet(flagSet)

createCmd.AddCommand(getAWSProviderCmd())

return createCmd
}
45 changes: 45 additions & 0 deletions pulumi/cmd/cmd/destroy.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package cmd

import (
"fmt"
"github.com/crc/crc-cloud/cmd/cmd/constants"
"os"

"github.com/crc/crc-cloud/pkg/manager"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"github.com/spf13/viper"
)

const (
destroyCmdName string = "destroy"
destroyCmdDescription string = "destroy crc cloud instance"
)

func init() {
rootCmd.AddCommand(crcCloudDestroyCmd)
flagSet := pflag.NewFlagSet(destroyCmdName, pflag.ExitOnError)
flagSet.StringP(constants.ProjectName, "", "", constants.ProjectNameDesc)
flagSet.StringP(constants.BackedURL, "", "", constants.BackedURLDesc)
flagSet.StringP(constants.Provider, "", "", constants.ProviderDesc)
crcCloudDestroyCmd.Flags().AddFlagSet(flagSet)
crcCloudDestroyCmd.MarkFlagRequired("provider")
}

var crcCloudDestroyCmd = &cobra.Command{
Use: destroyCmdName,
Short: destroyCmdDescription,
RunE: func(cmd *cobra.Command, args []string) error {
if err := viper.BindPFlags(cmd.Flags()); err != nil {
return err
}
if err := manager.Destroy(
viper.GetString(constants.ProjectName),
viper.GetString(constants.BackedURL),
manager.Provider(viper.GetString(constants.Provider))); err != nil {
fmt.Printf("error destroying the cluster: %s\n", err)
os.Exit(1)
}
return nil
},
}
51 changes: 51 additions & 0 deletions pulumi/cmd/cmd/import.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package cmd

import (
"fmt"
"github.com/crc/crc-cloud/cmd/cmd/constants"
"os"

"github.com/crc/crc-cloud/pkg/manager"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"github.com/spf13/viper"
)

const (
importImageCmdName string = "import"
importImageCmdDescription string = "import crc cloud image"
)

func init() {
rootCmd.AddCommand(crcCloudImportCmd)
flagSet := pflag.NewFlagSet(importImageCmdName, pflag.ExitOnError)
// Fixed params
flagSet.StringP(constants.ProjectName, "", "", constants.ProjectNameDesc)
flagSet.StringP(constants.BackedURL, "", "", constants.BackedURLDesc)
flagSet.StringP(constants.Provider, "", "", constants.ProviderDesc)
flagSet.StringP(constants.OutputFolder, "", "", constants.OutputFolderDesc)
flagSet.StringP(constants.BundleDownloadURL, "", "", constants.BundleDownloadURLDesc)
flagSet.StringP(constants.ShasumfileDownloadURL, "", "", constants.ShasumfileDownloadURLDesc)
crcCloudImportCmd.Flags().AddFlagSet(flagSet)
}

var crcCloudImportCmd = &cobra.Command{
Use: importImageCmdName,
Short: importImageCmdDescription,
RunE: func(cmd *cobra.Command, args []string) error {
if err := viper.BindPFlags(cmd.Flags()); err != nil {
return err
}
if err := manager.Import(
viper.GetString(constants.ProjectName),
viper.GetString(constants.BackedURL),
viper.GetString(constants.OutputFolder),
viper.GetString(constants.BundleDownloadURL),
viper.GetString(constants.ShasumfileDownloadURL),
manager.Provider(viper.GetString(constants.Provider))); err != nil {
fmt.Printf("error importing the image: %s\n", err)
os.Exit(1)
}
return nil
},
}
79 changes: 79 additions & 0 deletions pulumi/cmd/cmd/root.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package cmd

import (
"context"
"fmt"
"github.com/crc/crc-cloud/cmd/cmd/create"
"os"
"strings"

"github.com/spf13/cobra"
)

const (
commandName = "crc-cloud"
descriptionShort = "PoC for pulumi"
descriptionLong = "PoC for pulumi"

defaultErrorExitCode = 1
)

var rootCmd = &cobra.Command{
Use: commandName,
Short: descriptionShort,
Long: descriptionLong,
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
return runPrerun(cmd)
},
Run: func(cmd *cobra.Command, args []string) {
runRoot()
_ = cmd.Help()
},
SilenceUsage: true,
SilenceErrors: true,
}

func init() {
rootCmd.AddCommand(create.GetCreateCmd())

}

func runPrerun(cmd *cobra.Command) error {
return nil
}

func runRoot() {
fmt.Println("No command given")
}

func Execute() {
attachMiddleware([]string{}, rootCmd)

if err := rootCmd.ExecuteContext(context.Background()); err != nil {
runPostrun()
_, _ = fmt.Fprintln(os.Stderr, err.Error())
os.Exit(defaultErrorExitCode)
}
runPostrun()
}

func attachMiddleware(names []string, cmd *cobra.Command) {
if cmd.HasSubCommands() {
for _, command := range cmd.Commands() {
attachMiddleware(append(names, cmd.Name()), command)
}
} else if cmd.RunE != nil {
fullCmd := strings.Join(append(names, cmd.Name()), " ")
src := cmd.RunE
cmd.RunE = executeWithLogging(fullCmd, src)
}
}

func executeWithLogging(fullCmd string, input func(cmd *cobra.Command, args []string) error) func(cmd *cobra.Command, args []string) error {
return func(cmd *cobra.Command, args []string) error {
return input(cmd, args)
}
}

func runPostrun() {
}
7 changes: 7 additions & 0 deletions pulumi/cmd/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package main

import "github.com/crc/crc-cloud/cmd/cmd"

func main() {
cmd.Execute()
}
1 change: 1 addition & 0 deletions pulumi/docs/crc-cloud.drawio

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions pulumi/docs/crc-cloud.drawio.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading