Skip to content
This repository has been archived by the owner on Oct 10, 2023. It is now read-only.

Commit

Permalink
Update nodepool cc to default top level variable (#3750)
Browse files Browse the repository at this point in the history
* Update nodepool cc to default top level variable

This change adds a top level empty array for the nodePoolLabels variable
so that any nodePoolLabels override in machineDeployments will not fail
with a 'no top-level variable found' error.
  • Loading branch information
tenczar authored Oct 28, 2022
1 parent 1015471 commit 485f0ce
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 28 deletions.
1 change: 1 addition & 0 deletions cmd/cli/plugin/managementcluster/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,7 @@ require (
github.com/vmware-tanzu/tanzu-framework/capabilities/client v0.0.0-00010101000000-000000000000 // indirect
github.com/vmware-tanzu/tanzu-framework/packageclients v0.0.0-20220908202723-7a1ddb97efab // indirect
github.com/vmware-tanzu/tanzu-framework/tkr v0.0.0-00010101000000-000000000000 // indirect
github.com/vmware-tanzu/tanzu-framework/util v0.0.0-00010101000000-000000000000 // indirect
github.com/vmware/govmomi v0.27.1 // indirect
github.com/yalp/jsonpath v0.0.0-20180802001716-5cc68e5049a0 // indirect
go.mongodb.org/mongo-driver v1.5.1 // indirect
Expand Down
1 change: 1 addition & 0 deletions cmd/cli/plugin/package/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,7 @@ require (
github.com/vmware-tanzu/tanzu-framework/apis/run v0.0.0-00010101000000-000000000000 // indirect
github.com/vmware-tanzu/tanzu-framework/cli/core v0.0.0-20220914003300-5b2ed024556a // indirect
github.com/vmware-tanzu/tanzu-framework/tkr v0.0.0-00010101000000-000000000000 // indirect
github.com/vmware-tanzu/tanzu-framework/util v0.0.0-00010101000000-000000000000 // indirect
github.com/vmware/govmomi v0.27.1 // indirect
github.com/yalp/jsonpath v0.0.0-20180802001716-5cc68e5049a0 // indirect
go.mongodb.org/mongo-driver v1.5.1 // indirect
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,7 @@ require (
github.com/vmware-tanzu/tanzu-framework/capabilities/client v0.0.0-00010101000000-000000000000 // indirect
github.com/vmware-tanzu/tanzu-framework/cli/core v0.0.0-20220914003300-5b2ed024556a // indirect
github.com/vmware-tanzu/tanzu-framework/packageclients v0.0.0-00010101000000-000000000000 // indirect
github.com/vmware-tanzu/tanzu-framework/util v0.0.0-00010101000000-000000000000 // indirect
github.com/vmware/govmomi v0.27.1 // indirect
github.com/yalp/jsonpath v0.0.0-20180802001716-5cc68e5049a0 // indirect
go.mongodb.org/mongo-driver v1.5.1 // indirect
Expand Down
46 changes: 19 additions & 27 deletions tkg/client/machine_deployment_cc.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,19 @@ package client
import (
"bytes"
"encoding/json"
"errors"
"fmt"

v1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
capi "sigs.k8s.io/cluster-api/api/v1beta1"

"github.com/pkg/errors"

"github.com/vmware-tanzu/tanzu-framework/tkg/clusterclient"
"github.com/vmware-tanzu/tanzu-framework/util/topology"
)

const (
NodePoolLabelsName string = "nodePoolLabels"
NodePoolTaintsName string = "nodePoolTaints"
)

//nolint:funlen,gocyclo
Expand All @@ -24,9 +30,11 @@ func DoSetMachineDeploymentCC(clusterClient clusterclient.Client, cluster *capi.
return errors.New("cluster topology workers are not set. please repair your cluster before trying again")
}

mdIndex := -1
for i := range cluster.Spec.Topology.Workers.MachineDeployments {
if cluster.Spec.Topology.Workers.MachineDeployments[i].Name == options.Name {
update = &cluster.Spec.Topology.Workers.MachineDeployments[i]
mdIndex = i
}
if cluster.Spec.Topology.Workers.MachineDeployments[i].Name == options.BaseMachineDeployment {
base = cluster.Spec.Topology.Workers.MachineDeployments[i].DeepCopy()
Expand Down Expand Up @@ -56,6 +64,7 @@ func DoSetMachineDeploymentCC(clusterClient clusterclient.Client, cluster *capi.

cluster.Spec.Topology.Workers.MachineDeployments = append(cluster.Spec.Topology.Workers.MachineDeployments, *base)
base = &cluster.Spec.Topology.Workers.MachineDeployments[len(cluster.Spec.Topology.Workers.MachineDeployments)-1]
mdIndex = len(cluster.Spec.Topology.Workers.MachineDeployments) - 1
}

base.Name = options.Name
Expand All @@ -81,19 +90,10 @@ func DoSetMachineDeploymentCC(clusterClient clusterclient.Client, cluster *capi.
}

if options.Labels != nil {
nodeLabelsVar := getClusterVariableByName("nodePoolLabels", base.Variables.Overrides)
if nodeLabelsVar == nil {
nodeLabelsVar = &capi.ClusterVariable{
Name: "nodePoolLabels",
Value: v1.JSON{},
}
base.Variables.Overrides = append(base.Variables.Overrides, *nodeLabelsVar)
nodeLabelsVar = &base.Variables.Overrides[len(base.Variables.Overrides)-1]
}

var labels []map[string]string
// ignore nodePoolLabels not existing
_ = json.NewDecoder(bytes.NewBuffer(nodeLabelsVar.Value.Raw)).Decode(&labels)
if err := topology.GetMDVariable(cluster, mdIndex, NodePoolLabelsName, &labels); err != nil {
return errors.Errorf("unable to find or marshal variable with name %s", NodePoolLabelsName)
}

for k, v := range *options.Labels {
labels = append(labels, map[string]string{
Expand All @@ -102,23 +102,15 @@ func DoSetMachineDeploymentCC(clusterClient clusterclient.Client, cluster *capi.
})
}

output, _ := json.Marshal(labels)
nodeLabelsVar.Value.Raw = output
if err := topology.SetMDVariable(cluster, mdIndex, NodePoolLabelsName, labels); err != nil {
return errors.Errorf("unable to set variable with name %s", NodePoolLabelsName)
}
}

if options.Taints != nil {
nodeTaintsVar := getClusterVariableByName("nodePoolTaints", base.Variables.Overrides)
if nodeTaintsVar == nil {
nodeTaintsVar = &capi.ClusterVariable{
Name: "nodePoolTaints",
Value: v1.JSON{},
}
base.Variables.Overrides = append(base.Variables.Overrides, *nodeTaintsVar)
nodeTaintsVar = &base.Variables.Overrides[len(base.Variables.Overrides)-1]
if err := topology.SetMDVariable(cluster, mdIndex, NodePoolTaintsName, &options.Taints); err != nil {
return errors.Errorf("unable to set variable with name %s", NodePoolTaintsName)
}

output, _ := json.Marshal(options.Taints)
nodeTaintsVar.Value.Raw = output
}

if update != nil {
Expand Down
4 changes: 3 additions & 1 deletion tkg/client/machine_deployment_cc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ var _ = Describe("GetMachineDeploymentCC", func() {
})
})

var _ = Describe("GetMachineDeploymentCC", func() {
var _ = Describe("DeleteMachineDeploymentCC", func() {
var (
err error
regionalClusterClient *fakes.ClusterClient
Expand Down Expand Up @@ -501,6 +501,8 @@ var _ = Describe("SetMachineDeploymentCC", func() {
clusterInterface, _, _, _ := regionalClusterClient.UpdateResourceArgsForCall(0)
actual, ok := clusterInterface.(*capi.Cluster)
Expect(ok).To(BeTrue())
Expect(len(actual.Spec.Topology.Variables)).To(Equal(2))
Expect(actual.Spec.Topology.Variables[1].Name).To(Equal("nodePoolLabels"))
Expect(len(actual.Spec.Topology.Workers.MachineDeployments)).To(Equal(3))
Expect(actual.Spec.Topology.Workers.MachineDeployments[2].Variables.Overrides[0].Value.Raw).To(Equal(expected))
})
Expand Down

0 comments on commit 485f0ce

Please # to comment.