BREAKING. Support for namespace label matching and secret event handling #161
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
The two functional goals of this PR are:
Many changes were made to support these two functional goals, and more changes were made to simplify the code. I categorize these changes as breaking, functional, and cleanup.
BREAKING
valueFrominside thedatafield will no longer designate a source secret. To use a secret as the source, a newfromSecretfield (an object with required stringsnameandnamespaceand optional string arraykeys) is added to the CRD. A ClusterSecret will only be valid if it has one ofdataorfromSecret, but not both.ownerReferenceson secrets (instead of theCREATE_BY_ANNOTATION) now, which will break installations withREPLACE_EXISTING: falseas the controller will refuse to update preexisting secrets.Functional
matchLabelsandmatchedSetsJoinmatchLabelstakes objects with string values. It looks the same as labels set on a kubernetes resource.matchedSetsJointakes a string with value "union" or "intersection" and performs that operation to join the namespace lists generated by eachmatchLabeland bymatchNamespace.get_ns_listand duplicated in a newsecret_belongsfunction.on_namespace_eventand is triggered bykopf.on.eventinstead ofkopf.on.create(this also prevents kopf from setting the status field on namespace resources, which seemed excessive). The handler uses the newsecret_belongsfunction to check every clustersecret to find if any match the new namespace state and syncs, deletes, or does nothing to reach the desired state.secret_belongsfunction is added to check if a single namespace matches a clustersecret. This allows us to avoid a fullget_ns_listcall when only one namespace or secret changes.on_field_match_namespacehandler is renamed toon_match_fieldsand handles events for changes to thematchNamespace,avoidNamespaces,matchLabels, andmatchedSetsJoinfields.on_field_datahandler now also handles events for changes to thefromSecretfield.on_secret_eventhandler is added to both re-sync secrets managed by a ClusterSecret (determined by whether anownerReferencewithkind: ClusterSecretis set on the secret) and trigger a full cluster secret re-sync if the changed secret is the source for a ClusterSecret.create_secret_metadatafunction now adds anownerReferenceto managed secrets' metadata. This is used by theon_secret_eventhandler and allows us to reduce theon_deletehandler as secret deletion will be handled by kubernetes on clustersecret deletion.Cleanup
CREATE_BY_ANNOTATIONis removed from secrets in favor ofownerReferences.on_deletehandler is reduced to only removing the deleted ClusterSecret from the cache, as all secrets are now deleted by kubernetes through theownerReferences.LAST_SYNC_ANNOTATIONis removed from secrets. It seems unnecessary and complicates current vs desired state comparisons done before secret updates. If added again, thesync_secretfunction will need to be updated to ignore this annotation when comparing desired to current secret states.secret_belongsis added to test whether a single namespace matches a ClusterSecret.sync_clustersecretis added to sync all targets of a ClusterSecret.startup_fnhandler now syncs all ClusterSecrets found in the cluster on operator start. Thecreate_fnhandler no longer handles ClusterSecret resume events.fromSecretfield is added to the ClusterSecret CRD, which can be used to designate a source secret. This allows ClusterSecret structure validation to be handled by kubernetes, and allowsvalueFromto be a key in ClusterSecret data.sync_secretfunction. With the new CRD the validations are unnecessary.read_data_secretfunction is moved into thesync_secretfunction, the only calling location.CREATE_BY_LABELis added with value set to the controlling ClusterSecret's name.get_child_secret_namespacesis added to get a list of synced namespaces for a clustersecret using theCREATE_BY_LABEL. This is used in place of the removed syncedns lists.This PR is not ready to be merged. There are several TODOs and a few potential improvements.
TODO
Potential Improvements
Update CRD to limitdone 19ddde5datafield to only key/value pairs with string values now thatvalueFromindatais unsupported (seematchLabelsdefinition).matchedSetsJoinstring (ensure it's one of union or intersection) - can this be done in the CRD?Remove the cache - all comparisons could be done against cluster state instead of cached state.Removed syncedns from the cache, leaving the clustersecret body 4463328Removedone 4463328syncednsfrom ClusterSecret status - deletion of secrets that used to match would need to be handled differently.