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 {
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
}
143 changes: 140 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,8 +290,113 @@ 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)
}
}

func TestCountKubeflexControlPlaneContextsZero(t *testing.T) {
kconf := api.NewConfig()
kconf.Clusters["cluster1"] = &api.Cluster{Server: "https://example.com:6443"}
kconf.AuthInfos["user1"] = &api.AuthInfo{Token: "token"}

ext := NewRuntimeKubeflexExtension()
ext.Data[ExtensionContextsIsHostingCluster] = "true"

kconf.Contexts["ctx1"] = &api.Context{
Cluster: "cluster1",
AuthInfo: "user1",
Extensions: map[string]runtime.Object{ExtensionKubeflexKey: ext},
}

expected := 0
got := CountKubeflexControlPlaneContexts(*kconf)
if got != expected {
t.Errorf("Expected %d control plane context(s), got %d", expected, got)
}
}

func TestCountKubeflexControlPlaneContextsOne(t *testing.T) {
kconf := api.NewConfig()
kconf.Clusters["cluster1"] = &api.Cluster{Server: "https://example.com:6443"}
kconf.Clusters["cluster2"] = &api.Cluster{Server: "https://example2.com:6443"}
kconf.AuthInfos["user1"] = &api.AuthInfo{Token: "token"}

hostExt := NewRuntimeKubeflexExtension()
hostExt.Data[ExtensionContextsIsHostingCluster] = "true"
kconf.Contexts["ctx1"] = &api.Context{
Cluster: "cluster1",
AuthInfo: "user1",
Extensions: map[string]runtime.Object{ExtensionKubeflexKey: hostExt},
}

// Control plane
cpExt := NewRuntimeKubeflexExtension()
cpExt.Data[ExtensionControlPlaneName] = "control-plane-1"
kconf.Contexts["ctx2"] = &api.Context{
Cluster: "cluster2",
AuthInfo: "user1",
Extensions: map[string]runtime.Object{ExtensionKubeflexKey: cpExt},
}

expected := 1
got := CountKubeflexControlPlaneContexts(*kconf)
if got != expected {
t.Errorf("Expected %d control plane context(s), got %d", expected, got)
}
}

func TestCountKubeflexControlPlaneContextsMultiple(t *testing.T) {
kconf := api.NewConfig()
kconf.Clusters["cluster1"] = &api.Cluster{Server: "https://example.com:6443"}
kconf.Clusters["cluster2"] = &api.Cluster{Server: "https://example2.com:6443"}
kconf.Clusters["cluster3"] = &api.Cluster{Server: "https://example3.com:6443"}
kconf.Clusters["cluster4"] = &api.Cluster{Server: "https://example4.com:6443"}
kconf.AuthInfos["user1"] = &api.AuthInfo{Token: "token"}

hostExt := NewRuntimeKubeflexExtension()
hostExt.Data[ExtensionContextsIsHostingCluster] = "true"
kconf.Contexts["ctx1"] = &api.Context{
Cluster: "cluster1",
AuthInfo: "user1",
Extensions: map[string]runtime.Object{ExtensionKubeflexKey: hostExt},
}

// Control plane 1
cpExt1 := NewRuntimeKubeflexExtension()
cpExt1.Data[ExtensionControlPlaneName] = "cp-1"
kconf.Contexts["ctx2"] = &api.Context{
Cluster: "cluster2",
AuthInfo: "user1",
Extensions: map[string]runtime.Object{ExtensionKubeflexKey: cpExt1},
}

// Control plane 2
cpExt2 := NewRuntimeKubeflexExtension()
cpExt2.Data[ExtensionControlPlaneName] = "cp-2"
kconf.Contexts["ctx3"] = &api.Context{
Cluster: "cluster3",
AuthInfo: "user1",
Extensions: map[string]runtime.Object{ExtensionKubeflexKey: cpExt2},
}

// Control plane 3
cpExt3 := NewRuntimeKubeflexExtension()
cpExt3.Data[ExtensionControlPlaneName] = "cp-3"
kconf.Contexts["ctx4"] = &api.Context{
Cluster: "cluster4",
AuthInfo: "user1",
Extensions: map[string]runtime.Object{ExtensionKubeflexKey: cpExt3},
}

expected := 3
got := CountKubeflexControlPlaneContexts(*kconf)
if got != expected {
t.Errorf("Expected %d control plane context(s), got %d", expected, got)
}
}