github.com/1aal/kubeblocks@v0.0.0-20231107070852-e1c03e598921/pkg/controller/component/monitor_utils.go (about)

     1  /*
     2  Copyright (C) 2022-2023 ApeCloud Co., Ltd
     3  
     4  This file is part of KubeBlocks project
     5  
     6  This program is free software: you can redistribute it and/or modify
     7  it under the terms of the GNU Affero General Public License as published by
     8  the Free Software Foundation, either version 3 of the License, or
     9  (at your option) any later version.
    10  
    11  This program is distributed in the hope that it will be useful
    12  but WITHOUT ANY WARRANTY; without even the implied warranty of
    13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    14  GNU Affero General Public License for more details.
    15  
    16  You should have received a copy of the GNU Affero General Public License
    17  along with this program.  If not, see <http://www.gnu.org/licenses/>.
    18  */
    19  
    20  package component
    21  
    22  import (
    23  	"k8s.io/apimachinery/pkg/util/intstr"
    24  
    25  	appsv1alpha1 "github.com/1aal/kubeblocks/apis/apps/v1alpha1"
    26  )
    27  
    28  func buildMonitorConfig(
    29  	clusterCompDef *appsv1alpha1.ClusterComponentDefinition,
    30  	clusterCompSpec *appsv1alpha1.ClusterComponentSpec,
    31  	component *SynthesizedComponent) {
    32  	monitorEnable := false
    33  	if clusterCompSpec != nil {
    34  		monitorEnable = clusterCompSpec.Monitor
    35  	}
    36  
    37  	monitorConfig := clusterCompDef.Monitor
    38  	if !monitorEnable || monitorConfig == nil {
    39  		disableMonitor(component)
    40  		return
    41  	}
    42  
    43  	if !monitorConfig.BuiltIn {
    44  		if monitorConfig.Exporter == nil {
    45  			disableMonitor(component)
    46  			return
    47  		}
    48  		component.Monitor = &MonitorConfig{
    49  			Enable:     true,
    50  			BuiltIn:    false,
    51  			ScrapePath: monitorConfig.Exporter.ScrapePath,
    52  			ScrapePort: monitorConfig.Exporter.ScrapePort.IntVal,
    53  		}
    54  
    55  		if monitorConfig.Exporter.ScrapePort.Type == intstr.String {
    56  			portName := monitorConfig.Exporter.ScrapePort.StrVal
    57  			for _, c := range clusterCompDef.PodSpec.Containers {
    58  				for _, p := range c.Ports {
    59  					if p.Name == portName {
    60  						component.Monitor.ScrapePort = p.ContainerPort
    61  						break
    62  					}
    63  				}
    64  			}
    65  		}
    66  		return
    67  	}
    68  
    69  	component.Monitor = &MonitorConfig{
    70  		Enable:  true,
    71  		BuiltIn: true,
    72  	}
    73  }
    74  
    75  func disableMonitor(component *SynthesizedComponent) {
    76  	component.Monitor = &MonitorConfig{
    77  		Enable:  false,
    78  		BuiltIn: false,
    79  	}
    80  }