Add serviceMonitor and RBAC roles if directed by KubeVirt CR
@@ -25,6 +25,7 @@ import (
"fmt"
"strings"
+ promv1 "github.com/coreos/prometheus-operator/pkg/apis/monitoring/v1"
"github.com/ghodss/yaml"
secv1 "github.com/openshift/api/security/v1"
appsv1 "k8s.io/api/apps/v1"
@@ -78,6 +79,7 @@ type InstallStrategy struct {
customSCCPrivileges []*customSCCPrivilegedAccounts
sccs []*secv1.SecurityContextConstraints
+ serviceMonitors []*promv1.ServiceMonitor
}
func NewInstallStrategyConfigMap(config *operatorutil.KubeVirtDeploymentConfig) (*corev1.ConfigMap, error) {
@@ -174,6 +176,9 @@ func dumpInstallStrategyToBytes(strategy *InstallStrategy) []byte {
for _, entry := range strategy.sccs {
marshalutil.MarshallObject(entry, writer)
}
+ for _, entry := range strategy.serviceMonitors {
+ marshalutil.MarshallObject(entry, writer)
+ }
writer.Flush()
return b.Bytes()
@@ -195,6 +200,10 @@ func GenerateCurrentInstallStrategy(config *operatorutil.KubeVirtDeploymentConfi
rbaclist = append(rbaclist, rbac.GetAllController(config.GetNamespace())...)
rbaclist = append(rbaclist, rbac.GetAllHandler(config.GetNamespace())...)
+ monitorNamespace := config.GetMonitorNamespace()
+ monitorServiceAccount := config.GetMonitorServiceAccount()
+ rbaclist = append(rbaclist, rbac.GetAllServiceMonitor(config.GetNamespace(), monitorNamespace, monitorServiceAccount)...)
+
for _, entry := range rbaclist {
cr, ok := entry.(*rbacv1.ClusterRole)
if ok {
@@ -222,6 +231,7 @@ func GenerateCurrentInstallStrategy(config *operatorutil.KubeVirtDeploymentConfi
}
strategy.services = append(strategy.services, components.NewPrometheusService(config.GetNamespace()))
+ strategy.serviceMonitors = append(strategy.serviceMonitors, components.NewServiceMonitorCR(config.GetNamespace(), monitorNamespace, true))
strategy.services = append(strategy.services, components.NewApiServerService(config.GetNamespace()))
apiDeployment, err := components.NewApiServerDeployment(config.GetNamespace(), config.GetImageRegistry(), config.GetApiVersion(), config.GetImagePullPolicy(), config.GetVerbosity())
@@ -407,6 +417,12 @@ func loadInstallStrategyFromBytes(data string) (*InstallStrategy, error) {
return nil, err
}
strategy.sccs = append(strategy.sccs, s)
+ case "ServiceMonitor":
+ sm := &promv1.ServiceMonitor{}
+ if err := yaml.Unmarshal([]byte(entry), &sm); err != nil {
+ return nil, err
+ }
+ strategy.serviceMonitors = append(strategy.serviceMonitors, sm)
default:
return nil, fmt.Errorf("UNKNOWN TYPE %s detected", obj.Kind)
