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

Commit

Permalink
Display correct error message when passing Cluster based resource wit…
Browse files Browse the repository at this point in the history
…hout ClusterClass as part of cluster create (#3053)
  • Loading branch information
anujc25 authored Aug 4, 2022
1 parent 46b4f80 commit fcf7763
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 3 deletions.
3 changes: 2 additions & 1 deletion pkg/v1/tkg/constants/clusterclass_mapping.go
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,8 @@ const (

SPEC = "spec"

TopologyClassIncorrectValueErrMsg = "input cluster class file, attribute spec.topology.class has no value or incorrect value or not following correct naming convension"
TopologyClassIncorrectValueErrMsg = "input cluster class file, attribute spec.topology.class has no value or incorrect value or not following correct naming convention"
ClusterResourceWithoutTopologyNotSupportedErrMsg = "input file contains Cluster resource which doesn't have ClusterClass specified. Passing Cluster resource without ClusterClass specification is not supported"
)

// InfrastructureSpecificVariableMappingMap has, infra name to variable mapping map, which makes easy to get infra specific mapping map
Expand Down
2 changes: 1 addition & 1 deletion pkg/v1/tkg/tkgctl/create_cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,7 @@ var _ = Describe("Unit tests for (AWS) cluster_aws.yaml as input file for 'tanz
// most of cluster.yaml attributes are mapped to legacy variable for more look this - constants.clusterToLegacyVariablesMapAws
options.ClusterConfigFile = inputFileAwsEmptyClass
_, err := ctl.processWorkloadClusterInputFile(&options, isTKGSCluster)
Expect(fmt.Sprint(err)).To(Equal(constants.TopologyClassIncorrectValueErrMsg))
Expect(fmt.Sprint(err)).To(Equal(constants.ClusterResourceWithoutTopologyNotSupportedErrMsg))
})

It("When Input file is aws clusterclass.yaml file, but in-correct spec.topology.class name:", func() {
Expand Down
8 changes: 7 additions & 1 deletion pkg/v1/tkg/tkgctl/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@ func CheckIfInputFileIsClusterClassBased(clusterConfigFile string) (bool, unstru
if err != nil {
return isInputFileClusterClassBased, clusterObj, errors.Wrap(err, fmt.Sprintf("Unable to read input file: %v ", clusterConfigFile))
}

yamlObjects, err := utilyaml.ToUnstructured(content)
if err != nil {
return isInputFileClusterClassBased, clusterObj, errors.Wrap(err, fmt.Sprintf("Input file content is not yaml formatted, file path: %v", clusterConfigFile))
Expand All @@ -187,8 +188,13 @@ func CheckIfInputFileIsClusterClassBased(clusterConfigFile string) (bool, unstru
for i := range yamlObjects {
obj := yamlObjects[i]
if obj.GetKind() == constants.KindCluster {
isInputFileClusterClassBased = true
clusterObj = obj
class, exists, _ := unstructured.NestedString((&clusterObj).UnstructuredContent(), "spec", "topology", "class")
if exists && class != "" {
isInputFileClusterClassBased = true
} else {
return isInputFileClusterClassBased, clusterObj, errors.New(constants.ClusterResourceWithoutTopologyNotSupportedErrMsg)
}
break
}
}
Expand Down
64 changes: 64 additions & 0 deletions pkg/v1/tkg/tkgctl/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
. "github.com/onsi/gomega"

"github.com/vmware-tanzu/tanzu-framework/pkg/v1/tkg/constants"
"github.com/vmware-tanzu/tanzu-framework/pkg/v1/tkg/utils"
)

var _ = Describe("Cluster Class - IP Family Validation related test cases: ", func() {
Expand Down Expand Up @@ -211,3 +212,66 @@ var _ = Describe("Cluster Class - IP Family Validation related test cases: ", fu
})
})
})

var _ = Describe("Test cases for CheckIfInputFileIsClusterClassBased", func() {
Context("Test cases for CheckIfInputFileIsClusterClassBased", func() {
var (
configFile string
configFileContent string
isClusterClassBased bool
err error
)

JustBeforeEach(func() {
configFile, err = utils.CreateTempFile("", "")
Expect(err).To(BeNil())
err = utils.SaveFile(configFile, []byte(configFileContent))
Expect(err).To(BeNil())
isClusterClassBased, _, err = CheckIfInputFileIsClusterClassBased(configFile)
})
When("File contains cluster resource with clusterclass defined", func() {
BeforeEach(func() {
configFileContent = `apiVersion: cluster.x-k8s.io/v1beta1
kind: Cluster
metadata:
name: aws-workload-cluster1
namespace: default
spec:
topology:
class: tkg-aws-default`
})
It("should return true and error should be nil", func() {
Expect(isClusterClassBased).To(BeTrue())
Expect(err).NotTo(HaveOccurred())
})
})
When("File contains cluster resource without clusterclass defined", func() {
BeforeEach(func() {
configFileContent = `apiVersion: cluster.x-k8s.io/v1beta1
kind: Cluster
metadata:
name: aws-workload-cluster1
namespace: default
spec:
clusterNetwork:
pods:
cidrBlocks: []
`
})
It("should return false with error", func() {
Expect(isClusterClassBased).To(BeFalse())
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring(constants.ClusterResourceWithoutTopologyNotSupportedErrMsg))
})
})
When("File doesn't contain cluster resource", func() {
BeforeEach(func() {
configFileContent = ``
})
It("should return false without error", func() {
Expect(isClusterClassBased).To(Equal(false))
Expect(err).NotTo(HaveOccurred())
})
})
})
})

0 comments on commit fcf7763

Please sign in to comment.