Skip to content

Commit efc34ee

Browse files
authored
feat: strip KIND dependencies and use kind CLI (#491)
* 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
1 parent 4010793 commit efc34ee

16 files changed

Lines changed: 105 additions & 254 deletions

File tree

.github/workflows/ci.yaml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -381,19 +381,19 @@ jobs:
381381
runs-on: ubuntu-latest
382382
steps:
383383
- uses: actions/checkout@v3
384-
- uses: actions/setup-go@v4.1.0
384+
- uses: actions/setup-go@v5
385385
with:
386386
go-version-file: go.mod
387387
- run: make test
388388
lint:
389389
name: Lint
390390
runs-on: ubuntu-latest
391391
steps:
392-
- uses: actions/checkout@v3
393-
- uses: actions/setup-go@v4.1.0
392+
- uses: actions/checkout@v4
393+
- uses: actions/setup-go@v5
394394
with:
395395
go-version-file: go.mod
396396
- name: golangci-lint
397-
uses: golangci/golangci-lint-action@v3.7.0
397+
uses: golangci/golangci-lint-action@v4
398398
with:
399399
version: v1.54.2

.github/workflows/e2e.yaml

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,14 @@ jobs:
99
runs-on: ubuntu-latest
1010
steps:
1111
- name: Checkout
12-
uses: actions/checkout@v3
12+
uses: actions/checkout@v4
13+
- name: Install GO
14+
uses: actions/setup-go@v4
15+
with:
16+
go-version-file: go.mod
17+
cache: false
1318
- name: Create kind cluster
14-
uses: helm/kind-action@v1.8.0
19+
uses: helm/kind-action@v1.9.0
1520
with:
1621
install_only: true
1722
- run: |

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ BUILD ?= $(shell git rev-parse --short HEAD)
1212
DKR_HOST ?= dkr.plural.sh
1313
GOOS ?= darwin
1414
GOARCH ?= arm64
15-
GOLANG_CROSS_VERSION ?= v1.20.2
15+
GOLANG_CROSS_VERSION ?= v1.22.0
1616
PACKAGE ?= github.com/pluralsh/plural
1717
BASE_LDFLAGS ?= -s -w
1818
LDFLAGS ?= $(BASE_LDFLAGS) $\

cmd/plural/bootstrap.go

Lines changed: 42 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,15 @@ package plural
33
import (
44
"context"
55
"fmt"
6+
"os"
7+
"path"
68
"strings"
79
"time"
810

9-
"github.com/pkg/errors"
11+
"github.com/pluralsh/plural-cli/pkg/kubernetes"
12+
"github.com/pluralsh/plural-cli/pkg/manifest"
13+
"github.com/pluralsh/plural-cli/pkg/provider"
14+
"github.com/pluralsh/plural-cli/pkg/utils"
1015
"github.com/urfave/cli"
1116
corev1 "k8s.io/api/core/v1"
1217
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
@@ -21,13 +26,6 @@ import (
2126
clusterapi "sigs.k8s.io/cluster-api/api/v1beta1"
2227
apiclient "sigs.k8s.io/cluster-api/cmd/clusterctl/client"
2328
ctrlruntimeclient "sigs.k8s.io/controller-runtime/pkg/client"
24-
"sigs.k8s.io/kind/pkg/cluster"
25-
26-
"github.com/pluralsh/plural-cli/pkg/manifest"
27-
28-
"github.com/pluralsh/plural-cli/pkg/kubernetes"
29-
"github.com/pluralsh/plural-cli/pkg/provider"
30-
"github.com/pluralsh/plural-cli/pkg/utils"
3129
)
3230

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

40+
const (
41+
kindConfig = `kind: Cluster
42+
apiVersion: kind.x-k8s.io/v1alpha4
43+
networking:
44+
ipFamily: dual
45+
nodes:
46+
- role: control-plane
47+
extraMounts:
48+
- hostPath: /var/run/docker.sock
49+
containerPath: /var/run/docker.sock`
50+
)
51+
4252
func (p *Plural) bootstrapCommands() []cli.Command {
4353
return []cli.Command{
4454
{
@@ -87,13 +97,13 @@ func (p *Plural) bootstrapClusterCommands() []cli.Command {
8797
Usage: "skip creating when cluster exists",
8898
},
8999
},
90-
Action: latestVersion(requireArgs(handleCreateCluster, []string{"NAME"})),
100+
Action: latestVersion(requireKind(requireArgs(handleCreateCluster, []string{"NAME"}))),
91101
},
92102
{
93103
Name: "delete",
94104
ArgsUsage: "NAME",
95105
Usage: "Deletes bootstrap cluster",
96-
Action: latestVersion(requireArgs(handleDeleteCluster, []string{"NAME"})),
106+
Action: latestVersion(requireKind(requireArgs(handleDeleteCluster, []string{"NAME"}))),
97107
},
98108
{
99109
Name: "move",
@@ -254,62 +264,53 @@ func (p *Plural) handleCreateNamespace(c *cli.Context) error {
254264

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

262270
func handleCreateCluster(c *cli.Context) error {
263271
name := c.Args().Get(0)
264272
imageFlag := c.String("image")
265273
skipCreation := c.Bool("skip-if-exists")
266-
provider := cluster.NewProvider()
267-
utils.Highlight("Creating cluster %s ...\n", name)
268-
n, err := provider.ListNodes(name)
274+
if utils.IsKindClusterAlreadyExists(name) && skipCreation {
275+
utils.Highlight("Cluster %s already exists \n", name)
276+
return nil
277+
}
278+
279+
dir, err := os.MkdirTemp("", "kind")
269280
if err != nil {
270281
return err
271282
}
272-
if len(n) != 0 && skipCreation {
273-
utils.Highlight("Cluster %s already exists \n", name)
274-
return nil
283+
defer os.RemoveAll(dir)
284+
config := path.Join(dir, "config.yaml")
285+
if err := os.WriteFile(config, []byte(kindConfig), 0644); err != nil {
286+
return err
275287
}
276-
if err := provider.Create(
277-
name,
278-
cluster.CreateWithNodeImage(imageFlag),
279-
cluster.CreateWithRetain(false),
280-
cluster.CreateWithDisplayUsage(true),
281-
cluster.CreateWithDisplaySalutation(true),
282-
cluster.CreateWithRawConfig([]byte(`kind: Cluster
283-
apiVersion: kind.x-k8s.io/v1alpha4
284-
networking:
285-
ipFamily: dual
286-
nodes:
287-
- role: control-plane
288-
extraMounts:
289-
- hostPath: /var/run/docker.sock
290-
containerPath: /var/run/docker.sock
291-
`)),
292-
); err != nil {
293-
return errors.Wrap(err, "failed to create cluster")
288+
args := []string{"create", "cluster", "--name", name, "--config", config}
289+
if imageFlag != "" {
290+
args = append(args, "--image", imageFlag)
291+
}
292+
293+
if err := utils.Exec("kind", args...); err != nil {
294+
return err
294295
}
295-
kubeconfig, err := provider.KubeConfig(name, false)
296+
297+
kubeconfig, err := utils.GetKindClusterKubeconfig(name, false)
296298
if err != nil {
297299
return err
298300
}
301+
299302
client, err := getClient(kubeconfig)
300303
if err != nil {
301304
return err
302305
}
303-
304306
if err := client.Create(context.Background(), &corev1.Namespace{
305307
ObjectMeta: metav1.ObjectMeta{
306308
Name: "bootstrap",
307309
},
308310
}); err != nil {
309311
return err
310312
}
311-
312-
internalKubeconfig, err := provider.KubeConfig(name, true)
313+
internalKubeconfig, err := utils.GetKindClusterKubeconfig(name, true)
313314
if err != nil {
314315
return err
315316
}

cmd/plural/cd_contexts.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package plural
33
import (
44
"encoding/json"
55
"fmt"
6+
67
gqlclient "github.com/pluralsh/console-client-go"
78
"github.com/pluralsh/plural-cli/pkg/console"
89
"github.com/pluralsh/plural-cli/pkg/utils"

cmd/plural/template.go

Lines changed: 0 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package plural
22

33
import (
44
"bytes"
5-
"encoding/json"
65
"io"
76
"os"
87

@@ -12,11 +11,8 @@ import (
1211
"github.com/pluralsh/plural-cli/pkg/config"
1312
lua "github.com/pluralsh/plural-cli/pkg/scaffold/template"
1413
"github.com/pluralsh/plural-cli/pkg/template"
15-
"github.com/pluralsh/plural-operator/apis/platform/v1alpha1"
1614
"github.com/urfave/cli"
1715
"gopkg.in/yaml.v2"
18-
k8sjson "k8s.io/apimachinery/pkg/runtime/serializer/json"
19-
"k8s.io/client-go/kubernetes/scheme"
2016
)
2117

2218
func testTemplate(c *cli.Context) error {
@@ -107,44 +103,3 @@ type GrafanaDashboard struct {
107103
}
108104
}
109105
}
110-
111-
func formatDashboard(c *cli.Context) error {
112-
if err := v1alpha1.AddToScheme(scheme.Scheme); err != nil {
113-
return err
114-
}
115-
s := k8sjson.NewYAMLSerializer(k8sjson.DefaultMetaFactory, scheme.Scheme,
116-
scheme.Scheme)
117-
118-
dashboard := v1alpha1.Dashboard{}
119-
grafana := GrafanaDashboard{}
120-
data, err := io.ReadAll(os.Stdin)
121-
if err != nil {
122-
return err
123-
}
124-
125-
if err := json.Unmarshal(data, &grafana); err != nil {
126-
return err
127-
}
128-
129-
graphs := make([]*v1alpha1.DashboardGraph, 0)
130-
for _, panel := range grafana.Panels {
131-
graph := &v1alpha1.DashboardGraph{}
132-
graph.Name = panel.Title
133-
graph.Queries = make([]*v1alpha1.GraphQuery, 0)
134-
for _, target := range panel.Targets {
135-
query := &v1alpha1.GraphQuery{
136-
Query: target.Expr,
137-
Legend: target.LegendFormat,
138-
}
139-
graph.Queries = append(graph.Queries, query)
140-
}
141-
graphs = append(graphs, graph)
142-
}
143-
144-
dashboard.Spec.Graphs = graphs
145-
dashboard.Spec.Timeslices = []string{"1h", "2h", "6h", "1d", "7d"}
146-
dashboard.Spec.DefaultTime = "1h"
147-
dashboard.Spec.Name = grafana.Title
148-
149-
return s.Encode(&dashboard, os.Stdout)
150-
}

cmd/plural/utils_test.go

Lines changed: 0 additions & 88 deletions
This file was deleted.

cmd/plural/validation.go

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,6 @@ import (
55
"os"
66

77
"github.com/AlecAivazis/survey/v2"
8-
"github.com/pluralsh/polly/algorithms"
9-
"github.com/urfave/cli"
10-
118
"github.com/pluralsh/plural-cli/pkg/api"
129
"github.com/pluralsh/plural-cli/pkg/config"
1310
"github.com/pluralsh/plural-cli/pkg/executor"
@@ -17,6 +14,8 @@ import (
1714
"github.com/pluralsh/plural-cli/pkg/utils/errors"
1815
"github.com/pluralsh/plural-cli/pkg/utils/git"
1916
"github.com/pluralsh/plural-cli/pkg/utils/pathing"
17+
"github.com/pluralsh/polly/algorithms"
18+
"github.com/urfave/cli"
2019
)
2120

2221
func init() {
@@ -216,3 +215,14 @@ func upstreamSynced(fn func(*cli.Context) error) func(*cli.Context) error {
216215
return fn(c)
217216
}
218217
}
218+
219+
func requireKind(fn func(*cli.Context) error) func(*cli.Context) error {
220+
return func(c *cli.Context) error {
221+
exists, _ := utils.Which("kind")
222+
if !exists {
223+
return fmt.Errorf("The kind CLI is not installed")
224+
}
225+
226+
return fn(c)
227+
}
228+
}

0 commit comments

Comments
 (0)