From a2fbafadd56ede8c7a7c544add8c6e2f5fb7b89d Mon Sep 17 00:00:00 2001 From: Andrew Gouin Date: Mon, 6 Nov 2023 10:18:00 -0700 Subject: [PATCH] version check patch (#383) * use patch instead of update for height updates from version-check * tidy * fix test --- cmd/versioncheck.go | 41 +++++++++++++++++++------- internal/fullnode/rbac_builder.go | 2 +- internal/fullnode/rbac_builder_test.go | 2 +- 3 files changed, 33 insertions(+), 12 deletions(-) diff --git a/cmd/versioncheck.go b/cmd/versioncheck.go index eba06f5a..b642b84d 100644 --- a/cmd/versioncheck.go +++ b/cmd/versioncheck.go @@ -162,16 +162,8 @@ func checkVersion( } } - if crd.Status.Height == nil { - crd.Status.Height = make(map[string]uint64) - } - - crd.Status.Height[thisPod.Name] = uint64(height) - - if err := kClient.Status().Update( - ctx, crd, - ); err != nil { - return fmt.Errorf("failed to patch status: %w", err) + if err := patchStatusHeightIfNecessary(ctx, kClient, crd, thisPod.Name, uint64(height)); err != nil { + return err } var image string @@ -192,6 +184,35 @@ func checkVersion( return nil } +func patchStatusHeightIfNecessary( + ctx context.Context, + kClient client.Client, + crd *cosmosv1.CosmosFullNode, + instanceName string, + height uint64, +) error { + if crd.Status.Height != nil { + if h, ok := crd.Status.Height[instanceName]; ok && h == height { + // Status is up to date already. + return nil + } + } + + patch := crd.DeepCopy() + if patch.Status.Height == nil { + patch.Status.Height = make(map[string]uint64) + } + patch.Status.Height[instanceName] = height + + if err := kClient.Status().Patch( + ctx, patch, client.MergeFrom(crd.DeepCopy()), + ); err != nil { + return fmt.Errorf("failed to patch status: %w", err) + } + + return nil +} + func getBackend(backend string) dbm.BackendType { switch backend { case "goleveldb": diff --git a/internal/fullnode/rbac_builder.go b/internal/fullnode/rbac_builder.go index 219e313b..6e4022a8 100644 --- a/internal/fullnode/rbac_builder.go +++ b/internal/fullnode/rbac_builder.go @@ -72,7 +72,7 @@ func BuildRoles(crd *cosmosv1.CosmosFullNode) []diff.Resource[*rbacv1.Role] { { APIGroups: []string{"cosmos.strange.love"}, Resources: []string{"cosmosfullnodes/status"}, - Verbs: []string{"update"}, + Verbs: []string{"patch"}, }, }, } diff --git a/internal/fullnode/rbac_builder_test.go b/internal/fullnode/rbac_builder_test.go index c7681b4c..92ebb31b 100644 --- a/internal/fullnode/rbac_builder_test.go +++ b/internal/fullnode/rbac_builder_test.go @@ -71,7 +71,7 @@ func TestBuildRBAC(t *testing.T) { { APIGroups: []string{"cosmos.strange.love"}, Resources: []string{"cosmosfullnodes/status"}, - Verbs: []string{"update"}, + Verbs: []string{"patch"}, }, }, role.Rules)