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

Externalized OwnerCache into own package and added basic test suite #101

Merged
merged 1 commit into from
Aug 11, 2021
Merged
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
File renamed without changes.
20 changes: 15 additions & 5 deletions pkg/manager/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
package manager

import (
"github.com/onmetal/onmetal-api/pkg/ownercache"
"github.com/onmetal/onmetal-api/pkg/trigger"
"github.com/onmetal/onmetal-api/pkg/usagecache"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/client-go/rest"
"sigs.k8s.io/controller-runtime/pkg/controller"
Expand All @@ -25,28 +28,35 @@ import (

type Manager struct {
manager.Manager
triggers ReconcilationTrigger
ownerCache *OwnerCache
triggers trigger.ReconcilationTrigger
ownerCache ownercache.OwnerCache
usageCache usagecache.UsageCache
}

func NewManager(config *rest.Config, options manager.Options) (*Manager, error) {
mgr, err := manager.New(config, options)
if err != nil {
return nil, err
}
trig := NewReconcilationTrigger()
oc := NewOwnerCache(mgr, trig)
trig := trigger.NewReconcilationTrigger()
oc := ownercache.NewOwnerCache(mgr, trig)
uc := usagecache.NewUsageCache(mgr, trig)
return &Manager{
Manager: mgr,
ownerCache: oc,
usageCache: uc,
triggers: trig,
}, nil
}

func (m *Manager) GetOwnerCache() *OwnerCache {
func (m *Manager) GetOwnerCache() ownercache.OwnerCache {
return m.ownerCache
}

func (m *Manager) GetUsageCache() usagecache.UsageCache {
return m.usageCache
}

func (m *Manager) RegisterControllerFor(gk schema.GroupKind, controller controller.Controller) {
m.triggers.RegisterControllerFor(gk, controller)
}
61 changes: 61 additions & 0 deletions pkg/ownercache/interface.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* Copyright (c) 2021 by the OnMetal authors.
*
* 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 ownercache

import (
"context"
"github.com/onmetal/onmetal-api/pkg/utils"
"k8s.io/apimachinery/pkg/runtime/schema"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/manager"
)

type OwnerCache interface {
Wait()
RegisterGroupKind(ctx context.Context, gk schema.GroupKind) error
ReplaceObject(object client.Object) (utils.ObjectId, utils.ObjectIds)

DeleteObject(id utils.ObjectId) utils.ObjectIds

GetOwnersFor(id utils.ObjectId) utils.ObjectIds
GetOwnersByTypeFor(id utils.ObjectId, gk schema.GroupKind) utils.ObjectIds
GetSerfsFor(id utils.ObjectId) utils.ObjectIds
GetSerfsWithTypeFor(id utils.ObjectId, gk schema.GroupKind) utils.ObjectIds
GetSerfsForObject(obj client.Object) utils.ObjectIds
GetSerfsWithTypeForObject(obj client.Object, gk schema.GroupKind) utils.ObjectIds

CreateSerf(ctx context.Context, owner, serf client.Object, opts ...client.CreateOption) error
}

type Trigger interface {
Trigger(id utils.ObjectId)
}

func NewOwnerCache(manager manager.Manager, trigger Trigger) *ownerCache {
mgr := &ownerCache{
manager: manager,
trigger: trigger,
client: manager.GetClient(),
registrations: map[schema.GroupKind]*ownerReconciler{},
owners: map[utils.ObjectId]utils.ObjectIds{},
serfs: map[utils.ObjectId]utils.ObjectIds{},
}
if mgr.manager != nil {
mgr.client = manager.GetClient()
}
return mgr
}
44 changes: 17 additions & 27 deletions pkg/manager/ownercache.go → pkg/ownercache/ownercache.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,12 @@
* limitations under the License.
*/

package manager
package ownercache

import (
"context"
"fmt"
"github.com/onmetal/onmetal-api/pkg/trigger"
"sync"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand All @@ -29,33 +30,22 @@ import (
"github.com/onmetal/onmetal-api/pkg/utils"
)

type OwnerCache struct {
type ownerCache struct {
manager manager.Manager
trigger ReconcilationTrigger
trigger Trigger
client client.Client
registrations map[schema.GroupKind]*ownerReconciler
lock sync.RWMutex
owners map[utils.ObjectId]utils.ObjectIds
serfs map[utils.ObjectId]utils.ObjectIds
ready Ready
ready trigger.Ready
}

func NewOwnerCache(manager manager.Manager, trig ReconcilationTrigger) *OwnerCache {
return &OwnerCache{
manager: manager,
trigger: trig,
client: manager.GetClient(),
registrations: map[schema.GroupKind]*ownerReconciler{},
owners: map[utils.ObjectId]utils.ObjectIds{},
serfs: map[utils.ObjectId]utils.ObjectIds{},
}
}

func (o *OwnerCache) Wait() {
func (o *ownerCache) Wait() {
o.ready.Wait()
}

func (o *OwnerCache) RegisterGroupKind(ctx context.Context, gk schema.GroupKind) error {
func (o *ownerCache) RegisterGroupKind(ctx context.Context, gk schema.GroupKind) error {
o.lock.Lock()
defer o.lock.Unlock()

Expand All @@ -68,13 +58,13 @@ func (o *OwnerCache) RegisterGroupKind(ctx context.Context, gk schema.GroupKind)
return nil
}

func (o *OwnerCache) ReplaceObject(object client.Object) (utils.ObjectId, utils.ObjectIds) {
func (o *ownerCache) ReplaceObject(object client.Object) (utils.ObjectId, utils.ObjectIds) {
o.lock.Lock()
defer o.lock.Unlock()
return o.replaceObject(object)
}

func (o *OwnerCache) DeleteObject(id utils.ObjectId) utils.ObjectIds {
func (o *ownerCache) DeleteObject(id utils.ObjectId) utils.ObjectIds {
o.lock.Lock()
defer o.lock.Unlock()
old := o.serfs[id]
Expand All @@ -90,7 +80,7 @@ func (o *OwnerCache) DeleteObject(id utils.ObjectId) utils.ObjectIds {
return old
}

func (o *OwnerCache) replaceObject(object client.Object) (utils.ObjectId, utils.ObjectIds) {
func (o *ownerCache) replaceObject(object client.Object) (utils.ObjectId, utils.ObjectIds) {
id := utils.NewObjectId(object)
oids := utils.GetOwnerIdsFor(object)
old := o.serfs[id]
Expand Down Expand Up @@ -123,13 +113,13 @@ func (o *OwnerCache) replaceObject(object client.Object) (utils.ObjectId, utils.
return id, oids.Join(old)
}

func (o *OwnerCache) GetOwnersFor(id utils.ObjectId) utils.ObjectIds {
func (o *ownerCache) GetOwnersFor(id utils.ObjectId) utils.ObjectIds {
o.lock.RLock()
defer o.lock.RUnlock()
return o.serfs[id].Copy()
}

func (o *OwnerCache) GetOwnersByTypeFor(id utils.ObjectId, gk schema.GroupKind) utils.ObjectIds {
func (o *ownerCache) GetOwnersByTypeFor(id utils.ObjectId, gk schema.GroupKind) utils.ObjectIds {
o.lock.RLock()
defer o.lock.RUnlock()
ids := utils.ObjectIds{}
Expand All @@ -141,13 +131,13 @@ func (o *OwnerCache) GetOwnersByTypeFor(id utils.ObjectId, gk schema.GroupKind)
return ids
}

func (o *OwnerCache) GetSerfsFor(id utils.ObjectId) utils.ObjectIds {
func (o *ownerCache) GetSerfsFor(id utils.ObjectId) utils.ObjectIds {
o.lock.RLock()
defer o.lock.RUnlock()
return o.owners[id].Copy()
}

func (o *OwnerCache) GetSerfsWithTypeFor(id utils.ObjectId, gk schema.GroupKind) utils.ObjectIds {
func (o *ownerCache) GetSerfsWithTypeFor(id utils.ObjectId, gk schema.GroupKind) utils.ObjectIds {
o.lock.RLock()
defer o.lock.RUnlock()
ids := utils.ObjectIds{}
Expand All @@ -159,15 +149,15 @@ func (o *OwnerCache) GetSerfsWithTypeFor(id utils.ObjectId, gk schema.GroupKind)
return ids
}

func (o *OwnerCache) GetSerfsForObject(obj client.Object) utils.ObjectIds {
func (o *ownerCache) GetSerfsForObject(obj client.Object) utils.ObjectIds {
return o.GetSerfsFor(utils.NewObjectId(obj))
}

func (o *OwnerCache) GetSerfsWithTypeForObject(obj client.Object, gk schema.GroupKind) utils.ObjectIds {
func (o *ownerCache) GetSerfsWithTypeForObject(obj client.Object, gk schema.GroupKind) utils.ObjectIds {
return o.GetSerfsWithTypeFor(utils.NewObjectId(obj), gk)
}

func (o *OwnerCache) CreateSerf(ctx context.Context, owner, serf client.Object, opts ...client.CreateOption) error {
func (o *ownerCache) CreateSerf(ctx context.Context, owner, serf client.Object, opts ...client.CreateOption) error {
serf.SetOwnerReferences([]metav1.OwnerReference{OwnerRefForObject(owner)})
o.lock.Lock()
defer o.lock.Unlock()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

package manager
package ownercache

import (
"context"
Expand All @@ -34,7 +34,7 @@ type ownerReconciler struct {
client client.Client
gk schema.GroupKind
Log logr.Logger
cache *OwnerCache
cache *ownerCache
setupOnce sync.Once
objType reflect.Type
}
Expand Down Expand Up @@ -62,7 +62,7 @@ func (r *ownerReconciler) setup(ctx context.Context) {
}
}

func (r *ownerReconciler) SetupWithCache(ctx context.Context, cache *OwnerCache) error {
func (r *ownerReconciler) SetupWithCache(ctx context.Context, cache *ownerCache) error {
r.client = cache.client
r.cache = cache
obj := utils.GetObjectForGroupKind(r.client, r.gk)
Expand Down
28 changes: 28 additions & 0 deletions pkg/ownercache/suite_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* Copyright (c) 2021 by the OnMetal authors.
*
* 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 ownercache

import (
"github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"testing"
)

func Test(t *testing.T) {
RegisterFailHandler(ginkgo.Fail)
ginkgo.RunSpecs(t, "Ownercache")
}
42 changes: 42 additions & 0 deletions pkg/trigger/interface.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Copyright (c) 2021 by the OnMetal authors.
*
* 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 trigger

import (
"github.com/onmetal/onmetal-api/pkg/utils"
"k8s.io/apimachinery/pkg/runtime/schema"
"sigs.k8s.io/controller-runtime/pkg/controller"
)

// ReconcilationTrigger is used to register controllers
// to be externally triggerable for a dedicated object.
// given by its object id.
type ReconcilationTrigger interface {
// RegisterControllerFor registers a controller to receice
// reconcilation requests for a dedicated group kind, if
// the Trigger method is called for such an object id.
//
// ATTENTION: Because a controller is always responsible for ONLY
// one object type, but is does not provide a method to
// get this type, this is not validated during the registration.
// Therefore this method MUST only be called for a controller
// if it is handling this type of object.
RegisterControllerFor(gk schema.GroupKind, c controller.Controller)

// Trigger triggers all controllers registered for the GroupKind of the given id
Trigger(id utils.ObjectId)
}
2 changes: 1 addition & 1 deletion pkg/manager/ready.go → pkg/trigger/ready.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

package manager
package trigger

import "sync"

Expand Down
28 changes: 28 additions & 0 deletions pkg/trigger/suite_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* Copyright (c) 2021 by the OnMetal authors.
*
* 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 trigger

import (
"github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"testing"
)

func Test(t *testing.T) {
RegisterFailHandler(ginkgo.Fail)
ginkgo.RunSpecs(t, "Trigger")
}
Loading