mirror of
https://github.com/rancher/k3k.git
synced 2026-08-19 04:16:16 +00:00
Merge pull request #15 from galal-hussein/allocator_controller
Add init commit for address allocator
This commit is contained in:
@@ -54,6 +54,15 @@ spec:
|
||||
properties:
|
||||
enabled:
|
||||
type: boolean
|
||||
status:
|
||||
type: object
|
||||
properties:
|
||||
clusterCIDR:
|
||||
type: string
|
||||
serviceCIDR:
|
||||
type: string
|
||||
clusterDNS:
|
||||
type: string
|
||||
scope: Namespaced
|
||||
names:
|
||||
plural: clusters
|
||||
|
||||
@@ -11,7 +11,8 @@ type Cluster struct {
|
||||
metav1.ObjectMeta `json:"metadata,omitempty"`
|
||||
metav1.TypeMeta `json:",inline"`
|
||||
|
||||
Spec ClusterSpec `json:"spec"`
|
||||
Spec ClusterSpec `json:"spec"`
|
||||
Status ClusterStatus `json:"status"`
|
||||
}
|
||||
|
||||
type ClusterSpec struct {
|
||||
@@ -52,3 +53,9 @@ type IngressConfig struct {
|
||||
type LoadBalancerConfig struct {
|
||||
Enabled bool `json:"enabled"`
|
||||
}
|
||||
|
||||
type ClusterStatus struct {
|
||||
ClusterCIDR string `json:"clusterCIDR,omitempty"`
|
||||
ServiceCIDR string `json:"serviceCIDR,omitempty"`
|
||||
ClusterDNS string `json:"clusterDNS,omitempty"`
|
||||
}
|
||||
|
||||
@@ -15,6 +15,7 @@ func (in *Cluster) DeepCopyInto(out *Cluster) {
|
||||
in.ObjectMeta.DeepCopyInto(&out.ObjectMeta)
|
||||
out.TypeMeta = in.TypeMeta
|
||||
in.Spec.DeepCopyInto(&out.Spec)
|
||||
out.Status = in.Status
|
||||
return
|
||||
}
|
||||
|
||||
@@ -106,6 +107,22 @@ func (in *ClusterSpec) DeepCopy() *ClusterSpec {
|
||||
return out
|
||||
}
|
||||
|
||||
// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil.
|
||||
func (in *ClusterStatus) DeepCopyInto(out *ClusterStatus) {
|
||||
*out = *in
|
||||
return
|
||||
}
|
||||
|
||||
// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new ClusterStatus.
|
||||
func (in *ClusterStatus) DeepCopy() *ClusterStatus {
|
||||
if in == nil {
|
||||
return nil
|
||||
}
|
||||
out := new(ClusterStatus)
|
||||
in.DeepCopyInto(out)
|
||||
return out
|
||||
}
|
||||
|
||||
// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil.
|
||||
func (in *ExposeConfig) DeepCopyInto(out *ExposeConfig) {
|
||||
*out = *in
|
||||
|
||||
@@ -0,0 +1,50 @@
|
||||
package addressallocator
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/galal-hussein/k3k/pkg/apis/k3k.io/v1alpha1"
|
||||
"k8s.io/apimachinery/pkg/runtime"
|
||||
"sigs.k8s.io/controller-runtime/pkg/client"
|
||||
"sigs.k8s.io/controller-runtime/pkg/controller"
|
||||
"sigs.k8s.io/controller-runtime/pkg/handler"
|
||||
"sigs.k8s.io/controller-runtime/pkg/manager"
|
||||
"sigs.k8s.io/controller-runtime/pkg/reconcile"
|
||||
"sigs.k8s.io/controller-runtime/pkg/source"
|
||||
)
|
||||
|
||||
const (
|
||||
AddressAllocatorController = "address-allocator-controller"
|
||||
)
|
||||
|
||||
type AddressAllocatorReconciler struct {
|
||||
Client client.Client
|
||||
Scheme *runtime.Scheme
|
||||
}
|
||||
|
||||
// Add adds a new controller to the manager
|
||||
func Add(mgr manager.Manager) error {
|
||||
// initialize a new Reconciler
|
||||
reconciler := AddressAllocatorReconciler{
|
||||
Client: mgr.GetClient(),
|
||||
Scheme: mgr.GetScheme(),
|
||||
}
|
||||
|
||||
controller, err := controller.New(AddressAllocatorController, mgr, controller.Options{
|
||||
Reconciler: &reconciler,
|
||||
MaxConcurrentReconciles: 1,
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return controller.Watch(&source.Kind{Type: &v1alpha1.Cluster{}},
|
||||
&handler.EnqueueRequestForObject{})
|
||||
|
||||
}
|
||||
|
||||
// Reconcile will allocate cluster/service cidrs to new clusters
|
||||
func (r *AddressAllocatorReconciler) Reconcile(ctx context.Context, req reconcile.Request) (reconcile.Result, error) {
|
||||
return reconcile.Result{}, nil
|
||||
}
|
||||
@@ -1,74 +0,0 @@
|
||||
package ingressupdate
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/galal-hussein/k3k/pkg/controller/util"
|
||||
v1 "k8s.io/api/core/v1"
|
||||
"k8s.io/apimachinery/pkg/runtime"
|
||||
"sigs.k8s.io/controller-runtime/pkg/client"
|
||||
"sigs.k8s.io/controller-runtime/pkg/controller"
|
||||
"sigs.k8s.io/controller-runtime/pkg/handler"
|
||||
"sigs.k8s.io/controller-runtime/pkg/manager"
|
||||
"sigs.k8s.io/controller-runtime/pkg/reconcile"
|
||||
"sigs.k8s.io/controller-runtime/pkg/source"
|
||||
)
|
||||
|
||||
const (
|
||||
IngressUpdateController = "ingress-update-controller"
|
||||
)
|
||||
|
||||
type IngressUpdateReconciler struct {
|
||||
Client client.Client
|
||||
Scheme *runtime.Scheme
|
||||
}
|
||||
|
||||
// Add adds a new controller to the manager
|
||||
func Add(mgr manager.Manager) error {
|
||||
// initialize a new Reconciler
|
||||
reconciler := IngressUpdateReconciler{
|
||||
Client: mgr.GetClient(),
|
||||
Scheme: mgr.GetScheme(),
|
||||
}
|
||||
|
||||
controller, err := controller.New(IngressUpdateController, mgr, controller.Options{
|
||||
Reconciler: &reconciler,
|
||||
MaxConcurrentReconciles: 1,
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return controller.Watch(&source.Kind{Type: &v1.Node{}}, &handler.EnqueueRequestForObject{})
|
||||
|
||||
}
|
||||
|
||||
// Reconcile will update ingresses each time a node added/deleted/changes with the new addresses
|
||||
func (r *IngressUpdateReconciler) Reconcile(ctx context.Context, req reconcile.Request) (reconcile.Result, error) {
|
||||
|
||||
nodeList := v1.NodeList{}
|
||||
|
||||
if err := r.Client.List(ctx, &nodeList); err != nil {
|
||||
return reconcile.Result{}, util.WrapErr("failed to list nodes", err)
|
||||
}
|
||||
|
||||
// if err := r.Client.Get(ctx, req.NamespacedName, &cluster); err != nil {
|
||||
// return reconcile.Result{}, util.WrapErr(fmt.Sprintf("failed to get cluster %s", req.NamespacedName), err)
|
||||
// }
|
||||
|
||||
// // we create a namespace for each new cluster
|
||||
// ns := &v1.Namespace{}
|
||||
// if err := r.Client.Get(ctx, client.ObjectKey{Name: util.ClusterNamespace(&cluster)}, ns); err != nil {
|
||||
// if apierrors.IsNotFound(err) {
|
||||
// klog.Infof("creating new cluster")
|
||||
|
||||
// return reconcile.Result{}, r.createCluster(ctx, &cluster)
|
||||
// } else {
|
||||
// return reconcile.Result{},
|
||||
// util.WrapErr(fmt.Sprintf("failed to get cluster namespace %s", util.ClusterNamespace(&cluster)), err)
|
||||
// }
|
||||
// }
|
||||
|
||||
return reconcile.Result{}, nil
|
||||
}
|
||||
Reference in New Issue
Block a user