@@ -23,6 +23,7 @@ import (
2323 "os"
2424 "os/exec"
2525 "runtime"
26+ "slices"
2627 "strconv"
2728 "strings"
2829 "time"
@@ -124,6 +125,8 @@ nodes:
124125- role: control-plane
125126`
126127
128+ const dockerNetwork = "kube-bind-dev"
129+
127130// Color helper functions
128131func blueCommand (text string ) string {
129132 return "\033 [38;5;67m" + text + "\033 [0m"
@@ -151,7 +154,7 @@ func (o *DevOptions) runWithColors(ctx context.Context) error {
151154 return err
152155 }
153156
154- providerIP , err := o .getClusterIPAddress (ctx , o .ProviderClusterName , "kube-bind-dev" )
157+ providerIP , err := o .getClusterIPAddress (ctx , o .ProviderClusterName , dockerNetwork )
155158 if err != nil {
156159 fmt .Fprintf (o .Streams .ErrOut , "⚠️ Failed to get provider cluster IP address: %v\n " , err )
157160 providerIP = ""
@@ -190,7 +193,7 @@ func (o *DevOptions) runWithColors(ctx context.Context) error {
190193 if providerIP != "" {
191194 fmt .Fprintf (o .Streams .ErrOut , "%s\n " , blueCommand (fmt .Sprintf ("KUBECONFIG=%s.kubeconfig kubectl bind --konnector-host-alias %s:kube-bind.dev.local" , o .ConsumerClusterName , providerIP )))
192195 } else {
193- fmt .Fprintf (o .Streams .ErrOut , "%s\n " , blueCommand (fmt .Sprintf ("PROVIDER_IP=$(docker inspect %s-control-plane | jq -r '.[0].NetworkSettings.Networks[\" kube-bind-dev \" ].IPAddress') && KUBECONFIG=%s.kubeconfig kubectl bind --konnector-host-alias ${PROVIDER_IP}:kube-bind.dev.local" , o .ProviderClusterName , o .ConsumerClusterName )))
196+ fmt .Fprintf (o .Streams .ErrOut , "%s\n " , blueCommand (fmt .Sprintf ("PROVIDER_IP=$(docker inspect %s-control-plane | jq -r '.[0].NetworkSettings.Networks[\" %s \" ].IPAddress') && KUBECONFIG=%s.kubeconfig kubectl bind --konnector-host-alias ${PROVIDER_IP}:kube-bind.dev.local" , dockerNetwork , o .ProviderClusterName , o .ConsumerClusterName )))
194197 }
195198
196199 return nil
@@ -219,7 +222,7 @@ func (o *DevOptions) setupHostEntries(ctx context.Context) bool {
219222
220223func (o * DevOptions ) createCluster (ctx context.Context , clusterName , clusterConfig string , installKubeBind bool ) error {
221224 // Set experimental Docker network for kind clusters to communicate
222- os .Setenv ("KIND_EXPERIMENTAL_DOCKER_NETWORK" , "kube-bind-dev" )
225+ os .Setenv ("KIND_EXPERIMENTAL_DOCKER_NETWORK" , dockerNetwork )
223226
224227 provider := cluster .NewProvider ()
225228
@@ -228,46 +231,31 @@ func (o *DevOptions) createCluster(ctx context.Context, clusterName, clusterConf
228231 return err
229232 }
230233
231- var found bool
232- for _ , c := range clusters {
233- if c == clusterName {
234- fmt .Fprint (o .Streams .ErrOut , "Kind cluster " + clusterName + " already exists, skipping creation\n " )
235- found = true
236- break
237- }
238- }
234+ kubeconfigPath := fmt .Sprintf ("%s.kubeconfig" , clusterName )
239235
240- if ! found {
241- fmt .Fprint (o .Streams .ErrOut , "Creating kind cluster " + clusterName + " with network kube-bind-dev\n " )
236+ if slices .Contains (clusters , clusterName ) {
237+ fmt .Fprint (o .Streams .ErrOut , "Kind cluster " + clusterName + " already exists, skipping creation\n " )
238+ } else {
239+ fmt .Fprintf (o .Streams .ErrOut , "Creating kind cluster %s with network %s\n " , clusterName , dockerNetwork )
242240 err := provider .Create (clusterName ,
243241 cluster .CreateWithRawConfig ([]byte (clusterConfig )),
244242 cluster .CreateWithWaitForReady (o .WaitForReadyTimeout ),
245243 cluster .CreateWithDisplaySalutation (true ),
244+ cluster .CreateWithKubeconfigPath (kubeconfigPath ),
246245 )
247246 if err != nil {
248247 return err
249248 }
250249 fmt .Fprint (o .Streams .ErrOut , "Kind cluster " + clusterName + " created\n " )
251250 }
252251
253- kubeConfigFile , err := provider .KubeConfig (clusterName , false )
254- if err != nil {
255- return err
256- }
257-
258- kubeconfigPath := fmt .Sprintf ("%s.kubeconfig" , clusterName )
259- if err := os .WriteFile (kubeconfigPath , []byte (kubeConfigFile ), 0600 ); err != nil {
260- return fmt .Errorf ("failed to write kubeconfig file: %w" , err )
261- }
262- fmt .Fprint (o .Streams .ErrOut , "Wrote kubeconfig " + kubeconfigPath + "\n " )
263-
264252 if installKubeBind {
265- restConfig , err := base .LoadRestConfigFromString ( kubeConfigFile )
253+ restConfig , err := base .LoadRestConfigFromFile ( kubeconfigPath )
266254 if err != nil {
267255 return err
268256 }
269257 if err := o .installHelmChart (ctx , restConfig ); err != nil {
270- fmt .Fprint (o .Streams .ErrOut , "Failed to install helm chart\n " )
258+ fmt .Fprint (o .Streams .ErrOut , "Failed to install Helm chart\n " )
271259 return err
272260 }
273261 fmt .Fprint (o .Streams .ErrOut , "Helm chart installed successfully\n " )
0 commit comments