Skip to content

Commit

Permalink
✨ Support calling provider plugins.
Browse files Browse the repository at this point in the history
Signed-off-by: Thomas Guettler <[email protected]>
  • Loading branch information
guettli committed Jan 24, 2024
1 parent 6301e5c commit 491378b
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 5 deletions.
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) ...")
}
18 changes: 13 additions & 5 deletions pkg/cmd/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"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 @@ the cluster stack release in the current directory named "release/".
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,12 @@ func createAction(_ *cobra.Command, args []string) error {
return fmt.Errorf("failed to get config: %w", err)
}

pluginPath, err := providerplugin.CheckProviderExecutable(config)
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)
}
fmt.Printf("Found plugin %s\n", pluginPath)

// Skip downloading github release for the hash mode.
// var latestRepoRelease *string
if mode == hashMode {
Expand All @@ -109,7 +117,7 @@ func createAction(_ *cobra.Command, args []string) error {
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 +158,7 @@ func createAction(_ *cobra.Command, args []string) error {
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,7 +263,7 @@ func handleHashMode(metadata *csmctlclusterstack.MetaData) (*csmctlclusterstack.
return metadata, nil
}

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

Expand Down Expand Up @@ -287,5 +295,5 @@ func (c *CreateOptions) buildPackerAndGenerateRelease() error {
return fmt.Errorf("failed to create package: %w", err)
}

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

Check failure on line 298 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)
}
41 changes: 41 additions & 0 deletions pkg/providerplugin/providerplugin.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
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"
"strings"

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

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

Check warning on line 13 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 19 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 21 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 30 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)
if err != nil {
return err
}
args := []string{"create-node-images", clusterStackPath, clusterStackReleaseDir}
fmt.Printf("Calling Provider Plugin: %q %s\n", path, strings.Join(args, " "))
cmd := exec.Command(path, args...)

Check failure on line 37 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 40 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)
}

0 comments on commit 491378b

Please sign in to comment.