Skip to content

Commit b5a84fc

Browse files
Merge pull request #493 from redpinecube/feature/verify-control-plane-on-hosting-cluster
✨feature : implement VerifyControlPlaneOnHostingCluster
2 parents 51ae42a + 74cdc82 commit b5a84fc

File tree

2 files changed

+64
-4
lines changed

2 files changed

+64
-4
lines changed

pkg/kubeconfig/extensions.go

Lines changed: 62 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,17 @@ limitations under the License.
1717
package kubeconfig
1818

1919
import (
20+
"context"
2021
"encoding/json"
2122
"fmt"
23+
"github.com/go-logr/logr"
24+
tenancyv1alpha1 "github.com/kubestellar/kubeflex/api/v1alpha1"
25+
apierrors "k8s.io/apimachinery/pkg/api/errors"
26+
"k8s.io/client-go/tools/clientcmd"
27+
"sigs.k8s.io/controller-runtime/pkg/client"
2228
"time"
2329

30+
util "github.com/kubestellar/kubeflex/pkg/util"
2431
corev1 "k8s.io/api/core/v1"
2532
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2633
"k8s.io/apimachinery/pkg/runtime"
@@ -39,6 +46,7 @@ const (
3946
DiagnosisStatusWarning = "warning"
4047
DiagnosisStatusOK = "ok"
4148
DiagnosisStatusMissing = "no kubeflex extension found"
49+
LabelSelectorControlPlane = "app.kubernetes.io/component=control-plane"
4250
)
4351

4452
// Internal structure of Kubeflex global extension in a Kubeconfig file
@@ -234,20 +242,70 @@ func CheckHostingClusterContextName(kconf clientcmdapi.Config) string {
234242
}
235243
}
236244

245+
func GetControlPlaneByContextName(kconf clientcmdapi.Config, ctxName string) string {
246+
ctx, ok := kconf.Contexts[ctxName]
247+
if !ok {
248+
return DiagnosisStatusMissing
249+
}
250+
ext, ok := ctx.Extensions[ExtensionKubeflexKey]
251+
if !ok {
252+
return DiagnosisStatusMissing
253+
}
254+
ctxExtension := &RuntimeKubeflexExtension{}
255+
if err := ConvertRuntimeObjectToRuntimeExtension(ext, ctxExtension); err != nil {
256+
return DiagnosisStatusCritical
257+
}
258+
return ctxExtension.Data[ExtensionControlPlaneName]
259+
}
260+
237261
func VerifyControlPlaneOnHostingCluster(kconf clientcmdapi.Config, ctxName string) string {
238-
// TODO: implement actual control plane verification logic
239-
return DiagnosisStatusOK
262+
cpName := GetControlPlaneByContextName(kconf, ctxName)
263+
if cpName == "" {
264+
return DiagnosisStatusMissing
265+
}
266+
267+
clientConfig := clientcmd.NewDefaultClientConfig(kconf, &clientcmd.ConfigOverrides{
268+
CurrentContext: kconf.CurrentContext,
269+
})
270+
271+
restClient, err := clientConfig.ClientConfig()
272+
if err != nil {
273+
return DiagnosisStatusCritical
274+
}
275+
276+
runtimeClient, err := client.New(restClient, client.Options{})
277+
if err != nil {
278+
return DiagnosisStatusCritical
279+
}
280+
281+
controlPlane := &tenancyv1alpha1.ControlPlane{}
282+
if err := runtimeClient.Get(context.TODO(), client.ObjectKey{Name: cpName}, controlPlane); err != nil {
283+
if apierrors.IsNotFound(err) {
284+
return DiagnosisStatusMissing
285+
}
286+
return DiagnosisStatusCritical
287+
}
288+
289+
ready, err := util.IsAPIServerDeploymentReady(logr.Discard(), runtimeClient, *controlPlane)
290+
if err != nil {
291+
return DiagnosisStatusCritical
292+
}
293+
294+
if ready {
295+
return DiagnosisStatusOK
296+
}
297+
return DiagnosisStatusCritical
240298
}
241299

242300
func CheckContextScopeKubeflexExtensionSet(kconf clientcmdapi.Config, ctxName string) string {
243301
ctx, ok := kconf.Contexts[ctxName]
244302
if !ok {
245-
return DiagnosisStatusMissing // Context not found
303+
return DiagnosisStatusMissing
246304
}
247305

248306
ext, ok := ctx.Extensions[ExtensionKubeflexKey]
249307
if !ok {
250-
return DiagnosisStatusMissing // No kubeflex extension found
308+
return DiagnosisStatusMissing
251309
}
252310

253311
ctxExtension := &RuntimeKubeflexExtension{}

pkg/kubeconfig/extensions_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,8 @@ func TestCheckHostingClusterContextNameSingle(t *testing.T) {
206206
}
207207
}
208208

209+
210+
209211
func TestCheckContextScopeKubeflexExtensionSetNoKubeflexExtensions(t *testing.T) {
210212
kconf := api.NewConfig()
211213
kconf.Clusters["cluster1"] = &api.Cluster{Server: "https://example.com:6443"}

0 commit comments

Comments
 (0)