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
  • Loading branch information
anujc25 committed Aug 5, 2022
1 parent 0332a05 commit 9dc2e71
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 4 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"
ClusterResourceAsInputFileNotSupportedErrMsg = "input file with Cluster resource definition is not supported. Please provide cluster configuration"
)

// InfrastructureSpecificVariableMappingMap has, infra name to variable mapping map, which makes easy to get infra specific mapping map
Expand Down
10 changes: 7 additions & 3 deletions pkg/v1/tkg/tkgctl/create_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,10 +174,13 @@ func (t *tkgctl) processWorkloadClusterInputFile(cc *CreateClusterOptions, isTKG
if err != nil {
return isInputFileClusterClassBased, err
}

// If TKGm and user is passing Cluster resource as an input throw error if feature-flag is disabled
if !isTKGSCluster && !t.tkgClient.IsFeatureActivated(config.FeatureFlagPackageBasedLCM) && clusterobj.GetKind() == constants.KindCluster {
return isInputFileClusterClassBased, errors.New(constants.ClusterResourceAsInputFileNotSupportedErrMsg)
}

if isInputFileClusterClassBased {
if !isTKGSCluster && !t.tkgClient.IsFeatureActivated(config.FeatureFlagPackageBasedLCM) {
return isInputFileClusterClassBased, fmt.Errorf(constants.ErrorMsgCClassInputFeatureFlagDisabled, config.FeatureFlagPackageBasedLCM)
}
if isTKGSCluster {
t.TKGConfigReaderWriter().Set(constants.ConfigVariableClusterName, clusterobj.GetName())
t.TKGConfigReaderWriter().Set(constants.ConfigVariableNamespace, clusterobj.GetNamespace())
Expand All @@ -189,6 +192,7 @@ func (t *tkgctl) processWorkloadClusterInputFile(cc *CreateClusterOptions, isTKG
}
t.overrideClusterOptionsWithLatestEnvironmentConfigurationValues(cc)
}

if isTKGSCluster {
err = t.validateTKGSFeatureGateStatus(isInputFileClusterClassBased)
if err != nil {
Expand Down
13 changes: 13 additions & 0 deletions pkg/v1/tkg/tkgctl/create_cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,7 @@ var _ = Describe("Unit tests for (AWS) cluster_aws.yaml as input file for 'tanz
mappedVal, _ = ctl.TKGConfigReaderWriter().Get(constants.ConfigVariableNodeMachineType2)
Expect("worker2").To(Equal(fmt.Sprintf("%v", mappedVal)))
})

It("When Input file is cluster type with multiple objects, Environment should be updated with legacy variables and CreateClusterOptions also updated with Cluster attribute values:", func() {

// Process input cluster yaml file, this should process input cluster yaml file
Expand All @@ -352,6 +353,18 @@ var _ = Describe("Unit tests for (AWS) cluster_aws.yaml as input file for 'tanz
Expect("aws-workload-cluster1").To(Equal(fmt.Sprintf("%v", mappedVal)))
})

It("When Input file is cluster type with multiple objects but feature-flag (FeatureFlagPackageBasedLCM) is false, should return an error", func() {
tkgClient.IsFeatureActivatedReturns(false)
// Process input cluster yaml file, this should process input cluster yaml file
// and update the environment with legacy name and values
// most of cluster yaml attributes are mapped to legacy variable for more look this - constants.ClusterToLegacyVariablesMapAws
options.ClusterConfigFile = inputFileMultipleObjectsAws
IsInputFileClusterClassBased, err := ctl.processWorkloadClusterInputFile(&options, isTKGSCluster)
Expect(IsInputFileClusterClassBased).Should(BeTrue())
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring(constants.ClusterResourceAsInputFileNotSupportedErrMsg))
})

It("When Input file is cluster type does not have value for spec.topology.class, return an error", func() {

// Process input cluster.yaml file, this should process input cluster.yaml file
Expand Down
55 changes: 55 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,57 @@ 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 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())
})
})
When("File contain non-yaml string", func() {
BeforeEach(func() {
configFileContent = `incorrect yaml string`
})
It("should return false with error", func() {
Expect(isClusterClassBased).To(Equal(false))
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("Input file content is not yaml formatted"))
})
})
})
})

0 comments on commit 9dc2e71

Please # to comment.