|
| 1 | +/* |
| 2 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 | +you may not use this file except in compliance with the License. |
| 4 | +You may obtain a copy of the License at |
| 5 | +
|
| 6 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | +
|
| 8 | +Unless required by applicable law or agreed to in writing, software |
| 9 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | +See the License for the specific language governing permissions and |
| 12 | +limitations under the License. |
| 13 | +*/ |
| 14 | + |
| 15 | +package addons |
| 16 | + |
| 17 | +import ( |
| 18 | + "bytes" |
| 19 | + "context" |
| 20 | + "fmt" |
| 21 | + "html/template" |
| 22 | + |
| 23 | + "github.com/awslabs/kubernetes-iteration-toolkit/substrate/pkg/apis/v1alpha1" |
| 24 | + "github.com/awslabs/kubernetes-iteration-toolkit/substrate/pkg/utils/helm" |
| 25 | + "github.com/awslabs/kubernetes-iteration-toolkit/substrate/pkg/utils/kubectl" |
| 26 | + "sigs.k8s.io/controller-runtime/pkg/reconcile" |
| 27 | +) |
| 28 | + |
| 29 | +type PrometheusStack struct{} |
| 30 | + |
| 31 | +func (p *PrometheusStack) Create(ctx context.Context, substrate *v1alpha1.Substrate) (reconcile.Result, error) { |
| 32 | + if !substrate.Status.IsReady() { |
| 33 | + return reconcile.Result{Requeue: true}, nil |
| 34 | + } |
| 35 | + if err := helm.NewClient(*substrate.Status.Cluster.KubeConfig).Apply(ctx, &helm.Chart{ |
| 36 | + Namespace: "monitoring", |
| 37 | + Name: "kube-prometheus-stack", |
| 38 | + Repository: "https://github.com/prometheus-community/helm-charts/releases/download/kube-prometheus-stack-34.0.0/", |
| 39 | + Version: "34.0.0", |
| 40 | + CreateNamespace: true, |
| 41 | + Values: map[string]interface{}{ |
| 42 | + "coreDns": map[string]interface{}{"enabled": false}, |
| 43 | + "kubeProxy": map[string]interface{}{"enabled": false}, |
| 44 | + "kubeEtcd": map[string]interface{}{"enabled": false}, |
| 45 | + "alertmanager": map[string]interface{}{"enabled": false}, |
| 46 | + "kubeScheduler": map[string]interface{}{"enabled": false}, |
| 47 | + "kubeApiServer": map[string]interface{}{"enabled": false}, |
| 48 | + "kubeStateMetrics": map[string]interface{}{"enabled": false}, |
| 49 | + "kubeControllerManager": map[string]interface{}{"enabled": false}, |
| 50 | + "prometheus": map[string]interface{}{"serviceMonitor": map[string]interface{}{"selfMonitor": false}}, |
| 51 | + "prometheusOperator": map[string]interface{}{"serviceMonitor": map[string]interface{}{"selfMonitor": false}}, |
| 52 | + }, |
| 53 | + }); err != nil { |
| 54 | + return reconcile.Result{}, fmt.Errorf("applying chart, %w", err) |
| 55 | + } |
| 56 | + // configure podmonitors |
| 57 | + client, err := kubectl.NewClient(*substrate.Status.Cluster.KubeConfig) |
| 58 | + if err != nil { |
| 59 | + return reconcile.Result{}, fmt.Errorf("initializing client, %w", err) |
| 60 | + } |
| 61 | + for _, name := range []string{"apiserver", "etcd"} { |
| 62 | + var buf bytes.Buffer |
| 63 | + tmpl := template.Must(template.New("Text").Parse(podMonitorTemplate)) |
| 64 | + err := tmpl.Execute(&buf, struct{ ComponentName string }{ComponentName: name}) |
| 65 | + if err != nil { |
| 66 | + return reconcile.Result{}, fmt.Errorf("error when executing template, %w", err) |
| 67 | + } |
| 68 | + if err := client.ApplyYAML(ctx, buf.Bytes()); err != nil { |
| 69 | + return reconcile.Result{}, fmt.Errorf("applying pod monitors, %w", err) |
| 70 | + } |
| 71 | + } |
| 72 | + return reconcile.Result{}, nil |
| 73 | +} |
| 74 | + |
| 75 | +func (p *PrometheusStack) Delete(_ context.Context, _ *v1alpha1.Substrate) (reconcile.Result, error) { |
| 76 | + return reconcile.Result{}, nil |
| 77 | +} |
| 78 | + |
| 79 | +var podMonitorTemplate = ` |
| 80 | +apiVersion: monitoring.coreos.com/v1 |
| 81 | +kind: PodMonitor |
| 82 | +metadata: |
| 83 | + name: {{ .ComponentName }}-pods |
| 84 | + namespace: monitoring |
| 85 | + labels: |
| 86 | + release: kube-prometheus-stack |
| 87 | +spec: |
| 88 | + jobLabel: {{ .ComponentName }}-guest |
| 89 | + namespaceSelector: |
| 90 | + matchNames: |
| 91 | + - default |
| 92 | + selector: |
| 93 | + matchLabels: |
| 94 | + kit.k8s.sh/app: {{ .ComponentName }} |
| 95 | + podMetricsEndpoints: |
| 96 | + - port: metrics |
| 97 | +` |
0 commit comments