Skip to content

Commit

Permalink
Merge pull request #180 from grafana/use-in-memory-etcd
Browse files Browse the repository at this point in the history
Use tmpfs for etcd in e2e test clusters
  • Loading branch information
pablochacin authored Jul 24, 2023
2 parents 4857a35 + 4da5d7c commit 179a547
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 10 deletions.
25 changes: 24 additions & 1 deletion e2e/cluster/cluster_e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,30 @@ func Test_DefaultConfig(t *testing.T) {
cluster.Delete()
}

func Test_UseEtcdRamDisk(t *testing.T) {
// create cluster with default configuration
config, err := cluster.NewConfig(
"e2e-etcdramdisk-cluster",
cluster.Options{
Wait: time.Second * 60,
UseEtcdRAMDisk: true,
},
)
if err != nil {
t.Errorf("failed creating cluster configuration: %v", err)
return
}

cluster, err := config.Create()
if err != nil {
t.Errorf("failed to create cluster: %v", err)
return
}

// delete cluster
cluster.Delete()
}

func getKubernetesClient(kubeconfig string) (kubernetes.Interface, error) {
config, err := clientcmd.BuildConfigFromFlags("", kubeconfig)
if err != nil {
Expand Down Expand Up @@ -126,7 +150,6 @@ func Test_PreloadImages(t *testing.T) {
}
}


func Test_KubernetesVersion(t *testing.T) {
// create cluster with default configuration
config, err := cluster.NewConfig(
Expand Down
14 changes: 14 additions & 0 deletions pkg/testutils/cluster/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,14 @@ apiVersion: kind.x-k8s.io/v1alpha4
nodes:
- role: control-plane`

const etcdPatch = `
kubeadmConfigPatches:
- |
kind: ClusterConfiguration
etcd:
local:
dataDir: /tmp/etcd`

const kindPortMapping = `
- containerPort: %d
hostPort: %d
Expand Down Expand Up @@ -62,6 +70,8 @@ type Options struct {
Version string
// Path to Kubeconfig
Kubeconfig string
// UseEtcdRAMDisk
UseEtcdRAMDisk bool
}

// Config contains the configuration for creating a cluster
Expand Down Expand Up @@ -117,6 +127,10 @@ func (c *Config) Render() (string, error) {
fmt.Fprintf(&config, "\n- role: worker")
}

if c.options.UseEtcdRAMDisk {
fmt.Fprintf(&config, "\n%s", etcdPatch)
}

return config.String(), nil
}

Expand Down
30 changes: 21 additions & 9 deletions pkg/testutils/e2e/cluster/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,16 @@ type PostInstall func(ctx context.Context, cluster E2eCluster) error

// E2eClusterConfig defines the configuration of a e2e test cluster
type E2eClusterConfig struct {
Name string
Images []string
IngressAddr string
IngressPort int32
PostInstall []PostInstall
Reuse bool
Wait time.Duration
AutoCleanup bool
Kubeconfig string
Name string
Images []string
IngressAddr string
IngressPort int32
PostInstall []PostInstall
Reuse bool
Wait time.Duration
AutoCleanup bool
Kubeconfig string
UseEtcdRAMDisk bool
}

// E2eCluster defines the interface for accessing an e2e cluster
Expand Down Expand Up @@ -116,6 +117,7 @@ func DefaultE2eClusterConfig() E2eClusterConfig {
PostInstall: []PostInstall{
InstallContourIngress,
},
UseEtcdRAMDisk: true,
}
}

Expand Down Expand Up @@ -178,6 +180,14 @@ func WithReuse(reuse bool) E2eClusterOption {
}
}

// WithEtcdRAMDisk specifies if the cluster must be configured to use a RAM disk for Etcd
func WithEtcdRAMDisk(ramdisk bool) E2eClusterOption {
return func(c E2eClusterConfig) (E2eClusterConfig, error) {
c.UseEtcdRAMDisk = ramdisk
return c, nil
}
}

// e2eCluster maintains the status of a cluster
type e2eCluster struct {
cluster *cluster.Cluster
Expand All @@ -199,6 +209,7 @@ func createE2eCluster(e2eConfig E2eClusterConfig) (*e2eCluster, error) {
NodePort: 80,
},
},
UseEtcdRAMDisk: e2eConfig.UseEtcdRAMDisk,
},
)
if err != nil {
Expand Down Expand Up @@ -236,6 +247,7 @@ func createE2eCluster(e2eConfig E2eClusterConfig) (*e2eCluster, error) {
func mergeEnvVariables(config E2eClusterConfig) E2eClusterConfig {
config.AutoCleanup = utils.GetBooleanEnvVar("E2E_AUTOCLEANUP", config.AutoCleanup)
config.Reuse = utils.GetBooleanEnvVar("E2E_REUSE", config.Reuse)
config.UseEtcdRAMDisk = utils.GetBooleanEnvVar("E2E_ETCD_RAMDISK", config.UseEtcdRAMDisk)
return config
}

Expand Down

0 comments on commit 179a547

Please sign in to comment.