Skip to content

Commit

Permalink
Fix panic when datacenter obj is not found (#8494)
Browse files Browse the repository at this point in the history
Signed-off-by: Rahul Ganesh <rahulgab@amazon.com>
Co-authored-by: Rahul Ganesh <rahulgab@amazon.com>
  • Loading branch information
rahulbabu95 and Rahul Ganesh committed Jul 17, 2024
1 parent 31c6854 commit ec6f0d3
Show file tree
Hide file tree
Showing 10 changed files with 65 additions and 0 deletions.
4 changes: 4 additions & 0 deletions pkg/cluster/cloudstack.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package cluster

import (
"context"
"fmt"

"github.com/aws/eks-anywhere/pkg/api/v1alpha1"
anywherev1 "github.com/aws/eks-anywhere/pkg/api/v1alpha1"
)

Expand Down Expand Up @@ -38,6 +40,8 @@ func cloudstackEntry() *ConfigManagerEntry {
func(c *Config) error {
if c.CloudStackDatacenter != nil {
return c.CloudStackDatacenter.Validate()
} else if c.Cluster.Spec.DatacenterRef.Kind == v1alpha1.CloudStackDatacenterKind { // We need this conditional check as CloudStackDatacenter will be nil for other providers
return fmt.Errorf("CloudStackDatacenterConfig %s not found", c.Cluster.Spec.DatacenterRef.Name)
}
return nil
},
Expand Down
10 changes: 10 additions & 0 deletions pkg/cluster/cloudstack_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,16 @@ func TestParseConfigMissingCloudstackDatacenter(t *testing.T) {
g.Expect(got.CloudStackDatacenter).To(BeNil())
}

func TestValidateCloudStackDatacenterNotFoundError(t *testing.T) {
g := NewWithT(t)
got, _ := cluster.ParseConfigFromFile("testdata/cluster_cloudstack_missing_datacenter.yaml")
g.Expect(got.CloudStackDatacenter).To(BeNil())

cm, _ := cluster.NewDefaultConfigManager()
err := cm.Validate(got)
g.Expect(err).To(MatchError(ContainSubstring("CloudStackDatacenterConfig eksa-unit-test not found")))
}

func TestDefaultConfigClientBuilderBuildCloudStackClusterSuccess(t *testing.T) {
g := NewWithT(t)
ctx := context.Background()
Expand Down
4 changes: 4 additions & 0 deletions pkg/cluster/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package cluster

import (
"context"
"fmt"

"github.com/aws/eks-anywhere/pkg/api/v1alpha1"
anywherev1 "github.com/aws/eks-anywhere/pkg/api/v1alpha1"
)

Expand All @@ -20,6 +22,8 @@ func dockerEntry() *ConfigManagerEntry {
func(c *Config) error {
if c.DockerDatacenter != nil {
return c.DockerDatacenter.Validate()
} else if c.Cluster.Spec.DatacenterRef.Kind == v1alpha1.DockerDatacenterKind { // We need this conditional check as DockerDatacenter will be nil for other providers
return fmt.Errorf("DockerDatacenterConfig %s not found", c.Cluster.Spec.DatacenterRef.Name)
}
return nil
},
Expand Down
10 changes: 10 additions & 0 deletions pkg/cluster/docker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,16 @@ func TestParseConfigMissingDockerDatacenter(t *testing.T) {
g.Expect(got.DockerDatacenter).To(BeNil())
}

func TestValidateDockerDatacenterNotFoundError(t *testing.T) {
g := NewWithT(t)
got, _ := cluster.ParseConfigFromFile("testdata/cluster_docker_missing_datacenter.yaml")
g.Expect(got.DockerDatacenter).To(BeNil())

cm, _ := cluster.NewDefaultConfigManager()
err := cm.Validate(got)
g.Expect(err).To(MatchError(ContainSubstring("DockerDatacenterConfig eksa-unit-test not found")))
}

func TestDefaultConfigClientBuilderDockerCluster(t *testing.T) {
g := NewWithT(t)
ctx := context.Background()
Expand Down
4 changes: 4 additions & 0 deletions pkg/cluster/nutanix.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package cluster

import (
"context"
"fmt"

"github.com/aws/eks-anywhere/pkg/api/v1alpha1"
anywherev1 "github.com/aws/eks-anywhere/pkg/api/v1alpha1"
)

Expand All @@ -24,6 +26,8 @@ func nutanixEntry() *ConfigManagerEntry {
func(c *Config) error {
if c.NutanixDatacenter != nil {
return c.NutanixDatacenter.Validate()
} else if c.Cluster.Spec.DatacenterRef.Kind == v1alpha1.NutanixDatacenterKind { // We need this conditional check as NutanixDatacenter will be nil for other providers
return fmt.Errorf("NutanixDatacenterConfig %s not found", c.Cluster.Spec.DatacenterRef.Name)
}
return nil
},
Expand Down
5 changes: 5 additions & 0 deletions pkg/cluster/nutanix_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ func TestValidateNutanixEntry(t *testing.T) {

err = cm.Validate(config)
assert.NoError(t, err)

config.NutanixDatacenter = nil
err = cm.Validate(config)
assert.Error(t, err)
assert.Contains(t, err.Error(), "NutanixDatacenterConfig eksa-unit-test not found")
}

func TestNutanixConfigClientBuilder(t *testing.T) {
Expand Down
4 changes: 4 additions & 0 deletions pkg/cluster/tinkerbell.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package cluster

import (
"context"
"fmt"

"github.com/aws/eks-anywhere/pkg/api/v1alpha1"
anywherev1 "github.com/aws/eks-anywhere/pkg/api/v1alpha1"
)

Expand Down Expand Up @@ -32,6 +34,8 @@ func tinkerbellEntry() *ConfigManagerEntry {
if err := validateSameNamespace(c, c.TinkerbellDatacenter); err != nil {
return err
}
} else if c.Cluster.Spec.DatacenterRef.Kind == v1alpha1.TinkerbellDatacenterKind { // We need this conditional check as TinkerbellDatacenter will be nil for other providers
return fmt.Errorf("TinkerbellDatacenterConfig %s not found", c.Cluster.Spec.DatacenterRef.Name)
}
return nil
},
Expand Down
10 changes: 10 additions & 0 deletions pkg/cluster/tinkerbell_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,16 @@ func TestParseConfigFromFileTinkerbellCluster(t *testing.T) {
)
}

func TestValidateTinkerbellDatacenterNotFoundError(t *testing.T) {
g := NewWithT(t)
got, err := cluster.ParseConfigFromFile("testdata/cluster_tinkerbell_1_19.yaml")
g.Expect(err).NotTo(HaveOccurred())
got.TinkerbellDatacenter = nil
cm, _ := cluster.NewDefaultConfigManager()
err = cm.Validate(got)
g.Expect(err).To(MatchError(ContainSubstring("TinkerbellDatacenterConfig test not found")))
}

func TestDefaultConfigClientBuilderTinkerbellCluster(t *testing.T) {
g := NewWithT(t)
ctx := context.Background()
Expand Down
4 changes: 4 additions & 0 deletions pkg/cluster/vsphere.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package cluster

import (
"context"
"fmt"

"github.com/aws/eks-anywhere/pkg/api/v1alpha1"
anywherev1 "github.com/aws/eks-anywhere/pkg/api/v1alpha1"
)

Expand Down Expand Up @@ -39,6 +41,8 @@ func vsphereEntry() *ConfigManagerEntry {
func(c *Config) error {
if c.VSphereDatacenter != nil {
return c.VSphereDatacenter.Validate()
} else if c.Cluster.Spec.DatacenterRef.Kind == v1alpha1.VSphereDatacenterKind { // We need this conditional check as VSphereDatacenter will be nil for other providers
return fmt.Errorf("VSphereDatacenterConfig %s not found", c.Cluster.Spec.DatacenterRef.Name)
}
return nil
},
Expand Down
10 changes: 10 additions & 0 deletions pkg/cluster/vsphere_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,16 @@ func TestParseConfigMissingVSphereDatacenter(t *testing.T) {
g.Expect(got.VSphereDatacenter).To(BeNil())
}

func TestValidateVSphereDatacenterNotFoundError(t *testing.T) {
g := NewWithT(t)
got, _ := cluster.ParseConfigFromFile("testdata/cluster_vsphere_missing_datacenter.yaml")
g.Expect(got.VSphereDatacenter).To(BeNil())

cm, _ := cluster.NewDefaultConfigManager()
err := cm.Validate(got)
g.Expect(err).To(MatchError(ContainSubstring("VSphereDatacenterConfig eksa-unit-test2 not found")))
}

func TestDefaultConfigClientBuilderVSphereCluster(t *testing.T) {
g := NewWithT(t)
ctx := context.Background()
Expand Down

0 comments on commit ec6f0d3

Please sign in to comment.