Skip to content

Commit

Permalink
Adding a new Minikube flag for helm chart install of Apache Camel K (k…
Browse files Browse the repository at this point in the history
…native-extensions#521)

* 🎁 Adding a new flag on Minikube for helm chart install of Apache Camel K

Signed-off-by: Matthias Wessendorf <[email protected]>

* 💄 Applying feedbac from David S.

Signed-off-by: Matthias Wessendorf <[email protected]>

---------

Signed-off-by: Matthias Wessendorf <[email protected]>
  • Loading branch information
matzew authored Jun 20, 2024
1 parent 770d27b commit 136c4ab
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 3 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
5 changes: 5 additions & 0 deletions internal/command/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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")
}
Expand Down
3 changes: 2 additions & 1 deletion internal/command/minikube.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,14 @@ 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
clusterNameOption(minikubeCmd, "knative")
kubernetesVersionOption(minikubeCmd, "", "kubernetes version to use (1.x.y)")
installServingOption(minikubeCmd)
installEventingOption(minikubeCmd)
installCamelOption(minikubeCmd)
return minikubeCmd
}
62 changes: 62 additions & 0 deletions pkg/install/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down Expand Up @@ -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
}
25 changes: 24 additions & 1 deletion pkg/minikube/minikube.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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)
}
}
}
}

Expand Down Expand Up @@ -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
}

0 comments on commit 136c4ab

Please sign in to comment.