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

Adding a new Minikube flag for helm chart install of Apache Camel K #521

Merged
merged 2 commits into from
Jun 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
matzew marked this conversation as resolved.
Show resolved Hide resolved

### 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")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would prefer something like --extensions camel. That could potentially server for additional 3rd party installations. But not a hard opinion, we probably won't grow this list to unmanageable state.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that is a good idea for a future improvement. Sounds good?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll create a new issue from this suggestion. All good for the scope of this PR.

}

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...")
matzew marked this conversation as resolved.
Show resolved Hide resolved

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",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we mark the exec command with linter ignore command just to avoid further golangci-linter errors.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In addition I'd not mind checking if helm is installed first and displaying "Please install helm CLI" for a nicer UX.

Not a blocking issue though.

"--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
}
Loading