Skip to content
object.go 2.01 KiB
Newer Older
Timothee Gosselin's avatar
Timothee Gosselin committed
/*

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

// Objects package contains core kubernetes resources definitions that can be included in an application CRD.
// The definitions are stripped down version with only specs that should be user-defined.
//
// Every objects definied in this package implements a mutate interface which can create or update its associated kubernetes resource.
//
// All those objects also implements the Object inteface from which we can create an object syncer - see https://github.com/presslabs/controller-util/syncer
package objects

import (
	interfaces "k8s.libre.sh/interfaces"
	meta "k8s.libre.sh/meta"

	"github.com/presslabs/controller-util/syncer"
)

// Object interface must be supported by all types that want to sync an object.
// The object interface provides a mutate function and a runtime.Object that can be used in controller-runtime CreateOrUpdate
type Object interface {
	meta.Instance
	Mutate(obj interfaces.Object) error
	GetObject() interfaces.Object
Timothee Gosselin's avatar
Timothee Gosselin committed
	GetObjectKind() string
	GetObjectGroup() string
Timothee Gosselin's avatar
Timothee Gosselin committed
}

// NewObjectSyncer returns a syncer interface from https://github.com/presslabs/controller-util/syncer which can reconcile an object, create events and logs.
func NewObjectSyncer(i Object, owner interfaces.Object, r interfaces.Reconcile) syncer.Interface {
	obj := i.GetObject()
	obj.SetName(i.GetName())
	obj.SetNamespace(i.GetNamespace())

	return syncer.NewObjectSyncer(obj.GetObjectKind().GroupVersionKind().Kind, owner, obj, r.GetClient(), r.GetScheme(), func() error {
		return i.Mutate(obj)
	})
}