-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
825 additions
and
80 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package appcat | ||
|
||
import ( | ||
crossplane "github.com/crossplane/crossplane/apis/apiextensions/v1" | ||
v1 "github.com/vshn/appcat-apiserver/apis/appcat/v1" | ||
"k8s.io/apimachinery/pkg/runtime" | ||
genericregistry "k8s.io/apiserver/pkg/registry/generic" | ||
"k8s.io/apiserver/pkg/registry/rest" | ||
restbuilder "sigs.k8s.io/apiserver-runtime/pkg/builder/rest" | ||
"sigs.k8s.io/apiserver-runtime/pkg/util/loopback" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
) | ||
|
||
// +kubebuilder:rbac:groups="",resources=configmaps,verbs=get;list;watch,resourceNames=extension-apiserver-authentication | ||
// +kubebuilder:rbac:groups="admissionregistration.k8s.io",resources=mutatingwebhookconfigurations;validatingwebhookconfigurations,verbs=get;list;watch | ||
// +kubebuilder:rbac:groups="",resources=namespaces,verbs=get;list;watch;create;delete;update | ||
// +kubebuilder:rbac:groups="authorization.k8s.io",resources=subjectaccessreviews,verbs=get;list;watch;create;delete;update | ||
|
||
// New returns a new storage provider for AppCat | ||
func New() restbuilder.ResourceHandlerProvider { | ||
return func(s *runtime.Scheme, gasdf genericregistry.RESTOptionsGetter) (rest.Storage, error) { | ||
c, err := client.NewWithWatch(loopback.GetLoopbackMasterClientConfig(), client.Options{}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
err = v1.AddToScheme(c.Scheme()) | ||
if err != nil { | ||
return nil, err | ||
} | ||
err = crossplane.AddToScheme(c.Scheme()) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return &appcatStorage{ | ||
compositions: &kubeCompositionProvider{ | ||
Client: c, | ||
}, | ||
}, nil | ||
} | ||
} | ||
|
||
type appcatStorage struct { | ||
compositions compositionProvider | ||
} | ||
|
||
func (s *appcatStorage) New() runtime.Object { | ||
return &v1.AppCat{} | ||
} | ||
|
||
func (s *appcatStorage) Destroy() {} | ||
|
||
var _ rest.Scoper = &appcatStorage{} | ||
var _ rest.Storage = &appcatStorage{} | ||
|
||
func (s *appcatStorage) NamespaceScoped() bool { | ||
return false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
package appcat | ||
|
||
import ( | ||
"testing" | ||
|
||
crossplanev1 "github.com/crossplane/crossplane/apis/apiextensions/v1" | ||
v1 "github.com/vshn/appcat-apiserver/apis/appcat/v1" | ||
"github.com/vshn/appcat-apiserver/test/mocks" | ||
"k8s.io/apiserver/pkg/registry/rest" | ||
|
||
"github.com/golang/mock/gomock" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
) | ||
|
||
// newMockedAppCatStorage is a mocked instance of AppCatStorage | ||
func newMockedAppCatStorage(t *testing.T, ctrl *gomock.Controller) (rest.StandardStorage, *mocks.MockcompositionProvider) { | ||
t.Helper() | ||
comp := mocks.NewMockcompositionProvider(ctrl) | ||
stor := &appcatStorage{ | ||
compositions: comp, | ||
} | ||
return rest.Storage(stor).(rest.StandardStorage), comp | ||
} | ||
|
||
// Test AppCat instances | ||
var ( | ||
appCatOne = &v1.AppCat{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "one", | ||
}, | ||
|
||
Details: map[string]string{ | ||
"zone": "rma1", | ||
"displayname": "one", | ||
"docs": "https://docs.com", | ||
}, | ||
|
||
Status: v1.AppCatStatus{ | ||
CompositionName: "one", | ||
}, | ||
} | ||
compositionOne = &crossplanev1.Composition{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "one", | ||
Labels: map[string]string{ | ||
v1.OfferedKey: v1.OfferedValue, | ||
}, | ||
Annotations: map[string]string{ | ||
v1.PrefixAppCatKey + "/zone": "rma1", | ||
v1.PrefixAppCatKey + "/displayname": "one", | ||
v1.PrefixAppCatKey + "/docs": "https://docs.com", | ||
}, | ||
}, | ||
} | ||
appCatTwo = &v1.AppCat{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "two", | ||
}, | ||
|
||
Details: map[string]string{ | ||
"zone": "lpg", | ||
"displayname": "two", | ||
"docs": "https://docs.com", | ||
"productDescription": "product desc", | ||
}, | ||
|
||
Status: v1.AppCatStatus{ | ||
CompositionName: "two", | ||
}, | ||
} | ||
compositionTwo = &crossplanev1.Composition{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "two", | ||
Labels: map[string]string{ | ||
v1.OfferedKey: v1.OfferedValue, | ||
}, | ||
Annotations: map[string]string{ | ||
v1.PrefixAppCatKey + "/zone": "lpg", | ||
v1.PrefixAppCatKey + "/displayname": "two", | ||
v1.PrefixAppCatKey + "/docs": "https://docs.com", | ||
v1.PrefixAppCatKey + "/product-description": "product desc", | ||
}, | ||
}, | ||
} | ||
compositionNonOffered = &crossplanev1.Composition{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Labels: map[string]string{ | ||
v1.OfferedKey: "false", | ||
}, | ||
}, | ||
} | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package appcat | ||
|
||
import ( | ||
"context" | ||
v1 "github.com/crossplane/crossplane/apis/apiextensions/v1" | ||
metainternalversion "k8s.io/apimachinery/pkg/apis/meta/internalversion" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/apimachinery/pkg/watch" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
) | ||
|
||
// compositionProvider is an abstraction to interact with the K8s API | ||
type compositionProvider interface { | ||
GetComposition(ctx context.Context, name string, options *metav1.GetOptions) (*v1.Composition, error) | ||
ListCompositions(ctx context.Context, options *metainternalversion.ListOptions) (*v1.CompositionList, error) | ||
WatchCompositions(ctx context.Context, options *metainternalversion.ListOptions) (watch.Interface, error) | ||
} | ||
|
||
type kubeCompositionProvider struct { | ||
Client client.WithWatch | ||
} | ||
|
||
func (k *kubeCompositionProvider) GetComposition(ctx context.Context, name string, options *metav1.GetOptions) (*v1.Composition, error) { | ||
c := v1.Composition{} | ||
err := k.Client.Get(ctx, client.ObjectKey{Namespace: "", Name: name}, &c) | ||
return &c, err | ||
} | ||
|
||
func (k *kubeCompositionProvider) ListCompositions(ctx context.Context, options *metainternalversion.ListOptions) (*v1.CompositionList, error) { | ||
cl := v1.CompositionList{} | ||
err := k.Client.List(ctx, &cl, &client.ListOptions{ | ||
LabelSelector: options.LabelSelector, | ||
FieldSelector: options.FieldSelector, | ||
Limit: options.Limit, | ||
Continue: options.Continue, | ||
}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return &cl, nil | ||
} | ||
|
||
func (k *kubeCompositionProvider) WatchCompositions(ctx context.Context, options *metainternalversion.ListOptions) (watch.Interface, error) { | ||
cl := v1.CompositionList{} | ||
return k.Client.Watch(ctx, &cl, &client.ListOptions{ | ||
LabelSelector: options.LabelSelector, | ||
FieldSelector: options.FieldSelector, | ||
Limit: options.Limit, | ||
Continue: options.Continue, | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package appcat | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/apimachinery/pkg/runtime" | ||
"k8s.io/apiserver/pkg/registry/rest" | ||
) | ||
|
||
var _ rest.Creater = &appcatStorage{} | ||
|
||
func (s *appcatStorage) Create(ctx context.Context, obj runtime.Object, createValidation rest.ValidateObjectFunc, options *metav1.CreateOptions) (runtime.Object, error) { | ||
return nil, fmt.Errorf("method not implemented") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package appcat | ||
|
||
import ( | ||
"context" | ||
|
||
v1 "github.com/vshn/appcat-apiserver/apis/appcat/v1" | ||
metainternalversion "k8s.io/apimachinery/pkg/apis/meta/internalversion" | ||
|
||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/apimachinery/pkg/runtime" | ||
"k8s.io/apiserver/pkg/registry/rest" | ||
) | ||
|
||
var _ rest.GracefulDeleter = &appcatStorage{} | ||
var _ rest.CollectionDeleter = &appcatStorage{} | ||
|
||
func (s *appcatStorage) Delete(ctx context.Context, name string, deleteValidation rest.ValidateObjectFunc, options *metav1.DeleteOptions) (runtime.Object, bool, error) { | ||
return &v1.AppCat{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: name, | ||
}, | ||
}, false, nil | ||
} | ||
|
||
func (s *appcatStorage) DeleteCollection(ctx context.Context, deleteValidation rest.ValidateObjectFunc, options *metav1.DeleteOptions, listOptions *metainternalversion.ListOptions) (runtime.Object, error) { | ||
return &v1.AppCatList{ | ||
Items: []v1.AppCat{}, | ||
}, nil | ||
} |
Oops, something went wrong.