Skip to content

Commit

Permalink
mockkubeapiserver: Refactor storage to be pluggable
Browse files Browse the repository at this point in the history
This helps if we want e.g. to capture all the objects easily.
  • Loading branch information
justinsb committed Oct 18, 2023
1 parent e772071 commit c317b37
Show file tree
Hide file tree
Showing 30 changed files with 3,581 additions and 3,046 deletions.
38 changes: 32 additions & 6 deletions mockkubeapiserver/apiserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,31 +22,47 @@ import (
"strings"

"k8s.io/klog/v2"
"sigs.k8s.io/kubebuilder-declarative-pattern/mockkubeapiserver/hooks"
"sigs.k8s.io/kubebuilder-declarative-pattern/mockkubeapiserver/storage"
"sigs.k8s.io/kubebuilder-declarative-pattern/mockkubeapiserver/storage/memorystorage"
)

func NewMockKubeAPIServer(addr string) (*MockKubeAPIServer, error) {
func NewMockKubeAPIServer(addr string, options ...Option) (*MockKubeAPIServer, error) {
s := &MockKubeAPIServer{}
if addr == "" {
addr = ":http"
}

s.httpServer = &http.Server{Addr: addr, Handler: s}

var err error
for _, option := range options {
if err := option(s); err != nil {
return nil, err
}
}

s.storage, err = NewMemoryStorage(NewTestClock(), NewTestUIDGenerator())
if err != nil {
return nil, err
// If storage wasn't set with an option, default to memory storage
if s.storage == nil {
storage, err := memorystorage.NewMemoryStorage(storage.NewTestClock(), storage.NewTestUIDGenerator())
if err != nil {
return nil, err
}
s.storage = storage
}

// These hooks mock behaviour that would otherwise require full controllers
s.storage.AddStorageHook(&hooks.CRDHook{})
s.storage.AddStorageHook(&hooks.NamespaceHook{})
s.storage.AddStorageHook(&hooks.DeploymentHook{})

return s, nil
}

type MockKubeAPIServer struct {
httpServer *http.Server
listener net.Listener

storage *MemoryStorage
storage storage.Storage

hooks []Hook
}
Expand Down Expand Up @@ -337,3 +353,13 @@ func (s *MockKubeAPIServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
}
}

type Option func(config *MockKubeAPIServer) error

// WithStorage is an Option that allows specifying the storage implementation.
func WithStorage(storage storage.Storage) Option {
return func(config *MockKubeAPIServer) error {
config.storage = storage
return nil
}
}
213 changes: 0 additions & 213 deletions mockkubeapiserver/controllers.go

This file was deleted.

2 changes: 1 addition & 1 deletion mockkubeapiserver/deleteresource.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func (req *deleteResource) Run(ctx context.Context, s *MockKubeAPIServer) error
return fmt.Errorf("unexpected subresource on delete %q", req.SubResource)
}

deletedObject, err := s.storage.DeleteObject(ctx, resource, id)
deletedObject, err := resource.DeleteObject(ctx, id)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion mockkubeapiserver/getresource.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func (req *getResource) Run(ctx context.Context, s *MockKubeAPIServer) error {

id := types.NamespacedName{Namespace: req.Namespace, Name: req.Name}

object, found, err := s.storage.GetObject(ctx, resource, id)
object, found, err := resource.GetObject(ctx, id)
if err != nil {
return err
}
Expand Down
38 changes: 38 additions & 0 deletions mockkubeapiserver/hooks/crd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
Copyright 2022 The Kubernetes 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 hooks

import (
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/klog/v2"
"sigs.k8s.io/kubebuilder-declarative-pattern/mockkubeapiserver/storage"
)

// CRDHook implements functionality for CRD objects (the definitions themselves, not instances of CRDs)
type CRDHook struct {
storage storage.Storage
}

func (s *CRDHook) OnWatchEvent(ev *storage.WatchEvent) {
switch ev.GroupKind() {
case schema.GroupKind{Group: "apiextensions.k8s.io", Kind: "CustomResourceDefinition"}:
// When a CRD is created, we notify the storage layer so it can store instances of the CRD
if err := s.storage.UpdateCRD(ev); err != nil {
klog.Warningf("crd change was invalid: %v", err)
}
}
}
Loading

0 comments on commit c317b37

Please sign in to comment.