Skip to content

Commit

Permalink
Remove k8s dependencies from core module
Browse files Browse the repository at this point in the history
  • Loading branch information
phillc committed Aug 27, 2020
1 parent e4d9cf2 commit 81fdffc
Show file tree
Hide file tree
Showing 13 changed files with 516 additions and 281 deletions.
21 changes: 12 additions & 9 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -9,27 +9,25 @@ PACKAGES := $(shell go list ./... | grep -v integration)

SKIP_LINT ?= 0

.PHONY: build vet test refresh-fixtures clean-fixtures lint run_fixtures sanitize fixtures godoc testint testunit
.PHONY: build vet test refresh-fixtures clean-fixtures lint run_fixtures sanitize fixtures godoc testint testunit tidy

test: testunit testint
test: build lint testunit testint

citest: lint test

testunit: build lint
testunit:
go test -v $(PACKAGES) $(ARGS)

testint: build lint
@LINODE_FIXTURE_MODE="play" \
LINODE_TOKEN="awesometokenawesometokenawesometoken" \
LINODE_API_VERSION="v4beta" \
GO111MODULE="on" \
go test -v ./test/integration $(ARGS)
testint:
cd test && make test

build: vet lint
go build ./...
cd k8s && go build ./...

vet:
go vet ./...
cd k8s && go vet ./...

lint:
ifeq ($(SKIP_LINT), 1)
Expand Down Expand Up @@ -69,3 +67,8 @@ fixtures: run_fixtures sanitize

godoc:
@godoc -http=:6060

tidy:
go mod tidy
cd k8s && go mod tidy
cd test && go mod tidy
4 changes: 0 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
module github.com/linode/linodego

require (
github.com/dnaeon/go-vcr v1.0.1
github.com/go-resty/resty/v2 v2.1.1-0.20191201195748-d7b97669fe48
github.com/google/go-cmp v0.4.0
golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45
k8s.io/api v0.18.3
k8s.io/apimachinery v0.18.3
k8s.io/client-go v0.18.3
)

go 1.13
188 changes: 4 additions & 184 deletions go.sum

Large diffs are not rendered by default.

19 changes: 9 additions & 10 deletions internal/kubernetes/kubernetes.go → k8s/clientset.go
Original file line number Diff line number Diff line change
@@ -1,32 +1,31 @@
package kubernetes
package k8s

import (
"encoding/base64"
"fmt"

"github.com/linode/linodego"
"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

// NewClientsetFromBytes builds a Clientset from a given Kubeconfig.
//
// Takes an optional transport.WrapperFunc to add request/response middleware to
// api-server requests.
func BuildClientsetFromConfig(
kubeconfigBytes []byte,
lkeKubeconfig *linodego.LKEClusterKubeconfig,
transportWrapper transport.WrapperFunc,
) (Clientset, error) {
config, err := clientcmd.NewClientConfigFromBytes(kubeconfigBytes)
) (kubernetes.Interface, error) {
kubeConfigBytes, err := base64.StdEncoding.DecodeString(lkeKubeconfig.KubeConfig)
if err != nil {
return nil, fmt.Errorf("failed to parse LKE cluster kubeconfig: %s", err)
return nil, fmt.Errorf("failed to decode kubeconfig: %s", err)
}

restClientConfig, err := config.ClientConfig()
restClientConfig, err := clientcmd.RESTConfigFromKubeConfig(kubeConfigBytes)
if err != nil {
return nil, fmt.Errorf("failed to get REST client config: %s", err)
return nil, fmt.Errorf("failed to parse LKE cluster kubeconfig: %s", err)
}

if transportWrapper != nil {
Expand Down
12 changes: 12 additions & 0 deletions k8s/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
module github.com/linode/linodego/k8s

require (
github.com/linode/linodego v0.20.1
k8s.io/api v0.18.3
k8s.io/apimachinery v0.18.3
k8s.io/client-go v0.18.3
)

replace github.com/linode/linodego => ../

go 1.13
195 changes: 195 additions & 0 deletions k8s/go.sum

Large diffs are not rendered by default.

43 changes: 43 additions & 0 deletions k8s/pkg/condition/lke.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package condition

import (
"context"
"errors"
"fmt"

"github.com/linode/linodego"
"github.com/linode/linodego/k8s"
corev1 "k8s.io/api/core/v1"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

// ClusterHasReadyNode is a ClusterConditionFunc which polls for at least one node to have the
// condition NodeReady=True.
func ClusterHasReadyNode(ctx context.Context, options linodego.ClusterConditionOptions) (bool, error) {
clientset, err := k8s.BuildClientsetFromConfig(options.LKEClusterKubeconfig, options.TransportWrapper)
if err != nil {
return false, err
}

nodes, err := clientset.CoreV1().Nodes().List(ctx, v1.ListOptions{})
if err != nil {
return false, fmt.Errorf("failed to get nodes for cluster: %s", err)
}

for _, node := range nodes.Items {
for _, condition := range node.Status.Conditions {
if condition.Type == corev1.NodeReady && condition.Status == corev1.ConditionTrue {
return true, nil
}
}
}

return false, errors.New("no nodes in cluster are ready")
}

// WaitForLKEClusterReady polls with a given timeout for the LKE Cluster's api-server
// to be healthy and for the cluster to have at least one node with the NodeReady
// condition true.
func WaitForLKEClusterReady(ctx context.Context, client linodego.Client, clusterID int, options linodego.LKEClusterPollOptions) error {
return client.WaitForLKEClusterConditions(ctx, clusterID, options, ClusterHasReadyNode)
}
34 changes: 0 additions & 34 deletions pkg/condition/lke.go

This file was deleted.

8 changes: 8 additions & 0 deletions test/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
.PHONY: test

test:
@LINODE_FIXTURE_MODE="play" \
LINODE_TOKEN="awesometokenawesometokenawesometoken" \
LINODE_API_VERSION="v4beta" \
GO111MODULE="on" \
go test -v ./integration $(ARGS)
17 changes: 17 additions & 0 deletions test/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
module test

go 1.13

require (
github.com/dnaeon/go-vcr v1.0.1
github.com/google/go-cmp v0.5.2
github.com/linode/linodego v0.20.1
github.com/linode/linodego/k8s v0.0.0-00010101000000-000000000000
golang.org/x/oauth2 v0.0.0-20200107190931-bf48bf16ab8d
gopkg.in/yaml.v2 v2.3.0 // indirect
k8s.io/client-go v0.18.3
)

replace github.com/linode/linodego => ../

replace github.com/linode/linodego/k8s => ../k8s
Loading

0 comments on commit 81fdffc

Please sign in to comment.