Skip to content

Commit

Permalink
Fix bugs
Browse files Browse the repository at this point in the history
Signed-off-by: abarreiro <abarreiro@vmware.com>
  • Loading branch information
adambarreiro committed May 20, 2024
1 parent bfac615 commit 33ce3b3
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 17 deletions.
5 changes: 3 additions & 2 deletions govcd/cse/4.1/capiyaml_workerpool.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@ metadata:
name: "{{.NodePoolName}}"
namespace: "{{.TargetNamespace}}"
{{- if and .AutoscalerMaxSize .AutoscalerMinSize}}
cluster.x-k8s.io/cluster-api-Autoscaler-node-group-max-size: "{{.AutoscalerMaxSize}}"
cluster.x-k8s.io/cluster-api-Autoscaler-node-group-min-size: "{{.AutoscalerMinSize}}"
annotations:
cluster.x-k8s.io/cluster-api-autoscaler-node-group-max-size: "{{.AutoscalerMaxSize}}"
cluster.x-k8s.io/cluster-api-autoscaler-node-group-min-size: "{{.AutoscalerMinSize}}"
{{- end}}
spec:
clusterName: "{{.ClusterName}}"
Expand Down
5 changes: 3 additions & 2 deletions govcd/cse/4.2/capiyaml_workerpool.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@ metadata:
name: "{{.NodePoolName}}"
namespace: "{{.TargetNamespace}}"
{{- if and .AutoscalerMaxSize .AutoscalerMinSize}}
cluster.x-k8s.io/cluster-api-Autoscaler-node-group-max-size: "{{.AutoscalerMaxSize}}"
cluster.x-k8s.io/cluster-api-Autoscaler-node-group-min-size: "{{.AutoscalerMinSize}}"
annotations:
cluster.x-k8s.io/cluster-api-autoscaler-node-group-max-size: "{{.AutoscalerMaxSize}}"
cluster.x-k8s.io/cluster-api-autoscaler-node-group-min-size: "{{.AutoscalerMinSize}}"
{{- end}}
spec:
clusterName: "{{.ClusterName}}"
Expand Down
19 changes: 19 additions & 0 deletions govcd/cse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,25 @@ func (vcd *TestVCD) Test_Cse(check *C) {
}
}

// TODO Delete me
func (vcd *TestVCD) Test_CseFoo(check *C) {
requireCseConfig(check, vcd.config)

// Prerequisites: We need to read several items before creating the cluster.
clusterGet, err := vcd.client.CseGetKubernetesClusterById("urn:vcloud:entity:vmware:capvcdCluster:cf8c9b14-20e3-451d-bc21-959286533480")
check.Assert(err, IsNil)
check.Assert(clusterGet.Etag, Not(Equals), "")

// Update worker pool with autoscaler
err = clusterGet.UpdateWorkerPools(map[string]CseWorkerPoolUpdateInput{clusterGet.WorkerPools[0].Name: {
MachineCount: 0,
Autoscaler: &CseWorkerPoolAutoscaler{
MaxSize: 3,
MinSize: 2,
}}}, true)
check.Assert(err, IsNil)
}

// Test_CseWithAutoscaler tests the autoscaling capabilities in CSE clusters
func (vcd *TestVCD) Test_CseWithAutoscaler(check *C) {
requireCseConfig(check, vcd.config)
Expand Down
4 changes: 2 additions & 2 deletions govcd/cse_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -449,8 +449,8 @@ func cseConvertToCseKubernetesClusterType(rde *DefinedEntity) (*CseKubernetesClu
workerPool.MachineCount = int(traverseMapAndGet[float64](yamlDocument, "spec.replicas", "."))

// Get Autoscaler values
autoscalerMax := traverseMapAndGet[string](yamlDocument, "cluster.x-k8s.io/cluster-api-Autoscaler-node-group-max-size", "#")
autoscalerMin := traverseMapAndGet[string](yamlDocument, "cluster.x-k8s.io/cluster-api-Autoscaler-node-group-min-size", "#")
autoscalerMax := traverseMapAndGet[string](yamlDocument, "cluster.x-k8s.io/cluster-api-autoscaler-node-group-max-size", "#")
autoscalerMin := traverseMapAndGet[string](yamlDocument, "cluster.x-k8s.io/cluster-api-autoscaler-node-group-min-size", "#")
if autoscalerMax != "" && autoscalerMin != "" {
maxSize, err := strconv.Atoi(autoscalerMax)
if err != nil {
Expand Down
18 changes: 11 additions & 7 deletions govcd/cse_yaml.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,16 +210,21 @@ func cseUpdateWorkerPoolsInYaml(yamlDocuments []map[string]interface{}, workerPo
}

if workerPools[workerPoolToUpdate].Autoscaler != nil {
d["metadata"].(map[string]interface{})["cluster.x-k8s.io/cluster-api-Autoscaler-node-group-max-size"] = strconv.Itoa(workerPools[workerPoolToUpdate].Autoscaler.MaxSize)
d["metadata"].(map[string]interface{})["cluster.x-k8s.io/cluster-api-Autoscaler-node-group-min-size"] = strconv.Itoa(workerPools[workerPoolToUpdate].Autoscaler.MinSize)
if d["metadata"].(map[string]interface{})["annotations"] == nil {
d["metadata"].(map[string]interface{})["annotations"] = map[string]interface{}{}
}
d["metadata"].(map[string]interface{})["annotations"].(map[string]interface{})["cluster.x-k8s.io/cluster-api-autoscaler-node-group-max-size"] = strconv.Itoa(workerPools[workerPoolToUpdate].Autoscaler.MaxSize)
d["metadata"].(map[string]interface{})["annotations"].(map[string]interface{})["cluster.x-k8s.io/cluster-api-autoscaler-node-group-min-size"] = strconv.Itoa(workerPools[workerPoolToUpdate].Autoscaler.MinSize)
delete(d["spec"].(map[string]interface{}), "replicas") // This is required to avoid conflicts with Autoscaler
} else {
if workerPools[workerPoolToUpdate].MachineCount < 0 {
return fmt.Errorf("incorrect machine count for worker pool %s: %d. Should be at least 0", workerPoolToUpdate, workerPools[workerPoolToUpdate].MachineCount)
}
d["spec"].(map[string]interface{})["replicas"] = float64(workerPools[workerPoolToUpdate].MachineCount) // As it was originally unmarshalled as a float64
delete(d["metadata"].(map[string]interface{}), "cluster.x-k8s.io/cluster-api-Autoscaler-node-group-max-size")
delete(d["metadata"].(map[string]interface{}), "cluster.x-k8s.io/cluster-api-Autoscaler-node-group-min-size")
if d["metadata"].(map[string]interface{})["annotations"] != nil {
delete(d["metadata"].(map[string]interface{})["annotations"].(map[string]interface{}), "cluster.x-k8s.io/cluster-api-autoscaler-node-group-max-size")
delete(d["metadata"].(map[string]interface{})["annotations"].(map[string]interface{}), "cluster.x-k8s.io/cluster-api-autoscaler-node-group-min-size")
}
}
updated++
}
Expand Down Expand Up @@ -404,12 +409,11 @@ func cseUpdateAutoscalerInYaml(yamlDocuments []map[string]interface{}, clusterNa
if err != nil {
return nil, err
}
var autoscaler map[string]interface{}
err = yaml.Unmarshal([]byte(autoscalerYaml), &autoscaler)
autoscaler, err := unmarshalMultipleYamlDocuments(autoscalerYaml)
if err != nil {
return nil, err
}
return append(yamlDocuments, autoscaler), nil
return append(yamlDocuments, autoscaler...), nil
}

// Otherwise the documents are returned without change
Expand Down
8 changes: 4 additions & 4 deletions govcd/cse_yaml_unit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,11 +154,11 @@ func Test_cseUpdateWorkerPoolsInYaml(t *testing.T) {
if traverseMapAndGet[float64](document, "spec.replicas", ".") != float64(newReplicas) {
t.Fatalf("expected %d replicas but got %0.f", newReplicas, retrievedReplicas)
}
autoscalerMinSize := traverseMapAndGet[string](document, "metadata|cluster.x-k8s.io/cluster-api-Autoscaler-node-group-min-size", "|")
autoscalerMinSize := traverseMapAndGet[string](document, "metadata|annotations|cluster.x-k8s.io/cluster-api-autoscaler-node-group-min-size", "|")
if autoscalerMinSize != "" {
t.Fatalf("didn't expect autoscaler min size but got '%s'", autoscalerMinSize)
}
autoscalerMaxSize := traverseMapAndGet[string](document, "metadata|cluster.x-k8s.io/cluster-api-Autoscaler-node-group-max-size", "|")
autoscalerMaxSize := traverseMapAndGet[string](document, "metadata|annotations|cluster.x-k8s.io/cluster-api-autoscaler-node-group-max-size", "|")
if autoscalerMaxSize != "" {
t.Fatalf("didn't expect autoscaler max size but got '%s'", autoscalerMaxSize)
}
Expand Down Expand Up @@ -190,11 +190,11 @@ func Test_cseUpdateWorkerPoolsInYaml(t *testing.T) {
if retrievedReplicas != 0 {
t.Fatalf("didn't expect replicas but got '%0.f'", retrievedReplicas)
}
autoscalerMinSize := traverseMapAndGet[string](document, "metadata|cluster.x-k8s.io/cluster-api-Autoscaler-node-group-min-size", "|")
autoscalerMinSize := traverseMapAndGet[string](document, "metadata|annotations|cluster.x-k8s.io/cluster-api-autoscaler-node-group-min-size", "|")
if autoscalerMinSize != "10" {
t.Fatalf("expected autoscaler min size '10' but got '%s'", autoscalerMinSize)
}
autoscalerMaxSize := traverseMapAndGet[string](document, "metadata|cluster.x-k8s.io/cluster-api-Autoscaler-node-group-max-size", "|")
autoscalerMaxSize := traverseMapAndGet[string](document, "metadata|annotations|cluster.x-k8s.io/cluster-api-autoscaler-node-group-max-size", "|")
if autoscalerMaxSize != "50" {
t.Fatalf("expected autoscaler min size '50' but got '%s'", autoscalerMaxSize)
}
Expand Down

0 comments on commit 33ce3b3

Please sign in to comment.