Skip to content

Commit

Permalink
Add tests to the kubernetes options handling
Browse files Browse the repository at this point in the history
Signed-off-by: Zsolt <[email protected]>
  • Loading branch information
Zsolt committed Apr 12, 2022
1 parent a9ab466 commit 071eee2
Show file tree
Hide file tree
Showing 3 changed files with 299 additions and 36 deletions.
25 changes: 25 additions & 0 deletions commands/create_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package commands

import (
"github.com/stretchr/testify/require"
"testing"
)

func TestCsvToMap(t *testing.T) {
d := []string{
"\"tolerations=key=foo,value=bar;key=foo2,value=bar2\",replicas=1",
"namespace=default",
}
r, err := csvToMap(d)

require.NoError(t, err)

require.Contains(t, r, "tolerations")
require.Equal(t, r["tolerations"], "key=foo,value=bar;key=foo2,value=bar2")

require.Contains(t, r, "replicas")
require.Equal(t, r["replicas"], "1")

require.Contains(t, r, "namespace")
require.Equal(t, r["namespace"], "default")
}
81 changes: 45 additions & 36 deletions driver/kubernetes/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,39 @@ func (f *factory) New(ctx context.Context, cfg driver.InitConfig) (driver.Driver
clientset: clientset,
}

deploymentOpt, loadbalance, namespace, err := f.processDriverOpts(deploymentName, namespace, cfg)
if nil != err {
return nil, err
}

d.deployment, d.configMaps, err = manifest.NewDeployment(deploymentOpt)
if err != nil {
return nil, err
}

d.minReplicas = deploymentOpt.Replicas

d.deploymentClient = clientset.AppsV1().Deployments(namespace)
d.podClient = clientset.CoreV1().Pods(namespace)
d.configMapClient = clientset.CoreV1().ConfigMaps(namespace)

switch loadbalance {
case LoadbalanceSticky:
d.podChooser = &podchooser.StickyPodChooser{
Key: cfg.ContextPathHash,
PodClient: d.podClient,
Deployment: d.deployment,
}
case LoadbalanceRandom:
d.podChooser = &podchooser.RandomPodChooser{
PodClient: d.podClient,
Deployment: d.deployment,
}
}
return d, nil
}

func (f *factory) processDriverOpts(deploymentName string, namespace string, cfg driver.InitConfig) (*manifest.DeploymentOpt, string, string, error) {
deploymentOpt := &manifest.DeploymentOpt{
Name: deploymentName,
Image: bkimage.DefaultImage,
Expand All @@ -81,6 +114,7 @@ func (f *factory) New(ctx context.Context, cfg driver.InitConfig) (driver.Driver
deploymentOpt.Qemu.Image = bkimage.QemuImage

loadbalance := LoadbalanceSticky
var err error

for k, v := range cfg.DriverOpts {
switch k {
Expand All @@ -93,7 +127,7 @@ func (f *factory) New(ctx context.Context, cfg driver.InitConfig) (driver.Driver
case "replicas":
deploymentOpt.Replicas, err = strconv.Atoi(v)
if err != nil {
return nil, err
return nil, "", "", err
}
case "requests.cpu":
deploymentOpt.RequestsCPU = v
Expand All @@ -106,9 +140,11 @@ func (f *factory) New(ctx context.Context, cfg driver.InitConfig) (driver.Driver
case "rootless":
deploymentOpt.Rootless, err = strconv.ParseBool(v)
if err != nil {
return nil, err
return nil, "", "", err
}
if deploymentOpt.Image == bkimage.DefaultImage {
deploymentOpt.Image = bkimage.DefaultRootlessImage
}
deploymentOpt.Image = bkimage.DefaultRootlessImage
case "nodeselector":
kvs := strings.Split(strings.Trim(v, `"`), ",")
s := map[string]string{}
Expand All @@ -124,9 +160,6 @@ func (f *factory) New(ctx context.Context, cfg driver.InitConfig) (driver.Driver
deploymentOpt.Tolerations = []corev1.Toleration{}
for i := range ts {
kvs := strings.Split(ts[i], ",")
if len(kvs) == 0 {
return nil, errors.Errorf("invalid tolaration %q", v)
}

t := corev1.Toleration{}

Expand All @@ -145,12 +178,12 @@ func (f *factory) New(ctx context.Context, cfg driver.InitConfig) (driver.Driver
case "tolerationSeconds":
c, err := strconv.Atoi(kv[1])
if nil != err {
return nil, err
return nil, "", "", err
}
c64 := int64(c)
t.TolerationSeconds = &c64
default:
return nil, errors.Errorf("invalid tolaration %q", v)
return nil, "", "", errors.Errorf("invalid tolaration %q", v)
}
}
}
Expand All @@ -162,48 +195,24 @@ func (f *factory) New(ctx context.Context, cfg driver.InitConfig) (driver.Driver
case LoadbalanceSticky:
case LoadbalanceRandom:
default:
return nil, errors.Errorf("invalid loadbalance %q", v)
return nil, "", "", errors.Errorf("invalid loadbalance %q", v)
}
loadbalance = v
case "qemu.install":
deploymentOpt.Qemu.Install, err = strconv.ParseBool(v)
if err != nil {
return nil, err
return nil, "", "", err
}
case "qemu.image":
if v != "" {
deploymentOpt.Qemu.Image = v
}
default:
return nil, errors.Errorf("invalid driver option %s for driver %s", k, DriverName)
return nil, "", "", errors.Errorf("invalid driver option %s for driver %s", k, DriverName)
}
}

d.deployment, d.configMaps, err = manifest.NewDeployment(deploymentOpt)
if err != nil {
return nil, err
}

d.minReplicas = deploymentOpt.Replicas

d.deploymentClient = clientset.AppsV1().Deployments(namespace)
d.podClient = clientset.CoreV1().Pods(namespace)
d.configMapClient = clientset.CoreV1().ConfigMaps(namespace)

switch loadbalance {
case LoadbalanceSticky:
d.podChooser = &podchooser.StickyPodChooser{
Key: cfg.ContextPathHash,
PodClient: d.podClient,
Deployment: d.deployment,
}
case LoadbalanceRandom:
d.podChooser = &podchooser.RandomPodChooser{
PodClient: d.podClient,
Deployment: d.deployment,
}
}
return d, nil
return deploymentOpt, loadbalance, namespace, nil
}

func (f *factory) AllowsInstances() bool {
Expand Down
Loading

0 comments on commit 071eee2

Please sign in to comment.