Skip to content
object.go 2.44 KiB
Newer Older
/*

Licensed under the GNU AFFERO GENERAL PUBLIC LICENSE Version 3 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    https://www.gnu.org/licenses/agpl-3.0.html

Unless 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.
*/

package status

import (
	"k8s.io/apimachinery/pkg/runtime/schema"
Timothee Gosselin's avatar
Timothee Gosselin committed
	"k8s.libre.sh/controller-utils/interfaces"
)

// ComponentList is a generic status holder for the top level resource
// +kubebuilder:object:generate=true
type Resources struct {
	// Object status array for all matching objects
	Objects []ObjectStatus `json:"objects,omitempty"`
}

func Difference(original, desired *Resources) *Resources {
	// diff := original.DeepCopy()

	diff := &Resources{}
	diffByKey := make(map[string]ObjectStatus, len(original.Objects))
	desiredByKey := make(map[string]ObjectStatus, len(desired.Objects))

	for _, obj := range original.Objects {
		diffByKey[obj.Link] = obj
	}

	for _, obj := range desired.Objects {
		desiredByKey[obj.Link] = obj
	}

	for item := range desiredByKey {
		delete(diffByKey, item)
	}

	for _, obj := range diffByKey {
		diff.Objects = append(diff.Objects, obj)
	}

	return diff
}

func (res *Resources) GetObjects() []ObjectStatus {
	return res.Objects
}

// ObjectStatus is a generic status holder for objects
// +kubebuilder:object:generate=true
type ObjectStatus struct {
	// Link to object
	Link string `json:"link,omitempty"`
	// Name of object
	Name string `json:"name,omitempty"`
	// Kind of object
	Version string `json:"version,omitempty"`
	// Kind of object
	Kind string `json:"kind,omitempty"`
	// Object group
	Group string `json:"group,omitempty"`
	// Status. Values: InProgress, Ready, Unknown
	Status string `json:"status,omitempty"`
}

func (ost *ObjectStatus) GVK() schema.GroupVersionKind {
	return schema.GroupVersionKind{
		Group:   ost.Group,
		Version: ost.Version,
		Kind:    ost.Kind,
	}
}

func ObjectToStatus(obj interfaces.Object) ObjectStatus {
	return ObjectStatus{
		Link:    obj.GetSelfLink(),
		Name:    obj.GetName(),
		Group:   obj.GetObjectKind().GroupVersionKind().Group,
		Version: obj.GetObjectKind().GroupVersionKind().Version,
		Kind:    obj.GetObjectKind().GroupVersionKind().Kind,
	}
}