Skip to content

Commit

Permalink
Merge pull request #21 from prometheus-operator/chore/adding-node-exp…
Browse files Browse the repository at this point in the history
…orter

[CHORE] adding node exporter to the create stack cmd
  • Loading branch information
nicolastakashi authored Jul 12, 2024
2 parents 75c16ff + 1cdf005 commit 3f4b592
Show file tree
Hide file tree
Showing 4 changed files with 284 additions and 15 deletions.
240 changes: 240 additions & 0 deletions internal/builder/nodeExporter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,240 @@
// Copyright 2024 The prometheus-operator Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package builder

import (
"fmt"

monitoringv1 "github.com/prometheus-operator/prometheus-operator/pkg/client/applyconfiguration/monitoring/v1"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
"k8s.io/apimachinery/pkg/util/intstr"
applyCofongiAppsv1 "k8s.io/client-go/applyconfigurations/apps/v1"
applyConfigCorev1 "k8s.io/client-go/applyconfigurations/core/v1"
applyConfigMetav1 "k8s.io/client-go/applyconfigurations/meta/v1"
"k8s.io/utils/ptr"
)

const LatestNodeExporterVersion = "1.8.1"

type NodeExporterBuilder struct {
labels map[string]string
labelSelectors map[string]string
namespace string
manifests NodexExporterManifests
version string
}

type NodexExporterManifests struct {
ServiceAccount *applyConfigCorev1.ServiceAccountApplyConfiguration
DaemonSet *applyCofongiAppsv1.DaemonSetApplyConfiguration
PodMonitor *monitoringv1.PodMonitorApplyConfiguration
}

func NewNodeExporterBuilder(namespace, version string) *NodeExporterBuilder {
return &NodeExporterBuilder{
namespace: namespace,
version: version,
labels: map[string]string{
"app.kubernetes.io/name": "node-exporter",
},
labelSelectors: map[string]string{
"app.kubernetes.io/name": "node-exporter",
},
}
}

func (n *NodeExporterBuilder) WithServiceAccount() *NodeExporterBuilder {
n.manifests.ServiceAccount = &applyConfigCorev1.ServiceAccountApplyConfiguration{
TypeMetaApplyConfiguration: applyConfigMetav1.TypeMetaApplyConfiguration{
Kind: ptr.To("ServiceAccount"),
APIVersion: ptr.To("v1"),
},
ObjectMetaApplyConfiguration: &applyConfigMetav1.ObjectMetaApplyConfiguration{
Name: ptr.To("node-exporter"),
Labels: n.labels,
Namespace: ptr.To(n.namespace),
},
}
return n
}

func (n *NodeExporterBuilder) WithPodMonitor() *NodeExporterBuilder {
n.manifests.PodMonitor = &monitoringv1.PodMonitorApplyConfiguration{
TypeMetaApplyConfiguration: applyConfigMetav1.TypeMetaApplyConfiguration{
Kind: ptr.To("PodMonitor"),
APIVersion: ptr.To("monitoring.coreos.com/v1"),
},
ObjectMetaApplyConfiguration: &applyConfigMetav1.ObjectMetaApplyConfiguration{
Name: ptr.To("node-exporter"),
Labels: n.labels,
Namespace: ptr.To(n.namespace),
},
Spec: &monitoringv1.PodMonitorSpecApplyConfiguration{
JobLabel: ptr.To("app.kubernetes.io/name"),
Selector: &applyConfigMetav1.LabelSelectorApplyConfiguration{
MatchLabels: n.labelSelectors,
},
NamespaceSelector: &monitoringv1.NamespaceSelectorApplyConfiguration{
MatchNames: []string{n.namespace},
},
PodMetricsEndpoints: []monitoringv1.PodMetricsEndpointApplyConfiguration{
{
Port: ptr.To("metrics"),
HonorLabels: ptr.To(true),
FilterRunning: ptr.To(true),
},
},
},
}
return n
}

var nodeExporterArgs = []string{
"--web.listen-address=0.0.0.0:9100",
"--path.sysfs=/host/sys",
"--path.rootfs=/host/root",
"--path.udev.data=/host/root/run/udev/data",
"--no-collector.wifi",
"--no-collector.hwmon",
"--no-collector.btrfs",
"--collector.filesystem.mount-points-exclude=^/(dev|proc|sys|run/k3s/containerd/.+|var/lib/docker/.+|var/lib/kubelet/pods/.+)($|/)",
"--collector.netclass.ignored-devices=^(veth.*|[a-f0-9]{15})$",
"--collector.netdev.device-exclude=^(veth.*|[a-f0-9]{15})$",
}

func (n *NodeExporterBuilder) WithDaemonSet() *NodeExporterBuilder {
n.manifests.DaemonSet = &applyCofongiAppsv1.DaemonSetApplyConfiguration{
TypeMetaApplyConfiguration: applyConfigMetav1.TypeMetaApplyConfiguration{
Kind: ptr.To("DaemonSet"),
APIVersion: ptr.To("apps/v1"),
},
ObjectMetaApplyConfiguration: &applyConfigMetav1.ObjectMetaApplyConfiguration{
Name: ptr.To("node-exporter"),
Labels: n.labels,
Namespace: ptr.To(n.namespace),
},
Spec: &applyCofongiAppsv1.DaemonSetSpecApplyConfiguration{
Selector: &applyConfigMetav1.LabelSelectorApplyConfiguration{
MatchLabels: n.labelSelectors,
},
Template: &applyConfigCorev1.PodTemplateSpecApplyConfiguration{
ObjectMetaApplyConfiguration: &applyConfigMetav1.ObjectMetaApplyConfiguration{
Labels: n.labelSelectors,
},
Spec: &applyConfigCorev1.PodSpecApplyConfiguration{
ServiceAccountName: n.manifests.ServiceAccount.Name,
AutomountServiceAccountToken: ptr.To(true),
Containers: []applyConfigCorev1.ContainerApplyConfiguration{
{
Name: ptr.To("node-exporter"),
Image: ptr.To(fmt.Sprintf("quay.io/prometheus/node-exporter:v%s", n.version)),
Args: nodeExporterArgs,
Ports: []applyConfigCorev1.ContainerPortApplyConfiguration{
{
Name: ptr.To("metrics"),
ContainerPort: ptr.To(int32(9100)),
Protocol: ptr.To(corev1.ProtocolTCP),
},
},
Resources: &applyConfigCorev1.ResourceRequirementsApplyConfiguration{
Requests: &corev1.ResourceList{
"cpu": resource.MustParse("200m"),
"memory": resource.MustParse("200Mi"),
},
Limits: &corev1.ResourceList{
"cpu": resource.MustParse("200m"),
"memory": resource.MustParse("200Mi"),
},
},
SecurityContext: &applyConfigCorev1.SecurityContextApplyConfiguration{
AllowPrivilegeEscalation: ptr.To(false),
ReadOnlyRootFilesystem: ptr.To(true),
Capabilities: &applyConfigCorev1.CapabilitiesApplyConfiguration{
Add: []corev1.Capability{
"SYS_TIME",
},
Drop: []corev1.Capability{
"ALL",
},
},
},
VolumeMounts: []applyConfigCorev1.VolumeMountApplyConfiguration{
{
MountPath: ptr.To("/host/sys"),
MountPropagation: ptr.To(corev1.MountPropagationHostToContainer),
Name: ptr.To("sys"),
ReadOnly: ptr.To(true),
},
{
MountPath: ptr.To("/host/root"),
MountPropagation: ptr.To(corev1.MountPropagationHostToContainer),
Name: ptr.To("root"),
ReadOnly: ptr.To(true),
},
},
},
},
HostNetwork: ptr.To(true),
HostPID: ptr.To(true),
NodeSelector: map[string]string{
"kubernetes.io/os": "linux",
},
PriorityClassName: ptr.To("system-node-critical"),
SecurityContext: &applyConfigCorev1.PodSecurityContextApplyConfiguration{
RunAsGroup: ptr.To(int64(65534)),
RunAsNonRoot: ptr.To(true),
RunAsUser: ptr.To(int64(65534)),
},
Tolerations: []applyConfigCorev1.TolerationApplyConfiguration{
{
Operator: ptr.To(corev1.TolerationOpExists),
},
},
Volumes: []applyConfigCorev1.VolumeApplyConfiguration{
{
Name: ptr.To("sys"),
VolumeSourceApplyConfiguration: applyConfigCorev1.VolumeSourceApplyConfiguration{
HostPath: &applyConfigCorev1.HostPathVolumeSourceApplyConfiguration{
Path: ptr.To("/sys"),
},
},
},
{
Name: ptr.To("root"),
VolumeSourceApplyConfiguration: applyConfigCorev1.VolumeSourceApplyConfiguration{
HostPath: &applyConfigCorev1.HostPathVolumeSourceApplyConfiguration{
Path: ptr.To("/"),
},
},
},
},
},
},
UpdateStrategy: &applyCofongiAppsv1.DaemonSetUpdateStrategyApplyConfiguration{
Type: ptr.To(appsv1.RollingUpdateDaemonSetStrategyType),
RollingUpdate: &applyCofongiAppsv1.RollingUpdateDaemonSetApplyConfiguration{
MaxUnavailable: ptr.To(intstr.FromString("10%")),
},
},
},
}
return n
}

func (n *NodeExporterBuilder) Build() NodexExporterManifests {
return n.manifests
}
7 changes: 3 additions & 4 deletions internal/builder/operator.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import (
applyCofongiAppsv1 "k8s.io/client-go/applyconfigurations/apps/v1"
applyConfigCorev1 "k8s.io/client-go/applyconfigurations/core/v1"
applyConfigMetav1 "k8s.io/client-go/applyconfigurations/meta/v1"
metav1 "k8s.io/client-go/applyconfigurations/meta/v1"
applyConfigRbacv1 "k8s.io/client-go/applyconfigurations/rbac/v1"
"k8s.io/utils/ptr"
)
Expand Down Expand Up @@ -237,8 +236,8 @@ func (o *OperatorBuilder) WithDeployment() *OperatorBuilder {
},
Resources: &applyConfigCorev1.ResourceRequirementsApplyConfiguration{
Requests: &corev1.ResourceList{
"cpu": resource.MustParse("100m"),
"memory": resource.MustParse("100Mi"),
"cpu": resource.MustParse("200m"),
"memory": resource.MustParse("200Mi"),
},
Limits: &corev1.ResourceList{
"cpu": resource.MustParse("200m"),
Expand Down Expand Up @@ -310,7 +309,7 @@ func (o *OperatorBuilder) WithServiceMonitor() *OperatorBuilder {
Namespace: ptr.To(o.namespace),
},
Spec: &monitoringv1.ServiceMonitorSpecApplyConfiguration{
Selector: &metav1.LabelSelectorApplyConfiguration{
Selector: &applyConfigMetav1.LabelSelectorApplyConfiguration{
MatchLabels: o.labelSelectors,
},
Endpoints: []monitoringv1.EndpointApplyConfiguration{
Expand Down
22 changes: 11 additions & 11 deletions internal/builder/prometheus.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ type PrometheusBuilder struct {
labels map[string]string
labelSelectors map[string]string
namespace string
manifets PrometheusManifests
manifests PrometheusManifests
}

type PrometheusManifests struct {
Expand All @@ -54,7 +54,7 @@ func NewPrometheus(namespace string) *PrometheusBuilder {
}

func (p *PrometheusBuilder) WithServiceAccount() *PrometheusBuilder {
p.manifets.ServiceAccount = &applyConfigCorev1.ServiceAccountApplyConfiguration{
p.manifests.ServiceAccount = &applyConfigCorev1.ServiceAccountApplyConfiguration{
TypeMetaApplyConfiguration: applyConfigMetav1.TypeMetaApplyConfiguration{
Kind: ptr.To("ServiceAccount"),
APIVersion: ptr.To("v1"),
Expand All @@ -69,7 +69,7 @@ func (p *PrometheusBuilder) WithServiceAccount() *PrometheusBuilder {
}

func (p *PrometheusBuilder) WithClusterRole() *PrometheusBuilder {
p.manifets.ClusterRole = &applyConfigRbacv1.ClusterRoleApplyConfiguration{
p.manifests.ClusterRole = &applyConfigRbacv1.ClusterRoleApplyConfiguration{
TypeMetaApplyConfiguration: applyConfigMetav1.TypeMetaApplyConfiguration{
Kind: ptr.To("ClusterRole"),
APIVersion: ptr.To("rbac.authorization.k8s.io/v1"),
Expand Down Expand Up @@ -105,7 +105,7 @@ func (p *PrometheusBuilder) WithClusterRole() *PrometheusBuilder {
}

func (p *PrometheusBuilder) WithClusterRoleBinding() *PrometheusBuilder {
p.manifets.ClusterRoleBinding = &applyConfigRbacv1.ClusterRoleBindingApplyConfiguration{
p.manifests.ClusterRoleBinding = &applyConfigRbacv1.ClusterRoleBindingApplyConfiguration{
TypeMetaApplyConfiguration: applyConfigMetav1.TypeMetaApplyConfiguration{
Kind: ptr.To("ClusterRoleBinding"),
APIVersion: ptr.To("rbac.authorization.k8s.io/v1"),
Expand All @@ -118,12 +118,12 @@ func (p *PrometheusBuilder) WithClusterRoleBinding() *PrometheusBuilder {
RoleRef: &applyConfigRbacv1.RoleRefApplyConfiguration{
APIGroup: ptr.To("rbac.authorization.k8s.io"),
Kind: ptr.To("ClusterRole"),
Name: p.manifets.ClusterRole.Name,
Name: p.manifests.ClusterRole.Name,
},
Subjects: []applyConfigRbacv1.SubjectApplyConfiguration{
{
Kind: ptr.To("ServiceAccount"),
Name: p.manifets.ServiceAccount.Name,
Name: p.manifests.ServiceAccount.Name,
Namespace: ptr.To(p.namespace),
},
},
Expand All @@ -132,7 +132,7 @@ func (p *PrometheusBuilder) WithClusterRoleBinding() *PrometheusBuilder {
}

func (p *PrometheusBuilder) WithPrometheus() *PrometheusBuilder {
p.manifets.Prometheus = &monitoringv1.PrometheusApplyConfiguration{
p.manifests.Prometheus = &monitoringv1.PrometheusApplyConfiguration{
TypeMetaApplyConfiguration: applyConfigMetav1.TypeMetaApplyConfiguration{
Kind: ptr.To("Prometheus"),
APIVersion: ptr.To("monitoring.coreos.com/v1"),
Expand All @@ -144,7 +144,7 @@ func (p *PrometheusBuilder) WithPrometheus() *PrometheusBuilder {
},
Spec: &monitoringv1.PrometheusSpecApplyConfiguration{
CommonPrometheusFieldsApplyConfiguration: monitoringv1.CommonPrometheusFieldsApplyConfiguration{
ServiceAccountName: p.manifets.ServiceAccount.Name,
ServiceAccountName: p.manifests.ServiceAccount.Name,
ServiceMonitorSelector: &metav1.LabelSelectorApplyConfiguration{},
ServiceMonitorNamespaceSelector: &metav1.LabelSelectorApplyConfiguration{},
PodMonitorSelector: &metav1.LabelSelectorApplyConfiguration{},
Expand Down Expand Up @@ -173,7 +173,7 @@ func (p *PrometheusBuilder) WithPrometheus() *PrometheusBuilder {
}

func (p *PrometheusBuilder) WithService() *PrometheusBuilder {
p.manifets.Service = &applyConfigCorev1.ServiceApplyConfiguration{
p.manifests.Service = &applyConfigCorev1.ServiceApplyConfiguration{
TypeMetaApplyConfiguration: applyConfigMetav1.TypeMetaApplyConfiguration{
Kind: ptr.To("Service"),
APIVersion: ptr.To("v1"),
Expand All @@ -198,7 +198,7 @@ func (p *PrometheusBuilder) WithService() *PrometheusBuilder {
}

func (p *PrometheusBuilder) WithServiceMonitor() *PrometheusBuilder {
p.manifets.ServiceMonitor = &monitoringv1.ServiceMonitorApplyConfiguration{
p.manifests.ServiceMonitor = &monitoringv1.ServiceMonitorApplyConfiguration{
TypeMetaApplyConfiguration: applyConfigMetav1.TypeMetaApplyConfiguration{
Kind: ptr.To("ServiceMonitor"),
APIVersion: ptr.To("monitoring.coreos.com/v1"),
Expand All @@ -224,5 +224,5 @@ func (p *PrometheusBuilder) WithServiceMonitor() *PrometheusBuilder {
}

func (p *PrometheusBuilder) Build() PrometheusManifests {
return p.manifets
return p.manifests
}
Loading

0 comments on commit 3f4b592

Please sign in to comment.