Flux2 Database Schema
Flux2 uses a database to store information about the resources it manages. The database schema is defined by the kustomizev1.ResourceInventory
struct. This struct is used to track the state of resources in a Kubernetes cluster.
kustomizev1.ResourceInventory
Struct
The kustomizev1.ResourceInventory
struct is defined in the github.com/fluxcd/flux2/v2/internal/build/diff.go
file. It contains a slice of kustomizev1.ResourceRef
structs, which represent the metadata of each resource.
File: internal/build/diff.go
// addObjectsToInventory extracts the metadata from the given objects and adds it to the inventory.
func addObjectsToInventory(inv *kustomizev1.ResourceInventory, entry *ssa.ChangeSetEntry) error {
if entry == nil {
return nil
}
inv.Entries = append(inv.Entries, kustomizev1.ResourceRef{
ID: entry.ObjMetadata.String(),
Version: entry.GroupVersion,
})
return nil
}
// listMetaInInventory returns the inventory entries as object.ObjMetadata objects.
func listMetaInInventory(inv *kustomizev1.ResourceInventory) (object.ObjMetadataSet, error) {
var metas []object.ObjMetadata
for _, e := range inv.Entries {
m, err := object.ParseObjMetadata(e.ID)
if err != nil {
return metas, err
}
metas = append(metas, m)
}
return metas, nil
}
// diffInventory returns the slice of objects that do not exist in the target inventory.
func diffInventory(inv *kustomizev1.ResourceInventory, target *kustomizev1.ResourceInventory) ([]*unstructured.Unstructured, error) {
versionOf := func(i *kustomizev1.ResourceInventory, objMetadata object.ObjMetadata) string {
for _, entry := range i.Entries {
if entry.ID == objMetadata.String() {
return entry.Version
}
}
return ""
}
objects := make([]*unstructured.Unstructured, 0)
aList, err := listMetaInInventory(inv)
if err != nil {
return nil, err
}
bList, err := listMetaInInventory(target)
if err != nil {
return nil, err
}
list := aList.Diff(bList)
if len(list) == 0 {
return objects, nil
}
for _, metadata := range list {
u := &unstructured.Unstructured{}
u.SetGroupVersionKind(schema.GroupVersionKind{
Group: metadata.GroupKind.Group,
Kind: metadata.GroupKind.Kind,
Version: versionOf(inv, metadata),
})
u.SetName(metadata.Name)
u.SetNamespace(metadata.Namespace)
objects = append(objects, u)
}
sort.Sort(ssa.SortableUnstructureds(objects))
return objects, nil
}
func newInventory() *kustomizev1.ResourceInventory {
return &kustomizev1.ResourceInventory{
Entries: []kustomizev1.ResourceRef{},
}
}
kustomizev1.ResourceRef
Struct
The kustomizev1.ResourceRef
struct is defined within the kustomizev1.ResourceInventory
struct. It represents the metadata of a single resource.
// ResourceRef is a lightweight representation of a Kustomization resource.
type ResourceRef struct {
ID string `json:"id"`
Version string `json:"version"`
}
The ID
field is a string representation of the resource’s metadata, including its group, kind, name, and namespace. The Version
field stores the resource’s API version.
Example
An example of a kustomizev1.ResourceInventory
entry could be:
{
"id": "apps/v1/Deployment/nginx-deployment/default",
"version": "apps/v1"
}
This entry represents a Deployment
resource named nginx-deployment
in the default
namespace. The API version of the resource is apps/v1
.
Summary
The kustomizev1.ResourceInventory
struct is a key component of Flux2’s database schema. It provides a mechanism for storing and managing metadata about the resources that Flux2 is responsible for.
This information allows Flux2 to efficiently track changes in the Kubernetes cluster and to manage deployments and other resources.