Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: refine monitor-related api #7279

Merged
Merged
Show file tree
Hide file tree
Changes from 24 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 36 additions & 7 deletions apis/apps/v1alpha1/cluster_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ type ClusterSpec struct {
// This field is maintained for backward compatibility and its use is discouraged.
// Existing usage should be updated to the current preferred approach to avoid compatibility issues in future releases.
//
//+kubebuilder:deprecatedversion:warning="This field has been deprecated since 0.9.0"
// +kubebuilder:deprecatedversion:warning="This field has been deprecated since 0.9.0"
// +optional
Replicas *int32 `json:"replicas,omitempty"`

Expand Down Expand Up @@ -841,14 +841,25 @@ type ClusterComponentSpec struct {
// +optional
OfflineInstances []string `json:"offlineInstances,omitempty"`

// Defines the sidecar containers that will be attached to the Component's main container.
// It supports prometheus/victoriametrics operator.
//
// +kubebuilder:validation:MinItems=1
// +kubebuilder:validation:MaxItems=32
// +listType=set
// +optional
Sidecars []string `json:"sidecars,omitempty"`
MetricsStoreIntegration *MetricsStoreIntegration `json:"metricsStoreIntegration,omitempty"`

// Determines whether metrics exporter information is annotated on the Component's headless Service.
//
// If set to true, the following annotations will not be patched into the Service:
//
// - "monitor.kubeblocks.io/path"
// - "monitor.kubeblocks.io/port"
// - "monitor.kubeblocks.io/scheme"
//
// These annotations allow the Prometheus installed by KubeBlocks to discover and scrape metrics from the exporter.
//
// +optional
DisableExporter *bool `json:"disableExporter,omitempty"`

// Deprecated since v0.9
// Determines whether metrics exporter information is annotated on the Component's headless Service.
//
// If set to true, the following annotations will be patched into the Service:
Expand All @@ -860,7 +871,8 @@ type ClusterComponentSpec struct {
// These annotations allow the Prometheus installed by KubeBlocks to discover and scrape metrics from the exporter.
//
// +optional
MonitorEnabled *bool `json:"monitorEnabled,omitempty"`
// +kubebuilder:deprecatedversion:warning="This field has been deprecated since 0.10.0"
Monitor *bool `json:"monitor,omitempty"`
}

type ComponentMessageMap map[string]string
Expand Down Expand Up @@ -1614,6 +1626,23 @@ func (r *ClusterComponentSpec) ToVolumeClaimTemplates() []corev1.PersistentVolum
return ts
}

func (r *ClusterComponentSpec) GetDisableExporter() *bool {
if r.DisableExporter != nil {
return r.DisableExporter
}

toPointer := func(b bool) *bool {
p := b
return &p
}

// Compatible with previous versions of kb
if r.Monitor != nil {
return toPointer(!*r.Monitor)
}
return nil
}

func (t *InstanceTemplate) GetName() string {
return t.Name
}
Expand Down
99 changes: 39 additions & 60 deletions apis/apps/v1alpha1/clusterdefinition_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -485,63 +485,31 @@ type ServiceRefDeclarationSpec struct {
ServiceVersion string `json:"serviceVersion"`
}

type PrometheusScrapeConfig struct {
// Specifies the http/https url path to scrape for metrics.
// If empty, Prometheus uses the default value (e.g. `/metrics`).
//
// +kubebuilder:validation:default="/metrics"
// +optional
MetricsPath string `json:"metricsPath,omitempty"`

// Specifies the port name to scrape for metrics.
//
// +optional
MetricsPort string `json:"metricsPort,omitempty"`

// Specifies the schema to use for scraping.
// `http` and `https` are the expected values unless you rewrite the `__scheme__` label via relabeling.
// If empty, Prometheus uses the default value `http`.
//
// +kubebuilder:validation:default="http"
// +optional
Protocol PrometheusProtocol `json:"protocol,omitempty"`
}

type MonitorSource struct {
// Defines the kind of monitor, such as metrics or logs.
type ExporterConfig struct {
// scrapePort is exporter port for Time Series Database to scrape metrics.
// +kubebuilder:validation:Required
SidecarKind MonitorKind `json:"kind"`
// +kubebuilder:validation:XIntOrString
ScrapePort intstr.IntOrString `json:"scrapePort"`

// Defines the scrape configuration for the prometheus.
//
// scrapePath is exporter url path for Time Series Database to scrape metrics.
// +kubebuilder:validation:MaxLength=128
// +kubebuilder:default="/metrics"
// +optional
ScrapeConfig *PrometheusScrapeConfig `json:"scrapeConfig,omitempty"`
ScrapePath string `json:"scrapePath,omitempty"`
}

type SidecarContainerSource struct {
// Defines the function or purpose of the container, such as the monitor type sidecar.
//
type MonitorConfig struct {
// builtIn is a switch to enable KubeBlocks builtIn monitoring.
// If BuiltIn is set to true, monitor metrics will be scraped automatically.
// If BuiltIn is set to false, the provider should set ExporterConfig and Sidecar container own.
// +kubebuilder:default=false
// +optional
Monitor *MonitorSource `json:"monitor,omitempty"`
}

type SidecarContainerSpec struct {
corev1.Container `json:",inline"`
BuiltIn bool `json:"builtIn,omitempty"`

// Define the function or purpose of the container, such as the monitor type sidecar.
// In order to allow prometheus to scrape metrics from the sidecar container, the schema, port, and url will be injected into the annotation of the service.
//
// exporterConfig provided by provider, which specify necessary information to Time Series Database.
// exporterConfig is valid when builtIn is false.
// +optional
*SidecarContainerSource `json:",inline"`
}

type BuiltinMonitorContainerRef struct {
// Specifies the name of the built-in metrics exporter container.
//
// +kubebuilder:validation:Required
Name string `json:"name"`

PrometheusScrapeConfig `json:",inline"`
Exporter *ExporterConfig `json:"exporterConfig,omitempty"`
}

// ClusterComponentDefinition defines a Component within a ClusterDefinition but is deprecated and
Expand Down Expand Up @@ -721,22 +689,33 @@ type ClusterComponentDefinition struct {
// +optional
ServiceRefDeclarations []ServiceRefDeclaration `json:"serviceRefDeclarations,omitempty"`

// Defines the sidecar containers that will be attached to the component's main container.
// Defines the metrics exporter.
//
// +kubebuilder:pruning:PreserveUnknownFields
// +kubebuilder:validation:MinItems= 1
// +kubebuilder:validation:MaxItems= 32
// +patchMergeKey=name
// +patchStrategy=merge,retainKeys
// +listType=map
// +listMapKey=name
// +optional
SidecarContainerSpecs []SidecarContainerSpec `json:"sidecarContainerSpecs,omitempty"`
Exporter *Exporter `json:"exporter,omitempty"`

// Defines the built-in metrics exporter container.
// Deprecated since v0.9
// monitor is monitoring config which provided by provider.
//
// +kubebuilder:deprecatedversion:warning="This field has been deprecated since 0.10.0"
// +optional
BuiltinMonitorContainer *BuiltinMonitorContainerRef `json:"builtinMonitorContainer,omitempty"`
Monitor *MonitorConfig `json:"monitor,omitempty"`
}

func (r *ClusterComponentDefinition) GetPrometheusExporter() *Exporter {
if r.Exporter != nil {
return r.Exporter
}

// Compatible with previous versions of kb
if r.Monitor == nil || r.Monitor.Exporter == nil {
return nil
}

return &Exporter{
ScrapePath: r.Monitor.Exporter.ScrapePath,
TargetPort: &r.Monitor.Exporter.ScrapePort,
}
}

func (r *ClusterComponentDefinition) GetStatefulSetWorkload() StatefulSetWorkload {
Expand Down
11 changes: 4 additions & 7 deletions apis/apps/v1alpha1/component_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,17 +216,14 @@ type ComponentSpec struct {
// +optional
RuntimeClassName *string `json:"runtimeClassName,omitempty"`

// Defines the sidecar containers that will be attached to the Component's main container.
// It supports prometheus/victoriametrics operator.
//
// +kubebuilder:validation:MinItems=1
// +kubebuilder:validation:MaxItems=32
// +listType=set
// +optional
Sidecars []string `json:"sidecars,omitempty"`
MetricsStoreIntegration *MetricsStoreIntegration `json:"metricsStoreIntegration,omitempty"`

// Determines whether metrics exporter information is annotated on the Component's headless Service.
//
// If set to true, the following annotations will be patched into the Service:
// If set to true, the following annotations will not be patched into the Service:
//
// - "monitor.kubeblocks.io/path"
// - "monitor.kubeblocks.io/port"
Expand All @@ -235,7 +232,7 @@ type ComponentSpec struct {
// These annotations allow the Prometheus installed by KubeBlocks to discover and scrape metrics from the exporter.
//
// +optional
MonitorEnabled *bool `json:"monitorEnabled,omitempty"`
DisableExporter *bool `json:"disableExporter,omitempty"`
}

// ComponentStatus represents the observed state of a Component within the Cluster.
Expand Down
52 changes: 41 additions & 11 deletions apis/apps/v1alpha1/componentdefinition_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,23 +193,17 @@ type ComponentDefinitionSpec struct {
// +kubebuilder:validation:Required
Runtime corev1.PodSpec `json:"runtime"`

// Defines the sidecar containers that will be attached to the component's main container.
// Deprecated since v0.9
// monitor is monitoring config which provided by provider.
//
// +kubebuilder:pruning:PreserveUnknownFields
// +kubebuilder:validation:MinItems:= 1
// +kubebuilder:validation:MaxItems:= 32
// +patchMergeKey=name
// +patchStrategy=merge,retainKeys
// +listType=map
// +listMapKey=name
// +optional
// +kubebuilder:deprecatedversion:warning="This field has been deprecated since 0.10.0"
// +optional
SidecarContainerSpecs []SidecarContainerSpec `json:"sidecarContainerSpecs,omitempty"`
Monitor *MonitorConfig `json:"monitor,omitempty"`

// Defines the built-in metrics exporter container.
//
// +optional
BuiltinMonitorContainer *BuiltinMonitorContainerRef `json:"builtinMonitorContainer,omitempty"`
Exporter *Exporter `json:"exporter,omitempty"`

// Defines variables which are determined after Cluster instantiation and reflect
// dynamic or runtime attributes of instantiated Clusters.
Expand Down Expand Up @@ -629,6 +623,42 @@ type SystemAccount struct {
SecretRef *ProvisionSecretRef `json:"secretRef,omitempty"`
}

type Exporter struct {
// Specifies the name of the built-in metrics exporter container.
//
// +optional
ContainerName string `json:"containerName,omitempty"`

// Specifies the http/https url path to scrape for metrics.
// If empty, Prometheus uses the default value (e.g. `/metrics`).
//
// +kubebuilder:validation:default="/metrics"
// +optional
ScrapePath string `json:"scrapePath,omitempty"`

// Specifies the port name to scrape for metrics.
//
// +optional
ScrapePort string `json:"scrapePort,omitempty"`

// Specifies the schema to use for scraping.
// `http` and `https` are the expected values unless you rewrite the `__scheme__` label via relabeling.
// If empty, Prometheus uses the default value `http`.
//
// +kubebuilder:validation:default="http"
// +optional
ScrapeScheme PrometheusScheme `json:"scrapeScheme,omitempty"`

// Deprecated: use `scrapePort` instead.
// Compatible with previous versions of kb.
//
// Name or number of the target port of the `Pod` object behind the
// Service. The port must be specified with the container's port property.
//
// +optional
sophon-zt marked this conversation as resolved.
Show resolved Hide resolved
TargetPort *intstr.IntOrString `json:"targetPort,omitempty"`
}

// RoleArbitrator defines how to arbitrate the role of replicas.
//
// Deprecated since v0.9
Expand Down
23 changes: 12 additions & 11 deletions apis/apps/v1alpha1/configconstraint_conversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package v1alpha1
import (
"errors"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/controller-runtime/pkg/conversion"
logf "sigs.k8s.io/controller-runtime/pkg/log"

Expand Down Expand Up @@ -60,28 +61,27 @@ func convertToImpl(cc *ConfigConstraint, ccv1 *appsv1beta1.ConfigConstraint) err
func convertToConstraintSpec(cc *ConfigConstraintSpec, ccv1 *appsv1beta1.ConfigConstraintSpec) {
ccv1.MergeReloadAndRestart = cc.DynamicActionCanBeMerged
ccv1.ReloadStaticParamsBeforeRestart = cc.ReloadStaticParamsBeforeRestart
ccv1.DownwardAPITriggeredActions = cc.DownwardAPIOptions
ccv1.DownwardAPIChangeTriggeredActions = cc.DownwardAPIOptions
ccv1.StaticParameters = cc.StaticParameters
ccv1.DynamicParameters = cc.DynamicParameters
ccv1.ImmutableParameters = cc.ImmutableParameters
ccv1.ReloadedPodSelector = cc.Selector
ccv1.FileFormatConfig = cc.FormatterConfig
convertDynamicReloadAction(cc.ReloadOptions, ccv1, cc.ToolsImageSpec, cc.ScriptConfigs)
convertDynamicReloadAction(cc.ReloadOptions, ccv1, cc.ToolsImageSpec, cc.ScriptConfigs, cc.Selector)
convertSchema(cc.ConfigurationSchema, cc.CfgSchemaTopLevelName, ccv1)
}

func convertSchema(schema *CustomParametersValidation, topLevelKey string, ccv1 *appsv1beta1.ConfigConstraintSpec) {
if schema == nil {
return
}
ccv1.ConfigSchema = &appsv1beta1.ConfigSchema{
ccv1.ParametersSchema = &appsv1beta1.ParametersSchema{
TopLevelKey: topLevelKey,
CUE: schema.CUE,
SchemaInJSON: schema.Schema,
}
}

func convertDynamicReloadAction(options *ReloadOptions, ccv1 *appsv1beta1.ConfigConstraintSpec, toolsSetup *appsv1beta1.ToolsSetup, configs []appsv1beta1.ScriptConfig) {
func convertDynamicReloadAction(options *ReloadOptions, ccv1 *appsv1beta1.ConfigConstraintSpec, toolsSetup *appsv1beta1.ToolsSetup, configs []appsv1beta1.ScriptConfig, selector *metav1.LabelSelector) {
if options == nil {
return
}
Expand All @@ -90,6 +90,7 @@ func convertDynamicReloadAction(options *ReloadOptions, ccv1 *appsv1beta1.Config
ShellTrigger: options.ShellTrigger,
TPLScriptTrigger: options.TPLScriptTrigger,
AutoTrigger: options.AutoTrigger,
TargetPodSelector: selector,
}
if ccv1.ReloadAction.ShellTrigger != nil {
ccv1.ReloadAction.ShellTrigger.ToolsSetup = toolsSetup
Expand Down Expand Up @@ -120,12 +121,12 @@ func convertFromConstraintSpec(ccv1 *appsv1beta1.ConfigConstraintSpec, cc *Confi
cc.DynamicActionCanBeMerged = ccv1.MergeReloadAndRestart
cc.ReloadStaticParamsBeforeRestart = ccv1.ReloadStaticParamsBeforeRestart
cc.ToolsImageSpec = ccv1.GetToolsSetup()
cc.DownwardAPIOptions = ccv1.DownwardAPITriggeredActions
cc.DownwardAPIOptions = ccv1.DownwardAPIChangeTriggeredActions
cc.ScriptConfigs = ccv1.GetScriptConfigs()
cc.StaticParameters = ccv1.StaticParameters
cc.DynamicParameters = ccv1.DynamicParameters
cc.ImmutableParameters = ccv1.ImmutableParameters
cc.Selector = ccv1.ReloadedPodSelector
cc.Selector = ccv1.GetPodSelector()
cc.FormatterConfig = ccv1.FileFormatConfig

if ccv1.ReloadAction != nil {
Expand All @@ -136,11 +137,11 @@ func convertFromConstraintSpec(ccv1 *appsv1beta1.ConfigConstraintSpec, cc *Confi
AutoTrigger: ccv1.ReloadAction.AutoTrigger,
}
}
if ccv1.ConfigSchema != nil {
if ccv1.ParametersSchema != nil {
cc.ConfigurationSchema = &CustomParametersValidation{
Schema: ccv1.ConfigSchema.SchemaInJSON,
CUE: ccv1.ConfigSchema.CUE,
Schema: ccv1.ParametersSchema.SchemaInJSON,
CUE: ccv1.ParametersSchema.CUE,
}
cc.CfgSchemaTopLevelName = ccv1.ConfigSchema.TopLevelKey
cc.CfgSchemaTopLevelName = ccv1.ParametersSchema.TopLevelKey
}
}
2 changes: 1 addition & 1 deletion apis/apps/v1alpha1/configconstraint_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ type ConfigConstraintSpec struct {
// to reflect the new role.
//
// +optional
DownwardAPIOptions []appsv1beta1.DownwardAPITriggeredAction `json:"downwardAPIOptions,omitempty"`
DownwardAPIOptions []appsv1beta1.DownwardAPIChangeTriggeredAction `json:"downwardAPIOptions,omitempty"`

// A list of ScriptConfig Object.
//
Expand Down
Loading
Loading