Skip to content

Commit

Permalink
K3d further parameters (kyma-project#856)
Browse files Browse the repository at this point in the history
* add further k3d args

* modify k3s test

* update docs

* update k3d_mock

* remove manual check for empty arg flags

* add agent args flag

* update docs

* simplify loop

* simplify appending for cmd args
  • Loading branch information
shorim authored Apr 16, 2021
1 parent 8234ebc commit 2719426
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 9 deletions.
4 changes: 3 additions & 1 deletion cmd/kyma/alpha/provision/k3s/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@ func NewCmd(o *Options) *cobra.Command {
cmd.Flags().StringVar(&o.Name, "name", "kyma", `Name of the Kyma cluster (default: "kyma")`)
cmd.Flags().IntVar(&o.Workers, "workers", 1, "Number of worker nodes (k3s agents), default: 1")
cmd.Flags().StringSliceVarP(&o.ServerArgs, "server-arg", "s", []string{}, "One or more arguments passed to the Kubernetes API server (e.g. --server-arg='--alsologtostderr')")
cmd.Flags().StringSliceVarP(&o.AgentArgs, "agent-arg", "a", []string{}, "One or more arguments passed to the k3s agent command on agent nodes (e.g. --agent-arg='--alsologtostderr')")
cmd.Flags().DurationVar(&o.Timeout, "timeout", 5*time.Minute, `Maximum time for the provisioning (default: 5m0s). If you want no timeout, enter "0".`)
cmd.Flags().StringSliceVarP(&o.K3dArgs, "k3d-arg", "", []string{}, "One or more arguments passed to the k3d provisioning command (e.g. --k3d-arg='--no-rollback')")
return cmd
}

Expand Down Expand Up @@ -124,7 +126,7 @@ func (c *command) portAllocated(port int) bool {
func (c *command) createK3sCluster() error {
s := c.NewStep("Create K3s instance")
s.Status("Start K3s cluster")
err := k3s.StartCluster(c.Verbose, c.opts.Timeout, c.opts.Name, c.opts.Workers, c.opts.ServerArgs)
err := k3s.StartCluster(c.Verbose, c.opts.Timeout, c.opts.Name, c.opts.Workers, c.opts.ServerArgs, c.opts.AgentArgs, c.opts.K3dArgs)
if err != nil {
s.Failuref("Could not start k3s cluster")
return err
Expand Down
2 changes: 2 additions & 0 deletions cmd/kyma/alpha/provision/k3s/opts.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ type Options struct {
Workers int
Timeout time.Duration
ServerArgs []string
AgentArgs []string
K3dArgs []string
}

//NewOptions creates options with default values
Expand Down
2 changes: 2 additions & 0 deletions docs/gen-docs/kyma_alpha_provision_k3s.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ kyma alpha provision k3s [flags]
## Flags

```bash
-a, --agent-arg strings One or more arguments passed to the k3s agent command on agent nodes (e.g. --agent-arg='--alsologtostderr')
--k3d-arg strings One or more arguments passed to the k3d provisioning command (e.g. --k3d-arg='--no-rollback')
--name string Name of the Kyma cluster (default: "kyma") (default "kyma")
-s, --server-arg strings One or more arguments passed to the Kubernetes API server (e.g. --server-arg='--alsologtostderr')
--timeout duration Maximum time for the provisioning (default: 5m0s). If you want no timeout, enter "0". (default 5m0s)
Expand Down
16 changes: 10 additions & 6 deletions internal/k3s/k3s.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ func ClusterExists(verbose bool, clusterName string) (bool, error) {
}

//StartCluster starts a cluster
func StartCluster(verbose bool, timeout time.Duration, clusterName string, workers int, serverArgs []string) error {
func StartCluster(verbose bool, timeout time.Duration, clusterName string, workers int, serverArgs []string, agentArgs []string, k3dArgs []string) error {
cmdArgs := []string{
"cluster", "create", clusterName,
"--kubeconfig-update-default",
Expand All @@ -141,13 +141,17 @@ func StartCluster(verbose bool, timeout time.Duration, clusterName string, worke

//add further custom server args
for _, srvArg := range serverArgs {
if srvArg == "" {
continue
}
cmdArgs = append(cmdArgs, "--k3s-server-arg")
cmdArgs = append(cmdArgs, srvArg)
cmdArgs = append(cmdArgs, "--k3s-server-arg", srvArg)
}

//add agent args
for _, agentArg := range agentArgs {
cmdArgs = append(cmdArgs, "--k3s-agent-arg", agentArg)
}

//add further k3d args which are not offered by the Kyma CLI flags
cmdArgs = append(cmdArgs, k3dArgs...)

_, err := RunCmd(verbose, timeout, cmdArgs...)

return err
Expand Down
2 changes: 1 addition & 1 deletion internal/k3s/k3s_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ func TestInitializeFailed(t *testing.T) {
}

func TestStartCluster(t *testing.T) {
err := StartCluster(false, 5*time.Second, "kyma", 1, []string{})
err := StartCluster(false, 5*time.Second, "kyma", 1, []string{"--alsologtostderr"}, []string{"--alsologtostderr"}, []string{"--no-rollback"})
require.NoError(t, err)
}

Expand Down
2 changes: 1 addition & 1 deletion internal/k3s/mock/k3d
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ function cluster_list_-o_json {
#
# Mock for 'k3d cluster create kyma --kubeconfig-update-default --timeout 5s -p 80:80@loadbalancer -p 443:443@loadbalancer --agents 1 --k3s-server-arg --disable --k3s-server-arg traefik' command
#
function cluster_create_kyma_--kubeconfig-update-default_--timeout_5s_-p_80:80@loadbalancer_-p_443:443@loadbalancer_--agents_1_--k3s-server-arg_--disable_--k3s-server-arg_traefik {
function cluster_create_kyma_--kubeconfig-update-default_--timeout_5s_-p_80:80@loadbalancer_-p_443:443@loadbalancer_--agents_1_--k3s-server-arg_--disable_--k3s-server-arg_traefik_--k3s-server-arg_--alsologtostderr_--k3s-agent-arg_--alsologtostderr_--no-rollback {
dump_file cluster_create.txt
}

Expand Down

0 comments on commit 2719426

Please sign in to comment.