-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ Support calling provider plugins. (#97)
Signed-off-by: Thomas Guettler <thomas.guettler@syself.com>
- Loading branch information
Showing
10 changed files
with
179 additions
and
9 deletions.
There are no files selected for viewing
This file contains 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 |
---|---|---|
|
@@ -17,5 +17,6 @@ temp | |
# build and release | ||
dist | ||
csctl | ||
csctl-docker | ||
tmp/ | ||
releases/ |
This file contains 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 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,69 @@ | ||
/* | ||
Copyright 2024 The Kubernetes Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
// Package main provides a dummy plugin for csctl. You can use that code | ||
// to create a real csctl plugin. | ||
// You can implement the "create-node-images" command to create node images during | ||
// a `csctl create` call. | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
|
||
csctlclusterstack "github.com/SovereignCloudStack/csctl/pkg/clusterstack" | ||
) | ||
|
||
const provider = "docker" | ||
|
||
func usage() { | ||
fmt.Printf(`%s create-node-images cluster-stack-directory cluster-stack-release-directory | ||
This command is a csctl plugin. | ||
https://github.com/SovereignCloudStack/csctl | ||
`, 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 := csctlclusterstack.GetCsctlConfig(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.Printf("..... pretending to read config: %s\n", config.Config.Provider.Config["dummyKey"]) | ||
fmt.Printf("..... pretending to do heavy work (creating node images) ...\n") | ||
} |
This file contains 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 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 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 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 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 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,71 @@ | ||
/* | ||
Copyright 2024 The Kubernetes Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
// Package providerplugin implements calling the provider specific csctl plugin. | ||
package providerplugin | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"os/exec" | ||
"path/filepath" | ||
|
||
"github.com/SovereignCloudStack/csctl/pkg/clusterstack" | ||
) | ||
|
||
// GetProviderExecutable returns the path to the provider plugin (like "csctl-docker"). | ||
// If there is not "config" for the provider in csctl.yaml, then "needed" is false and "path" is the empty string. | ||
func GetProviderExecutable(config *clusterstack.CsctlConfig) (needed bool, path string, err error) { | ||
if len(config.Config.Provider.Config) == 0 { | ||
return false, "", nil | ||
} | ||
pluginName := "csctl-" + config.Config.Provider.Type | ||
_, err = os.Stat(pluginName) | ||
if err == nil { | ||
path, err := filepath.Abs(pluginName) | ||
if err != nil { | ||
return false, "", fmt.Errorf("filepath.Abs(%q) failed: %w", pluginName, err) | ||
} | ||
return true, path, nil | ||
} | ||
path, err = exec.LookPath(pluginName) | ||
if err != nil { | ||
return false, "", fmt.Errorf("could not find plugin %s in $PATH or current working directory", pluginName) | ||
} | ||
return true, path, nil | ||
} | ||
|
||
// CreateNodeImages calls the provider plugin command to create nodes images. | ||
func CreateNodeImages(config *clusterstack.CsctlConfig, clusterStackPath, clusterStackReleaseDir string) error { | ||
needed, path, err := GetProviderExecutable(config) | ||
if err != nil { | ||
return err | ||
} | ||
if !needed { | ||
fmt.Printf("No provider specifig configuration in csctl.yaml. No need to call a plugin for provider %q\n", config.Config.Provider.Type) | ||
return nil | ||
} | ||
args := []string{"create-node-images", clusterStackPath, clusterStackReleaseDir} | ||
fmt.Printf("Calling Provider Plugin: %s\n", path) | ||
cmd := exec.Command(path, args...) // #nosec G204 | ||
cmd.Stdout = os.Stdout | ||
cmd.Stderr = os.Stderr | ||
err = cmd.Run() | ||
if err != nil { | ||
return fmt.Errorf("cmd.Run() failed: %w", err) | ||
} | ||
return nil | ||
} |
This file contains 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