Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 56 additions & 5 deletions pkg/kubeconfig/extensions.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ package kubeconfig
import (
"encoding/json"
"fmt"
tenancyv1alpha1 "github.com/kubestellar/kubeflex/api/v1alpha1"
"github.com/kubestellar/kubeflex/cmd/kflex/common"
"github.com/kubestellar/kubeflex/pkg/client"
"k8s.io/client-go/tools/clientcmd"
"time"

corev1 "k8s.io/api/core/v1"
Expand Down Expand Up @@ -234,12 +238,32 @@ func CheckHostingClusterContextName(kconf clientcmdapi.Config) string {
}
}

func VerifyControlPlaneOnHostingCluster(kconf clientcmdapi.Config, ctxName string) string {
// TODO: implement actual control plane verification logic
return DiagnosisStatusOK
func VerifyControlPlaneOnHostingCluster(cp common.CP, ctxName string) string {
c, err := client.GetClient(cp.Kubeconfig)
if err != nil {
return DiagnosisStatusCritical
}

var controlPlanes tenancyv1alpha1.ControlPlaneList
if err := c.List(cp.Ctx, &controlPlanes); err != nil {
return DiagnosisStatusCritical
}

for _, controlPlane := range controlPlanes.Items {
if controlPlane.Name == ctxName {
return DiagnosisStatusOK
}
}

return DiagnosisStatusMissing
}

func CheckContextScopeKubeflexExtensionSet(kconf clientcmdapi.Config, ctxName string) string {
func CheckContextScopeKubeflexExtensionSet(cp common.CP, ctxName string) string {
kconf, err := clientcmd.LoadFromFile(cp.Kubeconfig)
if err != nil {
return DiagnosisStatusCritical
}

ctx, ok := kconf.Contexts[ctxName]
if !ok {
return DiagnosisStatusMissing // Context not found
Expand Down Expand Up @@ -269,10 +293,37 @@ func CheckContextScopeKubeflexExtensionSet(kconf clientcmdapi.Config, ctxName st
return DiagnosisStatusWarning
}

status := VerifyControlPlaneOnHostingCluster(kconf, ctxName)
status := VerifyControlPlaneOnHostingCluster(cp, ctxName)
if status != DiagnosisStatusOK {
return status
}

return DiagnosisStatusOK
}

func CountKubeflexControlPlaneContexts(kconf clientcmdapi.Config) int {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the point of counting cp?

count := 0

for _, ctx := range kconf.Contexts {
ext := ctx.Extensions
if ext == nil {
continue
}

runtimeObj, ok := ext[ExtensionKubeflexKey]
if !ok {
continue
}

var extData RuntimeKubeflexExtension
if err := ConvertRuntimeObjectToRuntimeExtension(runtimeObj, &extData); err != nil {
continue
}

if cpName, ok := extData.Data[ExtensionControlPlaneName]; ok && cpName != "" {
count++
}
}

return count
}
Comment on lines +304 to +329
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please review all comments listed on this PR #493

42 changes: 39 additions & 3 deletions pkg/kubeconfig/extensions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ package kubeconfig

import (
"fmt"
"github.com/kubestellar/kubeflex/cmd/kflex/common"
"k8s.io/client-go/tools/clientcmd"
"os"
"testing"

"k8s.io/apimachinery/pkg/runtime"
Expand Down Expand Up @@ -206,6 +209,27 @@ func TestCheckHostingClusterContextNameSingle(t *testing.T) {
}
}

func writeKubeconfigToTempFile(t *testing.T, kconf *api.Config) string {
data, err := clientcmd.Write(*kconf)
if err != nil {
t.Fatalf("failed to serialize kubeconfig: %v", err)
}

tmpfile, err := os.CreateTemp("", "kubeconfig-*.yaml")
if err != nil {
t.Fatalf("failed to create temp file: %v", err)
}

if _, err := tmpfile.Write(data); err != nil {
t.Fatalf("failed to write kubeconfig: %v", err)
}
if err := tmpfile.Close(); err != nil {
t.Fatalf("failed to close temp file: %v", err)
}

return tmpfile.Name()
}

func TestCheckContextScopeKubeflexExtensionSetNoKubeflexExtensions(t *testing.T) {
kconf := api.NewConfig()
kconf.Clusters["cluster1"] = &api.Cluster{Server: "https://example.com:6443"}
Expand All @@ -216,7 +240,11 @@ func TestCheckContextScopeKubeflexExtensionSetNoKubeflexExtensions(t *testing.T)
}
kconf.CurrentContext = "ctx1"

result := CheckContextScopeKubeflexExtensionSet(*kconf, "ctx1")
tmpFile := writeKubeconfigToTempFile(t, kconf)
defer os.Remove(tmpFile)

cp := common.NewCP(tmpFile)
result := CheckContextScopeKubeflexExtensionSet(cp, "ctx1")
if result != DiagnosisStatusMissing {
t.Errorf("Expected %s, got %s", DiagnosisStatusMissing, result)
}
Expand All @@ -237,7 +265,11 @@ func TestCheckContextScopeKubeflexExtensionSetNoData(t *testing.T) {
}
kconf.CurrentContext = "ctx1"

result := CheckContextScopeKubeflexExtensionSet(*kconf, "ctx1")
tmpFile := writeKubeconfigToTempFile(t, kconf)
defer os.Remove(tmpFile)

cp := common.NewCP(tmpFile)
result := CheckContextScopeKubeflexExtensionSet(cp, "ctx1")
if result != DiagnosisStatusCritical {
t.Errorf("Expected %s, got %s", DiagnosisStatusCritical, result)
}
Expand All @@ -258,7 +290,11 @@ func TestCheckContextScopeKubeflexExtensionSetPartialData(t *testing.T) {
}
kconf.CurrentContext = "ctx1"

result := CheckContextScopeKubeflexExtensionSet(*kconf, "ctx1")
tmpFile := writeKubeconfigToTempFile(t, kconf)
defer os.Remove(tmpFile)

cp := common.NewCP(tmpFile)
result := CheckContextScopeKubeflexExtensionSet(cp, "ctx1")
if result != DiagnosisStatusWarning {
t.Errorf("Expected %s, got %s", DiagnosisStatusWarning, result)
}
Expand Down