Skip to content

Commit

Permalink
feat: strip KIND dependencies and use kind CLI (#491)
Browse files Browse the repository at this point in the history
* strip KIND dependencies

* fix unit tests

* fix e2e test

* more fixes

* skip linter for linode provider

* more fixes

* more fixes

* more fixes

* more fixes

* more fixes

* more fixes

* more fixes
  • Loading branch information
zreigz authored Feb 16, 2024
1 parent 4010793 commit efc34ee
Show file tree
Hide file tree
Showing 16 changed files with 105 additions and 254 deletions.
8 changes: 4 additions & 4 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -381,19 +381,19 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-go@v4.1.0
- uses: actions/setup-go@v5
with:
go-version-file: go.mod
- run: make test
lint:
name: Lint
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-go@v4.1.0
- uses: actions/checkout@v4
- uses: actions/setup-go@v5
with:
go-version-file: go.mod
- name: golangci-lint
uses: golangci/golangci-lint-action@v3.7.0
uses: golangci/golangci-lint-action@v4
with:
version: v1.54.2
9 changes: 7 additions & 2 deletions .github/workflows/e2e.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,14 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3
uses: actions/checkout@v4
- name: Install GO
uses: actions/setup-go@v4
with:
go-version-file: go.mod
cache: false
- name: Create kind cluster
uses: helm/kind-action@v1.8.0
uses: helm/kind-action@v1.9.0
with:
install_only: true
- run: |
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ BUILD ?= $(shell git rev-parse --short HEAD)
DKR_HOST ?= dkr.plural.sh
GOOS ?= darwin
GOARCH ?= arm64
GOLANG_CROSS_VERSION ?= v1.20.2
GOLANG_CROSS_VERSION ?= v1.22.0
PACKAGE ?= github.com/pluralsh/plural
BASE_LDFLAGS ?= -s -w
LDFLAGS ?= $(BASE_LDFLAGS) $\
Expand Down
83 changes: 42 additions & 41 deletions cmd/plural/bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,15 @@ package plural
import (
"context"
"fmt"
"os"
"path"
"strings"
"time"

"github.com/pkg/errors"
"github.com/pluralsh/plural-cli/pkg/kubernetes"
"github.com/pluralsh/plural-cli/pkg/manifest"
"github.com/pluralsh/plural-cli/pkg/provider"
"github.com/pluralsh/plural-cli/pkg/utils"
"github.com/urfave/cli"
corev1 "k8s.io/api/core/v1"
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
Expand All @@ -21,13 +26,6 @@ import (
clusterapi "sigs.k8s.io/cluster-api/api/v1beta1"
apiclient "sigs.k8s.io/cluster-api/cmd/clusterctl/client"
ctrlruntimeclient "sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/kind/pkg/cluster"

"github.com/pluralsh/plural-cli/pkg/manifest"

"github.com/pluralsh/plural-cli/pkg/kubernetes"
"github.com/pluralsh/plural-cli/pkg/provider"
"github.com/pluralsh/plural-cli/pkg/utils"
)

var runtimescheme = runtime.NewScheme()
Expand All @@ -39,6 +37,18 @@ func init() {
utilruntime.Must(clusterapioperator.AddToScheme(runtimescheme))
}

const (
kindConfig = `kind: Cluster
apiVersion: kind.x-k8s.io/v1alpha4
networking:
ipFamily: dual
nodes:
- role: control-plane
extraMounts:
- hostPath: /var/run/docker.sock
containerPath: /var/run/docker.sock`
)

func (p *Plural) bootstrapCommands() []cli.Command {
return []cli.Command{
{
Expand Down Expand Up @@ -87,13 +97,13 @@ func (p *Plural) bootstrapClusterCommands() []cli.Command {
Usage: "skip creating when cluster exists",
},
},
Action: latestVersion(requireArgs(handleCreateCluster, []string{"NAME"})),
Action: latestVersion(requireKind(requireArgs(handleCreateCluster, []string{"NAME"}))),
},
{
Name: "delete",
ArgsUsage: "NAME",
Usage: "Deletes bootstrap cluster",
Action: latestVersion(requireArgs(handleDeleteCluster, []string{"NAME"})),
Action: latestVersion(requireKind(requireArgs(handleDeleteCluster, []string{"NAME"}))),
},
{
Name: "move",
Expand Down Expand Up @@ -254,62 +264,53 @@ func (p *Plural) handleCreateNamespace(c *cli.Context) error {

func handleDeleteCluster(c *cli.Context) error {
name := c.Args().Get(0)
provider := cluster.NewProvider()
utils.Highlight("Deleting cluster %s ...\n", name)
return provider.Delete(name, "")
return utils.Exec("kind", "delete", "cluster", "--name", name)
}

func handleCreateCluster(c *cli.Context) error {
name := c.Args().Get(0)
imageFlag := c.String("image")
skipCreation := c.Bool("skip-if-exists")
provider := cluster.NewProvider()
utils.Highlight("Creating cluster %s ...\n", name)
n, err := provider.ListNodes(name)
if utils.IsKindClusterAlreadyExists(name) && skipCreation {
utils.Highlight("Cluster %s already exists \n", name)
return nil
}

dir, err := os.MkdirTemp("", "kind")
if err != nil {
return err
}
if len(n) != 0 && skipCreation {
utils.Highlight("Cluster %s already exists \n", name)
return nil
defer os.RemoveAll(dir)
config := path.Join(dir, "config.yaml")
if err := os.WriteFile(config, []byte(kindConfig), 0644); err != nil {
return err
}
if err := provider.Create(
name,
cluster.CreateWithNodeImage(imageFlag),
cluster.CreateWithRetain(false),
cluster.CreateWithDisplayUsage(true),
cluster.CreateWithDisplaySalutation(true),
cluster.CreateWithRawConfig([]byte(`kind: Cluster
apiVersion: kind.x-k8s.io/v1alpha4
networking:
ipFamily: dual
nodes:
- role: control-plane
extraMounts:
- hostPath: /var/run/docker.sock
containerPath: /var/run/docker.sock
`)),
); err != nil {
return errors.Wrap(err, "failed to create cluster")
args := []string{"create", "cluster", "--name", name, "--config", config}
if imageFlag != "" {
args = append(args, "--image", imageFlag)
}

if err := utils.Exec("kind", args...); err != nil {
return err
}
kubeconfig, err := provider.KubeConfig(name, false)

kubeconfig, err := utils.GetKindClusterKubeconfig(name, false)
if err != nil {
return err
}

client, err := getClient(kubeconfig)
if err != nil {
return err
}

if err := client.Create(context.Background(), &corev1.Namespace{
ObjectMeta: metav1.ObjectMeta{
Name: "bootstrap",
},
}); err != nil {
return err
}

internalKubeconfig, err := provider.KubeConfig(name, true)
internalKubeconfig, err := utils.GetKindClusterKubeconfig(name, true)
if err != nil {
return err
}
Expand Down
1 change: 1 addition & 0 deletions cmd/plural/cd_contexts.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package plural
import (
"encoding/json"
"fmt"

gqlclient "github.com/pluralsh/console-client-go"
"github.com/pluralsh/plural-cli/pkg/console"
"github.com/pluralsh/plural-cli/pkg/utils"
Expand Down
45 changes: 0 additions & 45 deletions cmd/plural/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package plural

import (
"bytes"
"encoding/json"
"io"
"os"

Expand All @@ -12,11 +11,8 @@ import (
"github.com/pluralsh/plural-cli/pkg/config"
lua "github.com/pluralsh/plural-cli/pkg/scaffold/template"
"github.com/pluralsh/plural-cli/pkg/template"
"github.com/pluralsh/plural-operator/apis/platform/v1alpha1"
"github.com/urfave/cli"
"gopkg.in/yaml.v2"
k8sjson "k8s.io/apimachinery/pkg/runtime/serializer/json"
"k8s.io/client-go/kubernetes/scheme"
)

func testTemplate(c *cli.Context) error {
Expand Down Expand Up @@ -107,44 +103,3 @@ type GrafanaDashboard struct {
}
}
}

func formatDashboard(c *cli.Context) error {
if err := v1alpha1.AddToScheme(scheme.Scheme); err != nil {
return err
}
s := k8sjson.NewYAMLSerializer(k8sjson.DefaultMetaFactory, scheme.Scheme,
scheme.Scheme)

dashboard := v1alpha1.Dashboard{}
grafana := GrafanaDashboard{}
data, err := io.ReadAll(os.Stdin)
if err != nil {
return err
}

if err := json.Unmarshal(data, &grafana); err != nil {
return err
}

graphs := make([]*v1alpha1.DashboardGraph, 0)
for _, panel := range grafana.Panels {
graph := &v1alpha1.DashboardGraph{}
graph.Name = panel.Title
graph.Queries = make([]*v1alpha1.GraphQuery, 0)
for _, target := range panel.Targets {
query := &v1alpha1.GraphQuery{
Query: target.Expr,
Legend: target.LegendFormat,
}
graph.Queries = append(graph.Queries, query)
}
graphs = append(graphs, graph)
}

dashboard.Spec.Graphs = graphs
dashboard.Spec.Timeslices = []string{"1h", "2h", "6h", "1d", "7d"}
dashboard.Spec.DefaultTime = "1h"
dashboard.Spec.Name = grafana.Title

return s.Encode(&dashboard, os.Stdout)
}
88 changes: 0 additions & 88 deletions cmd/plural/utils_test.go

This file was deleted.

16 changes: 13 additions & 3 deletions cmd/plural/validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@ import (
"os"

"github.com/AlecAivazis/survey/v2"
"github.com/pluralsh/polly/algorithms"
"github.com/urfave/cli"

"github.com/pluralsh/plural-cli/pkg/api"
"github.com/pluralsh/plural-cli/pkg/config"
"github.com/pluralsh/plural-cli/pkg/executor"
Expand All @@ -17,6 +14,8 @@ import (
"github.com/pluralsh/plural-cli/pkg/utils/errors"
"github.com/pluralsh/plural-cli/pkg/utils/git"
"github.com/pluralsh/plural-cli/pkg/utils/pathing"
"github.com/pluralsh/polly/algorithms"
"github.com/urfave/cli"
)

func init() {
Expand Down Expand Up @@ -216,3 +215,14 @@ func upstreamSynced(fn func(*cli.Context) error) func(*cli.Context) error {
return fn(c)
}
}

func requireKind(fn func(*cli.Context) error) func(*cli.Context) error {
return func(c *cli.Context) error {
exists, _ := utils.Which("kind")
if !exists {
return fmt.Errorf("The kind CLI is not installed")
}

return fn(c)
}
}
Loading

0 comments on commit efc34ee

Please sign in to comment.