-
Notifications
You must be signed in to change notification settings - Fork 787
/
create_addon_prometheus.go
134 lines (120 loc) · 4.06 KB
/
create_addon_prometheus.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package cmd
import (
"fmt"
"io/ioutil"
"path"
"strings"
"github.com/jenkins-x/jx/pkg/helm"
"github.com/jenkins-x/jx/pkg/kube"
"github.com/jenkins-x/jx/pkg/util"
"github.com/pborman/uuid"
"github.com/pkg/errors"
"github.com/spf13/cobra"
core_v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1"
meta_v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/yaml"
)
type CreateAddonPrometheusOptions struct {
CreateOptions
Namespace string
Version string
ReleaseName string
HelmUpdate bool
SetValues string
Password string
}
func NewCmdCreateAddonPrometheus(commonOpts *CommonOptions) *cobra.Command {
options := &CreateAddonPrometheusOptions{
CreateOptions: CreateOptions{
CommonOptions: commonOpts,
},
}
cmd := &cobra.Command{
Use: "prometheus",
Short: "Creates a prometheus addon",
Long: `Creates a prometheus addon.
By default Prometheus Server is exposed via Ingress entry http://prometheus.jx.your.domain.com secured
with basic authentication. Admin username is 'admin' and default password is 'admin' (see --password flag).
`,
Run: func(cmd *cobra.Command, args []string) {
options.Cmd = cmd
options.Args = args
err := options.Run()
CheckErr(err)
},
}
options.addFlags(cmd, kube.DefaultNamespace, "prometheus")
return cmd
}
func (options *CreateAddonPrometheusOptions) addFlags(cmd *cobra.Command, defaultNamespace string, defaultOptionRelease string) {
cmd.Flags().StringVarP(&options.Namespace, "namespace", "n", defaultNamespace, "The Namespace to install into")
cmd.Flags().StringVarP(&options.ReleaseName, optionRelease, "r", defaultOptionRelease, "The chart release name")
cmd.Flags().BoolVarP(&options.HelmUpdate, "helm-update", "", true, "Should we run helm update first to ensure we use the latest version")
cmd.Flags().StringVarP(&options.SetValues, "set", "s", "", "The chart set values (can specify multiple or separate values with commas: key1=val1,key2=val2)")
cmd.Flags().StringVarP(&options.Password, "password", "", "admin", "Admin password to access Prometheus web UI.")
}
func (o *CreateAddonPrometheusOptions) Run() error {
err := o.ensureHelm()
if err != nil {
return errors.Wrap(err, "failed to ensure that helm is present")
}
data := make(map[string][]byte)
hash := util.HashPassword(o.Password)
data[kube.AUTH] = []byte(fmt.Sprintf("admin:{SHA}%s", hash))
sec := &core_v1.Secret{
Data: data,
ObjectMeta: v1.ObjectMeta{
Name: "prometheus-ingress",
},
}
client, err := o.KubeClient()
if err != nil {
return err
}
_, err = client.CoreV1().Secrets(o.Namespace).Create(sec)
if err != nil {
return fmt.Errorf("cannot create secret %s in target namespace %s: %v", "prometheus-ingress", o.Namespace, err)
}
ingressConfig, err := client.CoreV1().ConfigMaps(o.Namespace).Get("ingress-config", meta_v1.GetOptions{})
if err != nil {
return errors.Wrap(err, "Cannot get ingress config map.")
}
values := map[string]map[string]map[string]interface{}{
"server": {
"ingress": {
"enabled": true,
"hosts": []string{"prometheus.jx." + ingressConfig.Data["domain"]},
"annotations": map[string]string{
"kubernetes.io/ingress.class": "nginx",
"nginx.ingress.kubernetes.io/auth-type": "basic",
"nginx.ingress.kubernetes.io/auth-secret": "prometheus-ingress",
"nginx.ingress.kubernetes.io/auth-realm": "Authentication required to access Prometheus.",
},
},
},
}
valuesBytes, err := yaml.Marshal(values)
if err != nil {
return err
}
prometheusIngressConfig := path.Join("/tmp", "prometheusIngressConfig_"+uuid.New())
err = ioutil.WriteFile(prometheusIngressConfig, valuesBytes, 0644)
if err != nil {
return err
}
setValues := strings.Split(o.SetValues, ",")
err = o.installChartOptions(helm.InstallChartOptions{
ReleaseName: o.ReleaseName,
Chart: "stable/prometheus",
Version: o.Version,
Ns: o.Namespace,
HelmUpdate: o.HelmUpdate,
ValueFiles: []string{prometheusIngressConfig},
SetValues: setValues,
})
if err != nil {
return fmt.Errorf("Failed to install chart %s: %s", "prometheus", err)
}
return nil
}