Skip to content

Commit

Permalink
Virtual node checks API server status through the ForeignCluster
Browse files Browse the repository at this point in the history
  • Loading branch information
fra98 authored and adamjensenbot committed May 18, 2023
1 parent 6c350bf commit 77d9f50
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 5 deletions.
16 changes: 16 additions & 0 deletions deployments/liqo/files/liqo-virtual-kubelet-local-ClusterRole.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,22 @@ rules:
- get
- list
- watch
- apiGroups:
- discovery.liqo.io
resources:
- foreignclusters
verbs:
- get
- list
- watch
- apiGroups:
- discovery.liqo.io
resources:
- foreignclusters/status
verbs:
- get
- list
- watch
- apiGroups:
- net.liqo.io
resources:
Expand Down
29 changes: 28 additions & 1 deletion pkg/utils/foreignCluster/getForeignCluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,11 @@ import (
kerrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/selection"
"k8s.io/apimachinery/pkg/types"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
"k8s.io/client-go/dynamic"
"k8s.io/klog/v2"
"sigs.k8s.io/controller-runtime/pkg/client"

Expand All @@ -48,13 +50,38 @@ func GetForeignClusterByID(ctx context.Context, cl client.Client, clusterID stri
return nil, err
}

return getForeignCluster(&foreignClusterList, clusterID)
}

// GetForeignClusterByIDWithDynamicClient returns a ForeignCluster CR retrieving it by its clusterID, using the dynamic interface.
func GetForeignClusterByIDWithDynamicClient(ctx context.Context, dynClient dynamic.Interface, clusterID string) (
*discoveryv1alpha1.ForeignCluster, error) {
lSelector := labels.SelectorFromSet(labels.Set{
discovery.ClusterIDLabel: clusterID,
})
unstr, err := dynClient.Resource(discoveryv1alpha1.ForeignClusterGroupVersionResource).List(ctx, metav1.ListOptions{
LabelSelector: lSelector.String()})
if err != nil {
return nil, err
}

foreignClusterList := discoveryv1alpha1.ForeignClusterList{}
err = runtime.DefaultUnstructuredConverter.FromUnstructured(unstr.UnstructuredContent(), &foreignClusterList)
if err != nil {
return nil, err
}

return getForeignCluster(&foreignClusterList, clusterID)
}

func getForeignCluster(foreignClusterList *discoveryv1alpha1.ForeignClusterList, clusterID string) (*discoveryv1alpha1.ForeignCluster, error) {
switch len(foreignClusterList.Items) {
case 0:
return nil, kerrors.NewNotFound(discoveryv1alpha1.ForeignClusterGroupResource, fmt.Sprintf("foreign cluster with ID %s", clusterID))
case 1:
return &foreignClusterList.Items[0], nil
default:
return GetOlderForeignCluster(&foreignClusterList), nil
return GetOlderForeignCluster(foreignClusterList), nil
}
}

Expand Down
18 changes: 14 additions & 4 deletions pkg/virtualKubelet/liqoNodeProvider/nodeProvider.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package liqonodeprovider

import (
"context"
"fmt"
"sync"
"time"

Expand All @@ -24,6 +25,8 @@ import (
"k8s.io/client-go/dynamic"
"k8s.io/client-go/kubernetes"
"k8s.io/klog/v2"

foreignclusterutils "github.com/liqotech/liqo/pkg/utils/foreignCluster"
)

// LiqoNodeProvider is a node provider that manages the Liqo resources.
Expand All @@ -49,7 +52,7 @@ type LiqoNodeProvider struct {
updateMutex sync.Mutex
}

// Ping checks if the the node is still active.
// Ping checks if the node is still active.
func (p *LiqoNodeProvider) Ping(ctx context.Context) error {
if p.pingDisabled {
return nil
Expand All @@ -58,13 +61,20 @@ func (p *LiqoNodeProvider) Ping(ctx context.Context) error {
start := time.Now()
klog.V(4).Infof("Checking whether the remote API server is ready")

_, err := p.remoteDiscoveryClient.RESTClient().Get().AbsPath("/livez").DoRaw(ctx)
// Get the foreigncluster using the given clusterID
fc, err := foreignclusterutils.GetForeignClusterByIDWithDynamicClient(ctx, p.dynClient, p.foreignClusterID)
if err != nil {
klog.Errorf("API server readiness check failed: %v", err)
klog.Error(err)
return err
}

klog.V(4).Infof("Readiness check completed successfully in %v", time.Since(start))
// Check the foreign API server status
if !foreignclusterutils.IsAPIServerReady(fc) {
return fmt.Errorf("[%s] API server readiness check failed", fc.Spec.ClusterIdentity.ClusterName)
}

klog.V(4).Infof("[%s] API server readiness check completed successfully in %v",
fc.Spec.ClusterIdentity.ClusterName, time.Since(start))
return nil
}

Expand Down
2 changes: 2 additions & 0 deletions pkg/virtualKubelet/roles/local/role.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ package local
// +kubebuilder:rbac:groups=virtualkubelet.liqo.io,resources=namespacemaps,verbs=get;list;watch;
// +kubebuilder:rbac:groups=net.liqo.io,resources=tunnelendpoints,verbs=get;list;watch
// +kubebuilder:rbac:groups=sharing.liqo.io,resources=resourceoffers,verbs=get;list;watch;update;patch;delete
// +kubebuilder:rbac:groups=discovery.liqo.io,resources=foreignclusters,verbs=get;list;watch
// +kubebuilder:rbac:groups=discovery.liqo.io,resources=foreignclusters/status,verbs=get;list;watch

// +kubebuilder:rbac:groups=coordination.k8s.io,resources=leases,verbs=get;create;update;delete

Expand Down

0 comments on commit 77d9f50

Please sign in to comment.