Skip to content

Commit

Permalink
fix: ensure that Gateway managed DataPlanes are reduced
Browse files Browse the repository at this point in the history
  • Loading branch information
pmalek committed Mar 20, 2024
1 parent a66e5ab commit 39798c2
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 1 deletion.
8 changes: 7 additions & 1 deletion controllers/gateway/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import (
gatewayutils "github.com/kong/gateway-operator/pkg/utils/gateway"
k8sutils "github.com/kong/gateway-operator/pkg/utils/kubernetes"
"github.com/kong/gateway-operator/pkg/utils/kubernetes/compare"
k8sreduce "github.com/kong/gateway-operator/pkg/utils/kubernetes/reduce"
k8sresources "github.com/kong/gateway-operator/pkg/utils/kubernetes/resources"
"github.com/kong/gateway-operator/pkg/vars"
)
Expand Down Expand Up @@ -382,11 +383,16 @@ func (r *Reconciler) provisionDataPlane(

count := len(dataplanes)
if count > 1 {
err = fmt.Errorf("data plane deployments found: %d, expected: 1", count)
err = fmt.Errorf("data planes found: %d, expected: 1", count)
k8sutils.SetCondition(
createDataPlaneCondition(metav1.ConditionFalse, k8sutils.UnableToProvisionReason, err.Error(), gateway.Generation),
gatewayConditionsAndListenersAware(gateway),
)
log.Debug(logger, "reducing dataplanes", gateway, "count", count)
rErr := k8sreduce.ReduceDataPlanes(ctx, r.Client, dataplanes)
if rErr != nil {
return nil, fmt.Errorf("failed reducing data planes: %w", rErr)
}
return nil, err
}
if count == 0 {
Expand Down
22 changes: 22 additions & 0 deletions pkg/utils/kubernetes/reduce/filters.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
networkingv1 "k8s.io/api/networking/v1"
rbacv1 "k8s.io/api/rbac/v1"

operatorv1beta1 "github.com/kong/gateway-operator/apis/v1beta1"
"github.com/kong/gateway-operator/pkg/consts"
)

Expand Down Expand Up @@ -330,3 +331,24 @@ func filterValidatingWebhookConfigurations(webhookConfigurations []admregv1.Vali

return append(webhookConfigurations[:best], webhookConfigurations[best+1:]...)
}

// -----------------------------------------------------------------------------
// Filter functions - DataPlanes
// -----------------------------------------------------------------------------

// filterDataPlanes filters out the DataPlanes to be kept and returns all the DataPlanes
// to be deleted. The oldest DataPlane is kept.
func filterDataPlanes(dataplanes []operatorv1beta1.DataPlane) []operatorv1beta1.DataPlane {
if len(dataplanes) < 2 {
return []operatorv1beta1.DataPlane{}
}

best := 0
for i, dataplane := range dataplanes {
if dataplane.CreationTimestamp.Before(&dataplanes[best].CreationTimestamp) {
best = i
}
}

return append(dataplanes[:best], dataplanes[best+1:]...)
}
16 changes: 16 additions & 0 deletions pkg/utils/kubernetes/reduce/reduce.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import (
networkingv1 "k8s.io/api/networking/v1"
rbacv1 "k8s.io/api/rbac/v1"
"sigs.k8s.io/controller-runtime/pkg/client"

operatorv1beta1 "github.com/kong/gateway-operator/apis/v1beta1"
)

// PreDeleteHook is a function that can be executed before deleting an object.
Expand Down Expand Up @@ -176,3 +178,17 @@ func ReduceValidatingWebhookConfigurations(ctx context.Context, k8sClient client
}
return nil
}

// +kubebuilder:rbac:groups=gateway-operator.konghq.com,resources=dataplanes,verbs=get;list;watch;update;patch

// ReduceDataPlanes detects the best DataPlane in the set and deletes all the others.
func ReduceDataPlanes(ctx context.Context, k8sClient client.Client, dataplanes []operatorv1beta1.DataPlane) error {
filteredDataPlanes := filterDataPlanes(dataplanes)
for _, dataplane := range filteredDataPlanes {
dataplane := dataplane
if err := k8sClient.Delete(ctx, &dataplane); err != nil {
return err
}
}
return nil
}

0 comments on commit 39798c2

Please sign in to comment.