Package: ops.manifest
ops-lib-manifestsRationale for this libraryMost kubernetes projects deploy with manifest files which promote suggested deployment
parameters, but those manifests aren't consistent about which options are requirements
and which options are variable. In some cases the project's distribution uses different
means of indicating a need for replacement.For example, the following reference fromvsphere-cloud-controller-managergives the consumer of this yaml an indication there should be usernames and passwords set
in the Secret object.apiVersion:v1kind:Secretmetadata:name:vsphere-cloud-secretlabels:vsphere-cpi-infra:secretcomponent:cloud-controller-managernamespace:kube-system# NOTE: this is just an example configuration, update with real values based on your environmentstringData:10.0.0.1.username:"<ENTER_YOUR_VCENTER_USERNAME>"10.0.0.1.password:"<ENTER_YOUR_VCENTER_PASSWORD>"1.2.3.4.username:"<ENTER_YOUR_VCENTER_USERNAME>"1.2.3.4.password:"<ENTER_YOUR_VCENTER_PASSWORD>"Automation tools like a juju charm will need to read these yaml manifest files, manipulate
its content, and deploy those manifests when any of theconfigurabledata is changed.Supporting Multiple ReleasesLikewise, the projects which release reference manifest files, will also release versions
of manifests. It's possible for a charm to load all the supported manifest files into a
folder structure such the charm supports multiple releases. This library supports this
requirements by having the charm store upstream manifest files unchanged in a folder
structure like this:<base_path>
├── version                  - a file containing the default version
├── manifests                - a folder containing all the releases
│   ├── v1.1.10              - a folder matching a configurable version
│   │   ├── manifest-1.yaml  - any file with a `.yaml` file type
│   │   └── manifest-2.yaml
│   ├── v1.1.11
│   │   ├── manifest-1.yaml
│   │   └── manifest-2.yaml
│   │   └── manifest-3.yamlKey file-heirarchy requirements$base_pathA single charm can support multiple manifest releasesversionA text file indicating to the library which manifest version is the default when the 'release' config is unspecifiedmanifestsA folder containing the individual release manifest folders$releaseA folder containing the yaml files of the specific releaseSample UsageOnce your charm includes the above manifest file hierarchy, your charm will need to define the
mutations the library should make to the manifests.fromops.manifestsimportCollector,Manifests,ManifestLabel,ConfigRegistryclassExampleApp(Manifests):def__init__(self,charm,charm_config):manipulations=[ManifestLabel(self),ConfigRegistry(self),UpdateSecret(self),]super().__init__("example",charm.model,"upstream/example",manipulations)self.charm_config=charm_config@propertydefconfig(self)->Dict:"""Returns config mapped from charm config and joined relations."""config=dict(**self.charm_config)forkey,valueindict(**config).items():ifvalue==""orvalueisNone:delconfig[key]# blank out keys not currently set to somethingconfig["release"]=config.pop("example-release",None)returnconfigdefis_ready(self,obj,condition)->bool:"""Filter conditions by object and condition."""if(obj.kind=="Deployment"andobj.name=="MyDeployment"andcondition.type=="Ignored"):returnNone# ignore this conditionreturnsuper().is_ready(obj,condition)classExampleCharm(CharmBase):def__init__(self,*args):super().__init__(*args)# collection of ManifestImplsself.collector=Collector(ExampleApp(self,self.config))# Register actions callbacksself.framework.observe(self.on.list_versions_action,self._list_versions)# Register update status callbacksself.framework.observe(self.on.update_status,self._update_status)def_list_versions(self,event):self.collector.list_versions(event)def_update_status(self,_):unready=self.collector.unreadyifunready:self.unit.status=WaitingStatus(", ".join(unready))else:self.unit.status=ActiveStatus("Ready")self.unit.set_workload_version(self.collector.short_version)self.app.status=ActiveStatus(self.collector.long_version)ManifestsThis class provides the following functions:Integration with lightkube to create/read/update/delete resources into the clusterProvides a means to select a manifest releaseLoads manifest files from a known file hierarchy specific to a releaseManipulates resource objects of a specific releaseProvides comparisons between the installed resources and expected resourcesProvides user listing of available releasesCreating a Manifest ImplIt's expected that the developer create aManifestimpl -- a derived class -- that implements
one property --config.  This property provides some basic requirements to the
Manifest parent class and gives context for each customManipulationto act on
relation or config data.@propertydefconfig(self)->Dict:"""Returns config mapped from charm config and joined relations."""Expectedconfigkey mappingsreleaseoptionalstrwhich identifies which release of the manifest to choose.defaults toNonewhich will select thedefault_releaseif available.ifdefault_releaseisn't found, the latest release is chosen.image-registryoptionalstrwhich will be used by theConfigRegistrymanipulationdefaults toNonewhich uses the resources built-in registry locationif specified, will replace the text up to the first/with its contentsCluster CRUD methodsstatus()queries all in cluster resources associated with the current release which
has a.status.conditionsattribute.installed_resources()queries all in cluster resources associated with the current release which
is installed.labelled_resources()queries all in cluster resources associated with the charm and manifest in general
which is installed.this can be compared with theresourcesproperty to look for extra resources
installed which are no longer necessary.apply_manifests()applies all resources from the current release into the cluster.resources are force applied, overwriting existing resources.apply_resources(*resources)andapply_resource(...)applies itemized resources into the cluster.resources are force applied, overwriting existing resources.delete_manifests(...)will delete all current release resources from the clusterseedelete_resourcesfor keyword argumentsdelete_resources(...)delete a specified set of resources from the cluster with options to
seamlessly handle certain failures.delete_resource(...)alias todelete_resourcesfor when reading clarity demands only deleting
one resource.CollectorThis class provides a native collection for operating collectively on
the manifests within a single charm.  It provides methods for responding toaction list-versionsaction scrub-resourcesaction list-resourcesaction apply-missing-resourcesquerying the collective versions (short and long types)listing which resources have a non-active statusTo integrate into anops charm, for each
released application the charm manages, create a newManifestsimpl,
and add an instance of it to aCollector.classAlternateApp(Manifests):def__init__(self,charm,charm_config):super().__init__("alternate",charm.model,"upstream/example")self.charm_config=charm_config@propertydefconfig(self)->Dict:"""Returns config mapped from charm config and joined relations."""config=dict(**self.charm_config)forkey,valueindict(**config).items():ifvalue==""orvalueisNone:delconfig[key]# blank out keys not currently set to somethingconfig["release"]=config.pop("alternate-release",None)returnconfigclassExampleCharm(CharmBase):def__init__(self,*args):...# collection of ManifestImplsself.collector=Collector(ExampleApp(self,self.config),AlternateApp(self,self.config),)ManipulationsPatching a manifest resourceSome resources already exist within the manifest, and just need to be updated.Built in PatchersManifestLabeladds to each resource'smetadata.labelsthe following:juju.io/application: manifests.app_namejuju.io/manifest: manifests.namejuju.io/manifest-version: <manifests.name>-<version>ConfigRegistryupdates the image registry of everyPod,DaemonSet,Deployment, andStatefulSetfrom theimage-registryconfig item in the config
propertiesDict.If the charm doesn't wish to alter the config, ensure nothing exists
in theimage-registry.update_tolerationnot officially a patcher, but can be used by a custom Patcher
to adjust tolerations onPod,DaemonSet,Deployment, andStatefulSetresources.Adding a manifest resourceSome resources do not exist in the release manifest and must be added. TheAdditionmanipulations are added
before the rest of thePatchmanipulations are applied.Built in AddersCreateNamespace- Creates a namespace resource using either the manifest's default namespace or
an argument passed in to the constructor of this class.Subtracting a manifest resourceSome manifest resources are not needed and must be removed. TheSubtractionmanipulations are added
before the rest of thePatchmanipulations are applied.Built in SubtractorsSubtractEq- Subtracts a manifest resource equal to the resource passed in as an argument. Resources are considered
equal if they have the same kind, name, and namespace.Custom ManipulationsOf course the built-ins will not be enough, so your charm may extend its own manipulations by defining
new objects which inherit from eitherPatchorAddition.
.. _changelog:=========
ChangelogVersions followSemantic Versioning <https://semver.org/>_ (<major>.<minor>.<patch>).Backward incompatible (breaking) changes will only be introduced in major versionsops-lib-manifest 1.2.0 (2024-02-14)#31TheCollector.conditionsproperty returns a mapping that ends up
hiding information relating to all the conditions of a kubernetes
resource.  Only ONE condition is present in the mapping for each
resource.IntroduceCollector.all_conditionsproperty returning a list of
conditions and their associated manifests and objectCheck unready using theall_conditionspropertyAllows a manifest to filter the ready check of eachconditionof an
object that it has installed by overriding theis_ready(..)methodops-lib-manifest 1.1.4 (2024-01-10)only deletes resources created by this charm applicationLP#2025283 - Audit library to ensure that secrets aren't leaked to logsmaintains python 3.7 compatabilityops-lib-manifest 1.1.3 (2023-06-28)Issues ResolvedLP#2025087resolves issue where every item from a List
type resource object is read from the listops-lib-manifest 1.1.2 (2023-04-17)Issues ResolvedLP#2006619resolves status issues when trying to use a client
which cannot reach the API endpointops-lib-manifest 1.1.1 (2022-04-06)Issues ResolvedLP#1999427resolve issues when loading CRDs from an
unreachable API endpointops-lib-manifest 1.1.0 (2022-02-17)FeatureSupports image manipulation ofJob,CronJob,ReplicationControllerandReplicaSetobjectsops-lib-manifest 1.0.0 (2022-12-14)Issues ResolvedLP#1999427handles non-api errors from the client which are represented
as an http error response without json content.Breaking Changesno longer arelightkube.core.exceptions.ApiErrors raised on the following methods:Manifest.statusManifest.installed_resourcesManifest.apply_manifestManifest.delete_manifestManifest.apply_resourcesManifest.delete_resourcesinstead a more generic exceptionManifestClientErroris raised.Apache License
                     Version 2.0, January 2004
                  http://www.apache.org/licenses/TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTIONDefinitions."License" shall mean the terms and conditions for use, reproduction,
and distribution as defined by Sections 1 through 9 of this document."Licensor" shall mean the copyright owner or entity authorized by
the copyright owner that is granting the License."Legal Entity" shall mean the union of the acting entity and all
other entities that control, are controlled by, or are under common
control with that entity. For the purposes of this definition,
"control" means (i) the power, direct or indirect, to cause the
direction or management of such entity, whether by contract or
otherwise, or (ii) ownership of fifty percent (50%) or more of the
outstanding shares, or (iii) beneficial ownership of such entity."You" (or "Your") shall mean an individual or Legal Entity
exercising permissions granted by this License."Source" form shall mean the preferred form for making modifications,
including but not limited to software source code, documentation
source, and configuration files."Object" form shall mean any form resulting from mechanical
transformation or translation of a Source form, including but
not limited to compiled object code, generated documentation,
and conversions to other media types."Work" shall mean the work of authorship, whether in Source or
Object form, made available under the License, as indicated by a
copyright notice that is included in or attached to the work
(an example is provided in the Appendix below)."Derivative Works" shall mean any work, whether in Source or Object
form, that is based on (or derived from) the Work and for which the
editorial revisions, annotations, elaborations, or other modifications
represent, as a whole, an original work of authorship. For the purposes
of this License, Derivative Works shall not include works that remain
separable from, or merely link (or bind by name) to the interfaces of,
the Work and Derivative Works thereof."Contribution" shall mean any work of authorship, including
the original version of the Work and any modifications or additions
to that Work or Derivative Works thereof, that is intentionally
submitted to Licensor for inclusion in the Work by the copyright owner
or by an individual or Legal Entity authorized to submit on behalf of
the copyright owner. For the purposes of this definition, "submitted"
means any form of electronic, verbal, or written communication sent
to the Licensor or its representatives, including but not limited to
communication on electronic mailing lists, source code control systems,
and issue tracking systems that are managed by, or on behalf of, the
Licensor for the purpose of discussing and improving the Work, but
excluding communication that is conspicuously marked or otherwise
designated in writing by the copyright owner as "Not a Contribution.""Contributor" shall mean Licensor and any individual or Legal Entity
on behalf of whom a Contribution has been received by Licensor and
subsequently incorporated within the Work.Grant of Copyright License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
copyright license to reproduce, prepare Derivative Works of,
publicly display, publicly perform, sublicense, and distribute the
Work and such Derivative Works in Source or Object form.Grant of Patent License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
(except as stated in this section) patent license to make, have made,
use, offer to sell, sell, import, and otherwise transfer the Work,
where such license applies only to those patent claims licensable
by such Contributor that are necessarily infringed by their
Contribution(s) alone or by combination of their Contribution(s)
with the Work to which such Contribution(s) was submitted. If You
institute patent litigation against any entity (including a
cross-claim or counterclaim in a lawsuit) alleging that the Work
or a Contribution incorporated within the Work constitutes direct
or contributory patent infringement, then any patent licenses
granted to You under this License for that Work shall terminate
as of the date such litigation is filed.Redistribution. You may reproduce and distribute copies of the
Work or Derivative Works thereof in any medium, with or without
modifications, and in Source or Object form, provided that You
meet the following conditions:(a) You must give any other recipients of the Work or
Derivative Works a copy of this License; and(b) You must cause any modified files to carry prominent notices
stating that You changed the files; and(c) You must retain, in the Source form of any Derivative Works
that You distribute, all copyright, patent, trademark, and
attribution notices from the Source form of the Work,
excluding those notices that do not pertain to any part of
the Derivative Works; and(d) If the Work includes a "NOTICE" text file as part of its
distribution, then any Derivative Works that You distribute must
include a readable copy of the attribution notices contained
within such NOTICE file, excluding those notices that do not
pertain to any part of the Derivative Works, in at least one
of the following places: within a NOTICE text file distributed
as part of the Derivative Works; within the Source form or
documentation, if provided along with the Derivative Works; or,
within a display generated by the Derivative Works, if and
wherever such third-party notices normally appear. The contents
of the NOTICE file are for informational purposes only and
do not modify the License. You may add Your own attribution
notices within Derivative Works that You distribute, alongside
or as an addendum to the NOTICE text from the Work, provided
that such additional attribution notices cannot be construed
as modifying the License.You may add Your own copyright statement to Your modifications and
may provide additional or different license terms and conditions
for use, reproduction, or distribution of Your modifications, or
for any such Derivative Works as a whole, provided Your use,
reproduction, and distribution of the Work otherwise complies with
the conditions stated in this License.Submission of Contributions. Unless You explicitly state otherwise,
any Contribution intentionally submitted for inclusion in the Work
by You to the Licensor shall be under the terms and conditions of
this License, without any additional terms or conditions.
Notwithstanding the above, nothing herein shall supersede or modify
the terms of any separate license agreement you may have executed
with Licensor regarding such Contributions.Trademarks. This License does not grant permission to use the trade
names, trademarks, service marks, or product names of the Licensor,
except as required for reasonable and customary use in describing the
origin of the Work and reproducing the content of the NOTICE file.Disclaimer of Warranty. Unless required by applicable law or
agreed to in writing, Licensor provides the Work (and each
Contributor provides its Contributions) on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
implied, including, without limitation, any warranties or conditions
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
PARTICULAR PURPOSE. You are solely responsible for determining the
appropriateness of using or redistributing the Work and assume any
risks associated with Your exercise of permissions under this License.Limitation of Liability. In no event and under no legal theory,
whether in tort (including negligence), contract, or otherwise,
unless required by applicable law (such as deliberate and grossly
negligent acts) or agreed to in writing, shall any Contributor be
liable to You for damages, including any direct, indirect, special,
incidental, or consequential damages of any character arising as a
result of this License or out of the use or inability to use the
Work (including but not limited to damages for loss of goodwill,
work stoppage, computer failure or malfunction, or any and all
other commercial damages or losses), even if such Contributor
has been advised of the possibility of such damages.Accepting Warranty or Additional Liability. While redistributing
the Work or Derivative Works thereof, You may choose to offer,
and charge a fee for, acceptance of support, warranty, indemnity,
or other liability obligations and/or rights consistent with this
License. However, in accepting such obligations, You may act only
on Your own behalf and on Your sole responsibility, not on behalf
of any other Contributor, and only if You agree to indemnify,
defend, and hold each Contributor harmless for any liability
incurred by, or claims asserted against, such Contributor by reason
of your accepting any such warranty or additional liability.END OF TERMS AND CONDITIONSAPPENDIX: How to apply the Apache License to your work.To apply the Apache License to your work, attach the following
boilerplate notice, with the fields enclosed by brackets "[]"
replaced with your own identifying information. (Don't include
the brackets!)  The text should be enclosed in the appropriate
comment syntax for the file format. We also recommend that a
file or class name and description of purpose be included on the
same "printed page" as the copyright notice for easier
identification within third-party archives.Copyright [yyyy] [name of copyright owner]Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License athttp://www.apache.org/licenses/LICENSE-2.0Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.