Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support remote kind config ref #454

Open
nabuskey opened this issue Nov 20, 2024 · 3 comments
Open

Support remote kind config ref #454

nabuskey opened this issue Nov 20, 2024 · 3 comments
Labels
enhancement New feature or request
Milestone

Comments

@nabuskey
Copy link
Collaborator

nabuskey commented Nov 20, 2024

Currently the kind config supplied via the --kind-config flag can refer to local files only. It should be able to reference remote kind configs as well. This is useful when a stack needs a specific kind configuration. Currently users need to clone the repository to use such stacks. e.g. --kind-config https://github.com/stacks/kind.config

@nabuskey nabuskey added the enhancement New feature or request label Nov 20, 2024
@nabuskey nabuskey added this to the v1.0.0 milestone Nov 21, 2024
@cmoulliard
Copy link
Contributor

cmoulliard commented Jan 3, 2025

So your idea is to do this:

  • if the string passed to the flag kind-config starts with https:// then we parse the Url
  • We download the URL under a temporary folder
  • We replace the value of the field: NewBuildOptions.KindConfigPath with the new local path
  • We create the kind cluster using the local kind config file

Something like

package main

import (
	"fmt"
	"github.com/spf13/cobra"
	_ "github.com/spf13/cobra"
	"os"
	"snowdrop.dev/my-controller/cmd/util"
	"strings"
)

func main() {
	var kindConfig string

	// Root command (base command for the CLI)
	var rootCmd = &cobra.Command{
		Use:   "app",
		Short: "A simple CLI app using Cobra",
		Long:  `This is a simple CLI application built with Cobra to demonstrate the "create" command.`,
	}

	// Create command
	var createCmd = &cobra.Command{
		Use:   "create",
		Short: "Create a cluster",
		Long:  `The "create" command allows you to create a kind cluster.`,
		Run: func(cmd *cobra.Command, args []string) {
			if kindConfig == "" {
				fmt.Println("Error: --kind-config flag is required")
				os.Exit(1)
			}

			if util.IsValidUrl(kindConfig) {
				tempDir, err := os.MkdirTemp("", "idpbuilder_tmp")
				if err != nil {
					fmt.Println(err)
				}
				defer os.RemoveAll(tempDir)

				kindConfigFile := strings.Join([]string{tempDir, "kind.config"}, "/")

				err = util.DownloadFile(kindConfigFile, kindConfig)
				if err != nil {
					fmt.Println("Error downloading file: ", err)
					return
				}
			}

			fmt.Printf("Creating resource with config: %s\n", kindConfig)
		},
	}

	// Add flags to the create command
	createCmd.Flags().StringVarP(&kindConfig, "kind-config", "k", "", "Path to the kind configuration file (required)")

	// Add create command to root command
	rootCmd.AddCommand(createCmd)

	// Execute the root command
	if err := rootCmd.Execute(); err != nil {
		fmt.Println(err)
		os.Exit(1)
	}
}

func DownloadFile(filepath string, url string) error {
	resp, err := http.Get(url)
	if err != nil {
		return err
	}
	defer resp.Body.Close()

	// Create the file
	out, err := os.Create(filepath)
	if err != nil {
		return err
	}
	defer out.Close()

	// Write the body to file
	_, err = io.Copy(out, resp.Body)
	return err
}

func IsValidUrl(toParse string) bool {
	_, err := url.ParseRequestURI(toParse)
	if err != nil {
		return false
	}

	u, err := url.Parse(toParse)
	if err != nil || u.Scheme == "" || u.Host == "" {
		return false
	}
	return true
}

Correct ? @nabuskey

@nabuskey
Copy link
Collaborator Author

nabuskey commented Jan 3, 2025

We probably don't need to download it to a temp folder. I think we can do all that in-memory. Otherwise, yes that's inline with what I was thinking.

@cmoulliard
Copy link
Contributor

I think we can do all that in-memory.

I don't think so as kind go lib expects to read a file - see: https://github.com/kubernetes-sigs/kind/blob/main/pkg/internal/apis/config/encoding/load.go#L31-L51

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants