diff --git a/README.md b/README.md index d2ceb762..3de94fbe 100644 --- a/README.md +++ b/README.md @@ -6,7 +6,7 @@ ## Getting Started -Note: In order to use the `quickstart` plugin, you must install the [Kubernetes CLI `kubectl`](https://kubernetes.io/docs/tasks/tools/install-kubectl) and either [`kind`](https://kind.sigs.k8s.io/docs/user/quick-start) or [`minikube`](https://minikube.sigs.k8s.io/docs/start/). +Note: In order to use the `quickstart` plugin, you must install the [Kubernetes CLI `kubectl`](https://kubernetes.io/docs/tasks/tools/install-kubectl) and either [`kind`](https://kind.sigs.k8s.io/docs/user/quick-start) or [`minikube`](https://minikube.sigs.k8s.io/docs/start/). If you want to install the Apache Camel K option you also need the [`helm`](https://helm.sh/) CLI. ### Installation diff --git a/internal/command/flags.go b/internal/command/flags.go index 942f8277..f130e895 100644 --- a/internal/command/flags.go +++ b/internal/command/flags.go @@ -24,6 +24,7 @@ var name string var kubernetesVersion string var installServing bool var installEventing bool +var installCamel bool var installKindRegistry bool var installKindExtraMountHostPath string var installKindExtraMountContainerPath string @@ -55,6 +56,10 @@ func installEventingOption(targetCmd *cobra.Command) { targetCmd.Flags().BoolVar(&installEventing, "install-eventing", false, "install Eventing on quickstart cluster") } +func installCamelOption(targetCmd *cobra.Command) { + targetCmd.Flags().BoolVar(&installCamel, "install-camel", false, "install Apache Camel K on quickstart cluster") +} + func installKindRegistryOption(targetCmd *cobra.Command) { targetCmd.Flags().BoolVar(&installKindRegistry, "registry", false, "install registry for Kind quickstart cluster") } diff --git a/internal/command/minikube.go b/internal/command/minikube.go index 99c1cc7c..2e7e536f 100644 --- a/internal/command/minikube.go +++ b/internal/command/minikube.go @@ -30,7 +30,7 @@ func NewMinikubeCommand() *cobra.Command { Short: "Quickstart with Minikube", RunE: func(cmd *cobra.Command, args []string) error { fmt.Println("Running Knative Quickstart using Minikube") - return minikube.SetUp(name, kubernetesVersion, installServing, installEventing) + return minikube.SetUp(name, kubernetesVersion, installServing, installEventing, installCamel) }, } // Set minikubeCmd options @@ -38,5 +38,6 @@ func NewMinikubeCommand() *cobra.Command { kubernetesVersionOption(minikubeCmd, "", "kubernetes version to use (1.x.y)") installServingOption(minikubeCmd) installEventingOption(minikubeCmd) + installCamelOption(minikubeCmd) return minikubeCmd } diff --git a/pkg/install/install.go b/pkg/install/install.go index 999ed3b8..3d80503a 100644 --- a/pkg/install/install.go +++ b/pkg/install/install.go @@ -205,6 +205,34 @@ metadata: return nil } +func CamelK(registryAddress string) error { + fmt.Println("🐪 Installing Apache Camel K ... ") + + if err := addHelmRepo(); err != nil { + fmt.Printf("Error adding Helm repo: %v\n", err) + return err + } + fmt.Println("Helm repo added successfully") + + // Run the Helm install command + if err := runHelmInstall(registryAddress); err != nil { + fmt.Printf("Error: %v\n", err) + return err + } + + if err := waitForCRDsEstablished(); err != nil { + return fmt.Errorf("crds: %w", err) + } + fmt.Println(" CRDs installed...") + + if err := waitForPodsReady("default"); err != nil { + return fmt.Errorf("core: %w", err) + } + fmt.Println(" Apache Camel K installed...") + + return nil +} + func runCommand(c *exec.Cmd) error { if out, err := c.CombinedOutput(); err != nil { fmt.Println(string(out)) @@ -237,3 +265,37 @@ func waitForCRDsEstablished() error { func waitForPodsReady(ns string) error { return runCommand(exec.Command("kubectl", "wait", "pod", "--timeout=10m", "--for=condition=Ready", "-l", "!job-name", "-n", ns)) } + +//nolint:gosec // avoid linter warnings +func runHelmInstall(registryAddress string) error { + + // Check if helm CLI is installed + if _, err := exec.LookPath("helm"); err != nil { + return fmt.Errorf("Please install helm CLI") + } + + cmd := exec.Command("helm", "install", + "--generate-name", + "--set", fmt.Sprintf("platform.build.registry.address=%s", registryAddress), + "--set", "platform.build.registry.insecure=true", + "camel-k/camel-k") + + cmd.Stdout = cmd.Stderr + + if out, err := cmd.CombinedOutput(); err != nil { + fmt.Println(string(out)) + return fmt.Errorf("failed to run helm install: %w", err) + } + return nil +} + +func addHelmRepo() error { + cmd := exec.Command("helm", "repo", "add", "camel-k", "https://apache.github.io/camel-k/charts/") + cmd.Stdout = cmd.Stderr + + if out, err := cmd.CombinedOutput(); err != nil { + fmt.Println(string(out)) + return fmt.Errorf("failed to add helm repo: %w", err) + } + return nil +} diff --git a/pkg/minikube/minikube.go b/pkg/minikube/minikube.go index e7abb90c..87b913b9 100644 --- a/pkg/minikube/minikube.go +++ b/pkg/minikube/minikube.go @@ -35,7 +35,7 @@ var memory = "3072" var installKnative = true // SetUp creates a local Minikube cluster and installs all the relevant Knative components -func SetUp(name, kVersion string, installServing, installEventing bool) error { +func SetUp(name, kVersion string, installServing, installEventing, installCamel bool) error { start := time.Now() // if neither the "install-serving" or "install-eventing" flags are set, @@ -83,6 +83,19 @@ func SetUp(name, kVersion string, installServing, installEventing bool) error { if err := install.Eventing(); err != nil { return fmt.Errorf("install eventing: %w", err) } + + if installCamel { + + registryAddress, err := getMinikubeRegistryAddress() + if err != nil { + fmt.Printf("Error: %v\n", err) + return err + } + + if err := install.CamelK(registryAddress); err != nil { + return fmt.Errorf("install Apache Camel K: %w", err) + } + } } } @@ -271,3 +284,13 @@ func recreateCluster() error { } return nil } + +func getMinikubeRegistryAddress() (string, error) { + cmd := exec.Command("kubectl", "-n", "kube-system", "get", "service", "registry", "-o", "jsonpath={.spec.clusterIP}") + out, err := cmd.CombinedOutput() + if err != nil { + return "", fmt.Errorf("failed to get registry address: %w", err) + } + address := strings.TrimSpace(string(out)) + return address, nil +}