-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
[Feature] Document Calling EKSCtl Cobra subcommands from Go #7950
Labels
kind/feature
New feature or request
Comments
Filching parts of the code in the cmd section of eksctl allows you to embed it in other tooling that needs to create and tear down clusters without execing go get github.com/weaveworks/eksctl/pkg/ctl/cmdutils
go get github.com/weaveworks/eksctl/pkg/ctl/create Not all of it is necessary e.g. the logging etc. so can be more compact. Closing this and keeping it here as docs for anyone else looking to embed eksctl as a package in the future. package aws
import (
"bytes"
"context"
"fmt"
"log"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/kris-nova/logger"
"github.com/spf13/cobra"
"github.com/weaveworks/eksctl/pkg/ctl/cmdutils"
"github.com/weaveworks/eksctl/pkg/ctl/create"
)
type CreateCluster struct {
AccountID string
Region string
ClusterName string
KubeConfigFilePath string
}
func (c *CreateCluster) Run() error {
// Initialize AWS session
_, err := config.LoadDefaultConfig(context.TODO(),
config.WithRegion(c.Region),
)
if err != nil {
return fmt.Errorf("unable to load SDK config, %v", err)
}
// Create the ekctl root cmd to execute
rootCmd := &cobra.Command{
Use: "eksctl [command]",
Short: "The official CLI for Amazon EKS",
Run: func(c *cobra.Command, _ []string) {
if err := c.Help(); err != nil {
logger.Debug("ignoring cobra error %q", err.Error())
}
},
SilenceUsage: true,
}
loggerLevel := rootCmd.PersistentFlags().IntP("verbose", "v", 3, "set log level, use 0 to silence, 4 for debugging and 5 for debugging with AWS debug logging")
colorValue := rootCmd.PersistentFlags().StringP("color", "C", "true", "toggle colorized logs (valid options: true, false, fabulous)")
dumpLogsValue := rootCmd.PersistentFlags().BoolP("dumpLogs", "d", false, "dump logs to disk on failure if set to true")
logBuffer := new(bytes.Buffer)
cobra.OnInitialize(func() {
initLogger(*loggerLevel, *colorValue, logBuffer, *dumpLogsValue)
})
flagGrouping := cmdutils.NewGrouping()
createCmd := create.Command(flagGrouping)
rootCmd.AddCommand(createCmd)
checkCommand(rootCmd)
createArgs := []string{
"create",
"cluster",
"--kubeconfig",
c.KubeConfigFilePath,
"-n",
c.ClusterName,
"--region",
c.Region,
"--managed",
}
rootCmd.SetArgs(createArgs)
err = rootCmd.Execute()
if err != nil {
return fmt.Errorf("eksctl failed with %s", err)
}
log.Printf("Cluster created successfully!")
return nil
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
What feature/behavior/change do you want?
Clearly document imports and code required to call eksctl cobra command / sub-command from another go project as noted in #813.
Particularly:
Why do you want this feature?
Some projects would benefit from reusing eksctl in their CI to create and delete ephemeral clusters to test various AWS integrations, particularly CNI as shown here : microsoft/retina#667 , however linting rules do not allow Execing eksctl, being able to add the Cobra commands to a new rootCmd and Run / RunE those via the AWS Go SDK v2 will be useful in reusing the code here in those tests.
The text was updated successfully, but these errors were encountered: