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

fix: missing status update in KMP controller #1761

Merged
merged 1 commit into from
Sep 12, 2024

Conversation

duffney
Copy link
Contributor

@duffney duffney commented Aug 29, 2024

Description

What this PR does / why we need it:

In this PR the writeKMProviderStatus and helper functions were moved from the refresher back to the controller to ensure status messages get written to the KMP resource.

Which issue(s) this PR fixes (optional, using fixes #<issue number>(, fixes #<issue_number>, ...) format, will close the issue(s) when the PR gets merged):

Fixes #

#1733

Type of change

Please delete options that are not relevant.

  • Bug fix (non-breaking change which fixes an issue)

How Has This Been Tested?

Please describe the tests that you ran to verify your changes. Please also list any relevant details for your test configuration

  • Test A
  • Test B

Checklist:

  • Does the affected code have corresponding tests?
  • Do all new files have appropriate license header?

Post Merge Requirements

  • MAINTAINERS: manually trigger the "Publish Package" workflow after merging any PR that indicates Helm Chart Change

Copy link

codecov bot commented Aug 29, 2024

Codecov Report

Attention: Patch coverage is 95.50000% with 9 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
pkg/keymanagementprovider/mocks/client.go 55.55% 7 Missing and 1 partial ⚠️
pkg/keymanagementprovider/mocks/types.go 85.71% 1 Missing ⚠️
Files with missing lines Coverage Δ
...lusterresource/keymanagementprovider_controller.go 94.11% <100.00%> (+25.93%) ⬆️
...espaceresource/keymanagementprovider_controller.go 94.04% <100.00%> (+34.95%) ⬆️
pkg/keymanagementprovider/mocks/factory.go 100.00% <100.00%> (ø)
pkg/keymanagementprovider/refresh/factory.go 100.00% <100.00%> (+11.76%) ⬆️
pkg/keymanagementprovider/refresh/kubeRefresh.go 100.00% <100.00%> (+18.18%) ⬆️
pkg/keymanagementprovider/mocks/types.go 91.66% <85.71%> (-8.34%) ⬇️
pkg/keymanagementprovider/mocks/client.go 55.55% <55.55%> (-44.45%) ⬇️

... and 20 files with indirect coverage changes

isFetchSuccessful = true
emptyErrorString := ""

writeKMProviderStatus(ctx, r, &keyManagementProvider, logger, isFetchSuccessful, emptyErrorString, lastFetchedTime, status)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: we can remove line 122 and pass true directly, same to emptyErrorString

return ctrl.Result{}, err
}

config := map[string]interface{}{
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we can define a struct instead of creating a map.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That would make things a bit easier. Great suggestion, I'll start working on converting the map to a struct.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@binbin-li I've got some questions on how we could use a struct instead of a map and support other refresher types without pulling in refresher specific values into the controller logic. Or even if that's a necessary concern. I'll bring it up at the next community meeting. :)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

discussed in community meeting, see example at

type VerificationResponse struct {

}

config := map[string]interface{}{
"type": refresherType,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: I would rename it to refresherType since we have another providerType

// Create creates a new KubeRefresher instance
func (kr *KubeRefresher) Create(config map[string]interface{}) (Refresher, error) {
client, ok := config["client"].(client.Client)
provider, ok := config["provider"].(kmp.KeyManagementProvider)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Usually we need to check exist first and then do type assertion to avoid panic. But if we make config a struct instead of map, we could fetch it via config.Provider.

func (r *KeyManagementProviderReconciler) ReconcileWithType(ctx context.Context, req ctrl.Request, refresherType string) (ctrl.Result, error) {
logger := logrus.WithContext(ctx)

var resource = req.Name
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

QQ: Shall we use resource := req.Name here

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Excellent catch, you have a keen eye @junczhu! Yes := should be used here.

// updateKMProviderErrorStatus updates the key management provider status with error, brief error and last fetched time
func updateKMProviderErrorStatus(keyManagementProvider *configv1beta1.KeyManagementProvider, errorString string, operationTime *metav1.Time) {
// truncate brief error string to maxBriefErrLength
briefErr := errorString
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

actually we made some updates to the briefErr construction recently, could you help update it following the new pattern: https://github.com/ratify-project/ratify/blob/dev/pkg/controllers/clusterresource/policy_controller.go#L121

thanks!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. :) I think I implemented it correctly.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wonder if it's the latest commit, we can set briefErr by:
keyManagementProvider.Status.BriefError = err.GetConciseError(constants.MaxBriefErrLength)

GetStatus() interface{}
}

type RefresherConfig struct {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: could add some comments to Exported struct and fields

// updateKMProviderErrorStatus updates the key management provider status with error, brief error and last fetched time
func updateKMProviderErrorStatus(keyManagementProvider *configv1beta1.KeyManagementProvider, errorString string, operationTime *metav1.Time) {
// truncate brief error string to maxBriefErrLength
briefErr := errorString
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wonder if it's the latest commit, we can set briefErr by:
keyManagementProvider.Status.BriefError = err.GetConciseError(constants.MaxBriefErrLength)

if err != nil {
writeKMProviderStatus(ctx, r, &keyManagementProvider, logger, false, err.Error(), lastFetchedTime, nil)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

probably we also need to set KMP errors since keys/certs are not fetched yet.

		kmp.SetCertificateError(resource, kmpErr)
		kmp.SetKeyError(resource, kmpErr)

binbin-li
binbin-li previously approved these changes Sep 11, 2024
@binbin-li binbin-li enabled auto-merge (squash) September 12, 2024 02:39
@binbin-li
Copy link
Collaborator

@duffney PR lgtm, could you update commits to be verified so that we can merge it?

auto-merge was automatically disabled September 12, 2024 13:00

Head branch was pushed to by a user without write access

@binbin-li binbin-li merged commit b32db85 into ratify-project:dev Sep 12, 2024
19 checks passed
akashsinghal pushed a commit to akashsinghal/ratify that referenced this pull request Sep 13, 2024
binbin-li pushed a commit to binbin-li/ratify that referenced this pull request Sep 14, 2024
@duffney duffney deleted the issue-1733 branch September 25, 2024 14:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants