Skip to content

Commit

Permalink
OCPBUGS-39417: Add service account and token for service monitoring
Browse files Browse the repository at this point in the history
Adding service account and token needed for ServiceMonitoring, this will create a new service account compliance-operator-metrics and use create a metric token, and we will use that token for ServiceMonitoring.
  • Loading branch information
Vincent056 committed Sep 5, 2024
1 parent e5621e9 commit 6521a99
Show file tree
Hide file tree
Showing 9 changed files with 38 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ roleRef:
kind: ClusterRole
name: compliance-operator-metrics
subjects:
- kind: ServiceAccount
name: compliance-operator-metrics
namespace: openshift-compliance
- kind: ServiceAccount
name: prometheus-k8s
namespace: openshift-monitoring
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
apiVersion: v1
kind: ServiceAccount
metadata:
creationTimestamp: null
name: compliance-operator-metrics
7 changes: 7 additions & 0 deletions bundle/manifests/metrics-token_v1_secret.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
apiVersion: v1
kind: Secret
metadata:
annotations:
kubernetes.io/service-account.name: compliance-operator-metrics
name: metrics-token
type: kubernetes.io/service-account-token
34 changes: 3 additions & 31 deletions cmd/manager/operator.go
Original file line number Diff line number Diff line change
Expand Up @@ -681,7 +681,7 @@ func getDefaultRoles(platform PlatformType) []string {
return defaultRolesPerPlatform[PlatformGeneric]
}

func generateOperatorServiceMonitor(service *v1.Service, namespace, secretName string) *monitoring.ServiceMonitor {
func generateOperatorServiceMonitor(service *v1.Service, namespace string) *monitoring.ServiceMonitor {
serviceMonitor := GenerateServiceMonitor(service)
for i := range serviceMonitor.Spec.Endpoints {
if serviceMonitor.Spec.Endpoints[i].Port == ctrlMetrics.ControllerMetricsServiceName {
Expand All @@ -691,7 +691,7 @@ func generateOperatorServiceMonitor(service *v1.Service, namespace, secretName s
Type: "Bearer",
Credentials: &v1.SecretKeySelector{
LocalObjectReference: v1.LocalObjectReference{
Name: secretName,
Name: "metrics-token",
},
Key: "token",
},
Expand All @@ -707,25 +707,6 @@ func generateOperatorServiceMonitor(service *v1.Service, namespace, secretName s
return serviceMonitor
}

func getSecretNameForServiceAccount(clientset *kubernetes.Clientset, namespace string, serviceAccountName string) (string, error) {
// List all secrets in the specified namespace
secrets, err := clientset.CoreV1().Secrets(namespace).List(context.TODO(), metav1.ListOptions{})
if err != nil {
return "", err
}

// Iterate through the secrets to find the one associated with the service account
for _, secret := range secrets.Items {
if secret.Annotations != nil {
if saName, exists := secret.Annotations["kubernetes.io/service-account.name"]; exists && saName == serviceAccountName {
return secret.Name, nil
}
}
}

return "", errors.New("secret for service account not found")
}

// createOrUpdateServiceMonitor creates or updates the ServiceMonitor if it already exists.
func createOrUpdateServiceMonitor(ctx context.Context, mClient *monclientv1.MonitoringV1Client,
namespace string, serviceMonitor *monitoring.ServiceMonitor) error {
Expand Down Expand Up @@ -763,16 +744,7 @@ func handleServiceMonitor(ctx context.Context, cfg *rest.Config, mClient *moncli
return nil
}

serviceAccountName := "compliance-operator"
secretName, err := getSecretNameForServiceAccount(kubeClient, namespace, serviceAccountName)
if err != nil {
if kerr.IsNotFound(err) {
log.Infof("Unable to find secret associated with %s service account: %s", serviceAccountName, err)
} else {
log.Errorf("Failed to retrieve secret associated with %s service account for setting up metrics monitor: %s", serviceAccountName, err)
}
}
serviceMonitor := generateOperatorServiceMonitor(service, namespace, secretName)
serviceMonitor := generateOperatorServiceMonitor(service, namespace)

return createOrUpdateServiceMonitor(ctx, mClient, namespace, serviceMonitor)
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/manager/operator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ var _ = Describe("Operator Startup Function tests", func() {
When("Installing to non-controlled namespace", func() {
It("ServiceMonitor is generated with the proper TLSConfig ServerName", func() {
metricService := operatorMetricService("foobar")
sm := generateOperatorServiceMonitor(metricService, "foobar", "secret")
sm := generateOperatorServiceMonitor(metricService, "foobar")
controllerMetricServiceFound := false
for _, ep := range sm.Spec.Endpoints {
if ep.Port == metrics.ControllerMetricsServiceName && ep.TLSConfig != nil {
Expand Down
4 changes: 3 additions & 1 deletion config/rbac/kustomization.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,6 @@ resources:
- tailoredprofile_editor_role.yaml
- tailoredprofile_viewer_role.yaml
- metrics_cluster_role.yaml
- metrics_role_binding.yaml
- metrics_role_binding.yaml
- metrics_secret.yaml
- metrics_service_account.yaml
5 changes: 4 additions & 1 deletion config/rbac/metrics_role_binding.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ roleRef:
kind: ClusterRole
name: compliance-operator-metrics
subjects:
- kind: ServiceAccount
name: compliance-operator-metrics
namespace: openshift-compliance
- kind: ServiceAccount
name: prometheus-k8s
namespace: openshift-monitoring
namespace: openshift-monitoring
8 changes: 8 additions & 0 deletions config/rbac/metrics_secret.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
apiVersion: v1
kind: Secret
metadata:
name: metrics-token
namespace: openshift-compliance
annotations:
kubernetes.io/service-account.name: compliance-operator-metrics
type: kubernetes.io/service-account-token
4 changes: 4 additions & 0 deletions config/rbac/metrics_service_account.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
apiVersion: v1
kind: ServiceAccount
metadata:
name: compliance-operator-metrics

0 comments on commit 6521a99

Please sign in to comment.