Bladeren bron

Kcm-4679: K8s version information in cluster info meta provider (#3398)

Signed-off-by: Alan Rodrigues <alanrodrigues@Alans-MacBook-Pro.local>
Signed-off-by: Alan Rodrigues <alanrodrigues@Mac.attlocal.net>
Co-authored-by: Alan Rodrigues <alanrodrigues@Mac.attlocal.net>
Co-authored-by: Alan Rodrigues <alanrodrigues@Alans-MacBook-Pro.local>
Alan Rodrigues 7 maanden geleden
bovenliggende
commit
a423d35408
3 gewijzigde bestanden met toevoegingen van 101 en 0 verwijderingen
  1. 2 0
      core/pkg/clusters/clusterinfo.go
  2. 6 0
      core/pkg/clusters/util.go
  3. 93 0
      core/pkg/clusters/util_test.go

+ 2 - 0
core/pkg/clusters/clusterinfo.go

@@ -31,6 +31,7 @@ type ClusterInfo struct {
 	Project     string `json:"project"`
 	Region      string `json:"region"`
 	Provisioner string `json:"provisioner"`
+	Version     string `json:"version"`
 }
 
 // Clone creates a copy of ClusterInfo and returns it
@@ -48,6 +49,7 @@ func (ci *ClusterInfo) Clone() *ClusterInfo {
 		Project:     ci.Project,
 		Region:      ci.Region,
 		Provisioner: ci.Provisioner,
+		Version:     ci.Version,
 	}
 }
 

+ 6 - 0
core/pkg/clusters/util.go

@@ -25,6 +25,7 @@ func MapToClusterInfo(info map[string]string) (*ClusterInfo, error) {
 	var project string
 	var region string
 	var provisioner string
+	var version string
 
 	if cp, ok := info[ClusterInfoProfileKey]; ok {
 		clusterProfile = cp
@@ -50,6 +51,10 @@ func MapToClusterInfo(info map[string]string) (*ClusterInfo, error) {
 		provisioner = pvsr
 	}
 
+	if ver, ok := info[ClusterInfoVersionKey]; ok {
+		version = ver
+	}
+
 	return &ClusterInfo{
 		ID:          id,
 		Name:        name,
@@ -59,5 +64,6 @@ func MapToClusterInfo(info map[string]string) (*ClusterInfo, error) {
 		Project:     project,
 		Region:      region,
 		Provisioner: provisioner,
+		Version:     version,
 	}, nil
 }

+ 93 - 0
core/pkg/clusters/util_test.go

@@ -0,0 +1,93 @@
+package clusters
+
+import "testing"
+
+const (
+	testClusterInfoIDKey      = "testClusterID"
+	testClusterInfoNameKey    = "testClusterName"
+	testClusterProfileKey     = "testProfile"
+	testClusterProviderKey    = "testProvider"
+	testClusterAccountKey     = "testAccount"
+	testClusterProjectKey     = "testProject"
+	testClusterRegionKey      = "testRegion"
+	testClusterProvisionerKey = "testProvisioner"
+	testClusterVersionKey     = "testVersion"
+)
+
+func TestMapToClusterInfo(t *testing.T) {
+	mapWOVersion := map[string]string{
+		ClusterInfoIdKey:          testClusterInfoIDKey,
+		ClusterInfoNameKey:        testClusterInfoNameKey,
+		ClusterInfoProfileKey:     testClusterProfileKey,
+		ClusterInfoProviderKey:    testClusterProviderKey,
+		ClusterInfoAccountKey:     testClusterAccountKey,
+		ClusterInfoProjectKey:     testClusterProjectKey,
+		ClusterInfoRegionKey:      testClusterRegionKey,
+		ClusterInfoProvisionerKey: testClusterProvisionerKey,
+	}
+	expectedCIwoVersion := ClusterInfo{
+		ID:          testClusterInfoIDKey,
+		Name:        testClusterInfoNameKey,
+		Profile:     testClusterProfileKey,
+		Provider:    testClusterProviderKey,
+		Account:     testClusterAccountKey,
+		Project:     testClusterProjectKey,
+		Region:      testClusterRegionKey,
+		Provisioner: testClusterProvisionerKey,
+	}
+	mapWVersion := map[string]string{
+		ClusterInfoIdKey:          testClusterInfoIDKey,
+		ClusterInfoNameKey:        testClusterInfoNameKey,
+		ClusterInfoProfileKey:     testClusterProfileKey,
+		ClusterInfoProviderKey:    testClusterProviderKey,
+		ClusterInfoAccountKey:     testClusterAccountKey,
+		ClusterInfoProjectKey:     testClusterProjectKey,
+		ClusterInfoRegionKey:      testClusterRegionKey,
+		ClusterInfoProvisionerKey: testClusterProvisionerKey,
+		ClusterInfoVersionKey:     testClusterVersionKey,
+	}
+	expectedCIwVersion := ClusterInfo{
+		ID:          testClusterInfoIDKey,
+		Name:        testClusterInfoNameKey,
+		Profile:     testClusterProfileKey,
+		Provider:    testClusterProviderKey,
+		Account:     testClusterAccountKey,
+		Project:     testClusterProjectKey,
+		Region:      testClusterRegionKey,
+		Provisioner: testClusterProvisionerKey,
+		Version:     testClusterVersionKey,
+	}
+	tests := []struct {
+		name     string
+		input    map[string]string
+		expected ClusterInfo
+		wantErr  bool
+	}{
+		{
+			name:     "when version is not in the cluster info map",
+			input:    mapWOVersion,
+			expected: expectedCIwoVersion,
+			wantErr:  false,
+		},
+		{
+			name:     "when version is in the cluster info map",
+			input:    mapWVersion,
+			expected: expectedCIwVersion,
+			wantErr:  false,
+		},
+	}
+
+	for _, tc := range tests {
+		t.Run(tc.name, func(t *testing.T) {
+			returnCI, err := MapToClusterInfo(tc.input)
+			if (err != nil) != tc.wantErr {
+				t.Errorf("MapToClusterInfo() error = %v, wantErr %v", err, tc.wantErr)
+				return
+			}
+			if *returnCI != tc.expected {
+				t.Errorf("MapToClusterInfo() expected = %v, got %v", tc.expected, returnCI)
+				return
+			}
+		})
+	}
+}