Skip to content
This repository has been archived by the owner on Nov 9, 2022. It is now read-only.

Conditions lastUpdateTime is no longer updated continuously #90

Merged
merged 1 commit into from
Nov 19, 2020
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
34 changes: 21 additions & 13 deletions api/resources/v1alpha1/helper/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,14 @@ var Now = metav1.Now

// InitCondition initializes a new Condition with an Unknown status.
func InitCondition(conditionType resourcesv1alpha1.ConditionType) resourcesv1alpha1.ManagedResourceCondition {
now := Now()
return resourcesv1alpha1.ManagedResourceCondition{
Type: conditionType,
Status: resourcesv1alpha1.ConditionUnknown,
Reason: "ConditionInitialized",
Message: "The condition has been initialized but its semantic check has not been performed yet.",
LastTransitionTime: Now(),
LastTransitionTime: now,
LastUpdateTime: now,
}
}

Expand All @@ -39,10 +41,10 @@ func InitCondition(conditionType resourcesv1alpha1.ConditionType) resourcesv1alp
func GetCondition(conditions []resourcesv1alpha1.ManagedResourceCondition, conditionType resourcesv1alpha1.ConditionType) *resourcesv1alpha1.ManagedResourceCondition {
for _, condition := range conditions {
if condition.Type == conditionType {
return &condition
c := condition
return &c
}
}

return nil
}

Expand All @@ -52,23 +54,29 @@ func GetOrInitCondition(conditions []resourcesv1alpha1.ManagedResourceCondition,
if condition := GetCondition(conditions, conditionType); condition != nil {
return *condition
}

return InitCondition(conditionType)
}

// UpdatedCondition updates the properties of one specific condition.
func UpdatedCondition(condition resourcesv1alpha1.ManagedResourceCondition, status resourcesv1alpha1.ConditionStatus, reason, message string) resourcesv1alpha1.ManagedResourceCondition {
newCondition := resourcesv1alpha1.ManagedResourceCondition{
Type: condition.Type,
Status: status,
Reason: reason,
Message: message,
LastTransitionTime: condition.LastTransitionTime,
LastUpdateTime: Now(),
}
var (
newCondition = resourcesv1alpha1.ManagedResourceCondition{
Type: condition.Type,
Status: status,
Reason: reason,
Message: message,
LastTransitionTime: condition.LastTransitionTime,
LastUpdateTime: condition.LastUpdateTime,
}
now = Now()
)

if condition.Status != status {
newCondition.LastTransitionTime = Now()
newCondition.LastTransitionTime = now
}

if condition.Reason != reason || condition.Message != message {
newCondition.LastUpdateTime = now
}

return newCondition
Expand Down
2 changes: 1 addition & 1 deletion cmd/gardener-resource-manager/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ func NewControllerManagerCommand(parentCtx context.Context) *cobra.Command {

healthController, err := controller.New("health-controller", mgr, controller.Options{
MaxConcurrentReconciles: healthMaxConcurrentWorkers,
Reconciler: health.NewHealthReconciler(
Reconciler: health.NewReconciler(
ctx,
log.WithName("health-reconciler"),
mgr.GetClient(),
Expand Down
2 changes: 1 addition & 1 deletion example/20-managedresource.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ stringData:
spec:
containers:
- name: nginx
image: nginx:1.7.9
image: nginx:1.14.2
ports:
- containerPort: 80
---
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/gardener/gardener-resource-manager
go 1.15

require (
github.com/gardener/gardener v1.11.3
github.com/gardener/gardener v1.12.8
github.com/gardener/gardener-resource-manager/api v0.0.0-00010101000000-000000000000
github.com/gardener/hvpa-controller v0.3.1
github.com/go-logr/logr v0.1.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -175,8 +175,8 @@ github.com/gardener/gardener v1.1.2/go.mod h1:CP9I0tCDVXTLPkJv/jUtXVUh948kSNKEGU
github.com/gardener/gardener v1.3.1/go.mod h1:936P5tQbg6ViiW8BVC9ELM95sFrk4DgobKrxMNtn/LU=
github.com/gardener/gardener v1.4.1-0.20200519155656-a8ccc6cc779a h1:Gq7N2Dbn4s9Wdn4Q+HrTozx6b8kfWh7J7rO1z2Lt5/g=
github.com/gardener/gardener v1.4.1-0.20200519155656-a8ccc6cc779a/go.mod h1:t9oESM37bAMIuezi9I0H0I8+++8jy8BUPitcf4ERRXY=
github.com/gardener/gardener v1.11.3 h1:9xI7Mq0XXKiv4W1c4/pvSqsIIRtlE2dLSTrZszWs8oc=
github.com/gardener/gardener v1.11.3/go.mod h1:5DzqfOm+G8UftKu5zUbYJ+9Cnfd4XrvRNDabkM9AIp4=
github.com/gardener/gardener v1.12.8 h1:LOmCBMxeyWzisRAjnYhDCLxugIhlhtDSbxZoxTphzQo=
github.com/gardener/gardener v1.12.8/go.mod h1:N7MiaoNAPCKPq56p3z7P8yWyq5CTC28iZuiX+tGAjXk=
github.com/gardener/gardener-resource-manager v0.10.0/go.mod h1:0pKTHOhvU91eQB0EYr/6Ymd7lXc/5Hi8P8tF/gpV0VQ=
github.com/gardener/gardener-resource-manager v0.13.1/go.mod h1:0No/XttYRUwDn5lSppq9EqlKdo/XJQ44aCZz5BVu3Vw=
github.com/gardener/hvpa-controller v0.0.0-20191014062307-fad3bdf06a25/go.mod h1:yj7YJ6ijo4adcpXQKutPFZfQuKLdM5UMZZUlpbM3vig=
Expand Down
86 changes: 83 additions & 3 deletions pkg/api/resources/v1alpha1/helper/helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,100 @@ import (
"time"

resourcesv1alpha1 "github.com/gardener/gardener-resource-manager/api/resources/v1alpha1"
helper "github.com/gardener/gardener-resource-manager/api/resources/v1alpha1/helper"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"github.com/gardener/gardener-resource-manager/api/resources/v1alpha1/helper"

. "github.com/onsi/ginkgo"
. "github.com/onsi/ginkgo/extensions/table"
. "github.com/onsi/gomega"
. "github.com/onsi/gomega/gstruct"
. "github.com/onsi/gomega/types"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

func TestHelper(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "API v1alpha1 Helper Suite")
}

var zeroTime metav1.Time

var _ = Describe("helper", func() {
DescribeTable("#UpdatedCondition",
func(condition resourcesv1alpha1.ManagedResourceCondition, status resourcesv1alpha1.ConditionStatus, reason, message string, matcher GomegaMatcher) {
updated := helper.UpdatedCondition(condition, status, reason, message)

Expect(updated).To(matcher)
},
Entry("no update",
resourcesv1alpha1.ManagedResourceCondition{
Status: resourcesv1alpha1.ConditionTrue,
Reason: "reason",
Message: "message",
},
resourcesv1alpha1.ConditionTrue,
"reason",
"message",
MatchFields(IgnoreExtras, Fields{
"Status": Equal(resourcesv1alpha1.ConditionTrue),
"Reason": Equal("reason"),
"Message": Equal("message"),
"LastTransitionTime": Equal(zeroTime),
"LastUpdateTime": Equal(zeroTime),
}),
),
Entry("update reason",
resourcesv1alpha1.ManagedResourceCondition{
Status: resourcesv1alpha1.ConditionTrue,
Reason: "reason",
Message: "message",
},
resourcesv1alpha1.ConditionTrue,
"OtherReason",
"message",
MatchFields(IgnoreExtras, Fields{
"Status": Equal(resourcesv1alpha1.ConditionTrue),
"Reason": Equal("OtherReason"),
"Message": Equal("message"),
"LastTransitionTime": Equal(zeroTime),
"LastUpdateTime": Not(Equal(zeroTime)),
}),
),
Entry("update message",
resourcesv1alpha1.ManagedResourceCondition{
Status: resourcesv1alpha1.ConditionTrue,
Reason: "reason",
Message: "message",
},
resourcesv1alpha1.ConditionTrue,
"reason",
"OtherMessage",
MatchFields(IgnoreExtras, Fields{
"Status": Equal(resourcesv1alpha1.ConditionTrue),
"Reason": Equal("reason"),
"Message": Equal("OtherMessage"),
"LastTransitionTime": Equal(zeroTime),
"LastUpdateTime": Not(Equal(zeroTime)),
}),
),
Entry("update status",
resourcesv1alpha1.ManagedResourceCondition{
Status: resourcesv1alpha1.ConditionTrue,
Reason: "reason",
Message: "message",
},
resourcesv1alpha1.ConditionFalse,
"OtherReason",
"message",
MatchFields(IgnoreExtras, Fields{
"Status": Equal(resourcesv1alpha1.ConditionFalse),
"Reason": Equal("OtherReason"),
"Message": Equal("message"),
"LastTransitionTime": Not(Equal(zeroTime)),
"LastUpdateTime": Not(Equal(zeroTime)),
}),
),
)

Describe("#MergeConditions", func() {
It("should merge the conditions", func() {
var (
Expand Down
28 changes: 19 additions & 9 deletions pkg/controller/managedresources/health/health_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"github.com/gardener/gardener-resource-manager/pkg/controller/utils"

"github.com/go-logr/logr"
apiequality "k8s.io/apimachinery/pkg/api/equality"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
Expand All @@ -34,7 +35,8 @@ import (
"sigs.k8s.io/controller-runtime/pkg/reconcile"
)

type HealthReconciler struct {
// Reconciler performs health checks for resources managed by ManagedResources.
type Reconciler struct {
ctx context.Context
log logr.Logger
client client.Client
Expand All @@ -44,11 +46,13 @@ type HealthReconciler struct {
syncPeriod time.Duration
}

func NewHealthReconciler(ctx context.Context, log logr.Logger, client, targetClient client.Client, targetScheme *runtime.Scheme, classFilter *managedresources.ClassFilter, syncPeriod time.Duration) *HealthReconciler {
return &HealthReconciler{ctx, log, client, targetClient, targetScheme, classFilter, syncPeriod}
// NewReconciler returns a new health reconciler.
func NewReconciler(ctx context.Context, log logr.Logger, client, targetClient client.Client, targetScheme *runtime.Scheme, classFilter *managedresources.ClassFilter, syncPeriod time.Duration) *Reconciler {
return &Reconciler{ctx, log, client, targetClient, targetScheme, classFilter, syncPeriod}
}

func (r *HealthReconciler) Reconcile(req ctrl.Request) (ctrl.Result, error) {
// Reconcile performs health checks.
func (r *Reconciler) Reconcile(req ctrl.Request) (ctrl.Result, error) {
log := r.log.WithValues("object", req)
log.Info("Starting ManagedResource health checks")

Expand Down Expand Up @@ -88,7 +92,11 @@ func (r *HealthReconciler) Reconcile(req ctrl.Request) (ctrl.Result, error) {
return ctrl.Result{RequeueAfter: r.syncPeriod}, nil
}

resourcesObjectReferences := mr.Status.Resources
var (
oldConditions = []resourcesv1alpha1.ManagedResourceCondition{conditionResourcesHealthy}
resourcesObjectReferences = mr.Status.Resources
)

for _, ref := range resourcesObjectReferences {
var obj runtime.Object
// sigs.k8s.io/controller-runtime/pkg/client.DelegatingReader does not use the cache for unstructured.Unstructured
Expand Down Expand Up @@ -141,8 +149,11 @@ func (r *HealthReconciler) Reconcile(req ctrl.Request) (ctrl.Result, error) {
}

conditionResourcesHealthy = resourcesv1alpha1helper.UpdatedCondition(conditionResourcesHealthy, resourcesv1alpha1.ConditionTrue, "ResourcesHealthy", "All resources are healthy.")
if err := tryUpdateManagedResourceCondition(r.ctx, r.client, mr, conditionResourcesHealthy); err != nil {
return ctrl.Result{}, fmt.Errorf("could not update the ManagedResource status: %+v ", err)

if !apiequality.Semantic.DeepEqual(oldConditions, []resourcesv1alpha1.ManagedResourceCondition{conditionResourcesHealthy}) {
if err := tryUpdateManagedResourceCondition(r.ctx, r.client, mr, conditionResourcesHealthy); err != nil {
return ctrl.Result{}, fmt.Errorf("could not update the ManagedResource status: %+v ", err)
}
}

log.Info("Finished ManagedResource health checks")
Expand All @@ -151,8 +162,7 @@ func (r *HealthReconciler) Reconcile(req ctrl.Request) (ctrl.Result, error) {

func tryUpdateManagedResourceCondition(ctx context.Context, c client.Client, mr *resourcesv1alpha1.ManagedResource, condition resourcesv1alpha1.ManagedResourceCondition) error {
return utils.TryUpdateStatus(ctx, retry.DefaultBackoff, c, mr, func() error {
newConditions := resourcesv1alpha1helper.MergeConditions(mr.Status.Conditions, condition)
mr.Status.Conditions = newConditions
mr.Status.Conditions = resourcesv1alpha1helper.MergeConditions(mr.Status.Conditions, condition)
return nil
})
}
1 change: 1 addition & 0 deletions pkg/health/health.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (

"github.com/gardener/gardener-resource-manager/api/resources/v1alpha1"
"github.com/gardener/gardener-resource-manager/api/resources/v1alpha1/helper"

appsv1 "k8s.io/api/apps/v1"
batchv1 "k8s.io/api/batch/v1"
corev1 "k8s.io/api/core/v1"
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.