Skip to content

Commit

Permalink
move all direct k8s.io/* references to internal/kubernetes
Browse files Browse the repository at this point in the history
  • Loading branch information
0xch4z committed Apr 28, 2020
1 parent 6851a56 commit e0f96e8
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 53 deletions.
50 changes: 50 additions & 0 deletions internal/kubernetes/kubernetes.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package kubernetes

import (
"context"
"encoding/base64"
"fmt"

"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/clientcmd"
"k8s.io/client-go/transport"
)

// Clientset is an alias to k8s.io/client-go/kubernetes.Interface
type Clientset kubernetes.Interface

// buildClientsetFromConfigBytes builds a Clientset from a given Kubeconfig
// from the Linode API.
//
// Takes an optional transport.WrapperFunc to add request/response middleware to
// api-server requests.
func BuildClientsetFromConfigBytes(
ctx context.Context,
kubeconfig string,
transportWrapper transport.WrapperFunc,
) (Clientset, error) {
kubeConfigBytes, err := base64.StdEncoding.DecodeString(kubeconfig)
if err != nil {
return nil, fmt.Errorf("failed to decode kubeconfig: %s", err)
}

config, err := clientcmd.NewClientConfigFromBytes(kubeConfigBytes)
if err != nil {
return nil, fmt.Errorf("failed to parse LKE cluster kubeconfig: %s", err)
}

restClientConfig, err := config.ClientConfig()
if err != nil {
return nil, fmt.Errorf("failed to get REST client config: %s", err)
}

if transportWrapper != nil {
restClientConfig.Wrap(transportWrapper)
}

clientset, err := kubernetes.NewForConfig(restClientConfig)
if err != nil {
return nil, fmt.Errorf("failed to build k8s client from LKE cluster kubeconfig: %s", err)
}
return clientset, nil
}
45 changes: 0 additions & 45 deletions lke_clusters.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,11 @@ package linodego

import (
"context"
"encoding/base64"
"encoding/json"
"fmt"
"time"

"github.com/linode/linodego/internal/parseabletime"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/clientcmd"
"k8s.io/client-go/transport"
)

// LKEClusterStatus represents the status of an LKECluster
Expand Down Expand Up @@ -281,44 +277,3 @@ func (c *Client) ListLKEVersions(ctx context.Context, opts *ListOptions) ([]LKEV
}
return response.Data, nil
}

// buildK8sClientsetForLKECluster fetches the kubeconfig for the given cluster and
// builds a *kubernetes.Clientset from it.
//
// Takes an optional transport.WrapperFunc to add request/response middleware to
// api-server requests.
func (c *Client) buildK8sClientsetForLKECluster(
ctx context.Context,
clusterID int,
transportWrapper transport.WrapperFunc,
) (*kubernetes.Clientset, error) {
resp, err := c.GetLKEClusterKubeconfig(ctx, clusterID)
if err != nil {
return nil, fmt.Errorf("failed to resolve kubeconfig: %s", err)
}

kubeConfigBytes, err := base64.StdEncoding.DecodeString(resp.KubeConfig)
if err != nil {
return nil, fmt.Errorf("failed to decode kubeconfig: %s", err)
}

config, err := clientcmd.NewClientConfigFromBytes(kubeConfigBytes)
if err != nil {
return nil, fmt.Errorf("failed to parse LKE cluster kubeconfig: %s", err)
}

restClientConfig, err := config.ClientConfig()
if err != nil {
return nil, fmt.Errorf("failed to get REST client config: %s", err)
}

if transportWrapper != nil {
restClientConfig.Wrap(transportWrapper)
}

clientset, err := kubernetes.NewForConfig(restClientConfig)
if err != nil {
return nil, fmt.Errorf("failed to build k8s client from LKE cluster kubeconfig: %s", err)
}
return clientset, nil
}
6 changes: 3 additions & 3 deletions pkg/condition/lke.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,18 @@ import (
"errors"
"fmt"

"github.com/linode/linodego/internal/kubernetes"
corev1 "k8s.io/api/core/v1"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
)

// ClusterConditionFunc represents a function that tests a condition against an LKE cluster,
// returns true if the condition has been reached, false if it has not yet been reached.
type ClusterConditionFunc func(context.Context, *kubernetes.Clientset) (bool, error)
type ClusterConditionFunc func(context.Context, kubernetes.Clientset) (bool, error)

// ClusterHasReadyNode is a ClusterConditionFunc which polls for at least one node to have the
// condition NodeReady=True.
func ClusterHasReadyNode(ctx context.Context, clientset *kubernetes.Clientset) (bool, error) {
func ClusterHasReadyNode(ctx context.Context, clientset kubernetes.Clientset) (bool, error) {
nodes, err := clientset.CoreV1().Nodes().List(v1.ListOptions{})
if err != nil {
return false, fmt.Errorf("failed to get nodes for cluster: %s", err)
Expand Down
14 changes: 9 additions & 5 deletions waitfor.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import (
"strings"
"time"

"github.com/linode/linodego/internal/kubernetes"
"github.com/linode/linodego/pkg/condition"
"k8s.io/client-go/kubernetes"
)

// WaitForInstanceStatus waits for the Linode instance to reach the desired state
Expand Down Expand Up @@ -219,12 +219,16 @@ func (client Client) WaitForLKEClusterConditions(
defer ticker.Stop()

var prevLog string
var clientset *kubernetes.Clientset
var clientset kubernetes.Clientset

clientReady := func(ctx context.Context, c *kubernetes.Clientset) (bool, error) {
clientReady := func(ctx context.Context, c kubernetes.Clientset) (bool, error) {
if clientset == nil {
var err error
clientset, err = client.buildK8sClientsetForLKECluster(ctx, clusterID, options.TransportWrapper)
resp, err := client.GetLKEClusterKubeconfig(ctx, clusterID)
if err != nil {
return false, fmt.Errorf("failed to get Kubeconfig for LKE cluster %d: %s", clusterID, err)
}

clientset, err = kubernetes.BuildClientsetFromConfigBytes(ctx, resp.KubeConfig, options.TransportWrapper)
if err != nil {
return false, fmt.Errorf("failed to build client for LKE cluster %d: %s", clusterID, err)
}
Expand Down

0 comments on commit e0f96e8

Please sign in to comment.