-
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.
Add subnamespace controller functionality
- Loading branch information
Showing
6 changed files
with
183 additions
and
9 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
54 changes: 54 additions & 0 deletions
54
config/crd/bases/multitenancy.edge-net.io_subnamespaces.yaml
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,54 @@ | ||
--- | ||
apiVersion: apiextensions.k8s.io/v1 | ||
kind: CustomResourceDefinition | ||
metadata: | ||
annotations: | ||
controller-gen.kubebuilder.io/version: v0.14.0 | ||
name: subnamespaces.multitenancy.edge-net.io | ||
spec: | ||
group: multitenancy.edge-net.io | ||
names: | ||
kind: SubNamespace | ||
listKind: SubNamespaceList | ||
plural: subnamespaces | ||
singular: subnamespace | ||
scope: Namespaced | ||
versions: | ||
- name: v1 | ||
schema: | ||
openAPIV3Schema: | ||
description: SubNamespace is the Schema for the subnamespaces API | ||
properties: | ||
apiVersion: | ||
description: |- | ||
APIVersion defines the versioned schema of this representation of an object. | ||
Servers should convert recognized schemas to the latest internal value, and | ||
may reject unrecognized values. | ||
More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources | ||
type: string | ||
kind: | ||
description: |- | ||
Kind is a string value representing the REST resource this object represents. | ||
Servers may infer this from the endpoint the client submits requests to. | ||
Cannot be updated. | ||
In CamelCase. | ||
More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds | ||
type: string | ||
metadata: | ||
type: object | ||
spec: | ||
description: SubNamespaceSpec defines the desired state of SubNamespace | ||
properties: | ||
foo: | ||
description: Foo is an example field of SubNamespace. Edit subnamespace_types.go | ||
to remove/update | ||
type: string | ||
type: object | ||
status: | ||
description: SubNamespaceStatus defines the observed state of SubNamespace | ||
type: object | ||
type: object | ||
served: true | ||
storage: true | ||
subresources: | ||
status: {} |
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 |
---|---|---|
|
@@ -20,17 +20,21 @@ import ( | |
"context" | ||
|
||
"k8s.io/apimachinery/pkg/runtime" | ||
"k8s.io/client-go/tools/record" | ||
ctrl "sigs.k8s.io/controller-runtime" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
"sigs.k8s.io/controller-runtime/pkg/log" | ||
|
||
multitenancyv1 "github.com/edgenet-project/edgenet/api/multitenancy/v1" | ||
"github.com/edgenet-project/edgenet/internal/multitenancy/v1" | ||
"github.com/edgenet-project/edgenet/internal/utils" | ||
) | ||
|
||
// SubNamespaceReconciler reconciles a SubNamespace object | ||
type SubNamespaceReconciler struct { | ||
client.Client | ||
Scheme *runtime.Scheme | ||
Scheme *runtime.Scheme | ||
recorder record.EventRecorder | ||
} | ||
|
||
//+kubebuilder:rbac:groups=multitenancy.edge-net.io,resources=subnamespaces,verbs=get;list;watch;create;update;patch;delete | ||
|
@@ -47,15 +51,46 @@ type SubNamespaceReconciler struct { | |
// For more details, check Reconcile and its Result here: | ||
// - https://pkg.go.dev/sigs.k8s.io/[email protected]/pkg/reconcile | ||
func (r *SubNamespaceReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) { | ||
_ = log.FromContext(ctx) | ||
logger := log.FromContext(ctx) | ||
sns := multitenancyv1.SubNamespace{} | ||
isMarkedForDeletion, reconcileResult, err := utils.GetResourceWithFinalizer(ctx, r.Client, &sns, req.NamespacedName) | ||
|
||
// TODO(user): your logic here | ||
if !utils.IsObjectInitialized(&sns) { | ||
return reconcileResult, err | ||
} | ||
|
||
multiTenancyManager, err := multitenancy.NewMultiTenancyManager(ctx, r.Client) | ||
|
||
if err != nil { | ||
logger.Error(err, "cannot create multitenancy manager") | ||
return ctrl.Result{}, err | ||
} | ||
|
||
if isMarkedForDeletion { | ||
// Do a cleanup and allow tenant object for deletion | ||
if err := multiTenancyManager.SubNamespaceCleanup(ctx, &sns); err != nil { | ||
utils.RecordEventError(r.recorder, &sns, "SubNamespace cleanup failed") | ||
return ctrl.Result{Requeue: true}, err | ||
} | ||
|
||
return utils.AllowObjectDeletion(ctx, r.Client, &sns) | ||
} else { | ||
// TODO: What to do now? | ||
if err := multiTenancyManager.SetupSubNamespace(ctx, &sns); err != nil { | ||
utils.RecordEventError(r.recorder, &sns, "SubNamespace setup failed") | ||
return ctrl.Result{Requeue: true}, err | ||
} | ||
} | ||
|
||
utils.RecordEventInfo(r.recorder, &sns, "SubNamespace reconciliation successfull") | ||
return ctrl.Result{}, nil | ||
} | ||
|
||
// SetupWithManager sets up the controller with the Manager. | ||
func (r *SubNamespaceReconciler) SetupWithManager(mgr ctrl.Manager) error { | ||
// Setup the event recorder | ||
r.recorder = utils.GetEventRecorder(mgr) | ||
|
||
return ctrl.NewControllerManagedBy(mgr). | ||
For(&multitenancyv1.SubNamespace{}). | ||
Complete(r) | ||
|
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