Skip to content

Commit

Permalink
Apply basereconciler api changes
Browse files Browse the repository at this point in the history
  • Loading branch information
roivaz committed Dec 12, 2023
1 parent aee5061 commit 18b8930
Show file tree
Hide file tree
Showing 29 changed files with 218 additions and 302 deletions.
15 changes: 6 additions & 9 deletions controllers/apicast_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ import (
autoscalingv2 "k8s.io/api/autoscaling/v2"
corev1 "k8s.io/api/core/v1"
policyv1 "k8s.io/api/policy/v1"
"k8s.io/apimachinery/pkg/types"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/log"
)
Expand Down Expand Up @@ -59,10 +58,9 @@ func (r *ApicastReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ct
ctx = log.IntoContext(ctx, logger)

instance := &saasv1alpha1.Apicast{}
key := types.NamespacedName{Name: req.Name, Namespace: req.Namespace}
result, err := r.GetInstance(ctx, key, instance, nil, nil)
if result != nil || err != nil {
return *result, err
result := r.GetInstance(ctx, req, instance, nil, nil)
if result.IsReturnAndRequeue() {
return result.Values()
}

// Apply defaults for reconcile but do not store them in the API
Expand All @@ -87,10 +85,9 @@ func (r *ApicastReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ct
}
resources = append(resources, production...)

err = r.ReconcileOwnedResources(ctx, instance, resources)
if err != nil {
logger.Error(err, "unable to update owned resources")
return ctrl.Result{}, err
result = r.ReconcileOwnedResources(ctx, instance, resources)
if result.IsReturnAndRequeue() {
return result.Values()
}

return ctrl.Result{}, nil
Expand Down
15 changes: 6 additions & 9 deletions controllers/autossl_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ import (
autoscalingv2 "k8s.io/api/autoscaling/v2"
corev1 "k8s.io/api/core/v1"
policyv1 "k8s.io/api/policy/v1"
"k8s.io/apimachinery/pkg/types"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/log"
)
Expand Down Expand Up @@ -58,10 +57,9 @@ func (r *AutoSSLReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ct
ctx = log.IntoContext(ctx, logger)

instance := &saasv1alpha1.AutoSSL{}
key := types.NamespacedName{Name: req.Name, Namespace: req.Namespace}
result, err := r.GetInstance(ctx, key, instance, nil, nil)
if result != nil || err != nil {
return *result, err
result := r.GetInstance(ctx, req, instance, nil, nil)
if result.IsReturnAndRequeue() {
return result.Values()
}

// Apply defaults for reconcile but do not store them in the API
Expand All @@ -88,10 +86,9 @@ func (r *AutoSSLReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ct
}
resources = append(resources, workload...)

err = r.ReconcileOwnedResources(ctx, instance, resources)
if err != nil {
logger.Error(err, "unable to update owned resources")
return ctrl.Result{}, err
result = r.ReconcileOwnedResources(ctx, instance, resources)
if result.IsReturnAndRequeue() {
return result.Values()
}

return ctrl.Result{}, nil
Expand Down
15 changes: 6 additions & 9 deletions controllers/backend_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ import (
corev1 "k8s.io/api/core/v1"
policyv1 "k8s.io/api/policy/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/source"
Expand Down Expand Up @@ -64,10 +63,9 @@ func (r *BackendReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ct
ctx = log.IntoContext(ctx, logger)

instance := &saasv1alpha1.Backend{}
key := types.NamespacedName{Name: req.Name, Namespace: req.Namespace}
result, err := r.GetInstance(ctx, key, instance, nil, nil)
if result != nil || err != nil {
return *result, err
result := r.GetInstance(ctx, req, instance, nil, nil)
if result.IsReturnAndRequeue() {
return result.Values()
}

// Apply defaults for reconcile but do not store them in the API
Expand Down Expand Up @@ -103,10 +101,9 @@ func (r *BackendReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ct
resources = append(resources, cron_resources...)

// Reconcile all resources
err = r.ReconcileOwnedResources(ctx, instance, resources)
if err != nil {
logger.Error(err, "unable to reconcile owned resources")
return ctrl.Result{}, err
result = r.ReconcileOwnedResources(ctx, instance, resources)
if result.IsReturnAndRequeue() {
return result.Values()
}

return ctrl.Result{}, nil
Expand Down
15 changes: 6 additions & 9 deletions controllers/corsproxy_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ import (
corev1 "k8s.io/api/core/v1"
policyv1 "k8s.io/api/policy/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/source"
Expand Down Expand Up @@ -63,10 +62,9 @@ func (r *CORSProxyReconciler) Reconcile(ctx context.Context, req ctrl.Request) (
ctx = log.IntoContext(ctx, logger)

instance := &saasv1alpha1.CORSProxy{}
key := types.NamespacedName{Name: req.Name, Namespace: req.Namespace}
result, err := r.GetInstance(ctx, key, instance, nil, nil)
if result != nil || err != nil {
return *result, err
result := r.GetInstance(ctx, req, instance, nil, nil)
if result.IsReturnAndRequeue() {
return result.Values()
}

// Apply defaults for reconcile but do not store them in the API
Expand All @@ -89,10 +87,9 @@ func (r *CORSProxyReconciler) Reconcile(ctx context.Context, req ctrl.Request) (
}
resources = append(resources, workload...)

err = r.ReconcileOwnedResources(ctx, instance, resources)
if err != nil {
logger.Error(err, "unable to reconcile owned resources")
return ctrl.Result{}, err
result = r.ReconcileOwnedResources(ctx, instance, resources)
if result.IsReturnAndRequeue() {
return result.Values()
}

return ctrl.Result{}, nil
Expand Down
15 changes: 6 additions & 9 deletions controllers/echoapi_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ import (
autoscalingv2 "k8s.io/api/autoscaling/v2"
corev1 "k8s.io/api/core/v1"
policyv1 "k8s.io/api/policy/v1"
"k8s.io/apimachinery/pkg/types"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/log"
)
Expand Down Expand Up @@ -58,10 +57,9 @@ func (r *EchoAPIReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ct
ctx = log.IntoContext(ctx, logger)

instance := &saasv1alpha1.EchoAPI{}
key := types.NamespacedName{Name: req.Name, Namespace: req.Namespace}
result, err := r.GetInstance(ctx, key, instance, nil, nil)
if result != nil || err != nil {
return *result, err
result := r.GetInstance(ctx, req, instance, nil, nil)
if result.IsReturnAndRequeue() {
return result.Values()
}

// Apply defaults for reconcile but do not store them in the API
Expand All @@ -78,10 +76,9 @@ func (r *EchoAPIReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ct
return ctrl.Result{}, err
}

err = r.ReconcileOwnedResources(ctx, instance, resources)
if err != nil {
logger.Error(err, "unable to update owned resources")
return ctrl.Result{}, err
result = r.ReconcileOwnedResources(ctx, instance, resources)
if result.IsReturnAndRequeue() {
return result.Values()
}

return ctrl.Result{}, nil
Expand Down
15 changes: 6 additions & 9 deletions controllers/mappingservice_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ import (
corev1 "k8s.io/api/core/v1"
policyv1 "k8s.io/api/policy/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/source"
Expand Down Expand Up @@ -63,10 +62,9 @@ func (r *MappingServiceReconciler) Reconcile(ctx context.Context, req ctrl.Reque
ctx = log.IntoContext(ctx, logger)

instance := &saasv1alpha1.MappingService{}
key := types.NamespacedName{Name: req.Name, Namespace: req.Namespace}
result, err := r.GetInstance(ctx, key, instance, nil, nil)
if result != nil || err != nil {
return *result, err
result := r.GetInstance(ctx, req, instance, nil, nil)
if result.IsReturnAndRequeue() {
return result.Values()
}

// Apply defaults for reconcile but do not store them in the API
Expand All @@ -89,10 +87,9 @@ func (r *MappingServiceReconciler) Reconcile(ctx context.Context, req ctrl.Reque
}
resources = append(resources, workload...)

err = r.ReconcileOwnedResources(ctx, instance, resources)
if err != nil {
logger.Error(err, "unable to reconcile owned resources")
return ctrl.Result{}, err
result = r.ReconcileOwnedResources(ctx, instance, resources)
if result.IsReturnAndRequeue() {
return result.Values()
}

return ctrl.Result{}, nil
Expand Down
40 changes: 20 additions & 20 deletions controllers/redisshard_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (
"fmt"
"time"

basereconciler "github.com/3scale-ops/basereconciler/reconciler"
"github.com/3scale-ops/basereconciler/reconciler"
saasv1alpha1 "github.com/3scale/saas-operator/api/v1alpha1"
"github.com/3scale/saas-operator/pkg/generators/redisshard"
"github.com/3scale/saas-operator/pkg/redis/client"
Expand All @@ -38,7 +38,7 @@ import (

// RedisShardReconciler reconciles a RedisShard object
type RedisShardReconciler struct {
basereconciler.Reconciler
*reconciler.Reconciler
Log logr.Logger
Pool *redis.ServerPool
}
Expand All @@ -57,10 +57,9 @@ func (r *RedisShardReconciler) Reconcile(ctx context.Context, req ctrl.Request)
ctx = log.IntoContext(ctx, logger)

instance := &saasv1alpha1.RedisShard{}
key := types.NamespacedName{Name: req.Name, Namespace: req.Namespace}
result, err := r.GetInstance(ctx, key, instance, nil, nil)
if result != nil || err != nil {
return *result, err
result := r.GetInstance(ctx, req, instance, nil, nil)
if result.IsReturnAndRequeue() {
return result.Values()
}

// Apply defaults for reconcile but do not store them in the API
Expand All @@ -72,19 +71,18 @@ func (r *RedisShardReconciler) Reconcile(ctx context.Context, req ctrl.Request)
instance.Spec,
)

if err := r.ReconcileOwnedResources(ctx, instance, gen.Resources()); err != nil {
logger.Error(err, "unable to update owned resources")
return ctrl.Result{}, err
result = r.ReconcileOwnedResources(ctx, instance, gen.Resources())
if result.IsReturnAndRequeue() {
return result.Values()
}

shard, result, err := r.setRedisRoles(ctx, types.NamespacedName{Name: req.Name, Namespace: req.Namespace},
shard, result := r.setRedisRoles(ctx, types.NamespacedName{Name: req.Name, Namespace: req.Namespace},
*instance.Spec.MasterIndex, *instance.Spec.SlaveCount+1, gen.ServiceName(), logger)

if result != nil || err != nil {
return *result, err
if result.IsReturnAndRequeue() {
return result.Values()
}

if err = r.updateStatus(ctx, shard, instance, logger); err != nil {
if err := r.updateStatus(ctx, shard, instance, logger); err != nil {
return ctrl.Result{}, err
}

Expand All @@ -101,7 +99,8 @@ func (r *RedisShardReconciler) SetupWithManager(mgr ctrl.Manager) error {
Complete(r)
}

func (r *RedisShardReconciler) setRedisRoles(ctx context.Context, key types.NamespacedName, masterIndex, replicas int32, serviceName string, log logr.Logger) (*sharded.Shard, *ctrl.Result, error) {
func (r *RedisShardReconciler) setRedisRoles(ctx context.Context, key types.NamespacedName,
masterIndex, replicas int32, serviceName string, log logr.Logger) (*sharded.Shard, reconciler.Result) {

var masterHostPort string
redisURLs := make(map[string]string, replicas)
Expand All @@ -110,11 +109,12 @@ func (r *RedisShardReconciler) setRedisRoles(ctx context.Context, key types.Name
key := types.NamespacedName{Name: fmt.Sprintf("%s-%d", serviceName, i), Namespace: key.Namespace}
err := r.Client.Get(ctx, key, pod)
if err != nil {
return &sharded.Shard{Name: key.Name}, &ctrl.Result{}, err
return &sharded.Shard{Name: key.Name}, reconciler.Result{Error: err}
}
if pod.Status.PodIP == "" {
log.Info("waiting for pod IP to be allocated")
return &sharded.Shard{Name: key.Name}, &ctrl.Result{RequeueAfter: 5 * time.Second}, nil
// return &sharded.Shard{Name: key.Name}, &ctrl.Result{RequeueAfter: 5 * time.Second}, nil
return &sharded.Shard{Name: key.Name}, reconciler.Result{Requeue: true, RequeueAfter: 5 * time.Second}
}

redisURLs[fmt.Sprintf("%s-%d", serviceName, i)] = fmt.Sprintf("redis://%s:%d", pod.Status.PodIP, 6379)
Expand All @@ -125,16 +125,16 @@ func (r *RedisShardReconciler) setRedisRoles(ctx context.Context, key types.Name

shard, err := sharded.NewShardFromTopology(key.Name, redisURLs, r.Pool)
if err != nil {
return shard, &ctrl.Result{}, err
return shard, reconciler.Result{Error: err}
}

_, err = shard.Init(ctx, masterHostPort)
if err != nil {
log.Info("waiting for redis shard init")
return shard, &ctrl.Result{Requeue: true, RequeueAfter: 10 * time.Second}, nil
return shard, reconciler.Result{Requeue: true, RequeueAfter: 10 * time.Second}
}

return shard, nil, nil
return shard, reconciler.Result{}
}

func (r *RedisShardReconciler) updateStatus(ctx context.Context, shard *sharded.Shard, instance *saasv1alpha1.RedisShard, log logr.Logger) error {
Expand Down
24 changes: 11 additions & 13 deletions controllers/sentinel_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (
"errors"
"time"

basereconciler "github.com/3scale-ops/basereconciler/reconciler"
"github.com/3scale-ops/basereconciler/reconciler"
saasv1alpha1 "github.com/3scale/saas-operator/api/v1alpha1"
"github.com/3scale/saas-operator/pkg/generators/sentinel"
"github.com/3scale/saas-operator/pkg/reconcilers/threads"
Expand All @@ -36,7 +36,6 @@ import (
corev1 "k8s.io/api/core/v1"
policyv1 "k8s.io/api/policy/v1"
"k8s.io/apimachinery/pkg/api/equality"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/util/workqueue"
"k8s.io/utils/pointer"
ctrl "sigs.k8s.io/controller-runtime"
Expand All @@ -49,7 +48,7 @@ import (

// SentinelReconciler reconciles a Sentinel object
type SentinelReconciler struct {
basereconciler.Reconciler
*reconciler.Reconciler
Log logr.Logger
SentinelEvents threads.Manager
Metrics threads.Manager
Expand All @@ -72,14 +71,13 @@ func (r *SentinelReconciler) Reconcile(ctx context.Context, req ctrl.Request) (c
ctx = log.IntoContext(ctx, logger)

instance := &saasv1alpha1.Sentinel{}
key := types.NamespacedName{Name: req.Name, Namespace: req.Namespace}
result, err := r.GetInstance(ctx,
key,
instance,
result := r.GetInstance(ctx, req, instance,
pointer.String(saasv1alpha1.Finalizer),
[]func(){r.SentinelEvents.CleanupThreads(instance), r.Metrics.CleanupThreads(instance)})
if result != nil || err != nil {
return *result, err
r.SentinelEvents.CleanupThreads(instance),
r.Metrics.CleanupThreads(instance),
)
if result.IsReturnAndRequeue() {
return result.Values()
}

// Apply defaults for reconcile but do not store them in the API
Expand All @@ -91,9 +89,9 @@ func (r *SentinelReconciler) Reconcile(ctx context.Context, req ctrl.Request) (c
instance.Spec,
)

if err := r.ReconcileOwnedResources(ctx, instance, gen.Resources()); err != nil {
logger.Error(err, "unable to update owned resources")
return ctrl.Result{}, err
result = r.ReconcileOwnedResources(ctx, instance, gen.Resources())
if result.IsReturnAndRequeue() {
return result.Values()
}

clustermap, err := gen.ClusterTopology(ctx)
Expand Down
Loading

0 comments on commit 18b8930

Please sign in to comment.