Skip to content
object.go 3.98 KiB
Newer Older
Timothee Gosselin's avatar
WIP
Timothee Gosselin committed
/*
Copyright 2018 Pressinfra SRL.

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 at

    http://www.apache.org/licenses/LICENSE-2.0

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 syncer

import (
	"fmt"

	"github.com/ankitrgadiya/operatorlib/pkg/interfaces"
	corev1 "k8s.io/api/core/v1"
	"k8s.io/apimachinery/pkg/runtime"
	"k8s.io/client-go/tools/record"
	"sigs.k8s.io/controller-runtime/pkg/client"
	"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
)

var (
	errOwnerDeleted = fmt.Errorf("owner is deleted")

	// ErrIgnore when returned the syncer ignores it and returns nil
	ErrIgnore = fmt.Errorf("ignored error")
)

// ObjectSyncer is a syncer.Interface for syncing kubernetes.Objects only by
// passing a SyncFn
type ObjectSyncer struct {
	Owner          interfaces.Object
	Obj            interfaces.Object
Timothee Gosselin's avatar
WIP
Timothee Gosselin committed
	SyncFn         controllerutil.MutateFn
	Name           string
	Reconcile      interfaces.Reconcile
	previousObject runtime.Object
}

// stripSecrets returns the object without secretData
func stripSecrets(obj runtime.Object) runtime.Object {
	// if obj is secret, don't print secret data
	s, ok := obj.(*corev1.Secret)
	if ok {
		s.Data = nil
		s.StringData = nil

		return s
	}

	return obj
}

// Object returns the ObjectSyncer subject
func (s *ObjectSyncer) Object() interfaces.Object {
Timothee Gosselin's avatar
WIP
Timothee Gosselin committed
	return s.Obj
}

// ObjectOwner returns the ObjectSyncer owner
func (s *ObjectSyncer) ObjectOwner() interfaces.Object {
Timothee Gosselin's avatar
WIP
Timothee Gosselin committed
	return s.Owner
}

// ObjectWithoutSecretData returns the ObjectSyncer subject without secret data
func (s *ObjectSyncer) ObjectWithoutSecretData() runtime.Object {
	return stripSecrets(s.Obj)
}

// PreviousWithoutSecretData returns the ObjectSyncer previous subject without secret data
func (s *ObjectSyncer) PreviousWithoutSecretData() runtime.Object {
	return stripSecrets(s.previousObject)
}

// ObjectType returns the type of the ObjectSyncer subject
func (s *ObjectSyncer) ObjectType() string {
	return fmt.Sprintf("%T", s.Obj)
}

// OwnerType returns the type of the ObjectSyncer owner
func (s *ObjectSyncer) ObjectOwnerType() string {
	return fmt.Sprintf("%T", s.Owner)
}

// OwnerType returns the type of the ObjectSyncer owner
func (s *ObjectSyncer) ObjectName() string {
Timothee Gosselin's avatar
WIP
Timothee Gosselin committed
	return s.Name
}

// OwnerType returns the type of the ObjectSyncer owner
func (s *ObjectSyncer) SetPreviousObject() {
Timothee Gosselin's avatar
Timothee Gosselin committed
	s.previousObject = s.Object()
	//s.previousObject = s.Object()
Timothee Gosselin's avatar
WIP
Timothee Gosselin committed
	return
}

// OwnerType returns the type of the ObjectSyncer owner
func (s *ObjectSyncer) Mutate() controllerutil.MutateFn {
	return s.SyncFn
}

// OwnerType returns the type of the ObjectSyncer owner
func (s *ObjectSyncer) GetClient() client.Client {
	return s.Reconcile.GetClient()
}

// OwnerType returns the type of the ObjectSyncer owner
func (s *ObjectSyncer) GetScheme() *runtime.Scheme {
	return s.Reconcile.GetScheme()
}

// OwnerType returns the type of the ObjectSyncer owner
func (s *ObjectSyncer) GetRecorder() record.EventRecorder {
	return s.Reconcile.GetRecorder()
}

// OwnerType returns the type of the ObjectSyncer owner
// func (s *ObjectSyncer) GetLogger() logr.Logger {
//	return s.Reconcile.GetLogger()
//}

// NewObjectSyncer creates a new kubernetes.Object syncer for a given object
// with an owner and persists data using controller-runtime's CreateOrUpdate.
// The name is used for logging and event emitting purposes and should be an
// valid go identifier in upper camel case. (eg. MysqlStatefulSet)
func NewObjectSyncer(name string, owner, obj interfaces.Object, r interfaces.Reconcile,
Timothee Gosselin's avatar
WIP
Timothee Gosselin committed
	syncFn controllerutil.MutateFn) Interface {
	return &ObjectSyncer{
		Owner:     owner,
		Obj:       obj,
		SyncFn:    syncFn,
		Name:      name,
		Reconcile: r,
	}
}