Skip to content

Commit

Permalink
Kind Quickstart: Add extraMount Option (#522)
Browse files Browse the repository at this point in the history
* add extraMount for functions

* add extraMountHostPath and extraMountContainerPath options for kind

* next

* next
  • Loading branch information
eupharis authored Jun 14, 2024
1 parent 602967d commit ac9614f
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 15 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,13 @@ kn quickstart kind --registry

Note: we automatically configure tag resolution for the local registry when this flag is passed

Kind can also be configured with an [extra mount](https://kind.sigs.k8s.io/docs/user/configuration#extra-mounts) so your containers can access files on your local machine.

```bash
kn quickstart kind --extraMountHostPath /home/myname/foo --extraMountContainerPath /foo
```


### Quickstart with Minikube

Set up a local Knative cluster using [Minikube](https://minikube.sigs.k8s.io/):
Expand Down
10 changes: 10 additions & 0 deletions internal/command/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ var kubernetesVersion string
var installServing bool
var installEventing bool
var installKindRegistry bool
var installKindExtraMountHostPath string
var installKindExtraMountContainerPath string

func clusterNameOption(targetCmd *cobra.Command, flagDefault string) {
targetCmd.Flags().StringVarP(
Expand Down Expand Up @@ -56,3 +58,11 @@ func installEventingOption(targetCmd *cobra.Command) {
func installKindRegistryOption(targetCmd *cobra.Command) {
targetCmd.Flags().BoolVar(&installKindRegistry, "registry", false, "install registry for Kind quickstart cluster")
}

func installKindExtraMountHostPathOption(targetCmd *cobra.Command) {
targetCmd.Flags().StringVarP(&installKindExtraMountHostPath, "extraMountHostPath", "", "", "set the extraMount hostPath on Kind quickstart cluster")
}

func installKindExtraMountContainerPathOption(targetCmd *cobra.Command) {
targetCmd.Flags().StringVarP(&installKindExtraMountContainerPath, "extraMountContainerPath", "", "", "set the extraMount containerPath on Kind quickstart cluster")
}
4 changes: 3 additions & 1 deletion internal/command/kind.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func NewKindCommand() *cobra.Command {
Short: "Quickstart with Kind",
RunE: func(cmd *cobra.Command, args []string) error {
fmt.Println("Running Knative Quickstart using Kind")
return kind.SetUp(name, kubernetesVersion, installServing, installEventing, installKindRegistry)
return kind.SetUp(name, kubernetesVersion, installServing, installEventing, installKindRegistry, installKindExtraMountHostPath, installKindExtraMountContainerPath)
},
}
// Set kindCmd options
Expand All @@ -37,6 +37,8 @@ func NewKindCommand() *cobra.Command {
installServingOption(kindCmd)
installEventingOption(kindCmd)
installKindRegistryOption(kindCmd)
installKindExtraMountHostPathOption(kindCmd)
installKindExtraMountContainerPathOption(kindCmd)

return kindCmd
}
40 changes: 26 additions & 14 deletions pkg/kind/kind.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ var container_reg_port = "5001"
var installKnative = true

// SetUp creates a local Kind cluster and installs all the relevant Knative components
func SetUp(name, kVersion string, installServing, installEventing, installKindRegistry bool) error {
func SetUp(name, kVersion string, installServing, installEventing, installKindRegistry bool, installKindExtraMountHostPath string, installKindExtraMountContainerPath string) error {
start := time.Now()

// if neither the "install-serving" or "install-eventing" flags are set,
Expand All @@ -60,7 +60,7 @@ func SetUp(name, kVersion string, installServing, installEventing, installKindRe
}
}

if err := createKindCluster(installKindRegistry); err != nil {
if err := createKindCluster(installKindRegistry, installKindExtraMountHostPath, installKindExtraMountContainerPath); err != nil {
return fmt.Errorf("creating cluster: %w", err)
}
if installKnative {
Expand Down Expand Up @@ -95,7 +95,7 @@ func SetUp(name, kVersion string, installServing, installEventing, installKindRe
return nil
}

func createKindCluster(registry bool) error {
func createKindCluster(registry bool, extraMountHostPath string, extraMountContainerPath string) error {

if err := checkDocker(); err != nil {
return fmt.Errorf("%w", err)
Expand All @@ -116,7 +116,7 @@ func createKindCluster(registry bool) error {
fmt.Print(" To create a local registry, use the --registry flag.\n\n")
}

if err := checkForExistingCluster(registry); err != nil {
if err := checkForExistingCluster(registry, extraMountHostPath, extraMountContainerPath); err != nil {
return fmt.Errorf("existing cluster: %w", err)
}

Expand Down Expand Up @@ -205,7 +205,7 @@ func checkKindVersion() error {
// checkForExistingCluster checks if the user already has a Kind cluster. If so, it provides
// the option of deleting the existing cluster and recreating it. If not, it proceeds to
// creating a new cluster
func checkForExistingCluster(registry bool) error {
func checkForExistingCluster(registry bool, extraMountHostPath string, extraMountContainerPath string) error {

getClusters := exec.Command("kind", "get", "clusters", "-q")
out, err := getClusters.CombinedOutput()
Expand All @@ -220,7 +220,7 @@ func checkForExistingCluster(registry bool) error {
fmt.Print("\nKnative Cluster kind-" + clusterName + " already installed.\nDelete and recreate [y/N]: ")
fmt.Scanf("%s", &resp)
if resp == "y" || resp == "Y" {
if err := recreateCluster(registry); err != nil {
if err := recreateCluster(registry, extraMountHostPath, extraMountContainerPath); err != nil {
return fmt.Errorf("new cluster: %w", err)
}
} else {
Expand All @@ -236,7 +236,7 @@ func checkForExistingCluster(registry bool) error {
fmt.Print("Knative installation already exists.\nDelete and recreate the cluster [y/N]: ")
fmt.Scanf("%s", &resp)
if resp == "y" || resp == "Y" {
if err := recreateCluster(registry); err != nil {
if err := recreateCluster(registry, extraMountHostPath, extraMountContainerPath); err != nil {
return fmt.Errorf("new cluster: %w", err)
}
} else {
Expand All @@ -248,7 +248,7 @@ func checkForExistingCluster(registry bool) error {
return nil
}
} else {
if err := createNewCluster(); err != nil {
if err := createNewCluster(extraMountHostPath, extraMountContainerPath); err != nil {
return fmt.Errorf("new cluster: %w", err)
}
if registry {
Expand All @@ -265,7 +265,7 @@ func checkForExistingCluster(registry bool) error {
}

// recreateCluster recreates a Kind cluster
func recreateCluster(registry bool) error {
func recreateCluster(registry bool, extraMountHostPath string, extraMountContainerPath string) error {
fmt.Println("\n Deleting cluster...")
deleteCluster := exec.Command("kind", "delete", "cluster", "--name", clusterName)
if err := deleteCluster.Run(); err != nil {
Expand All @@ -275,7 +275,7 @@ func recreateCluster(registry bool) error {
if err := deleteContainerRegistry.Run(); err != nil {
return fmt.Errorf("delete container registry: %w", err)
}
if err := createNewCluster(); err != nil {
if err := createNewCluster(extraMountHostPath, extraMountContainerPath); err != nil {
return fmt.Errorf("new cluster: %w", err)
}
if registry {
Expand All @@ -290,9 +290,21 @@ func recreateCluster(registry bool) error {
}

// createNewCluster creates a new Kind cluster
func createNewCluster() error {
func createNewCluster(extraMountHostPath string, extraMountContainerPath string) error {
extraMount := ""
if extraMountHostPath != "" && extraMountContainerPath != "" {
extraMount = fmt.Sprintf(`
extraMounts:
- hostPath: %s
containerPath: %s`, extraMountHostPath, extraMountContainerPath)
}

if extraMount == "" {
fmt.Println("☸ Creating Kind cluster...")
} else {
fmt.Println("☸ Creating Kind cluster with extraMounts...")
}

fmt.Println("☸ Creating Kind cluster...")
config := fmt.Sprintf(`
kind: Cluster
apiVersion: kind.x-k8s.io/v1alpha4
Expand All @@ -303,11 +315,11 @@ containerdConfigPatches:
endpoint = ["http://%s:5000"]
nodes:
- role: control-plane
image: %s
image: %s %s
extraPortMappings:
- containerPort: 31080
listenAddress: 0.0.0.0
hostPort: 80`, clusterName, container_reg_port, container_reg_name, kubernetesVersion)
hostPort: 80`, clusterName, container_reg_port, container_reg_name, kubernetesVersion, extraMount)

createCluster := exec.Command("kind", "create", "cluster", "--wait=120s", "--config=-")
createCluster.Stdin = strings.NewReader(config)
Expand Down

0 comments on commit ac9614f

Please sign in to comment.