Skip to content

Commit

Permalink
add static config
Browse files Browse the repository at this point in the history
Signed-off-by: Thibault Mange <[email protected]>
  • Loading branch information
thibaultmg committed Jul 25, 2024
1 parent f18d2f4 commit e1cd6ca
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 57 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@ type ObservabilityAddonReconciler struct {
HubNamespace string
ServiceAccountName string
InstallPrometheus bool
HostIP string
}

// +kubebuilder:rbac:groups=observability.open-cluster-management.io.open-cluster-management.io,resources=observabilityaddons,verbs=get;list;watch;create;update;patch;delete
Expand Down Expand Up @@ -251,7 +250,7 @@ func (r *ObservabilityAddonReconciler) Reconcile(ctx context.Context, req ctrl.R
}

if len(microshiftVersion) > 0 {
mcs := microshift.NewMicroshift(r.Client, r.Namespace, r.HostIP)
mcs := microshift.NewMicroshift(r.Client, r.Namespace)
toDeploy, err = mcs.Render(ctx, toDeploy)
if err != nil {
return ctrl.Result{}, fmt.Errorf("failed to render microshift templates: %w", err)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,6 @@ func TestIntegrationReconcileMicroshift(t *testing.T) {
reconciler := ObservabilityAddonReconciler{
Client: k8sClientSpoke,
HubClient: hubClientWithReload,
HostIP: "192.168.10.10",
Scheme: scheme,
IsHubMetricsCollector: false,
Namespace: testNamespace,
Expand Down
90 changes: 36 additions & 54 deletions operators/endpointmetrics/pkg/microshift/microshift.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"fmt"

promv1 "github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring/v1"
promcommon "github.com/prometheus/common/config"
batchv1 "k8s.io/api/batch/v1"
corev1 "k8s.io/api/core/v1"
rbacv1 "k8s.io/api/rbac/v1"
Expand All @@ -20,24 +21,23 @@ import (
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/intstr"
"sigs.k8s.io/controller-runtime/pkg/client"
)

const (
etcdClientCertSecretName = "etcd-client-cert" //nolint:gosec
etcdClientCertSecretName = "etcd-client-cert" //nolint:gosec
prometheusScrapeCfgSecret = "prometheus-scrape-config"
scrapeConfigKey = "scrape-config.yaml"
)

type Microshift struct {
client client.Client
addonNamespace string
hostIP string
}

func NewMicroshift(c client.Client, addonNs, hostIP string) *Microshift {
func NewMicroshift(c client.Client, addonNs string) *Microshift {
return &Microshift{
addonNamespace: addonNs,
hostIP: hostIP,
client: c,
}
}
Expand Down Expand Up @@ -80,6 +80,13 @@ func (m *Microshift) renderPrometheus(res []*unstructured.Unstructured) error {

prom.Spec.Secrets = append(prom.Spec.Secrets, etcdClientCertSecretName)
prom.Spec.HostNetwork = true
// add scrape config for etcd that is running on the host
prom.Spec.AdditionalScrapeConfigs = &corev1.SecretKeySelector{
LocalObjectReference: corev1.LocalObjectReference{
Name: prometheusScrapeCfgSecret,
},
Key: scrapeConfigKey,
}

promRes.Object, err = runtime.DefaultUnstructuredConverter.ToUnstructured(prom)
if err != nil {
Expand Down Expand Up @@ -344,70 +351,45 @@ func (m *Microshift) renderCronJobExposingMicroshiftSecrets() ([]*unstructured.U
func (m *Microshift) renderEtcdResources() ([]*unstructured.Unstructured, error) {
ret := []*unstructured.Unstructured{}

// Expose etcd endpoint in the addon namespace
endpoint := &corev1.Endpoints{
TypeMeta: metav1.TypeMeta{
APIVersion: "v1",
Kind: "Endpoints",
// create secret containing scrape config for etcd running on the host
scrapeCfg := ScrapeConfig{}
scrapeCfg.JobName = "etcd"
scrapeCfg.Scheme = "https"
scrapeCfg.HTTPClientConfig = promcommon.HTTPClientConfig{
TLSConfig: promcommon.TLSConfig{
CertFile: fmt.Sprintf("/etc/prometheus/secrets/%s/ca.crt", etcdClientCertSecretName),
KeyFile: fmt.Sprintf("/etc/prometheus/secrets/%s/ca.key", etcdClientCertSecretName),
CAFile: fmt.Sprintf("/etc/prometheus/secrets/%s/ca.crt", etcdClientCertSecretName),
},
ObjectMeta: metav1.ObjectMeta{
Name: "etcd",
Namespace: m.addonNamespace,
Labels: map[string]string{
"app": "etcd",
},
},
Subsets: []corev1.EndpointSubset{
{
Addresses: []corev1.EndpointAddress{
{
IP: m.hostIP,
},
},
Ports: []corev1.EndpointPort{
{
Name: "metrics",
Port: 2381,
Protocol: corev1.ProtocolTCP,
},
},
},
}
scrapeCfg.StaticConfigs = []StaticConfig{
{
Targets: []string{"localhost:2381"},
},
}
scapeCfgs := ScrapeConfigs{
ScrapeConfigs: []ScrapeConfig{scrapeCfg},
}

unstructuredEndpoint, err := convertToUnstructured(endpoint)
scrapeCfgsYaml, err := scapeCfgs.MarshalYAML()
if err != nil {
return nil, fmt.Errorf("failed to convert endpoint to unstructured: %w", err)
return nil, fmt.Errorf("failed to marshal scrape config: %w", err)
}
ret = append(ret, unstructuredEndpoint)

service := &corev1.Service{
scrapeSecret := &corev1.Secret{
TypeMeta: metav1.TypeMeta{
APIVersion: "v1",
Kind: "Service",
Kind: "Secret",
},
ObjectMeta: metav1.ObjectMeta{
Name: "etcd",
Name: prometheusScrapeCfgSecret,
Namespace: m.addonNamespace,
Labels: map[string]string{
"app": "etcd",
},
},
Spec: corev1.ServiceSpec{
Ports: []corev1.ServicePort{
{
Name: "metrics",
Port: 2381,
TargetPort: intstr.FromInt(2381),
},
},
Selector: map[string]string{
"app": "etcd",
},
Data: map[string][]byte{
scrapeConfigKey: scrapeCfgsYaml,
},
}

unstructuredService, err := convertToUnstructured(service)
unstructuredService, err := convertToUnstructured(scrapeSecret)
if err != nil {
return nil, fmt.Errorf("failed to convert service to unstructured: %w", err)
}
Expand Down
27 changes: 27 additions & 0 deletions operators/endpointmetrics/pkg/microshift/scrape.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package microshift

import (
promcfg "github.com/prometheus/prometheus/config"
"gopkg.in/yaml.v2"
)

type ScrapeConfigs struct {
ScrapeConfigs []ScrapeConfig `yaml:"scrape_configs"`
}

type ScrapeConfig struct {
promcfg.ScrapeConfig `yaml:",inline"`
StaticConfigs []StaticConfig `yaml:"static_configs,omitempty"`
}

type StaticConfig struct {
Targets []string `yaml:"targets"`
}

func (sc ScrapeConfigs) MarshalYAML() ([]byte, error) {
ret, err := yaml.Marshal(sc)
if err != nil {
return nil, err
}
return ret, nil
}

0 comments on commit e1cd6ca

Please sign in to comment.