Skip to content

Commit

Permalink
Using int64 for improved readability
Browse files Browse the repository at this point in the history
  • Loading branch information
gab-satchi committed Mar 12, 2020
1 parent fa20457 commit 0e1ae44
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 30 deletions.
28 changes: 15 additions & 13 deletions cmd/clusterctl/client/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ limitations under the License.
package client

import (
utilpointer "k8s.io/utils/pointer"
"k8s.io/utils/pointer"
"strconv"

"github.com/pkg/errors"
Expand Down Expand Up @@ -77,10 +77,12 @@ type GetClusterTemplateOptions struct {
KubernetesVersion string

// ControlPlaneMachineCount defines the number of control plane machines to be added to the workload cluster.
ControlPlaneMachineCount *int32
// It can be set through the cli flag, CONTROL_PLANE_MACHINE_COUNT environment variable or will default to 1
ControlPlaneMachineCount *int64

// WorkerMachineCount defines number of worker machines to be added to the workload cluster.
WorkerMachineCount *int32
// It can be set through the cli flag, WORKER_MACHINE_COUNT environment variable or will default to 0
WorkerMachineCount *int64

// listVariablesOnly sets the GetClusterTemplate method to return the list of variables expected by the template
// without executing any further processing.
Expand Down Expand Up @@ -304,39 +306,39 @@ func (c *clusterctlClient) templateOptionsToVariables(options GetClusterTemplate

// the ControlPlaneMachineCount, if valid, can be used in templates using the ${ CONTROL_PLANE_MACHINE_COUNT } variable.
if options.ControlPlaneMachineCount == nil {
//Check if set through env variable and default to 1 otherwise
// Check if set through env variable and default to 1 otherwise
if v, err := c.configClient.Variables().Get("CONTROL_PLANE_MACHINE_COUNT"); err != nil {
options.ControlPlaneMachineCount = utilpointer.Int32Ptr(1)
options.ControlPlaneMachineCount = pointer.Int64Ptr(1)
} else {
i, err := strconv.Atoi(v)
i, err := strconv.ParseInt(v, 10, 64)
if err != nil {
return errors.Errorf("invalid value for CONTROL_PLANE_MACHINE_COUNT set")
}
options.ControlPlaneMachineCount = utilpointer.Int32Ptr(int32(i))
options.ControlPlaneMachineCount = &i
}
}
if *options.ControlPlaneMachineCount < 1 {
return errors.Errorf("invalid ControlPlaneMachineCount. Please use a number greater or equal than 1")
}
c.configClient.Variables().Set("CONTROL_PLANE_MACHINE_COUNT", strconv.Itoa(int(*options.ControlPlaneMachineCount)))
c.configClient.Variables().Set("CONTROL_PLANE_MACHINE_COUNT", strconv.FormatInt(*options.ControlPlaneMachineCount, 10))

// the WorkerMachineCount, if valid, can be used in templates using the ${ WORKER_MACHINE_COUNT } variable.
if options.WorkerMachineCount == nil {
//Check if set through env variable and default to 0 otherwise
// Check if set through env variable and default to 0 otherwise
if v, err := c.configClient.Variables().Get("WORKER_MACHINE_COUNT"); err != nil {
options.WorkerMachineCount = utilpointer.Int32Ptr(0)
options.WorkerMachineCount = pointer.Int64Ptr(0)
} else {
i, err := strconv.Atoi(v)
i, err := strconv.ParseInt(v, 10, 64)
if err != nil {
return errors.Errorf("invalid value for WORKER_MACHINE_COUNT set")
}
options.WorkerMachineCount = utilpointer.Int32Ptr(int32(i))
options.WorkerMachineCount = &i
}
}
if *options.WorkerMachineCount < 0 {
return errors.Errorf("invalid WorkerMachineCount. Please use a number greater or equal than 0")
}
c.configClient.Variables().Set("WORKER_MACHINE_COUNT", strconv.Itoa(int(*options.WorkerMachineCount)))
c.configClient.Variables().Set("WORKER_MACHINE_COUNT", strconv.FormatInt(*options.WorkerMachineCount, 10))

return nil
}
26 changes: 13 additions & 13 deletions cmd/clusterctl/client/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ package client
import (
"fmt"
"io/ioutil"
utilpointer "k8s.io/utils/pointer"
"k8s.io/utils/pointer"
"os"
"path/filepath"
"reflect"
Expand Down Expand Up @@ -193,8 +193,8 @@ func Test_clusterctlClient_templateOptionsToVariables(t *testing.T) {
ClusterName: "foo",
TargetNamespace: "bar",
KubernetesVersion: "v1.2.3",
ControlPlaneMachineCount: utilpointer.Int32Ptr(1),
WorkerMachineCount: utilpointer.Int32Ptr(2),
ControlPlaneMachineCount: pointer.Int64Ptr(1),
WorkerMachineCount: pointer.Int64Ptr(2),
},
},
wantVars: map[string]string{
Expand All @@ -213,8 +213,8 @@ func Test_clusterctlClient_templateOptionsToVariables(t *testing.T) {
ClusterName: "foo",
TargetNamespace: "bar",
KubernetesVersion: "", // empty means to use value from env variables/config file
ControlPlaneMachineCount: utilpointer.Int32Ptr(1),
WorkerMachineCount: utilpointer.Int32Ptr(2),
ControlPlaneMachineCount: pointer.Int64Ptr(1),
WorkerMachineCount: pointer.Int64Ptr(2),
},
},
wantVars: map[string]string{
Expand Down Expand Up @@ -281,7 +281,7 @@ func Test_clusterctlClient_templateOptionsToVariables(t *testing.T) {
ClusterName: "foo",
TargetNamespace: "bar",
KubernetesVersion: "v1.2.3",
ControlPlaneMachineCount: utilpointer.Int32Ptr(-1),
ControlPlaneMachineCount: pointer.Int64Ptr(-1),
},
},
wantErr: true,
Expand All @@ -293,8 +293,8 @@ func Test_clusterctlClient_templateOptionsToVariables(t *testing.T) {
ClusterName: "foo",
TargetNamespace: "bar",
KubernetesVersion: "v1.2.3",
ControlPlaneMachineCount: utilpointer.Int32Ptr(1),
WorkerMachineCount: utilpointer.Int32Ptr(-1),
ControlPlaneMachineCount: pointer.Int64Ptr(1),
WorkerMachineCount: pointer.Int64Ptr(-1),
},
},
wantErr: true,
Expand Down Expand Up @@ -439,7 +439,7 @@ func Test_clusterctlClient_GetClusterTemplate(t *testing.T) {
},
ClusterName: "test",
TargetNamespace: "ns1",
ControlPlaneMachineCount: utilpointer.Int32Ptr(1),
ControlPlaneMachineCount: pointer.Int64Ptr(1),
},
},
want: templateValues{
Expand All @@ -459,7 +459,7 @@ func Test_clusterctlClient_GetClusterTemplate(t *testing.T) {
},
ClusterName: "test",
TargetNamespace: "ns1",
ControlPlaneMachineCount: utilpointer.Int32Ptr(1),
ControlPlaneMachineCount: pointer.Int64Ptr(1),
},
},
want: templateValues{
Expand All @@ -479,7 +479,7 @@ func Test_clusterctlClient_GetClusterTemplate(t *testing.T) {
},
ClusterName: "test",
TargetNamespace: "", // empty triggers usage of the current namespace
ControlPlaneMachineCount: utilpointer.Int32Ptr(1),
ControlPlaneMachineCount: pointer.Int64Ptr(1),
},
},
want: templateValues{
Expand All @@ -498,7 +498,7 @@ func Test_clusterctlClient_GetClusterTemplate(t *testing.T) {
},
ClusterName: "test",
TargetNamespace: "ns1",
ControlPlaneMachineCount: utilpointer.Int32Ptr(1),
ControlPlaneMachineCount: pointer.Int64Ptr(1),
},
},
want: templateValues{
Expand All @@ -519,7 +519,7 @@ func Test_clusterctlClient_GetClusterTemplate(t *testing.T) {
},
ClusterName: "test",
TargetNamespace: "ns1",
ControlPlaneMachineCount: utilpointer.Int32Ptr(1),
ControlPlaneMachineCount: pointer.Int64Ptr(1),
},
},
want: templateValues{
Expand Down
8 changes: 4 additions & 4 deletions cmd/clusterctl/cmd/config_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ type configClusterOptions struct {

targetNamespace string
kubernetesVersion string
controlPlaneMachineCount int32
workerMachineCount int32
controlPlaneMachineCount int64
workerMachineCount int64

url string
configMapNamespace string
Expand Down Expand Up @@ -100,9 +100,9 @@ func init() {
"The namespace to use for the workload cluster. If unspecified, the current namespace will be used.")
configClusterClusterCmd.Flags().StringVar(&cc.kubernetesVersion, "kubernetes-version", "",
"The Kubernetes version to use for the workload cluster. If unspecified, the value from OS environment variables or the .cluster-api/clusterctl.yaml config file will be used.")
configClusterClusterCmd.Flags().Int32Var(&cc.controlPlaneMachineCount, "control-plane-machine-count", 1,
configClusterClusterCmd.Flags().Int64Var(&cc.controlPlaneMachineCount, "control-plane-machine-count", 1,
"The number of control plane machines for the workload cluster.")
configClusterClusterCmd.Flags().Int32Var(&cc.workerMachineCount, "worker-machine-count", 0,
configClusterClusterCmd.Flags().Int64Var(&cc.workerMachineCount, "worker-machine-count", 0,
"The number of worker machines for the workload cluster.")

// flags for the repository source
Expand Down

0 comments on commit 0e1ae44

Please sign in to comment.