Skip to content

Commit

Permalink
dev: modules
Browse files Browse the repository at this point in the history
  • Loading branch information
sunsingerus committed Nov 29, 2023
1 parent 8fdad88 commit a0d1452
Show file tree
Hide file tree
Showing 34 changed files with 1,068 additions and 1,061 deletions.
160 changes: 80 additions & 80 deletions pkg/controller/chi/controller.go

Large diffs are not rendered by default.

36 changes: 18 additions & 18 deletions pkg/controller/chi/creator.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,19 @@ import (
"fmt"

"gopkg.in/d4l3k/messagediff.v1"
appsV1 "k8s.io/api/apps/v1"
coreV1 "k8s.io/api/core/v1"
apps "k8s.io/api/apps/v1"
core "k8s.io/api/core/v1"
apiErrors "k8s.io/apimachinery/pkg/api/errors"

log "github.com/altinity/clickhouse-operator/pkg/announcer"
chiV1 "github.com/altinity/clickhouse-operator/pkg/apis/clickhouse.altinity.com/v1"
api "github.com/altinity/clickhouse-operator/pkg/apis/clickhouse.altinity.com/v1"
"github.com/altinity/clickhouse-operator/pkg/chop"
"github.com/altinity/clickhouse-operator/pkg/controller"
"github.com/altinity/clickhouse-operator/pkg/util"
)

// createStatefulSet is an internal function, used in reconcileStatefulSet only
func (c *Controller) createStatefulSet(ctx context.Context, host *chiV1.ChiHost) ErrorCRUD {
func (c *Controller) createStatefulSet(ctx context.Context, host *api.ChiHost) ErrorCRUD {
log.V(1).M(host).F().P()

if util.IsContextDone(ctx) {
Expand Down Expand Up @@ -60,9 +60,9 @@ func (c *Controller) createStatefulSet(ctx context.Context, host *chiV1.ChiHost)
// updateStatefulSet is an internal function, used in reconcileStatefulSet only
func (c *Controller) updateStatefulSet(
ctx context.Context,
oldStatefulSet *appsV1.StatefulSet,
newStatefulSet *appsV1.StatefulSet,
host *chiV1.ChiHost,
oldStatefulSet *apps.StatefulSet,
newStatefulSet *apps.StatefulSet,
host *api.ChiHost,
) ErrorCRUD {
log.V(2).M(host).F().P()

Expand Down Expand Up @@ -126,7 +126,7 @@ func (c *Controller) updateStatefulSet(

// Comment out PV
// updatePersistentVolume
//func (c *Controller) updatePersistentVolume(ctx context.Context, pv *coreV1.PersistentVolume) (*coreV1.PersistentVolume, error) {
//func (c *Controller) updatePersistentVolume(ctx context.Context, pv *core.PersistentVolume) (*core.PersistentVolume, error) {
// log.V(2).M(pv).F().P()
// if util.IsContextDone(ctx) {
// log.V(2).Info("task is done")
Expand All @@ -145,7 +145,7 @@ func (c *Controller) updateStatefulSet(
//}

// updatePersistentVolumeClaim
func (c *Controller) updatePersistentVolumeClaim(ctx context.Context, pvc *coreV1.PersistentVolumeClaim) (*coreV1.PersistentVolumeClaim, error) {
func (c *Controller) updatePersistentVolumeClaim(ctx context.Context, pvc *core.PersistentVolumeClaim) (*core.PersistentVolumeClaim, error) {
log.V(2).M(pvc).F().P()
if util.IsContextDone(ctx) {
log.V(2).Info("task is done")
Expand Down Expand Up @@ -182,26 +182,26 @@ func (c *Controller) updatePersistentVolumeClaim(ctx context.Context, pvc *coreV

// onStatefulSetCreateFailed handles situation when StatefulSet create failed
// It can just delete failed StatefulSet or do nothing
func (c *Controller) onStatefulSetCreateFailed(ctx context.Context, host *chiV1.ChiHost) ErrorCRUD {
func (c *Controller) onStatefulSetCreateFailed(ctx context.Context, host *api.ChiHost) ErrorCRUD {
if util.IsContextDone(ctx) {
log.V(2).Info("task is done")
return errCRUDIgnore
}

// What to do with StatefulSet - look into chop configuration settings
switch chop.Config().Reconcile.StatefulSet.Create.OnFailure {
case chiV1.OnStatefulSetCreateFailureActionAbort:
case api.OnStatefulSetCreateFailureActionAbort:
// Report appropriate error, it will break reconcile loop
log.V(1).M(host).F().Info("abort")
return errCRUDAbort

case chiV1.OnStatefulSetCreateFailureActionDelete:
case api.OnStatefulSetCreateFailureActionDelete:
// Delete gracefully failed StatefulSet
log.V(1).M(host).F().Info("going to DELETE FAILED StatefulSet %s", util.NamespaceNameString(host.DesiredStatefulSet.ObjectMeta))
_ = c.deleteHost(ctx, host)
return c.shouldContinueOnCreateFailed()

case chiV1.OnStatefulSetCreateFailureActionIgnore:
case api.OnStatefulSetCreateFailureActionIgnore:
// Ignore error, continue reconcile loop
log.V(1).M(host).F().Info("going to ignore error %s", util.NamespaceNameString(host.DesiredStatefulSet.ObjectMeta))
return errCRUDIgnore
Expand All @@ -216,7 +216,7 @@ func (c *Controller) onStatefulSetCreateFailed(ctx context.Context, host *chiV1.

// onStatefulSetUpdateFailed handles situation when StatefulSet update failed
// It can try to revert StatefulSet to its previous version, specified in rollbackStatefulSet
func (c *Controller) onStatefulSetUpdateFailed(ctx context.Context, rollbackStatefulSet *appsV1.StatefulSet, host *chiV1.ChiHost) ErrorCRUD {
func (c *Controller) onStatefulSetUpdateFailed(ctx context.Context, rollbackStatefulSet *apps.StatefulSet, host *api.ChiHost) ErrorCRUD {
if util.IsContextDone(ctx) {
log.V(2).Info("task is done")
return errCRUDIgnore
Expand All @@ -227,12 +227,12 @@ func (c *Controller) onStatefulSetUpdateFailed(ctx context.Context, rollbackStat

// What to do with StatefulSet - look into chop configuration settings
switch chop.Config().Reconcile.StatefulSet.Update.OnFailure {
case chiV1.OnStatefulSetUpdateFailureActionAbort:
case api.OnStatefulSetUpdateFailureActionAbort:
// Report appropriate error, it will break reconcile loop
log.V(1).M(host).F().Info("abort StatefulSet %s", util.NamespaceNameString(rollbackStatefulSet.ObjectMeta))
return errCRUDAbort

case chiV1.OnStatefulSetUpdateFailureActionRollback:
case api.OnStatefulSetUpdateFailureActionRollback:
// Need to revert current StatefulSet to oldStatefulSet
log.V(1).M(host).F().Info("going to ROLLBACK FAILED StatefulSet %s", util.NamespaceNameString(rollbackStatefulSet.ObjectMeta))
statefulSet, err := c.getStatefulSet(host)
Expand All @@ -252,7 +252,7 @@ func (c *Controller) onStatefulSetUpdateFailed(ctx context.Context, rollbackStat

return c.shouldContinueOnUpdateFailed()

case chiV1.OnStatefulSetUpdateFailureActionIgnore:
case api.OnStatefulSetUpdateFailureActionIgnore:
// Ignore error, continue reconcile loop
log.V(1).M(host).F().Info("going to ignore error %s", util.NamespaceNameString(rollbackStatefulSet.ObjectMeta))
return errCRUDIgnore
Expand Down Expand Up @@ -293,7 +293,7 @@ func (c *Controller) shouldContinueOnUpdateFailed() ErrorCRUD {
return errCRUDAbort
}

func (c *Controller) createSecret(ctx context.Context, secret *coreV1.Secret) error {
func (c *Controller) createSecret(ctx context.Context, secret *core.Secret) error {
log.V(1).M(secret).F().P()

if util.IsContextDone(ctx) {
Expand Down
32 changes: 16 additions & 16 deletions pkg/controller/chi/deleter.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,19 @@ import (
"context"
"time"

appsV1 "k8s.io/api/apps/v1"
coreV1 "k8s.io/api/core/v1"
apps "k8s.io/api/apps/v1"
core "k8s.io/api/core/v1"
apiErrors "k8s.io/apimachinery/pkg/api/errors"

log "github.com/altinity/clickhouse-operator/pkg/announcer"
chiV1 "github.com/altinity/clickhouse-operator/pkg/apis/clickhouse.altinity.com/v1"
api "github.com/altinity/clickhouse-operator/pkg/apis/clickhouse.altinity.com/v1"
"github.com/altinity/clickhouse-operator/pkg/controller"
model "github.com/altinity/clickhouse-operator/pkg/model/chi"
"github.com/altinity/clickhouse-operator/pkg/util"
)

// deleteHost deletes all kubernetes resources related to replica *chop.ChiHost
func (c *Controller) deleteHost(ctx context.Context, host *chiV1.ChiHost) error {
func (c *Controller) deleteHost(ctx context.Context, host *api.ChiHost) error {
log.V(1).M(host).S().Info(host.Address.ClusterNameString())

// Each host consists of:
Expand All @@ -45,7 +45,7 @@ func (c *Controller) deleteHost(ctx context.Context, host *chiV1.ChiHost) error
}

// deleteConfigMapsCHI
func (c *Controller) deleteConfigMapsCHI(ctx context.Context, chi *chiV1.ClickHouseInstallation) error {
func (c *Controller) deleteConfigMapsCHI(ctx context.Context, chi *api.ClickHouseInstallation) error {
if util.IsContextDone(ctx) {
log.V(2).Info("task is done")
return nil
Expand Down Expand Up @@ -88,7 +88,7 @@ func (c *Controller) deleteConfigMapsCHI(ctx context.Context, chi *chiV1.ClickHo
}

// statefulSetDeletePod delete a pod of a StatefulSet. This requests StatefulSet to relaunch deleted pod
func (c *Controller) statefulSetDeletePod(ctx context.Context, statefulSet *appsV1.StatefulSet, host *chiV1.ChiHost) error {
func (c *Controller) statefulSetDeletePod(ctx context.Context, statefulSet *apps.StatefulSet, host *api.ChiHost) error {
if util.IsContextDone(ctx) {
log.V(2).Info("task is done")
return nil
Expand All @@ -110,7 +110,7 @@ func (c *Controller) statefulSetDeletePod(ctx context.Context, statefulSet *apps
}

// deleteStatefulSet gracefully deletes StatefulSet through zeroing Pod's count
func (c *Controller) deleteStatefulSet(ctx context.Context, host *chiV1.ChiHost) error {
func (c *Controller) deleteStatefulSet(ctx context.Context, host *api.ChiHost) error {
if util.IsContextDone(ctx) {
log.V(2).Info("task is done")
return nil
Expand Down Expand Up @@ -164,7 +164,7 @@ func (c *Controller) deleteStatefulSet(ctx context.Context, host *chiV1.ChiHost)
}

// syncStatefulSet
func (c *Controller) syncStatefulSet(ctx context.Context, host *chiV1.ChiHost) {
func (c *Controller) syncStatefulSet(ctx context.Context, host *api.ChiHost) {
for {
if util.IsContextDone(ctx) {
log.V(2).Info("task is done")
Expand All @@ -183,7 +183,7 @@ func (c *Controller) syncStatefulSet(ctx context.Context, host *chiV1.ChiHost) {
}

// deletePVC deletes PersistentVolumeClaim
func (c *Controller) deletePVC(ctx context.Context, host *chiV1.ChiHost) error {
func (c *Controller) deletePVC(ctx context.Context, host *api.ChiHost) error {
if util.IsContextDone(ctx) {
log.V(2).Info("task is done")
return nil
Expand All @@ -193,7 +193,7 @@ func (c *Controller) deletePVC(ctx context.Context, host *chiV1.ChiHost) error {
defer log.V(2).M(host).E().P()

namespace := host.Address.Namespace
c.walkDiscoveredPVCs(host, func(pvc *coreV1.PersistentVolumeClaim) {
c.walkDiscoveredPVCs(host, func(pvc *core.PersistentVolumeClaim) {
if util.IsContextDone(ctx) {
log.V(2).Info("task is done")
return
Expand Down Expand Up @@ -222,7 +222,7 @@ func (c *Controller) deletePVC(ctx context.Context, host *chiV1.ChiHost) error {
}

// deleteConfigMap deletes ConfigMap
func (c *Controller) deleteConfigMap(ctx context.Context, host *chiV1.ChiHost) error {
func (c *Controller) deleteConfigMap(ctx context.Context, host *api.ChiHost) error {
if util.IsContextDone(ctx) {
log.V(2).Info("task is done")
return nil
Expand Down Expand Up @@ -256,7 +256,7 @@ func (c *Controller) deleteConfigMap(ctx context.Context, host *chiV1.ChiHost) e
}

// deleteServiceHost deletes Service
func (c *Controller) deleteServiceHost(ctx context.Context, host *chiV1.ChiHost) error {
func (c *Controller) deleteServiceHost(ctx context.Context, host *api.ChiHost) error {
if util.IsContextDone(ctx) {
log.V(2).Info("task is done")
return nil
Expand All @@ -269,7 +269,7 @@ func (c *Controller) deleteServiceHost(ctx context.Context, host *chiV1.ChiHost)
}

// deleteServiceShard
func (c *Controller) deleteServiceShard(ctx context.Context, shard *chiV1.ChiShard) error {
func (c *Controller) deleteServiceShard(ctx context.Context, shard *api.ChiShard) error {
if util.IsContextDone(ctx) {
log.V(2).Info("task is done")
return nil
Expand All @@ -282,7 +282,7 @@ func (c *Controller) deleteServiceShard(ctx context.Context, shard *chiV1.ChiSha
}

// deleteServiceCluster
func (c *Controller) deleteServiceCluster(ctx context.Context, cluster *chiV1.Cluster) error {
func (c *Controller) deleteServiceCluster(ctx context.Context, cluster *api.Cluster) error {
if util.IsContextDone(ctx) {
log.V(2).Info("task is done")
return nil
Expand All @@ -295,7 +295,7 @@ func (c *Controller) deleteServiceCluster(ctx context.Context, cluster *chiV1.Cl
}

// deleteServiceCHI
func (c *Controller) deleteServiceCHI(ctx context.Context, chi *chiV1.ClickHouseInstallation) error {
func (c *Controller) deleteServiceCHI(ctx context.Context, chi *api.ClickHouseInstallation) error {
if util.IsContextDone(ctx) {
log.V(2).Info("task is done")
return nil
Expand Down Expand Up @@ -334,7 +334,7 @@ func (c *Controller) deleteServiceIfExists(ctx context.Context, namespace, name
}

// deleteSecretCluster
func (c *Controller) deleteSecretCluster(ctx context.Context, cluster *chiV1.Cluster) error {
func (c *Controller) deleteSecretCluster(ctx context.Context, cluster *api.Cluster) error {
if util.IsContextDone(ctx) {
log.V(2).Info("task is done")
return nil
Expand Down
20 changes: 10 additions & 10 deletions pkg/controller/chi/discoverer.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,16 @@ package chi
import (
"context"

metaV1 "k8s.io/apimachinery/pkg/apis/meta/v1"
meta "k8s.io/apimachinery/pkg/apis/meta/v1"

log "github.com/altinity/clickhouse-operator/pkg/announcer"
chiV1 "github.com/altinity/clickhouse-operator/pkg/apis/clickhouse.altinity.com/v1"
api "github.com/altinity/clickhouse-operator/pkg/apis/clickhouse.altinity.com/v1"
"github.com/altinity/clickhouse-operator/pkg/controller"
model "github.com/altinity/clickhouse-operator/pkg/model/chi"
"github.com/altinity/clickhouse-operator/pkg/util"
)

func (c *Controller) discovery(ctx context.Context, chi *chiV1.ClickHouseInstallation) *model.Registry {
func (c *Controller) discovery(ctx context.Context, chi *api.ClickHouseInstallation) *model.Registry {
if util.IsContextDone(ctx) {
log.V(2).Info("task is done")
return nil
Expand All @@ -45,7 +45,7 @@ func (c *Controller) discovery(ctx context.Context, chi *chiV1.ClickHouseInstall
return r
}

func (c *Controller) discoveryStatefulSets(ctx context.Context, r *model.Registry, chi *chiV1.ClickHouseInstallation, opts metaV1.ListOptions) {
func (c *Controller) discoveryStatefulSets(ctx context.Context, r *model.Registry, chi *api.ClickHouseInstallation, opts meta.ListOptions) {
list, err := c.kubeClient.AppsV1().StatefulSets(chi.Namespace).List(ctx, opts)
if err != nil {
log.M(chi).F().Error("FAIL list StatefulSet err: %v", err)
Expand All @@ -60,7 +60,7 @@ func (c *Controller) discoveryStatefulSets(ctx context.Context, r *model.Registr
}
}

func (c *Controller) discoveryConfigMaps(ctx context.Context, r *model.Registry, chi *chiV1.ClickHouseInstallation, opts metaV1.ListOptions) {
func (c *Controller) discoveryConfigMaps(ctx context.Context, r *model.Registry, chi *api.ClickHouseInstallation, opts meta.ListOptions) {
list, err := c.kubeClient.CoreV1().ConfigMaps(chi.Namespace).List(ctx, opts)
if err != nil {
log.M(chi).F().Error("FAIL list ConfigMap err: %v", err)
Expand All @@ -75,7 +75,7 @@ func (c *Controller) discoveryConfigMaps(ctx context.Context, r *model.Registry,
}
}

func (c *Controller) discoveryServices(ctx context.Context, r *model.Registry, chi *chiV1.ClickHouseInstallation, opts metaV1.ListOptions) {
func (c *Controller) discoveryServices(ctx context.Context, r *model.Registry, chi *api.ClickHouseInstallation, opts meta.ListOptions) {
list, err := c.kubeClient.CoreV1().Services(chi.Namespace).List(ctx, opts)
if err != nil {
log.M(chi).F().Error("FAIL list Service err: %v", err)
Expand All @@ -90,7 +90,7 @@ func (c *Controller) discoveryServices(ctx context.Context, r *model.Registry, c
}
}

func (c *Controller) discoverySecrets(ctx context.Context, r *model.Registry, chi *chiV1.ClickHouseInstallation, opts metaV1.ListOptions) {
func (c *Controller) discoverySecrets(ctx context.Context, r *model.Registry, chi *api.ClickHouseInstallation, opts meta.ListOptions) {
list, err := c.kubeClient.CoreV1().Secrets(chi.Namespace).List(ctx, opts)
if err != nil {
log.M(chi).F().Error("FAIL list Secret err: %v", err)
Expand All @@ -105,7 +105,7 @@ func (c *Controller) discoverySecrets(ctx context.Context, r *model.Registry, ch
}
}

func (c *Controller) discoveryPVCs(ctx context.Context, r *model.Registry, chi *chiV1.ClickHouseInstallation, opts metaV1.ListOptions) {
func (c *Controller) discoveryPVCs(ctx context.Context, r *model.Registry, chi *api.ClickHouseInstallation, opts meta.ListOptions) {
list, err := c.kubeClient.CoreV1().PersistentVolumeClaims(chi.Namespace).List(ctx, opts)
if err != nil {
log.M(chi).F().Error("FAIL list PVC err: %v", err)
Expand All @@ -121,7 +121,7 @@ func (c *Controller) discoveryPVCs(ctx context.Context, r *model.Registry, chi *
}

// Comment out PV
//func (c *Controller) discoveryPVs(ctx context.Context, r *chopModel.Registry, chi *chiV1.ClickHouseInstallation, opts metaV1.ListOptions) {
//func (c *Controller) discoveryPVs(ctx context.Context, r *chopModel.Registry, chi *api.ClickHouseInstallation, opts meta.ListOptions) {
// list, err := c.kubeClient.CoreV1().PersistentVolumes().List(ctx, opts)
// if err != nil {
// log.M(chi).F().Error("FAIL list PV err: %v", err)
Expand All @@ -136,7 +136,7 @@ func (c *Controller) discoveryPVCs(ctx context.Context, r *model.Registry, chi *
// }
//}

func (c *Controller) discoveryPDBs(ctx context.Context, r *model.Registry, chi *chiV1.ClickHouseInstallation, opts metaV1.ListOptions) {
func (c *Controller) discoveryPDBs(ctx context.Context, r *model.Registry, chi *api.ClickHouseInstallation, opts meta.ListOptions) {
list, err := c.kubeClient.PolicyV1().PodDisruptionBudgets(chi.Namespace).List(ctx, opts)
if err != nil {
log.M(chi).F().Error("FAIL list PDB err: %v", err)
Expand Down
Loading

0 comments on commit a0d1452

Please sign in to comment.