Skip to content

Commit e546772

Browse files
authored
Merge pull request #39 from kubero-dev/feature/add-longterm-metrics
Feature / Add monitoring installation
2 parents 3d78a14 + 61851a4 commit e546772

File tree

2 files changed

+159
-15
lines changed

2 files changed

+159
-15
lines changed

cmd/kuberoCli/install.go

+155-15
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,9 @@ required binaries:
5858
case "ingress":
5959
installIngress()
6060
return
61+
case "monitoring":
62+
installMonitoring()
63+
return
6164
case "kubernetes":
6265
installKubernetes()
6366
checkCluster()
@@ -71,6 +74,7 @@ required binaries:
7174
installMetrics()
7275
installCertManager()
7376
installKuberoOperator()
77+
installMonitoring()
7478
installKuberoUi()
7579
writeCLIconfig()
7680
printDNSinfo()
@@ -91,13 +95,14 @@ var arg_portSecure string
9195
var clusterType string
9296
var arg_component string
9397
var install_olm bool
98+
var monitoringInstalled bool
9499
var ingressControllerVersion = "v1.10.0" // https://github.com/kubernetes/ingress-nginx/tags -> controller-v1.5.1
95100

96101
// var clusterTypeSelection = "[scaleway,linode,gke,digitalocean,kind]"
97102
var clusterTypeList = []string{"kind", "linode", "scaleway", "gke", "digitalocean"}
98103

99104
func init() {
100-
installCmd.Flags().StringVarP(&arg_component, "component", "c", "", "install component (kubernetes,olm,ingress,metrics,certmanager,kubero-operator,kubero-ui)")
105+
installCmd.Flags().StringVarP(&arg_component, "component", "c", "", "install component (kubernetes,olm,ingress,metrics,certmanager,kubero-operator,monitoring,kubero-ui)")
101106
installCmd.Flags().StringVarP(&arg_adminUser, "user", "u", "", "Admin username for the kubero UI")
102107
installCmd.Flags().StringVarP(&arg_adminPassword, "user-password", "U", "", "Password for the admin user")
103108
installCmd.Flags().StringVarP(&arg_apiToken, "apitoken", "a", "", "API token for the admin user")
@@ -107,6 +112,7 @@ func init() {
107112
rootCmd.AddCommand(installCmd)
108113

109114
install_olm = false
115+
monitoringInstalled = false
110116
}
111117

112118
func checkAllBinaries() {
@@ -140,8 +146,9 @@ func printInstallSteps() {
140146
4. Install the metrics server {{(optional, but recommended)}}::gray
141147
5. Install the cert-manager {{(optional)}}::gray
142148
6. Install the kubero operator {{(required)}}::gray
143-
7. Install the kubero UI {{(optional, but highly recommended)}}::gray
144-
8. Write the kubero CLI config
149+
7. Install the monitoring stack {{(optional, but recommended)}}::gray
150+
8. Install the kubero UI {{(optional, but highly recommended)}}::gray
151+
9. Write the kubero CLI config
145152
`)
146153
}
147154

@@ -378,6 +385,72 @@ func installIngress() {
378385
ingressSpinner.Error("Failed to run command. Try runnig this command manually: kubectl apply -f " + URL)
379386
log.Fatal(ingressErr)
380387
}
388+
389+
patch := `{
390+
"spec": {
391+
"template": {
392+
"metadata": {
393+
"annotations": {
394+
"prometheus.io/port": "10254",
395+
"prometheus.io/scrape": "true"
396+
}
397+
},
398+
"spec": {
399+
"containers": [
400+
{
401+
"name": "controller",
402+
"ports": [
403+
{
404+
"containerPort": 10254,
405+
"name": "prometheus",
406+
"protocol": "TCP"
407+
}
408+
],
409+
"args": [
410+
"/nginx-ingress-controller",
411+
"--election-id=ingress-nginx-leader",
412+
"--controller-class=k8s.io/ingress-nginx",
413+
"--ingress-class=nginx",
414+
"--configmap=$(POD_NAMESPACE)/ingress-nginx-controller",
415+
"--validating-webhook=:8443",
416+
"--validating-webhook-certificate=/usr/local/certificates/cert",
417+
"--validating-webhook-key=/usr/local/certificates/key",
418+
"--watch-ingress-without-class=true",
419+
"--enable-metrics=true",
420+
"--publish-status-address=localhost"
421+
]
422+
}
423+
]
424+
}
425+
}
426+
}
427+
}`
428+
_, ingressPatch := exec.Command("kubectl", "patch", "deployments.apps", "ingress-nginx-controller", "-n", "ingress-nginx", "-p", patch).Output()
429+
if ingressPatch != nil {
430+
ingressSpinner.Error("Failed to patch the ingress controller. Here is a detailled information how to do it manually: https://github.com/kubernetes/ingress-nginx/blob/main/docs/user-guide/monitoring.md")
431+
//log.Fatal(ingressPatch)
432+
}
433+
434+
patch = `{
435+
"spec": {
436+
"ports": [
437+
{
438+
"name": "prometheus",
439+
"nodePort": 31280,
440+
"port": 10254,
441+
"protocol": "TCP",
442+
"targetPort": "prometheus"
443+
}
444+
]
445+
}
446+
}`
447+
448+
_, ingressPatch = exec.Command("kubectl", "patch", "svc", "ingress-nginx-controller", "-n", "ingress-nginx", "-p", patch).Output()
449+
if ingressPatch != nil {
450+
ingressSpinner.Error("Failed to patch the ingress controller service. Here is a detailled information how to do it manually: https://github.com/kubernetes/ingress-nginx/blob/main/docs/user-guide/monitoring.md")
451+
//log.Fatal(ingressPatch)
452+
}
453+
381454
ingressSpinner.Success("Ingress installed sucessfully")
382455
}
383456

@@ -446,25 +519,30 @@ func installKuberoOperatorSlim() {
446519

447520
}
448521

449-
func installKuberoUi() {
522+
func createNamespace(namespace string) {
450523

451-
ingressInstall := promptLine("7) Install Kubero UI", "[y,n]", "y")
452-
if ingressInstall != "y" {
453-
return
454-
}
455-
456-
kuberoNSinstalled, _ := exec.Command("kubectl", "get", "ns", "kubero").Output()
524+
kuberoNSinstalled, _ := exec.Command("kubectl", "get", "ns", namespace).Output()
457525
if len(kuberoNSinstalled) > 0 {
458-
cfmt.Println("{{✓ Kubero Namespace exists}}::lightGreen")
526+
cfmt.Printf("{{✓ Namespace %s exists}}::lightGreen\n", namespace)
459527
} else {
460-
_, kuberoNSErr := exec.Command("kubectl", "create", "namespace", "kubero").Output()
528+
_, kuberoNSErr := exec.Command("kubectl", "create", "namespace", namespace).Output()
461529
if kuberoNSErr != nil {
462-
fmt.Println("Failed to run command to create the namespace. Try runnig this command manually: kubectl create namespace kubero")
530+
fmt.Println("Failed to run command to create the namespace. Try runnig this command manually: kubectl create namespace " + namespace)
463531
log.Fatal(kuberoNSErr)
464532
} else {
465-
cfmt.Println("{{✓ Kubero Namespace created}}::lightGreen")
533+
cfmt.Printf("{{✓ Namespace %s created}}::lightGreen\n", namespace)
466534
}
467535
}
536+
}
537+
538+
func installKuberoUi() {
539+
540+
ingressInstall := promptLine("7) Install Kubero UI", "[y,n]", "y")
541+
if ingressInstall != "y" {
542+
return
543+
}
544+
545+
createNamespace("kubero")
468546

469547
kuberoSecretInstalled, _ := exec.Command("kubectl", "get", "secret", "kubero-secrets", "-n", "kubero").Output()
470548
if len(kuberoSecretInstalled) > 0 {
@@ -636,7 +714,14 @@ func installKuberoUi() {
636714

637715
}
638716

639-
kuberoUIconsole := promptLine("Enable Console Access to running containers", "[y/n]", "n")
717+
if monitoringInstalled {
718+
kuberoUIConfig.Spec.Prometheus.Enabled = true
719+
kuberoUIConfig.Spec.Prometheus.Endpoint = promptLine("Prometheus URL", "", "http://kubero-prometheus-server")
720+
} else {
721+
kuberoUIConfig.Spec.Prometheus.Enabled = false
722+
}
723+
724+
kuberoUIconsole := promptLine("Enable Console Access to running containers", "[y/n]", "y")
640725

641726
if kuberoUIconsole == "y" {
642727
kuberoUIConfig.Spec.Kubero.Config.Kubero.Console.Enabled = true
@@ -700,6 +785,61 @@ func installKuberoUi() {
700785

701786
}
702787

788+
func installMonitoring() {
789+
790+
if promptLine("Enable longterm metrics", "[y/n]", "y") == "y" {
791+
monitoringInstalled = true
792+
} else {
793+
monitoringInstalled = false
794+
return
795+
}
796+
797+
createNamespace("kubero")
798+
799+
spinner := spinner.New("enable metrics")
800+
if promptLine("Create local Prometheus instance", "[y/n]", "y") == "y" {
801+
URL := "https://raw.githubusercontent.com/kubero-dev/kubero-operator/main/config/samples/application_v1alpha1_kuberoprometheus.yaml"
802+
spinner.Start("run command : kubectl apply -f " + URL)
803+
_, ingressErr := exec.Command("kubectl", "apply", "-n", "kubero", "-f", URL).Output()
804+
if ingressErr != nil {
805+
spinner.Error("Failed to run command. Try runnig this command manually: kubectl apply -f " + URL)
806+
log.Fatal(ingressErr)
807+
}
808+
809+
spinner.UpdateMessage("Waiting for Prometheus to be ready")
810+
811+
time.Sleep(5 * time.Second)
812+
// kubectl wait --for=condition=available deployment/kubero -n kubero --timeout=180s
813+
_, olmWaitErr := exec.Command("kubectl", "wait", "--for=condition=available", "deployment/kubero-prometheus-server", "-n", "kubero", "--timeout=180s").Output()
814+
if olmWaitErr != nil {
815+
spinner.Error("Failed to wait for Prometheus to become ready")
816+
log.Fatal(olmWaitErr)
817+
}
818+
spinner.Success("Prometheus installed sucessfully")
819+
}
820+
821+
if promptLine("Enable Kubemetrtics", "[y/n]", "y") == "y" {
822+
spinner.Start("run command : kubectl patch kuberoes kubero -n kubero --type=merge")
823+
824+
patch := `{
825+
"spec": {
826+
"prometheus": {
827+
"kube-state-metrics": {
828+
"enabled": true
829+
}
830+
}
831+
}
832+
}`
833+
834+
_, patchResult := exec.Command("kubectl", "patch", "kuberoprometheuses", "kubero-prometheus", "-n", "kubero", "--type=merge", "-p", patch).Output()
835+
if patchResult != nil {
836+
spinner.Error("Failed to patch the kubero prometheus CRD to enable kube metrics", patchResult.Error(), patch)
837+
}
838+
spinner.Success("metrics enabled sucessfully")
839+
840+
}
841+
}
842+
703843
func installCertManager() {
704844

705845
install := promptLine("5) Install SSL Certmanager", "[y,n]", "y")

cmd/kuberoCli/install.types.go

+4
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,10 @@ type KuberoUIConfig struct {
152152
} `yaml:"podAnnotations"`
153153
PodSecurityContext struct {
154154
} `yaml:"podSecurityContext"`
155+
Prometheus struct {
156+
Enabled bool `yaml:"enabled"`
157+
Endpoint string `yaml:"endpoint"`
158+
} `yaml:"prometheus,omitempty"`
155159
Registry struct {
156160
Enabled bool `yaml:"enabled"`
157161
Create bool `yaml:"create"`

0 commit comments

Comments
 (0)