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

replace UpdateStatus() for ApplyStatus Prometheus #5912

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions pkg/operator/operator.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ import (
monitoringv1 "github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring/v1"
)

const PrometheusOperatorFieldManager = "PrometheusOperator"

var (
syncsDesc = prometheus.NewDesc(
"prometheus_operator_syncs",
Expand Down
5 changes: 3 additions & 2 deletions pkg/prometheus/agent/operator.go
Original file line number Diff line number Diff line change
Expand Up @@ -806,8 +806,9 @@ func (c *Operator) UpdateStatus(ctx context.Context, key string) error {
return errors.Wrap(err, "failed to get prometheus agent status")
}
p.Status = *pStatus
if _, err = c.mclient.MonitoringV1alpha1().PrometheusAgents(p.Namespace).UpdateStatus(ctx, p, metav1.UpdateOptions{}); err != nil {
return errors.Wrap(err, "failed to update prometheus agent status subresource")

if _, err = c.mclient.MonitoringV1alpha1().PrometheusAgents(p.Namespace).ApplyStatus(ctx, prompkg.ApplyConfigurationFromPrometheusAgent(p), metav1.ApplyOptions{FieldManager: operator.PrometheusOperatorFieldManager, Force: true}); err != nil {
return errors.Wrap(err, "failed to Apply prometheus agent status subresource")
}

return nil
Expand Down
66 changes: 66 additions & 0 deletions pkg/prometheus/applyconfiguration.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// Copyright 2019 The prometheus-operator Authors
//
// Licensed 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 prometheus

import (
monitoringv1 "github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring/v1"
monitoringv1alpha1 "github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring/v1alpha1"
monitoringv1ac "github.com/prometheus-operator/prometheus-operator/pkg/client/applyconfiguration/monitoring/v1"
monitoringv1alpha1ac "github.com/prometheus-operator/prometheus-operator/pkg/client/applyconfiguration/monitoring/v1alpha1"
)

func ApplyConfigurationFromPrometheusAgent(p *monitoringv1alpha1.PrometheusAgent) *monitoringv1alpha1ac.PrometheusAgentApplyConfiguration {
psac := prometheusStatusApplyConfigurationFromPrometheusStatus(&p.Status)
return monitoringv1alpha1ac.PrometheusAgent(p.Name, p.Namespace).WithStatus(psac)
}

func ApplyConfigurationFromPrometheus(p *monitoringv1.Prometheus) *monitoringv1ac.PrometheusApplyConfiguration {
psac := prometheusStatusApplyConfigurationFromPrometheusStatus(&p.Status)
return monitoringv1ac.Prometheus(p.Name, p.Namespace).WithStatus(psac)
}

func prometheusStatusApplyConfigurationFromPrometheusStatus(status *monitoringv1.PrometheusStatus) *monitoringv1ac.PrometheusStatusApplyConfiguration {
psac := monitoringv1ac.PrometheusStatus().
WithPaused(status.Paused).
WithReplicas(status.Replicas).
WithAvailableReplicas(status.AvailableReplicas).
WithUpdatedReplicas(status.UpdatedReplicas).
WithUnavailableReplicas(status.UnavailableReplicas)

for _, condition := range status.Conditions {
psac.WithConditions(
monitoringv1ac.Condition().
WithType(condition.Type).
WithStatus(condition.Status).
WithLastTransitionTime(condition.LastTransitionTime).
WithReason(condition.Reason).
WithMessage(condition.Message).
WithObservedGeneration(condition.ObservedGeneration),
)
}

for _, shardStatus := range status.ShardStatuses {
psac.WithShardStatuses(
monitoringv1ac.ShardStatus().
WithShardID(shardStatus.ShardID).
WithReplicas(shardStatus.Replicas).
WithUpdatedReplicas(shardStatus.UpdatedReplicas).
WithAvailableReplicas(shardStatus.AvailableReplicas).
WithUnavailableReplicas(shardStatus.UnavailableReplicas),
)
}

return psac
}
5 changes: 3 additions & 2 deletions pkg/prometheus/server/operator.go
Original file line number Diff line number Diff line change
Expand Up @@ -1369,8 +1369,9 @@ func (c *Operator) UpdateStatus(ctx context.Context, key string) error {
}

p.Status = *pStatus
if _, err = c.mclient.MonitoringV1().Prometheuses(p.Namespace).UpdateStatus(ctx, p, metav1.UpdateOptions{}); err != nil {
return errors.Wrap(err, "failed to update prometheus status subresource")

if _, err = c.mclient.MonitoringV1().Prometheuses(p.Namespace).ApplyStatus(ctx, prompkg.ApplyConfigurationFromPrometheus(p), metav1.ApplyOptions{FieldManager: operator.PrometheusOperatorFieldManager, Force: true}); err != nil {
return errors.Wrap(err, "failed to apply prometheus status subresource")
}

return nil
Expand Down