Skip to content

Commit 29d35d9

Browse files
committed
[tmpnet] Define reusable flags for configuring kubernetes
Previously `tmpnetctl start-kind-cluster` and the bootstrap-monitor determined their kubeconfig and context separately. Simpler to do it one way and this will be reusable by the kube node runtime.
1 parent e221a0f commit 29d35d9

File tree

9 files changed

+105
-34
lines changed

9 files changed

+105
-34
lines changed

Taskfile.yml

+4
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,10 @@ tasks:
8989
desc: Generates protobuf
9090
cmd: ./scripts/protobuf_codegen.sh
9191

92+
ginkgo-build:
93+
desc: Runs ginkgo against the current working directory
94+
cmd: ./bin/ginkgo build {{.USER_WORKING_DIR}}
95+
9296
install-nix:
9397
desc: Installs nix with the determinate systems installer
9498
cmd: curl --proto '=https' --tlsv1.2 -sSf -L https://install.determinate.systems/nix | sh -s -- install

flake.lock

+5-5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

flake.nix

+1
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545

4646
# Kube tools
4747
kubectl # Kubernetes CLI
48+
k9s # Kubernetes TUI
4849
kind # Kubernetes-in-Docker
4950
kubernetes-helm # Helm CLI (Kubernetes package manager)
5051
self.packages.${system}.kind-with-registry # Script installing kind configured with a local registry

scripts/start_kind_cluster.sh

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#!/usr/bin/env bash
2+
3+
set -euo pipefail
4+
5+
# Enable reuse of the arguments to ginkgo relevant to starting a cluster
6+
START_CLUSTER_ARGS=()
7+
for arg in "$@"; do
8+
if [[ "${arg}" =~ "--kubeconfig" || "${arg}" =~ "--kubeconfig-context" ]]; then
9+
START_CLUSTER_ARGS+=("${arg}")
10+
fi
11+
done
12+
./bin/tmpnetctl start-kind-cluster "${START_CLUSTER_ARGS[@]}"

scripts/tests.e2e.bootstrap_monitor.sh

+2-3
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,5 @@ if ! [[ "$0" =~ scripts/tests.e2e.bootstrap_monitor.sh ]]; then
99
exit 255
1010
fi
1111

12-
./bin/tmpnetctl start-kind-cluster
13-
14-
KUBECONFIG="$HOME/.kube/config" ./bin/ginkgo -v ./tests/fixture/bootstrapmonitor/e2e
12+
./scripts/start_kind_cluster.sh "$@"
13+
./bin/ginkgo -v ./tests/fixture/bootstrapmonitor/e2e "$@"

tests/fixture/bootstrapmonitor/common.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
"k8s.io/client-go/kubernetes"
2020

2121
"github.com/ava-labs/avalanchego/tests/fixture/tmpnet"
22+
"github.com/ava-labs/avalanchego/tests/fixture/tmpnet/flags"
2223
"github.com/ava-labs/avalanchego/utils/logging"
2324
"github.com/ava-labs/avalanchego/version"
2425

@@ -221,6 +222,6 @@ func getLatestImageDetails(
221222

222223
func getClientset(log logging.Logger) (*kubernetes.Clientset, error) {
223224
log.Info("Initializing clientset")
224-
kubeConfigPath := os.Getenv("KUBECONFIG")
225+
kubeConfigPath := os.Getenv(flags.KubeConfigPathEnvVar)
225226
return tmpnet.GetClientset(log, kubeConfigPath, "")
226227
}

tests/fixture/bootstrapmonitor/e2e/e2e_test.go

+4-2
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
"github.com/ava-labs/avalanchego/tests/fixture/bootstrapmonitor"
2525
"github.com/ava-labs/avalanchego/tests/fixture/e2e"
2626
"github.com/ava-labs/avalanchego/tests/fixture/tmpnet"
27+
"github.com/ava-labs/avalanchego/tests/fixture/tmpnet/flags"
2728
"github.com/ava-labs/avalanchego/utils/constants"
2829

2930
appsv1 "k8s.io/api/apps/v1"
@@ -60,13 +61,15 @@ const (
6061
)
6162

6263
var (
64+
kubeconfigVars *flags.KubeconfigVars
6365
skipAvalanchegoImageBuild bool
6466
skipMonitorImageBuild bool
6567

6668
nodeDataDir = bootstrapmonitor.NodeDataDir(dataDir) // Use a subdirectory of the data path so that os.RemoveAll can be used when starting a new test
6769
)
6870

6971
func init() {
72+
kubeconfigVars = flags.NewKubeconfigFlagVars()
7073
flag.BoolVar(
7174
&skipAvalanchegoImageBuild,
7275
"skip-avalanchego-image-build",
@@ -103,8 +106,7 @@ var _ = ginkgo.Describe("[Bootstrap Tester]", func() {
103106
}
104107

105108
ginkgo.By("Configuring a kubernetes client")
106-
kubeconfigPath := os.Getenv("KUBECONFIG")
107-
kubeconfig, err := tmpnet.GetClientConfig(tc.Log(), kubeconfigPath, "")
109+
kubeconfig, err := tmpnet.GetClientConfig(tc.Log(), kubeconfigVars.Path, kubeconfigVars.Context)
108110
require.NoError(err)
109111
clientset, err := kubernetes.NewForConfig(kubeconfig)
110112
require.NoError(err)
+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
// Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved.
2+
// See the file LICENSE for licensing terms.
3+
4+
package flags
5+
6+
import (
7+
"flag"
8+
"fmt"
9+
"os"
10+
11+
"github.com/spf13/pflag"
12+
13+
"github.com/ava-labs/avalanchego/tests/fixture/tmpnet"
14+
)
15+
16+
const KubeConfigPathEnvVar = "KUBECONFIG"
17+
18+
type KubeconfigVars struct {
19+
Path string
20+
Context string
21+
}
22+
23+
// NewKubeconfigFlagVars registers kubeconfig flag variables for stdlib flag
24+
func NewKubeconfigFlagVars() *KubeconfigVars {
25+
return newKubeconfigFlagVars("")
26+
}
27+
28+
// internal method enabling configuration of the doc prefix
29+
func newKubeconfigFlagVars(docPrefix string) *KubeconfigVars {
30+
v := &KubeconfigVars{}
31+
v.register(flag.StringVar, docPrefix)
32+
return v
33+
}
34+
35+
// NewKubeconfigFlagSetVars registers kubeconfig flag variables for pflag
36+
func NewKubeconfigFlagSetVars(flagSet *pflag.FlagSet) *KubeconfigVars {
37+
return newKubeconfigFlagSetVars(flagSet, "")
38+
}
39+
40+
// internal method enabling configuration of the doc prefix
41+
func newKubeconfigFlagSetVars(flagSet *pflag.FlagSet, docPrefix string) *KubeconfigVars {
42+
v := &KubeconfigVars{}
43+
v.register(flagSet.StringVar, docPrefix)
44+
return v
45+
}
46+
47+
func (v *KubeconfigVars) register(stringVar varFunc[string], docPrefix string) {
48+
stringVar(
49+
&v.Path,
50+
"kubeconfig",
51+
tmpnet.GetEnvWithDefault(KubeConfigPathEnvVar, os.ExpandEnv("$HOME/.kube/config")),
52+
docPrefix+fmt.Sprintf(
53+
"The path to a kubernetes configuration file for the target cluster. Also possible to configure via the %s env variable.",
54+
KubeConfigPathEnvVar,
55+
),
56+
)
57+
stringVar(
58+
&v.Context,
59+
"kubeconfig-context",
60+
"",
61+
docPrefix+"The optional kubeconfig context to use",
62+
)
63+
}

tests/fixture/tmpnet/tmpnetctl/main.go

+12-23
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import (
1212
"path/filepath"
1313

1414
"github.com/spf13/cobra"
15-
"github.com/spf13/pflag"
1615
"go.uber.org/zap"
1716

1817
"github.com/ava-labs/avalanchego/tests"
@@ -24,7 +23,10 @@ import (
2423

2524
const cliVersion = "0.0.1"
2625

27-
var errNetworkDirRequired = fmt.Errorf("--network-dir or %s are required", tmpnet.NetworkDirEnvName)
26+
var (
27+
errNetworkDirRequired = fmt.Errorf("--network-dir or %s is required", tmpnet.NetworkDirEnvName)
28+
errKubeconfigRequired = errors.New("--kubeconfig is required")
29+
)
2830

2931
func main() {
3032
var (
@@ -226,10 +228,7 @@ func main() {
226228
)
227229
rootCmd.AddCommand(checkLogsCmd)
228230

229-
var (
230-
kubeConfigPath string
231-
kubeConfigContext string
232-
)
231+
var kubeconfigVars *flags.KubeconfigVars
233232
startKindClusterCmd := &cobra.Command{
234233
Use: "start-kind-cluster",
235234
Short: "Starts a local kind cluster with an integrated registry",
@@ -240,10 +239,15 @@ func main() {
240239
if err != nil {
241240
return err
242241
}
243-
return tmpnet.StartKindCluster(ctx, log, kubeConfigPath, kubeConfigContext)
242+
// A valid kubeconfig is required for local kind usage but this is not validated by KubeconfigVars
243+
// since unlike kind, tmpnet usage may involve an implicit in-cluster config.
244+
if len(kubeconfigVars.Path) == 0 {
245+
return errKubeconfigRequired
246+
}
247+
return tmpnet.StartKindCluster(ctx, log, kubeconfigVars.Path, kubeconfigVars.Context)
244248
},
245249
}
246-
SetKubeConfigFlags(startKindClusterCmd.PersistentFlags(), &kubeConfigPath, &kubeConfigContext)
250+
kubeconfigVars = flags.NewKubeconfigFlagSetVars(startKindClusterCmd.PersistentFlags())
247251
rootCmd.AddCommand(startKindClusterCmd)
248252

249253
if err := rootCmd.Execute(); err != nil {
@@ -252,18 +256,3 @@ func main() {
252256
}
253257
os.Exit(0)
254258
}
255-
256-
func SetKubeConfigFlags(flagSet *pflag.FlagSet, kubeConfigPath *string, kubeConfigContext *string) {
257-
flagSet.StringVar(
258-
kubeConfigPath,
259-
"kubeconfig",
260-
os.Getenv("KUBECONFIG"),
261-
"The path to a kubernetes configuration file for the target cluster",
262-
)
263-
flagSet.StringVar(
264-
kubeConfigContext,
265-
"kubeconfig-context",
266-
"",
267-
"The path to a kubernetes configuration file for the target cluster",
268-
)
269-
}

0 commit comments

Comments
 (0)