Files
my-rook-config/pkg/operator/k8sutil/prometheus.go
T
Joshua Hoblitt b29e89ef67 k8sutil: remove unused helper functions
Remove dead code from the k8sutil package; none of these symbols had any
callers anywhere in the repo (verified with deadcode, staticcheck U1000,
and repo-wide grep, tests included):

- patcher.go: delete file (Patcher type, NewPatcher, Patch)
- replicaset.go: delete file (DeleteReplicaSet)
- customresource.go: WatchCR (the CustomResource type remains in use)
- deployment.go: WaitForDeploymentImage
- job.go: WaitForJobCompletion
- k8sutil.go: GetK8SVersion
- pod.go: ConfigOverrideMount, ConfigOverrideVolume, GetPodLog
- prometheus.go: DeleteServiceMonitor
- service.go: DeleteService
- volume.go: NodeConfigURI, BinariesMountInfo, YamlToVolumes,
  YamlToVolumeMounts, and the BinariesMountPath const (only used by the
  removed BinariesMountInfo)

Now-unused imports are dropped from each file. The shared
deleteResourceAndWait helper remains (still used by DeleteDeployment).

Signed-off-by: Joshua Hoblitt <josh@hoblitt.com>
2026-06-09 15:05:19 -07:00

106 lines
3.5 KiB
Go

/*
Copyright 2020 The Rook Authors. All rights reserved.
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 k8sutil for Kubernetes helpers.
package k8sutil
import (
"context"
"fmt"
monitoringv1 "github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring/v1"
monitoringclient "github.com/prometheus-operator/prometheus-operator/pkg/client/versioned"
"github.com/rook/rook/pkg/clusterd"
kerrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/utils/ptr"
)
func getMonitoringClient(context *clusterd.Context) (*monitoringclient.Clientset, error) {
client, err := monitoringclient.NewForConfig(context.KubeConfig)
if err != nil {
return nil, fmt.Errorf("failed to get monitoring client. %v", err)
}
return client, nil
}
// GetServiceMonitor creates serviceMonitor object template
func GetServiceMonitor(name string, namespace string, portName string) *monitoringv1.ServiceMonitor {
return &monitoringv1.ServiceMonitor{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: namespace,
Labels: map[string]string{
"team": "rook",
},
},
Spec: monitoringv1.ServiceMonitorSpec{
NamespaceSelector: monitoringv1.NamespaceSelector{
MatchNames: []string{
namespace,
},
},
Selector: metav1.LabelSelector{
MatchLabels: map[string]string{
"app": name,
"rook_cluster": namespace,
},
},
Endpoints: []monitoringv1.Endpoint{
{
Port: portName,
Path: "/metrics",
Interval: "10s",
HonorLabels: true,
RelabelConfigs: []monitoringv1.RelabelConfig{{
TargetLabel: "cluster",
Replacement: ptr.To(namespace),
}},
},
},
},
}
}
// CreateOrUpdateServiceMonitor creates serviceMonitor object or an error
func CreateOrUpdateServiceMonitor(context *clusterd.Context, ctx context.Context, serviceMonitorDefinition *monitoringv1.ServiceMonitor) (*monitoringv1.ServiceMonitor, error) {
name := serviceMonitorDefinition.GetName()
namespace := serviceMonitorDefinition.GetNamespace()
logger.Debugf("creating servicemonitor %s", name)
client, err := getMonitoringClient(context)
if err != nil {
return nil, fmt.Errorf("failed to get monitoring client. %v", err)
}
oldSm, err := client.MonitoringV1().ServiceMonitors(namespace).Get(ctx, name, metav1.GetOptions{})
if err != nil {
if kerrors.IsNotFound(err) {
sm, err := client.MonitoringV1().ServiceMonitors(namespace).Create(ctx, serviceMonitorDefinition, metav1.CreateOptions{})
if err != nil {
return nil, fmt.Errorf("failed to create servicemonitor. %v", err)
}
return sm, nil
}
return nil, fmt.Errorf("failed to retrieve servicemonitor. %v", err)
}
oldSm.Spec = serviceMonitorDefinition.Spec
oldSm.ObjectMeta.Labels = serviceMonitorDefinition.ObjectMeta.Labels
sm, err := client.MonitoringV1().ServiceMonitors(namespace).Update(ctx, oldSm, metav1.UpdateOptions{})
if err != nil {
return nil, fmt.Errorf("failed to update servicemonitor. %v", err)
}
return sm, nil
}