Skip to content

Commit

Permalink
feat: add partly reconciler
Browse files Browse the repository at this point in the history
  • Loading branch information
whg517 committed May 16, 2024
1 parent 14eb047 commit 0fdb5a9
Show file tree
Hide file tree
Showing 20 changed files with 993 additions and 4 deletions.
2 changes: 1 addition & 1 deletion api/v1alpha1/image_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ package v1alpha1
type ImageSpec struct {
Custom string `json:"custom,omitempty"`
Repo string `json:"repo,omitempty"`
KdsVersion string `json:"kdsVersion,omitempty"`
KDSVersion string `json:"kdsVersion,omitempty"`
ProductVersion string `json:"productVersion,omitempty"`
}
68 changes: 68 additions & 0 deletions internal/controller/cluster/cluster.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package cluster

import (
"context"

supersetv1alpha1 "github.com/zncdata-labs/superset-operator/api/v1alpha1"
"github.com/zncdata-labs/superset-operator/internal/controller/node"
"github.com/zncdata-labs/superset-operator/internal/controller/worker"
apiv1alpha1 "github.com/zncdata-labs/superset-operator/pkg/apis/v1alpha1"
"github.com/zncdata-labs/superset-operator/pkg/image"
"github.com/zncdata-labs/superset-operator/pkg/reconciler"
)

type Reconciler struct {
reconciler.BaseClusterReconciler[*supersetv1alpha1.SupersetClusterSpec]
ClusterOperation *apiv1alpha1.ClusterOperationSpec
ClusterConfig *supersetv1alpha1.ClusterConfigSpec
Image image.Image
}

func (r *Reconciler) RegisterResources(_ context.Context) error {

node := node.NewReconciler(
r.Client,
r.ClusterConfig,
r.ClusterOperation,
r.Image,
"node",
r.Spec.Node,
)
r.AddResource(node)

worker := worker.NewReconciler(
r.Client,
r.ClusterConfig,
r.ClusterOperation,
r.Image,
"worker",
r.Spec.Worker,
)
r.AddResource(worker)

return nil
}

func NewReconciler(
client reconciler.ResourceClient,
cluster *supersetv1alpha1.SupersetCluster,
) *Reconciler {

image := image.Image{
Repo: cluster.Spec.Image.Repo,
Custom: cluster.Spec.Image.Custom,
KDSVersion: cluster.Spec.Image.KDSVersion,
ProductVersion: cluster.Spec.Image.ProductVersion,
}

return &Reconciler{
BaseClusterReconciler: *reconciler.NewBaseClusterReconciler(
client,
client.GetOwnerName(),
&cluster.Spec,
),
ClusterOperation: cluster.Spec.ClusterOperation,
ClusterConfig: cluster.Spec.ClusterConfig,
Image: image,
}
}
37 changes: 37 additions & 0 deletions internal/controller/cluster/job.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package cluster

import (
supersetv1alpha1 "github.com/zncdata-labs/superset-operator/api/v1alpha1"
"github.com/zncdata-labs/superset-operator/pkg/image"
"github.com/zncdata-labs/superset-operator/pkg/reconciler"
batchv1 "k8s.io/api/batch/v1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

type JobReconciler struct {
reconciler.BaseResourceReconciler[*supersetv1alpha1.SupersetClusterSpec]
Image image.Image
}

func (r *JobReconciler) Build() (*batchv1.Job, error) {
obj := &batchv1.Job{
ObjectMeta: r.GetObjectMeta(),
Spec: batchv1.JobSpec{
Template: corev1.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{
Labels: r.Client.GetLabels(),
},
Spec: corev1.PodSpec{
Containers: []corev1.Container{
{
Name: r.GetName(),
Image: r.Image.Custom,
},
},
},
},
},
}
return obj, nil
}
37 changes: 37 additions & 0 deletions internal/controller/node/deployment.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package node

import (
supersetv1alpha1 "github.com/zncdata-labs/superset-operator/api/v1alpha1"
"github.com/zncdata-labs/superset-operator/pkg/image"
"github.com/zncdata-labs/superset-operator/pkg/reconciler"
corev1 "k8s.io/api/core/v1"
)

type DeploymentReconciler struct {
reconciler.DeploymentReconciler[*supersetv1alpha1.NodeRoleGroupSpec]
ClusterConfig *supersetv1alpha1.ClusterConfigSpec
}

func NewDeploymentReconciler(
client reconciler.ResourceClient,

name string,

clusterConfig *supersetv1alpha1.ClusterConfigSpec,
image image.Image,
ports []corev1.ContainerPort,

spec *supersetv1alpha1.NodeRoleGroupSpec,
) *DeploymentReconciler {

return &DeploymentReconciler{
DeploymentReconciler: *reconciler.NewDeploymentReconciler(
client,
name,
image,
ports,
spec,
),
ClusterConfig: clusterConfig,
}
}
31 changes: 31 additions & 0 deletions internal/controller/node/ingress.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package node

import (
supersetv1alpha1 "github.com/zncdata-labs/superset-operator/api/v1alpha1"
"github.com/zncdata-labs/superset-operator/pkg/reconciler"
corev1 "k8s.io/api/core/v1"
networkv1 "k8s.io/api/networking/v1"
)

type IngressReconciler struct {
reconciler.BaseResourceReconciler[*supersetv1alpha1.NodeRoleGroupSpec]
Ports []corev1.ContainerPort
}

func (r *IngressReconciler) Build() (*networkv1.Ingress, error) {
panic("unimplemented")
}

func NewIngressReconciler(
client reconciler.ResourceClient,
roleGroupName string,
spec *supersetv1alpha1.NodeRoleGroupSpec,
) *IngressReconciler {
return &IngressReconciler{
BaseResourceReconciler: *reconciler.NewBaseResourceReconciler(
client,
roleGroupName,
spec,
),
}
}
12 changes: 12 additions & 0 deletions internal/controller/node/port.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package node

import corev1 "k8s.io/api/core/v1"

var (
Ports = []corev1.ContainerPort{
{
Name: "http",
ContainerPort: 8088,
},
}
)
78 changes: 78 additions & 0 deletions internal/controller/node/role.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package node

import (
"context"

supersetv1alpha1 "github.com/zncdata-labs/superset-operator/api/v1alpha1"
apiv1alpha1 "github.com/zncdata-labs/superset-operator/pkg/apis/v1alpha1"
"github.com/zncdata-labs/superset-operator/pkg/image"
"github.com/zncdata-labs/superset-operator/pkg/reconciler"
)

var _ reconciler.RoleReconciler = &Reconciler{}

type Reconciler struct {
reconciler.BaseRoleReconciler[*supersetv1alpha1.NodeSpec]
ClusterConfig *supersetv1alpha1.ClusterConfigSpec
}

func (r *Reconciler) RegisterResources(ctx context.Context) error {
for name, rg := range r.Spec.RoleGroups {
mergedRoleGroup := rg.DeepCopy()
r.MergeRoleGroup(&mergedRoleGroup)

if err := r.RegisterResourceWithRoleGroup(ctx, name, mergedRoleGroup); err != nil {
return err
}
}
return nil
}

func (r *Reconciler) RegisterResourceWithRoleGroup(
_ context.Context,
name string,
roleGroup *supersetv1alpha1.NodeRoleGroupSpec,
) error {

deployment := NewDeploymentReconciler(
r.Client,
name,
r.ClusterConfig,
r.Image,
Ports,
roleGroup,
)
r.AddResource(deployment)

service := NewServiceReconciler(
r.Client,
name,
Ports,
roleGroup,
)
r.AddResource(service)

return nil
}

func NewReconciler(
client reconciler.ResourceClient,
clusterConfig *supersetv1alpha1.ClusterConfigSpec,
clusterOperation *apiv1alpha1.ClusterOperationSpec,
imageSpec image.Image,
name string,
spec *supersetv1alpha1.NodeSpec,

) *Reconciler {

return &Reconciler{
BaseRoleReconciler: *reconciler.NewBaseRoleReconciler(
client,
name,
clusterOperation,
imageSpec,
spec,
),
ClusterConfig: clusterConfig,
}
}
53 changes: 53 additions & 0 deletions internal/controller/node/service.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package node

import (
"context"

supersetv1alpha1 "github.com/zncdata-labs/superset-operator/api/v1alpha1"
"github.com/zncdata-labs/superset-operator/pkg/reconciler"
corev1 "k8s.io/api/core/v1"
)

type ServiceReconciler struct {
reconciler.BaseResourceReconciler[*supersetv1alpha1.NodeRoleGroupSpec]
Ports []corev1.ContainerPort
}

func (r *ServiceReconciler) Build(_ context.Context) (*corev1.Service, error) {
var ports []corev1.ServicePort

for _, port := range r.Ports {
ports = append(ports, corev1.ServicePort{
Name: port.Name,
Port: port.ContainerPort,
Protocol: port.Protocol,
})
}

obj := &corev1.Service{
ObjectMeta: r.GetObjectMeta(),
Spec: corev1.ServiceSpec{
Selector: map[string]string{
"app": r.GetName(),
},
Ports: ports,
},
}
return obj, nil
}

func NewServiceReconciler(
client reconciler.ResourceClient,
roleGroupName string,
ports []corev1.ContainerPort,
spec *supersetv1alpha1.NodeRoleGroupSpec,
) *ServiceReconciler {
return &ServiceReconciler{
BaseResourceReconciler: *reconciler.NewBaseResourceReconciler(
client,
roleGroupName,
spec,
),
Ports: ports,
}
}
45 changes: 42 additions & 3 deletions internal/controller/supersetcluster_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ package controller
import (
"context"

"github.com/zncdata-labs/superset-operator/internal/controller/cluster"
"github.com/zncdata-labs/superset-operator/pkg/reconciler"
"k8s.io/apimachinery/pkg/runtime"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
Expand All @@ -32,13 +34,50 @@ type SupersetClusterReconciler struct {
Scheme *runtime.Scheme
}

//+kubebuilder:rbac:groups=superset.zncdata.dev,resources=supersetclusters,verbs=get;list;watch;create;update;patch;delete
//+kubebuilder:rbac:groups=superset.zncdata.dev,resources=supersetclusters/status,verbs=get;update;patch
//+kubebuilder:rbac:groups=superset.zncdata.dev,resources=supersetclusters/finalizers,verbs=update
var (
logger = ctrl.Log.WithName("common").WithName("reconciler")
)

// +kubebuilder:rbac:groups=superset.zncdata.dev,resources=supersetclusters,verbs=get;list;watch;create;update;patch;delete
// +kubebuilder:rbac:groups=superset.zncdata.dev,resources=supersetclusters/status,verbs=get;update;patch
// +kubebuilder:rbac:groups=superset.zncdata.dev,resources=supersetclusters/finalizers,verbs=update

// For more details, check Reconcile and its Result here:
// - https://pkg.go.dev/sigs.k8s.io/[email protected]/pkg/reconcile
func (r *SupersetClusterReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {

logger.V(0).Info("Reconciling SupersetCluster")

instance := &supersetv1alpha1.SupersetCluster{}
err := r.Get(ctx, req.NamespacedName, instance)
if err != nil {
if client.IgnoreNotFound(err) == nil {
logger.V(1).Info("SupersetCluster resource not found. Ignoring since object must be deleted.")
return ctrl.Result{}, nil
}
return ctrl.Result{}, err
}

resourceClient := reconciler.ResourceClient{
Client: r.Client,
}

clusterRreconciler := cluster.NewReconciler(resourceClient, instance)

if err := clusterRreconciler.RegisterResources(ctx); err != nil {
return ctrl.Result{}, err
}

if result := clusterRreconciler.Reconcile(); result.RequeueOrNot() {
return result.Result()
}

if result := clusterRreconciler.Ready(); result.RequeueOrNot() {
return result.Result()
}

logger.V(0).Info("Reconcile completed")

return ctrl.Result{}, nil
}

Expand Down
Loading

0 comments on commit 0fdb5a9

Please sign in to comment.