-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
138 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
// Package kind provides function for creating and managing kind clusters. | ||
package kind | ||
|
||
import ( | ||
"context" | ||
"os" | ||
|
||
"sigs.k8s.io/kind/pkg/cluster" | ||
"sigs.k8s.io/kind/pkg/cmd" | ||
) | ||
|
||
type Cluster struct { | ||
provider *cluster.Provider | ||
kubeconfigFilePath string | ||
name string | ||
} | ||
|
||
const defaultClusterName = "kind" | ||
|
||
// CreateCluster creates a new kind cluster with the given name. | ||
func CreateCluster(ctx context.Context, name string) (*Cluster, error) { | ||
select { | ||
case <-ctx.Done(): | ||
return nil, ctx.Err() | ||
default: | ||
} | ||
|
||
kubeconfigFile, err := os.CreateTemp("", "*-kubeconfig") | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
provider := cluster.NewProvider(cluster.ProviderWithLogger(cmd.NewLogger())) | ||
if name == "" { | ||
name = defaultClusterName | ||
} | ||
err = provider.Create(name, cluster.CreateWithKubeconfigPath(kubeconfigFile.Name())) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
// ExportKubeConfig exports the kubeconfig for the cluster with the given name to the standard output or a file. | ||
// This makes it easy for other applications, such as fluxcd, to work with the cluster. | ||
err = provider.ExportKubeConfig(name, "", false) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &Cluster{ | ||
provider: provider, | ||
kubeconfigFilePath: kubeconfigFile.Name(), | ||
name: name, | ||
}, nil | ||
} | ||
|
||
// Delete deletes the cluster and the temporary kubeconfig file. | ||
func (c *Cluster) Delete(ctx context.Context, name string) error { | ||
select { | ||
case <-ctx.Done(): | ||
return ctx.Err() | ||
default: | ||
} | ||
|
||
err := c.provider.Delete(c.name, c.kubeconfigFilePath) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return os.Remove(c.kubeconfigFilePath) | ||
} | ||
|
||
func (c *Cluster) KubeconfigFilePath() string { | ||
return c.kubeconfigFilePath | ||
} | ||
|
||
func (c *Cluster) Name() string { | ||
return c.name | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package kind | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestCreateCluster(t *testing.T) { | ||
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Minute) | ||
defer cancel() | ||
|
||
// create a test cluster name | ||
name := "test-cluster" | ||
cluster, err := CreateCluster(ctx, name) | ||
|
||
// assert that there is no error and the cluster is not nil | ||
assert.NoError(t, err) | ||
assert.NotNil(t, cluster) | ||
|
||
// assert that the cluster has the expected name and kubeconfig file path | ||
assert.Equal(t, name, cluster.Name()) | ||
assert.NotEmpty(t, cluster.KubeconfigFilePath()) | ||
|
||
// delete the cluster | ||
err = cluster.Delete(ctx, name) | ||
assert.NoError(t, err) | ||
} |