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

Clean up and comment some desiredset funcs #292

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions pkg/apply/desiredset_apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ func (o *desiredSet) apply() error {
return err
}

// Create maps for labels and annotations containing the identifying hash used for rate limiting.
labelSet, annotationSet, err := GetLabelsAndAnnotations(o.setID, o.owner)
if err != nil {
return o.err(err)
Expand Down Expand Up @@ -141,6 +142,7 @@ func (o *desiredSet) debugID() string {
})
}

// collect converts a list of runtime objects into an ObjectByGVK map.
func (o *desiredSet) collect(objList []runtime.Object) objectset.ObjectByGVK {
result := objectset.ObjectByGVK{}
for _, obj := range objList {
Expand Down Expand Up @@ -189,6 +191,8 @@ func GetSelectorFromOwner(setID string, owner runtime.Object) (labels.Selector,
return GetSelector(ownerLabel)
}

// GetSelector returns the label selector for the owner object, which is useful
// to list the dependents.
func GetSelector(labelSet map[string]string) (labels.Selector, error) {
req, err := labels.NewRequirement(LabelHash, selection.Equals, []string{labelSet[LabelHash]})
if err != nil {
Expand All @@ -197,6 +201,10 @@ func GetSelector(labelSet map[string]string) (labels.Selector, error) {
return labels.NewSelector().Add(*req), nil
}

// GetLabelsAndAnnotations returns a new map of annotations with the setID
// and/or owner in it. It also returns a new map of labels, which contains a
// hash over the annotations. The hash is used to identify resources that
// belong to the same setID/owner.
func GetLabelsAndAnnotations(setID string, owner runtime.Object) (map[string]string, map[string]string, error) {
if setID == "" && owner == nil {
return nil, nil, fmt.Errorf("set ID or owner must be set")
Expand Down Expand Up @@ -227,6 +235,8 @@ func GetLabelsAndAnnotations(setID string, owner runtime.Object) (map[string]str
return labels, annotations, nil
}

// injectLabelsAndAnnotations returns the desiredSet's objects. The given
// labels and annotations extend and overwrite existing labels.
func (o *desiredSet) injectLabelsAndAnnotations(labels, annotations map[string]string) ([]runtime.Object, error) {
var result []runtime.Object

Expand All @@ -248,6 +258,7 @@ func (o *desiredSet) injectLabelsAndAnnotations(labels, annotations map[string]s
return result, nil
}

// setAnnotations overwrites the annotations in meta with the given annotations map.
func setAnnotations(meta metav1.Object, annotations map[string]string) {
objAnn := meta.GetAnnotations()
if objAnn == nil {
Expand All @@ -260,6 +271,7 @@ func setAnnotations(meta metav1.Object, annotations map[string]string) {
meta.SetAnnotations(objAnn)
}

// setLabels overwrites the labels in meta with the given labels map.
func setLabels(meta metav1.Object, labels map[string]string) {
objLabels := meta.GetLabels()
if objLabels == nil {
Expand Down
11 changes: 9 additions & 2 deletions pkg/apply/desiredset_compare.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (
"k8s.io/apimachinery/pkg/util/json"
"k8s.io/apimachinery/pkg/util/jsonmergepatch"
"k8s.io/apimachinery/pkg/util/strategicpatch"
"k8s.io/client-go/dynamic"
)

const (
Expand All @@ -45,6 +44,10 @@ var (
}
)

// prepareObjectForCreate returns a copy of the object with the "applied"
// annotation set. The "applied" annotation contains a serialized and pruned
// representation of obj, e.g. Strings are shortened to 64 characters and bytes
// are skipped.
func prepareObjectForCreate(gvk schema.GroupVersionKind, obj runtime.Object) (runtime.Object, error) {
serialized, err := serializeApplied(obj)
if err != nil {
Expand Down Expand Up @@ -76,6 +79,8 @@ func prepareObjectForCreate(gvk schema.GroupVersionKind, obj runtime.Object) (ru
return obj, nil
}

// originalAndModified returns the original bytes from the oldObject's
// "applied" annotation and the modified bytes for the newObject.
func originalAndModified(gvk schema.GroupVersionKind, oldMetadata v1.Object, newObject runtime.Object) ([]byte, []byte, error) {
original, err := getOriginalBytes(gvk, oldMetadata)
if err != nil {
Expand Down Expand Up @@ -169,6 +174,7 @@ func sanitizePatch(patch []byte, removeObjectSetAnnotation bool) ([]byte, error)
return json.Marshal(data)
}

// applyPatch applies the patch to the object and returns true if the object was changed.
func applyPatch(gvk schema.GroupVersionKind, reconciler Reconciler, patcher Patcher, debugID string, ignoreOriginal bool, oldObject, newObject runtime.Object, diffPatches [][]byte) (bool, error) {
oldMetadata, err := meta.Accessor(oldObject)
if err != nil {
Expand Down Expand Up @@ -235,7 +241,8 @@ func applyPatch(gvk schema.GroupVersionKind, reconciler Reconciler, patcher Patc
return true, err
}

func (o *desiredSet) compareObjects(gvk schema.GroupVersionKind, reconciler Reconciler, patcher Patcher, client dynamic.NamespaceableResourceInterface, debugID string, oldObject, newObject runtime.Object, force bool) error {
// compareObjects updates the resource with the passed in patcher. It considers WithDiffPatch and WithReconciler.
func (o *desiredSet) compareObjects(gvk schema.GroupVersionKind, reconciler Reconciler, patcher Patcher, debugID string, oldObject, newObject runtime.Object) error {
oldMetadata, err := meta.Accessor(oldObject)
if err != nil {
return err
Expand Down
5 changes: 4 additions & 1 deletion pkg/apply/desiredset_process.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,9 @@ func (o *desiredSet) filterCrossVersion(gvk schema.GroupVersionKind, keys []obje
return result
}

// process executes the apply by creating, updating, and deleting objects. It
// compares the desired state to the current state and builds necessary
// patchers.
func (o *desiredSet) process(debugID string, set labels.Selector, gvk schema.GroupVersionKind, objs objectset.ObjectByKey) {
controller, client, err := o.getControllerAndClient(debugID, gvk)
if err != nil {
Expand Down Expand Up @@ -316,7 +319,7 @@ func (o *desiredSet) process(debugID string, set labels.Selector, gvk schema.Gro
}

updateF := func(k objectset.ObjectKey) {
err := o.compareObjects(gvk, reconciler, patcher, client, debugID, existing[k], objs[k], len(toCreate) > 0 || len(toDelete) > 0)
err := o.compareObjects(gvk, reconciler, patcher, debugID, existing[k], objs[k])
if err == ErrReplace {
deleteF(k, true)
o.err(fmt.Errorf("DesiredSet - Replace Wait %s %s for %s", gvk, k, debugID))
Expand Down