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

rbd: Allow user to disable key rotation #2817

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

black-dragon74
Copy link
Contributor

This patch allows user to disable automatic
key rotation by annotating StorageCluster
with keyrotation.csiaddons.openshift.io/enable=false

@@ -281,6 +283,7 @@ func newCephBlockPoolStorageClassConfiguration(initData *ocsv1.StorageCluster) S
persistentVolumeReclaimDelete := corev1.PersistentVolumeReclaimDelete
allowVolumeExpansion := true
managementSpec := initData.Spec.ManagedResources.CephBlockPools
disableKeyRotation := !util.IsAnnotationTruthy(initData, keyRotationEnableAnnotation)
Copy link
Contributor

Choose a reason for hiding this comment

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

I believe we should use the variable name 'isKeyRotationEnabled'. What do you think?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I decided to go with disableKeyRotation as it conveys the intent more clearly. If we go with enable we will have to reverse the checks.

The goal is to add the annotation on SC only when KR is disabled.

WDYT?

Copy link
Contributor

@rchikatw rchikatw Sep 30, 2024

Choose a reason for hiding this comment

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

What I mean here is something like this:
isKeyRotationEnabled := util.IsAnnotationTruthy(initData, keyRotationEnableAnnotation)
Later you do the logic based on this var,
The main reason to use the value as is in the variable and perform the logic accordingly. Both ways are the same, but it's your choice which one you prefer.

@@ -367,6 +374,10 @@ func newNonResilientCephBlockPoolStorageClassConfiguration(initData *ocsv1.Stora
},
isClusterExternal: initData.Spec.ExternalStorage.Enable,
}
if disableKeyRotation {
Copy link
Contributor

Choose a reason for hiding this comment

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

The same piece of code is in 2 places. I think we can refactor it into a single method and reuse it. it.

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

@openshift-merge-robot openshift-merge-robot added the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Oct 1, 2024
@openshift-merge-robot openshift-merge-robot removed the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Oct 7, 2024
@@ -27,6 +27,8 @@ const (

//storage class driver name prefix
storageclassDriverNamePrefix = "openshift-storage"

Copy link
Contributor

Choose a reason for hiding this comment

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

No need for a separating line, both const are private.

Comment on lines 319 to 321
util.If(!util.IsAnnotationTruthy(initData, keyRotationEnableAnnotation), func() {
util.AddAnnotation(scc.storageClass, keyRotationEnableAnnotation, "false")
})
Copy link
Contributor

Choose a reason for hiding this comment

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

Why use a util that invokes a function for a simple if?

Suggested change
util.If(!util.IsAnnotationTruthy(initData, keyRotationEnableAnnotation), func() {
util.AddAnnotation(scc.storageClass, keyRotationEnableAnnotation, "false")
})
if !util.IsAnnotationTruthy(initData, keyRotationEnableAnnotation) {
util.AddAnnotation(scc.storageClass, keyRotationEnableAnnotation, "false")
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is exactly how it was initially. It was suggested to have a util to prevent duplication in an earlier review so I added it :D

@@ -314,6 +316,9 @@ func newCephBlockPoolStorageClassConfiguration(initData *ocsv1.StorageCluster) S
if initData.Spec.ManagedResources.CephBlockPools.DefaultStorageClass {
scc.storageClass.Annotations[defaultStorageClassAnnotation] = "true"
}
util.If(!util.IsAnnotationTruthy(initData, keyRotationEnableAnnotation), func() {
util.AddAnnotation(scc.storageClass, keyRotationEnableAnnotation, "false")
})
return scc
Copy link
Contributor

Choose a reason for hiding this comment

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

Don't we need to issue an update comment for the storagecluster to apply the new annotation?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

newCephBlockPoolStorageClassConfiguration generates config for a new SC that is going to be created. Upon creation, it will have the annotation present.

Comment on lines 112 to 117
annotations := obj.GetAnnotations()

if val, found := annotations[key]; found {
return strings.ToLower(val) == "true"
}
return false
Copy link
Contributor

Choose a reason for hiding this comment

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

The entire function just equal to

return obj.GetAnnotations()(key) == "true"

And it that case we don't need a util at all, just use the above line in the call sites

Comment on lines 120 to 125
// Execute the provided function if the condition is true.
func If(cond bool, fn func()) {
if cond {
fn()
}
}
Copy link
Contributor

Choose a reason for hiding this comment

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

This util does not make sense. What would be a good reason to call a function and pass another function into it instead of just putting a normal if in place?

Usually one creates an IfElse utils for cases where you want to treat the if as an expression (not as a statement). In that case the implementation will be something like

func IfElse[T any](cond bool, trueVal, falseVal T) T {
    if cond {
       return trueVal
    } else {
        return falseVal
    }
}

To enable you doing stuff like:

instance := someStruct {
      ...
      b: util.IfElse(someCond, Val1, val2),
      ...
}

But this is not the case here and the implementation above does not provide any value.
Please remove it

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This util does not make sense.

Couldn't agree more :)

This patch allows user to disable automatic
key rotation by annotating StorageCluster
with `keyrotation.csiaddons.openshift.io/enable=false`

Signed-off-by: Niraj Yadav <niryadav@redhat.com>
Copy link
Contributor

openshift-ci bot commented Oct 8, 2024

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: black-dragon74
Once this PR has been reviewed and has the lgtm label, please ask for approval from nb-ohad. For more information see the Kubernetes Code Review Process.

The full list of commands accepted by this bot can be found here.

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

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