Skip to content

Commit

Permalink
🎁 Adding a new flag for helm chart install of Apache Camel K
Browse files Browse the repository at this point in the history
Signed-off-by: Matthias Wessendorf <[email protected]>
  • Loading branch information
matzew committed Jun 10, 2024
1 parent 602967d commit b3723a7
Show file tree
Hide file tree
Showing 7 changed files with 95 additions and 5 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

func clusterNameOption(targetCmd *cobra.Command, flagDefault string) {
Expand Down Expand Up @@ -53,6 +54,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")
}
3 changes: 2 additions & 1 deletion internal/command/kind.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,15 @@ 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, installCamel)
},
}
// Set kindCmd options
clusterNameOption(kindCmd, "knative")
kubernetesVersionOption(kindCmd, "", "kubernetes version to use (1.x.y) or (kindest/node:v1.x.y)")
installServingOption(kindCmd)
installEventingOption(kindCmd)
installCamelOption(kindCmd)
installKindRegistryOption(kindCmd)

return kindCmd
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
}
71 changes: 71 additions & 0 deletions pkg/install/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,40 @@ metadata:
return nil
}

func CamelK() 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")

registryAddress, err := getRegistryAddress()
if err != nil {
fmt.Printf("Error: %v\n", err)
return err
}

// 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 +271,40 @@ 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))
}

func getRegistryAddress() (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
}

func runHelmInstall(registryAddress string) error {
cmd := exec.Command("helm", "install",

Check failure on line 286 in pkg/install/install.go

View workflow job for this annotation

GitHub Actions / style / Golang / Lint

G204: Subprocess launched with a potential tainted input or cmd arguments (gosec)
"--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
}
8 changes: 7 additions & 1 deletion 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, installCamel bool) error {
start := time.Now()

// if neither the "install-serving" or "install-eventing" flags are set,
Expand Down Expand Up @@ -86,6 +86,12 @@ func SetUp(name, kVersion string, installServing, installEventing, installKindRe
if err := install.Eventing(); err != nil {
return fmt.Errorf("install eventing: %w", err)
}

if installCamel {
if err := install.CamelK(); err != nil {
return fmt.Errorf("install Apache Camel K: %w", err)
}
}
}
}

Expand Down
8 changes: 7 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,12 @@ func SetUp(name, kVersion string, installServing, installEventing bool) error {
if err := install.Eventing(); err != nil {
return fmt.Errorf("install eventing: %w", err)
}

if installCamel {
if err := install.CamelK(); err != nil {
return fmt.Errorf("install Apache Camel K: %w", err)
}
}
}
}

Expand Down

0 comments on commit b3723a7

Please sign in to comment.