Skip to content

Commit

Permalink
Delete yaml parsing code from api package
Browse files Browse the repository at this point in the history
This code was mostly unused and redudant given the parsing tools in the
cluster package, which is a better place for it.
  • Loading branch information
g-gaston committed Sep 13, 2023
1 parent b981df0 commit 20fa44e
Show file tree
Hide file tree
Showing 32 changed files with 125 additions and 1,006 deletions.
6 changes: 4 additions & 2 deletions cmd/eks-a-tool/cmd/vsphereautofill.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"sigs.k8s.io/yaml"

"github.com/aws/eks-anywhere/pkg/api/v1alpha1"
"github.com/aws/eks-anywhere/pkg/cluster"
"github.com/aws/eks-anywhere/pkg/filewriter"
"github.com/aws/eks-anywhere/pkg/validations"
)
Expand Down Expand Up @@ -65,10 +66,11 @@ func autofill(ctx context.Context) error {
if err != nil {
return fmt.Errorf("unable to get datacenter config from file: %v", err)
}
machineConfig, err := v1alpha1.GetVSphereMachineConfigs(clusterConfigFileName)
config, err := cluster.ParseConfigFromFile(clusterConfigFileName)
if err != nil {
return fmt.Errorf("unable to get machine config from file: %v", err)
return err
}
machineConfig := config.VSphereMachineConfigs
controlPlaneMachineConfig := machineConfig[clusterConfig.Spec.ControlPlaneConfiguration.MachineGroupRef.Name]
workerMachineConfig := machineConfig[clusterConfig.Spec.WorkerNodeGroupConfigurations[0].MachineGroupRef.Name]
var updatedFields []string
Expand Down
38 changes: 0 additions & 38 deletions pkg/api/v1alpha1/cloudstackmachineconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,6 @@ import (
"fmt"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/yaml"

"github.com/aws/eks-anywhere/pkg/utils/file"
yamlutil "github.com/aws/eks-anywhere/pkg/utils/yaml"
)

// DefaultCloudStackUser is the default CloudStackMachingConfig username.
Expand Down Expand Up @@ -65,40 +61,6 @@ func (c *CloudStackMachineConfigGenerate) Name() string {
return c.ObjectMeta.Name
}

func GetCloudStackMachineConfigs(fileName string) (map[string]*CloudStackMachineConfig, error) {
configs := make(map[string]*CloudStackMachineConfig)

r, err := file.ReadFile(fileName)
if err != nil {
return nil, err
}

resources, err := yamlutil.SplitDocuments(r)
if err != nil {
return nil, err
}

for _, d := range resources {
var config CloudStackMachineConfig
if err = yaml.UnmarshalStrict(d, &config); err == nil {
if config.Kind == CloudStackMachineConfigKind {
configs[config.Name] = &config
continue
}
}

_ = yaml.Unmarshal(d, &config) // this is to check if there is a bad spec in the file
if config.Kind == CloudStackMachineConfigKind {
return nil, fmt.Errorf("unable to unmarshall content from file due to: %v", err)
}
}

if len(configs) == 0 {
return nil, fmt.Errorf("unable to find kind %v in file", CloudStackMachineConfigKind)
}
return configs, nil
}

func validateCloudStackMachineConfig(machineConfig *CloudStackMachineConfig) error {
if len(machineConfig.Spec.ComputeOffering.Id) == 0 && len(machineConfig.Spec.ComputeOffering.Name) == 0 {
return fmt.Errorf("computeOffering is not set for CloudStackMachineConfig %s. Default computeOffering is not supported in CloudStack, please provide a computeOffering name or ID", machineConfig.Name)
Expand Down
239 changes: 0 additions & 239 deletions pkg/api/v1alpha1/cloudstackmachineconfig_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package v1alpha1

import (
"fmt"
"reflect"
"testing"

. "github.com/onsi/gomega"
Expand Down Expand Up @@ -40,243 +38,6 @@ var cloudStackMachineConfigSpec1 = &CloudStackMachineConfigSpec{
AffinityGroupIds: []string{"affinityGroupId1"},
}

func TestGetCloudStackMachineConfigs(t *testing.T) {
tests := []struct {
testName string
fileName string
wantCloudStackMachineConfigs map[string]*CloudStackMachineConfig
wantErr bool
}{
{
testName: "file doesn't exist",
fileName: "testdata/fake_file.yaml",
wantCloudStackMachineConfigs: nil,
wantErr: true,
},
{
testName: "not parseable file",
fileName: "testdata/not_parseable_cluster.yaml",
wantCloudStackMachineConfigs: nil,
wantErr: true,
},
{
testName: "non-splitable manifest",
fileName: "testdata/invalid_manifest.yaml",
wantCloudStackMachineConfigs: nil,
wantErr: true,
},
{
testName: "valid 1.19",
fileName: "testdata/cluster_1_19_cloudstack.yaml",
wantCloudStackMachineConfigs: map[string]*CloudStackMachineConfig{
"eksa-unit-test": {
TypeMeta: metav1.TypeMeta{
Kind: CloudStackMachineConfigKind,
APIVersion: SchemeBuilder.GroupVersion.String(),
},
ObjectMeta: metav1.ObjectMeta{
Name: "eksa-unit-test",
},
Spec: CloudStackMachineConfigSpec{
Template: CloudStackResourceIdentifier{
Name: "centos7-k8s-119",
},
ComputeOffering: CloudStackResourceIdentifier{
Name: "m4-large",
},
Users: []UserConfiguration{{
Name: "mySshUsername",
SshAuthorizedKeys: []string{"mySshAuthorizedKey"},
}},
},
},
},
wantErr: false,
},
{
testName: "valid 1.21",
fileName: "testdata/cluster_1_21_cloudstack.yaml",
wantCloudStackMachineConfigs: map[string]*CloudStackMachineConfig{
"eksa-unit-test": {
TypeMeta: metav1.TypeMeta{
Kind: CloudStackMachineConfigKind,
APIVersion: SchemeBuilder.GroupVersion.String(),
},
ObjectMeta: metav1.ObjectMeta{
Name: "eksa-unit-test",
},
Spec: CloudStackMachineConfigSpec{
Template: CloudStackResourceIdentifier{
Id: "centos7-k8s-121-id",
},
ComputeOffering: CloudStackResourceIdentifier{
Id: "m4-large-id",
},
DiskOffering: &CloudStackResourceDiskOffering{
CloudStackResourceIdentifier: CloudStackResourceIdentifier{
Name: "Small",
},
MountPath: "/data-small",
Device: "/dev/vdb",
Filesystem: "ext4",
Label: "data_disk",
},
Users: []UserConfiguration{{
Name: "mySshUsername",
SshAuthorizedKeys: []string{"mySshAuthorizedKey"},
}},
},
},
},
wantErr: false,
},
{
testName: "valid with extra delimiters",
fileName: "testdata/cluster_extra_delimiters_cloudstack.yaml",
wantCloudStackMachineConfigs: map[string]*CloudStackMachineConfig{
"eksa-unit-test": {
TypeMeta: metav1.TypeMeta{
Kind: CloudStackMachineConfigKind,
APIVersion: SchemeBuilder.GroupVersion.String(),
},
ObjectMeta: metav1.ObjectMeta{
Name: "eksa-unit-test",
},
Spec: CloudStackMachineConfigSpec{
Template: CloudStackResourceIdentifier{
Name: "centos7-k8s-118",
},
ComputeOffering: CloudStackResourceIdentifier{
Name: "m4-large",
},
Users: []UserConfiguration{{
Name: "mySshUsername",
SshAuthorizedKeys: []string{"mySshAuthorizedKey"},
}},
},
},
},
wantErr: false,
},
{
testName: "valid 1.20",
fileName: "testdata/cluster_1_20_cloudstack.yaml",
wantCloudStackMachineConfigs: map[string]*CloudStackMachineConfig{
"eksa-unit-test": {
TypeMeta: metav1.TypeMeta{
Kind: CloudStackMachineConfigKind,
APIVersion: SchemeBuilder.GroupVersion.String(),
},
ObjectMeta: metav1.ObjectMeta{
Name: "eksa-unit-test",
},
Spec: CloudStackMachineConfigSpec{
Template: CloudStackResourceIdentifier{
Name: "centos7-k8s-120",
},
ComputeOffering: CloudStackResourceIdentifier{
Name: "m4-large",
},
Users: []UserConfiguration{{
Name: "mySshUsername",
SshAuthorizedKeys: []string{"mySshAuthorizedKey"},
}},
},
},
},
wantErr: false,
},
{
testName: "valid different machine configs",
fileName: "testdata/cluster_different_machine_configs_cloudstack.yaml",
wantCloudStackMachineConfigs: map[string]*CloudStackMachineConfig{
"eksa-unit-test": {
TypeMeta: metav1.TypeMeta{
Kind: CloudStackMachineConfigKind,
APIVersion: SchemeBuilder.GroupVersion.String(),
},
ObjectMeta: metav1.ObjectMeta{
Name: "eksa-unit-test",
},
Spec: CloudStackMachineConfigSpec{
Template: CloudStackResourceIdentifier{
Name: "centos7-k8s-118",
},
ComputeOffering: CloudStackResourceIdentifier{
Name: "m4-large",
},
DiskOffering: &CloudStackResourceDiskOffering{
CloudStackResourceIdentifier: CloudStackResourceIdentifier{
Name: "Small",
},
MountPath: "/data-small",
Device: "/dev/vdb",
Filesystem: "ext4",
Label: "data_disk",
},
Users: []UserConfiguration{{
Name: "mySshUsername",
SshAuthorizedKeys: []string{"mySshAuthorizedKey"},
}},
},
},
"eksa-unit-test-2": {
TypeMeta: metav1.TypeMeta{
Kind: CloudStackMachineConfigKind,
APIVersion: SchemeBuilder.GroupVersion.String(),
},
ObjectMeta: metav1.ObjectMeta{
Name: "eksa-unit-test-2",
},
Spec: CloudStackMachineConfigSpec{
Template: CloudStackResourceIdentifier{
Name: "centos7-k8s-118",
},
ComputeOffering: CloudStackResourceIdentifier{
Name: "m5-xlarge",
},
DiskOffering: &CloudStackResourceDiskOffering{
CloudStackResourceIdentifier: CloudStackResourceIdentifier{
Name: "Medium",
},
MountPath: "/data-medium",
Device: "/dev/vdb",
Filesystem: "ext4",
Label: "data_disk",
},
Users: []UserConfiguration{{
Name: "mySshUsername",
SshAuthorizedKeys: []string{"mySshAuthorizedKey"},
}},
},
},
},
wantErr: false,
},
{
testName: "invalid kind",
fileName: "testdata/cluster_invalid_kinds.yaml",
wantCloudStackMachineConfigs: nil,
wantErr: true,
},
}
for i, tt := range tests {
if i != 2 {
continue
}
t.Run(tt.testName, func(t *testing.T) {
got, err := GetCloudStackMachineConfigs(tt.fileName)
fmt.Println(err)
if (err != nil) != tt.wantErr {
t.Fatalf("GetCloudStackMachineConfigs() error = %v, wantErr %v", err, tt.wantErr)
}
if !reflect.DeepEqual(got, tt.wantCloudStackMachineConfigs) {
t.Fatalf("GetCloudStackMachineConfigs() = %#v, want %#v", got, tt.wantCloudStackMachineConfigs)
}
})
}
}

func TestCloudStackMachineConfigValidate(t *testing.T) {
tests := []struct {
name string
Expand Down
10 changes: 0 additions & 10 deletions pkg/api/v1alpha1/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,16 +204,6 @@ func GetClusterConfig(fileName string) (*Cluster, error) {
return clusterConfig, nil
}

// GetClusterConfigFromContent parses a Cluster object from a multiobject yaml content.
func GetClusterConfigFromContent(content []byte) (*Cluster, error) {
clusterConfig := &Cluster{}
err := ParseClusterConfigFromContent(content, clusterConfig)
if err != nil {
return clusterConfig, err
}
return clusterConfig, nil
}

// GetClusterConfig parses a Cluster object from a multiobject yaml file in disk
// sets defaults if necessary and validates the Cluster.
func GetAndValidateClusterConfig(fileName string) (*Cluster, error) {
Expand Down
Loading

0 comments on commit 20fa44e

Please sign in to comment.