diff --git a/api/doris/v1/types.go b/api/doris/v1/types.go index cb691198..7b1fbe39 100644 --- a/api/doris/v1/types.go +++ b/api/doris/v1/types.go @@ -55,6 +55,9 @@ type DorisClusterSpec struct { // the password key is `password`. the username defaults to `root` and is omitempty. AuthSecret string `json:"authSecret,omitempty"` + // EnableServiceMonitor creates a Secret with prometheus-additional.yaml for Prometheus additionalScrapeConfigs. Default false. + EnableServiceMonitor bool `json:"enableServiceMonitor,omitempty"` + // EnableRestartWhenConfigChange configmap monitoring, default is false. // When EnableRestartWhenConfigChange is true, changing the doris core configmap will cause a rolling restart of the corresponding node EnableRestartWhenConfigChange bool `json:"enableRestartWhenConfigChange,omitempty"` diff --git a/config/crd/bases/crds.yaml b/config/crd/bases/crds.yaml index bdbd96f8..d9858fa1 100644 --- a/config/crd/bases/crds.yaml +++ b/config/crd/bases/crds.yaml @@ -7096,6 +7096,10 @@ spec: EnableRestartWhenConfigChange configmap monitoring, default is false. When EnableRestartWhenConfigChange is true, changing the doris core configmap will cause a rolling restart of the corresponding node type: boolean + enableServiceMonitor: + description: EnableServiceMonitor creates a Secret with prometheus-additional.yaml + for Prometheus additionalScrapeConfigs. Default false. + type: boolean feSpec: description: defines the fe cluster state that will be created by operator. diff --git a/config/crd/bases/doris.apache.com_dorisclusters.yaml b/config/crd/bases/doris.apache.com_dorisclusters.yaml index c02e110d..81bd7ba4 100644 --- a/config/crd/bases/doris.apache.com_dorisclusters.yaml +++ b/config/crd/bases/doris.apache.com_dorisclusters.yaml @@ -7096,6 +7096,10 @@ spec: EnableRestartWhenConfigChange configmap monitoring, default is false. When EnableRestartWhenConfigChange is true, changing the doris core configmap will cause a rolling restart of the corresponding node type: boolean + enableServiceMonitor: + description: EnableServiceMonitor creates a Secret with prometheus-additional.yaml + for Prometheus additionalScrapeConfigs. Default false. + type: boolean feSpec: description: defines the fe cluster state that will be created by operator. diff --git a/config/crd/bases/doris.selectdb.com_dorisclusters.yaml b/config/crd/bases/doris.selectdb.com_dorisclusters.yaml index c02e110d..81bd7ba4 100644 --- a/config/crd/bases/doris.selectdb.com_dorisclusters.yaml +++ b/config/crd/bases/doris.selectdb.com_dorisclusters.yaml @@ -7096,6 +7096,10 @@ spec: EnableRestartWhenConfigChange configmap monitoring, default is false. When EnableRestartWhenConfigChange is true, changing the doris core configmap will cause a rolling restart of the corresponding node type: boolean + enableServiceMonitor: + description: EnableServiceMonitor creates a Secret with prometheus-additional.yaml + for Prometheus additionalScrapeConfigs. Default false. + type: boolean feSpec: description: defines the fe cluster state that will be created by operator. diff --git a/config/rbac/role.yaml b/config/rbac/role.yaml index d5016a6c..d14800df 100644 --- a/config/rbac/role.yaml +++ b/config/rbac/role.yaml @@ -94,6 +94,10 @@ rules: - get - list - watch + - create + - update + - patch + - delete - apiGroups: - "" resources: diff --git a/helm-charts/doris-operator/crds/doris.apache.com_dorisclusters.yaml b/helm-charts/doris-operator/crds/doris.apache.com_dorisclusters.yaml index c02e110d..81bd7ba4 100644 --- a/helm-charts/doris-operator/crds/doris.apache.com_dorisclusters.yaml +++ b/helm-charts/doris-operator/crds/doris.apache.com_dorisclusters.yaml @@ -7096,6 +7096,10 @@ spec: EnableRestartWhenConfigChange configmap monitoring, default is false. When EnableRestartWhenConfigChange is true, changing the doris core configmap will cause a rolling restart of the corresponding node type: boolean + enableServiceMonitor: + description: EnableServiceMonitor creates a Secret with prometheus-additional.yaml + for Prometheus additionalScrapeConfigs. Default false. + type: boolean feSpec: description: defines the fe cluster state that will be created by operator. diff --git a/helm-charts/doris-operator/templates/clusterrole.yaml b/helm-charts/doris-operator/templates/clusterrole.yaml index fe9b18db..649058b2 100644 --- a/helm-charts/doris-operator/templates/clusterrole.yaml +++ b/helm-charts/doris-operator/templates/clusterrole.yaml @@ -96,6 +96,9 @@ rules: - update - list - watch + - create + - patch + - delete - apiGroups: - "admissionregistration.k8s.io" resources: diff --git a/helm-charts/doris/templates/doriscluster.yaml b/helm-charts/doris/templates/doriscluster.yaml index 2f6ec320..eeaf7622 100644 --- a/helm-charts/doris/templates/doriscluster.yaml +++ b/helm-charts/doris/templates/doriscluster.yaml @@ -31,6 +31,7 @@ metadata: {{- toYaml .Values.dorisCluster.annotations | nindent 4 }} {{- end }} spec: + enableServiceMonitor: {{ .Values.dorisCluster.enableServiceMonitor | default false }} {{- if .Values.dorisCluster.adminUser }} adminUser: {{- toYaml .Values.dorisCluster.adminUser | nindent 4 }} diff --git a/helm-charts/doris/values.yaml b/helm-charts/doris/values.yaml index 76664431..c9a06140 100644 --- a/helm-charts/doris/values.yaml +++ b/helm-charts/doris/values.yaml @@ -25,6 +25,8 @@ dorisCluster: enabledCn: false # specify the broker deploy or not. enabledBroker: false + # generate a Secret with prometheus-additional.yaml for Prometheus additionalScrapeConfigs. + enableServiceMonitor: false # specify the doriscluster adminuser or not. # set adminUser will record the password in clear text. This method has been abandoned and is not recommended. # When adding adminuser. should follow the steps: diff --git a/pkg/common/utils/resource/prometheus.go b/pkg/common/utils/resource/prometheus.go new file mode 100644 index 00000000..00949e7b --- /dev/null +++ b/pkg/common/utils/resource/prometheus.go @@ -0,0 +1,202 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you 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 resource + +import ( + "bytes" + "fmt" + + dorisv1 "github.com/apache/doris-operator/api/doris/v1" + "gopkg.in/yaml.v3" + corev1 "k8s.io/api/core/v1" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/apimachinery/pkg/types" +) + +const ( + // PrometheusAdditionalConfigKey is the Secret key referenced by Prometheus additionalScrapeConfigs. + PrometheusAdditionalConfigKey = "prometheus-additional.yaml" + prometheusAdditionalSuffix = "-prometheus-additional" +) + +type prometheusScrapeConfig struct { + JobName string `yaml:"job_name"` + MetricsPath string `yaml:"metrics_path"` + KubernetesSDConfigs []prometheusK8sSD `yaml:"kubernetes_sd_configs"` + RelabelConfigs []prometheusRelabel `yaml:"relabel_configs"` +} + +type prometheusK8sSD struct { + Role string `yaml:"role"` + Namespaces prometheusSDNamespaces `yaml:"namespaces"` +} + +type prometheusSDNamespaces struct { + Names []string `yaml:"names"` +} + +type prometheusRelabel struct { + SourceLabels []string `yaml:"source_labels,omitempty"` + TargetLabel string `yaml:"target_label,omitempty"` + Regex string `yaml:"regex,omitempty"` + Action string `yaml:"action"` +} + +type prometheusTarget struct { + jobName string + namespace string + serviceName string + portName string + path string +} + +type prometheusEndpoint struct { + // jobSuffix distinguishes multiple endpoints of one component, e.g. "-brpc". + jobSuffix string + portKey string + path string +} + +type prometheusComponent struct { + component dorisv1.ComponentType + enabled func(*dorisv1.DorisCluster) bool + endpoints []prometheusEndpoint +} + +// prometheusComponents defines the components and endpoints scraped by Prometheus. +var prometheusComponents = []prometheusComponent{ + { + component: dorisv1.Component_FE, + enabled: func(dcr *dorisv1.DorisCluster) bool { return dcr.Spec.FeSpec != nil }, + endpoints: []prometheusEndpoint{ + {portKey: HTTP_PORT, path: "/metrics"}, + }, + }, + { + component: dorisv1.Component_BE, + enabled: func(dcr *dorisv1.DorisCluster) bool { return dcr.Spec.BeSpec != nil }, + endpoints: []prometheusEndpoint{ + {portKey: WEBSERVER_PORT, path: "/metrics"}, + {jobSuffix: "-brpc", portKey: BRPC_PORT, path: "/brpc_metrics"}, + }, + }, +} + +// PrometheusAdditionalSecretName returns the name of the generated scrape config Secret. +func PrometheusAdditionalSecretName(clusterName string) string { + return clusterName + prometheusAdditionalSuffix +} + +// BuildPrometheusAdditionalSecret builds the Secret consumed by Prometheus additionalScrapeConfigs for a DorisCluster. +func BuildPrometheusAdditionalSecret(dcr *dorisv1.DorisCluster) (*corev1.Secret, error) { + targets := make([]prometheusTarget, 0, 3) + for _, component := range prometheusComponents { + if component.enabled(dcr) { + targets = append(targets, component.prometheusTargets(dcr)...) + } + } + + labels := map[string]string{dorisv1.OwnerReference: dcr.Name} + owner := prometheusOwnerReference(dorisv1.GroupVersion.String(), "DorisCluster", dcr.Name, dcr.UID) + return buildPrometheusAdditionalSecret(dcr.Name, dcr.Namespace, labels, owner, targets) +} + +func (c prometheusComponent) prometheusTargets(dcr *dorisv1.DorisCluster) []prometheusTarget { + serviceName := dorisv1.GenerateExternalServiceName(dcr, c.component) + targets := make([]prometheusTarget, 0, len(c.endpoints)) + for _, endpoint := range c.endpoints { + jobComponent := string(c.component) + endpoint.jobSuffix + targets = append(targets, newPrometheusTarget(dcr.Namespace, dcr.Name, jobComponent, serviceName, GetPortKey(endpoint.portKey), endpoint.path)) + } + return targets +} + +func newPrometheusTarget(namespace, clusterName, component, serviceName, portName, path string) prometheusTarget { + return prometheusTarget{ + jobName: fmt.Sprintf("%s/%s/%s", namespace, clusterName, component), + namespace: namespace, + serviceName: serviceName, + portName: portName, + path: path, + } +} + +func prometheusOwnerReference(apiVersion, kind, name string, uid types.UID) metav1.OwnerReference { + controller := true + return metav1.OwnerReference{APIVersion: apiVersion, Kind: kind, Name: name, UID: uid, Controller: &controller} +} + +func buildPrometheusAdditionalSecret(clusterName, namespace string, labels map[string]string, owner metav1.OwnerReference, targets []prometheusTarget) (*corev1.Secret, error) { + configs := make([]prometheusScrapeConfig, 0, len(targets)) + for _, target := range targets { + configs = append(configs, target.scrapeConfig()) + } + var buffer bytes.Buffer + encoder := yaml.NewEncoder(&buffer) + encoder.SetIndent(2) + if err := encoder.Encode(configs); err != nil { + return nil, err + } + if err := encoder.Close(); err != nil { + return nil, err + } + data := buffer.Bytes() + + return &corev1.Secret{ + ObjectMeta: metav1.ObjectMeta{ + Name: PrometheusAdditionalSecretName(clusterName), + Namespace: namespace, + Labels: labels, + OwnerReferences: []metav1.OwnerReference{owner}, + }, + Data: map[string][]byte{PrometheusAdditionalConfigKey: data}, + }, nil +} + +func (t prometheusTarget) scrapeConfig() prometheusScrapeConfig { + return prometheusScrapeConfig{ + JobName: t.jobName, + MetricsPath: t.path, + KubernetesSDConfigs: []prometheusK8sSD{{ + Role: "endpoints", + Namespaces: prometheusSDNamespaces{Names: []string{t.namespace}}, + }}, + RelabelConfigs: []prometheusRelabel{ + { + SourceLabels: []string{"__meta_kubernetes_service_name"}, + Regex: t.serviceName, + Action: "keep", + }, + { + SourceLabels: []string{"__meta_kubernetes_endpoint_port_name"}, + Regex: t.portName, + Action: "keep", + }, + { + SourceLabels: []string{"__meta_kubernetes_namespace"}, + TargetLabel: "namespace", + Action: "replace", + }, + { + SourceLabels: []string{"__meta_kubernetes_pod_name"}, + TargetLabel: "pod", + Action: "replace", + }, + }, + } +} diff --git a/pkg/common/utils/resource/prometheus_test.go b/pkg/common/utils/resource/prometheus_test.go new file mode 100644 index 00000000..ada5bd84 --- /dev/null +++ b/pkg/common/utils/resource/prometheus_test.go @@ -0,0 +1,101 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you 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 resource + +import ( + "testing" + + dorisv1 "github.com/apache/doris-operator/api/doris/v1" + "gopkg.in/yaml.v3" + corev1 "k8s.io/api/core/v1" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" +) + +func Test_BuildPrometheusAdditionalSecret(t *testing.T) { + dcr := &dorisv1.DorisCluster{ + ObjectMeta: metav1.ObjectMeta{Name: "demo", Namespace: "doris"}, + Spec: dorisv1.DorisClusterSpec{ + FeSpec: &dorisv1.FeSpec{}, + BeSpec: &dorisv1.BeSpec{}, + }, + } + + secret, err := BuildPrometheusAdditionalSecret(dcr) + if err != nil { + t.Fatalf("build secret failed: %s", err) + } + if secret.Name != "demo-prometheus-additional" || secret.Namespace != "doris" { + t.Fatalf("unexpected secret metadata: %s/%s", secret.Namespace, secret.Name) + } + if len(secret.OwnerReferences) != 1 || secret.OwnerReferences[0].Name != "demo" { + t.Fatalf("unexpected owner references: %#v", secret.OwnerReferences) + } + + jobs := scrapeJobs(t, secret) + assertTarget(t, jobs, "doris/demo/fe", "demo-fe-service", "http-port", "/metrics") + assertTarget(t, jobs, "doris/demo/be", "demo-be-service", "webserver-port", "/metrics") + assertTarget(t, jobs, "doris/demo/be-brpc", "demo-be-service", "brpc-port", "/brpc_metrics") + if len(jobs) != 3 { + t.Fatalf("expected 3 scrape jobs, got %d", len(jobs)) + } +} + +func scrapeJobs(t *testing.T, secret *corev1.Secret) map[string]prometheusScrapeConfig { + t.Helper() + data, ok := secret.Data[PrometheusAdditionalConfigKey] + if !ok { + t.Fatalf("secret missing key %s", PrometheusAdditionalConfigKey) + } + var configs []prometheusScrapeConfig + if err := yaml.Unmarshal(data, &configs); err != nil { + t.Fatalf("unmarshal scrape configs failed: %s", err) + } + jobs := make(map[string]prometheusScrapeConfig, len(configs)) + for _, config := range configs { + jobs[config.JobName] = config + } + return jobs +} + +func assertTarget(t *testing.T, jobs map[string]prometheusScrapeConfig, jobName, serviceName, portName, path string) { + t.Helper() + job, ok := jobs[jobName] + if !ok { + t.Fatalf("job %s not found", jobName) + } + if job.MetricsPath != path { + t.Errorf("job %s metrics_path = %s, want %s", jobName, job.MetricsPath, path) + } + if len(job.KubernetesSDConfigs) != 1 || len(job.KubernetesSDConfigs[0].Namespaces.Names) != 1 || + job.KubernetesSDConfigs[0].Namespaces.Names[0] != "doris" { + t.Errorf("job %s unexpected kubernetes_sd_configs: %#v", jobName, job.KubernetesSDConfigs) + } + + relabels := map[string]string{} + for _, relabel := range job.RelabelConfigs { + if len(relabel.SourceLabels) == 1 && relabel.Action == "keep" { + relabels[relabel.SourceLabels[0]] = relabel.Regex + } + } + if relabels["__meta_kubernetes_service_name"] != serviceName { + t.Errorf("job %s service selector = %s, want %s", jobName, relabels["__meta_kubernetes_service_name"], serviceName) + } + if relabels["__meta_kubernetes_endpoint_port_name"] != portName { + t.Errorf("job %s port selector = %s, want %s", jobName, relabels["__meta_kubernetes_endpoint_port_name"], portName) + } +} diff --git a/pkg/controller/doriscluster_controller.go b/pkg/controller/doriscluster_controller.go index 622f37b3..32219b70 100644 --- a/pkg/controller/doriscluster_controller.go +++ b/pkg/controller/doriscluster_controller.go @@ -40,6 +40,7 @@ import ( dorisv1 "github.com/apache/doris-operator/api/doris/v1" "github.com/apache/doris-operator/pkg/common/utils/feheap" "github.com/apache/doris-operator/pkg/common/utils/k8s" + "github.com/apache/doris-operator/pkg/common/utils/resource" "github.com/apache/doris-operator/pkg/common/utils/runtimeconfig" "github.com/apache/doris-operator/pkg/controller/sub_controller" "github.com/apache/doris-operator/pkg/controller/sub_controller/be" @@ -96,7 +97,7 @@ var ( //+kubebuilder:rbac:groups=apps,resources=statefulsets,verbs=get;list;watch;create;update;patch;delete //+kubebuilder:rbac:groups=apps,resources=statefulsets/status,verbs=get //+kubebuilder:rbac:groups=core,resources=serviceaccounts,verbs=get;list;watch;create;update;patch;delete -//+kubebuilder:rbac:groups=core,resources=secrets,verbs=get;list;watch +//+kubebuilder:rbac:groups=core,resources=secrets,verbs=get;list;watch;create;update;patch;delete //+kubebuilder:rbac:groups=rbac.authorization.k8s.io,resources=rolebindings,verbs=get;list;watch;create;update;patch;delete //+kubebuilder:rbac:groups=rbac.authorization.k8s.io,resources=clusterrolebindings,verbs=get;list;watch;create;update;patch;delete //+kubebuilder:rbac:groups=autoscaling,resources=horizontalpodautoscalers,verbs=get;list;watch;create;update;patch;delete @@ -157,6 +158,13 @@ func (r *DorisClusterReconciler) Reconcile(ctx context.Context, req ctrl.Request } } + if err := reconcilePrometheusAdditionalSecret(ctx, r.Client, dcr, dcr.Spec.EnableServiceMonitor, func() (*corev1.Secret, error) { + return resource.BuildPrometheusAdditionalSecret(dcr) + }); err != nil { + klog.Error("DorisClusterReconciler reconcile prometheus additional config failed ", "namespace: ", dcr.Namespace, " name: ", dcr.Name, " error: ", err) + return requeueIfError(err) + } + //generate the dcr status. if err := r.clearNoEffectResources(ctx, dcr); err != nil { return requeueIfError(err) diff --git a/pkg/controller/prometheus_additional.go b/pkg/controller/prometheus_additional.go new file mode 100644 index 00000000..17796434 --- /dev/null +++ b/pkg/controller/prometheus_additional.go @@ -0,0 +1,67 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you 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 controller + +import ( + "context" + + "github.com/apache/doris-operator/pkg/common/utils/k8s" + "github.com/apache/doris-operator/pkg/common/utils/resource" + corev1 "k8s.io/api/core/v1" + "k8s.io/apimachinery/pkg/api/equality" + apierrors "k8s.io/apimachinery/pkg/api/errors" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "sigs.k8s.io/controller-runtime/pkg/client" +) + +func reconcilePrometheusAdditionalSecret(ctx context.Context, c client.Client, owner client.Object, enabled bool, + build func() (*corev1.Secret, error)) error { + if !enabled { + secret := &corev1.Secret{} + key := client.ObjectKey{Namespace: owner.GetNamespace(), Name: resource.PrometheusAdditionalSecretName(owner.GetName())} + err := c.Get(ctx, key, secret) + if apierrors.IsNotFound(err) { + return nil + } + if err != nil { + return err + } + if !metav1.IsControlledBy(secret, owner) { + return nil + } + return client.IgnoreNotFound(c.Delete(ctx, secret)) + } + + secret, err := build() + if err != nil { + return err + } + + existing := &corev1.Secret{} + if err := c.Get(ctx, client.ObjectKeyFromObject(secret), existing); err == nil { + if equality.Semantic.DeepEqual(existing.Data, secret.Data) && + equality.Semantic.DeepEqual(existing.Labels, secret.Labels) && + equality.Semantic.DeepEqual(existing.OwnerReferences, secret.OwnerReferences) { + return nil + } + } else if !apierrors.IsNotFound(err) { + return err + } + + return k8s.CreateOrUpdateClientObject(ctx, c, secret) +} diff --git a/pkg/controller/prometheus_additional_test.go b/pkg/controller/prometheus_additional_test.go new file mode 100644 index 00000000..d095880a --- /dev/null +++ b/pkg/controller/prometheus_additional_test.go @@ -0,0 +1,78 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you 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 controller + +import ( + "context" + "testing" + + dorisv1 "github.com/apache/doris-operator/api/doris/v1" + "github.com/apache/doris-operator/pkg/common/utils/resource" + corev1 "k8s.io/api/core/v1" + apierrors "k8s.io/apimachinery/pkg/api/errors" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/apimachinery/pkg/runtime" + "sigs.k8s.io/controller-runtime/pkg/client" + "sigs.k8s.io/controller-runtime/pkg/client/fake" +) + +func Test_ReconcilePrometheusAdditionalSecret(t *testing.T) { + scheme := runtime.NewScheme() + if err := corev1.AddToScheme(scheme); err != nil { + t.Fatalf("add corev1 to scheme failed: %s", err) + } + c := fake.NewClientBuilder().WithScheme(scheme).Build() + ctx := context.Background() + + dcr := &dorisv1.DorisCluster{ + ObjectMeta: metav1.ObjectMeta{Name: "demo", Namespace: "doris", UID: "demo-uid"}, + Spec: dorisv1.DorisClusterSpec{ + FeSpec: &dorisv1.FeSpec{}, + BeSpec: &dorisv1.BeSpec{}, + }, + } + build := func() (*corev1.Secret, error) { + return resource.BuildPrometheusAdditionalSecret(dcr) + } + + for i := 0; i < 2; i++ { + if err := reconcilePrometheusAdditionalSecret(ctx, c, dcr, true, build); err != nil { + t.Fatalf("reconcile enabled failed: %s", err) + } + secret := &corev1.Secret{} + if err := c.Get(ctx, client.ObjectKey{Namespace: "doris", Name: "demo-prometheus-additional"}, secret); err != nil { + t.Fatalf("get generated secret failed: %s", err) + } + if _, ok := secret.Data[resource.PrometheusAdditionalConfigKey]; !ok { + t.Fatalf("generated secret missing config key") + } + } + + if err := reconcilePrometheusAdditionalSecret(ctx, c, dcr, false, build); err != nil { + t.Fatalf("reconcile disabled failed: %s", err) + } + secret := &corev1.Secret{} + err := c.Get(ctx, client.ObjectKey{Namespace: "doris", Name: "demo-prometheus-additional"}, secret) + if !apierrors.IsNotFound(err) { + t.Fatalf("expected secret deleted, got err: %v", err) + } + + if err := reconcilePrometheusAdditionalSecret(ctx, c, dcr, false, build); err != nil { + t.Fatalf("reconcile disabled twice failed: %s", err) + } +} diff --git a/pkg/controller/upgrade_test.go b/pkg/controller/upgrade_test.go index 5e9c3326..e4b4b0f5 100644 --- a/pkg/controller/upgrade_test.go +++ b/pkg/controller/upgrade_test.go @@ -25,6 +25,7 @@ import ( dorisv1 "github.com/apache/doris-operator/api/doris/v1" sc "github.com/apache/doris-operator/pkg/controller/sub_controller" appsv1 "k8s.io/api/apps/v1" + corev1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/runtime" "k8s.io/apimachinery/pkg/types" @@ -61,6 +62,9 @@ func TestDcrBackendWaitContinuesSyncAndRequeuesHealthyCluster(t *testing.T) { if err := dorisv1.AddToScheme(scheme); err != nil { t.Fatal(err) } + if err := corev1.AddToScheme(scheme); err != nil { + t.Fatal(err) + } dcr := &dorisv1.DorisCluster{ObjectMeta: metav1.ObjectMeta{Name: "test", Namespace: "test"}, Spec: dorisv1.DorisClusterSpec{FeSpec: &dorisv1.FeSpec{BaseSpec: dorisv1.BaseSpec{Replicas: ptr.To(int32(1))}}}, Status: dorisv1.DorisClusterStatus{FEStatus: &dorisv1.ComponentStatus{ComponentCondition: dorisv1.ComponentCondition{Phase: dorisv1.Available}}}} c := fake.NewClientBuilder().WithScheme(scheme).WithStatusSubresource(dcr).WithObjects(dcr).Build() feCalls, beCalls := 0, 0