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

✨ Support calling provider plugins. #36

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,6 @@ temp
# build and release
dist
csmctl
csmctl-docker
tmp/
releases/
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ export GOBIN := $(abspath $(TOOLS_BIN_DIR))
.PHONY: build
build: # build the csmctl binary
go build -o csmctl main.go
go build -o csmctl-docker csmctldocker/csmctldocker_main.go

.PHONY: lint-golang
lint-golang: ## Lint Golang codebase
Expand Down
48 changes: 48 additions & 0 deletions csmctldocker/csmctldocker_main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package main

Check warning on line 1 in csmctldocker/csmctldocker_main.go

View workflow job for this annotation

GitHub Actions / Lint Pull Request

package-comments: should have a package comment (revive)

import (
"fmt"
"os"

csmctlclusterstack "github.com/SovereignCloudStack/csmctl/pkg/clusterstack"
)

const provider = "docker"

func usage() {
fmt.Printf(`%s create-node-images cluster-stack-directory cluster-stack-release-directory
This command is a csmctl plugin.

https://github.com/SovereignCloudStack/csmctl
`, os.Args[0])
}

func main() {
if len(os.Args) != 4 {
usage()
os.Exit(1)
}
if os.Args[1] != "create-node-images" {
usage()
os.Exit(1)
}
clusterStackPath := os.Args[2]
config, err := csmctlclusterstack.GetCsmctlConfig(clusterStackPath)
if err != nil {
fmt.Println(err.Error())
os.Exit(1)
}
if config.Config.Provider.Type != provider {
fmt.Printf("Wrong provider in %s. Expected %s\n", clusterStackPath, provider)
os.Exit(1)
}
releaseDir := os.Args[3]
_, err = os.Stat(releaseDir)
if err != nil {
fmt.Println(err.Error())
os.Exit(1)
}
fmt.Printf("clusterStackPath: %s\n", clusterStackPath)
fmt.Printf("releaseDir: %s\n", releaseDir)
fmt.Println(".... pretending to do heavy work (creating node images) ...")
}
19 changes: 13 additions & 6 deletions pkg/cmd/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"github.com/SovereignCloudStack/csmctl/pkg/git"
"github.com/SovereignCloudStack/csmctl/pkg/github"
"github.com/SovereignCloudStack/csmctl/pkg/hash"
"github.com/SovereignCloudStack/csmctl/pkg/providerplugin"
"github.com/SovereignCloudStack/csmctl/pkg/template"
"github.com/spf13/cobra"
"gopkg.in/yaml.v3"
Expand Down Expand Up @@ -64,7 +65,8 @@
Supported modes are - stable, alpha, beta, hash

note - Hash mode takes the last hash of the git commit.`,
RunE: createAction,
RunE: createAction,
SilenceUsage: true,
}

func init() {
Expand All @@ -86,6 +88,11 @@
return fmt.Errorf("failed to get config: %w", err)
}

_, err = providerplugin.CheckProviderExecutable(config)
janiskemper marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
return err

Check failure on line 93 in pkg/cmd/create.go

View workflow job for this annotation

GitHub Actions / Lint Pull Request

error returned from external package is unwrapped: sig: func github.com/SovereignCloudStack/csmctl/pkg/providerplugin.CheckProviderExecutable(config *github.com/SovereignCloudStack/csmctl/pkg/clusterstack.CsmctlConfig) (path string, err error) (wrapcheck)
}

// Skip downloading github release for the hash mode.
// var latestRepoRelease *string
if mode == hashMode {
Expand All @@ -109,7 +116,7 @@
clusterStackReleaseDir: csrDirName,
currentHash: currentHash,
}
if err := create.buildPackerAndGenerateRelease(); err != nil {
if err := create.buildNodeImagesAndGenerateRelease(); err != nil {
return fmt.Errorf("failed to build packer and generate release: %w", err)
}
return nil
Expand Down Expand Up @@ -150,7 +157,7 @@
clusterStackReleaseDir: csrDirName,
currentHash: currentHash,
}
if err := create.buildPackerAndGenerateRelease(); err != nil {
if err := create.buildNodeImagesAndGenerateRelease(); err != nil {
return fmt.Errorf("failed to build packer and generate release: %w", err)
}

Expand Down Expand Up @@ -255,10 +262,10 @@
return metadata, nil
}

func (c *CreateOptions) buildPackerAndGenerateRelease() error {
func (c *CreateOptions) buildNodeImagesAndGenerateRelease() error {
// Release directory name
releaseDirectory := filepath.Join(outputDirectory, c.clusterStackReleaseDir)

fmt.Printf("Creating output in %s\n", releaseDirectory)
// Write the current hash
hashJSONData, err := json.MarshalIndent(c.currentHash, "", " ")
if err != nil {
Expand Down Expand Up @@ -287,5 +294,5 @@
return fmt.Errorf("failed to create package: %w", err)
}

return nil
return providerplugin.CreateNodeImages(c.config, c.clusterStackPath, releaseDirectory)

Check failure on line 297 in pkg/cmd/create.go

View workflow job for this annotation

GitHub Actions / Lint Pull Request

error returned from external package is unwrapped: sig: func github.com/SovereignCloudStack/csmctl/pkg/providerplugin.CreateNodeImages(config *github.com/SovereignCloudStack/csmctl/pkg/clusterstack.CsmctlConfig, clusterStackPath string, clusterStackReleaseDir string) error (wrapcheck)
}
40 changes: 40 additions & 0 deletions pkg/providerplugin/providerplugin.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package providerplugin

Check warning on line 1 in pkg/providerplugin/providerplugin.go

View workflow job for this annotation

GitHub Actions / Lint Pull Request

package-comments: should have a package comment (revive)

import (
"fmt"
"os"
"os/exec"
"path/filepath"

"github.com/SovereignCloudStack/csmctl/pkg/clusterstack"
)

func CheckProviderExecutable(config *clusterstack.CsmctlConfig) (path string, err error) {

Check warning on line 12 in pkg/providerplugin/providerplugin.go

View workflow job for this annotation

GitHub Actions / Lint Pull Request

exported: exported function CheckProviderExecutable should have comment or be unexported (revive)
pluginName := "csmctl-" + config.Config.Provider.Type
_, err = os.Stat(pluginName)
if err == nil {
path, err := filepath.Abs(pluginName)
if err != nil {
return "", err

Check failure on line 18 in pkg/providerplugin/providerplugin.go

View workflow job for this annotation

GitHub Actions / Lint Pull Request

error returned from external package is unwrapped: sig: func path/filepath.Abs(path string) (string, error) (wrapcheck)
}
return path, err

Check failure on line 20 in pkg/providerplugin/providerplugin.go

View workflow job for this annotation

GitHub Actions / Lint Pull Request

error returned from external package is unwrapped: sig: func path/filepath.Abs(path string) (string, error) (wrapcheck)
}
path, err = exec.LookPath(pluginName)
if err != nil {
return "", fmt.Errorf("could not find plugin %s in $PATH or current working directory", pluginName)
}
return path, nil
}

func CreateNodeImages(config *clusterstack.CsmctlConfig, clusterStackPath string, clusterStackReleaseDir string) error {

Check failure on line 29 in pkg/providerplugin/providerplugin.go

View workflow job for this annotation

GitHub Actions / Lint Pull Request

paramTypeCombine: func(config *clusterstack.CsmctlConfig, clusterStackPath string, clusterStackReleaseDir string) error could be replaced with func(config *clusterstack.CsmctlConfig, clusterStackPath, clusterStackReleaseDir string) error (gocritic)
path, err := CheckProviderExecutable(config)
guettli marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
return err
}
args := []string{"create-node-images", clusterStackPath, clusterStackReleaseDir}
fmt.Printf("Calling Provider Plugin: %s\n", path)
cmd := exec.Command(path, args...)

Check failure on line 36 in pkg/providerplugin/providerplugin.go

View workflow job for this annotation

GitHub Actions / Lint Pull Request

G204: Subprocess launched with variable (gosec)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
return cmd.Run()

Check failure on line 39 in pkg/providerplugin/providerplugin.go

View workflow job for this annotation

GitHub Actions / Lint Pull Request

error returned from external package is unwrapped: sig: func (*os/exec.Cmd).Run() error (wrapcheck)
}
Loading