Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: ensure that Gateway managed DataPlanes are reduced #43

Merged
merged 1 commit into from
Mar 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,14 @@
- [v0.1.1](#v011)
- [v0.1.0](#v010)

## Unreleased

### Fixes

- Fixes an issue where managed `Gateway`s controller wasn't able to reduce
the created `DataPlane` objects when too many have been created.
[#43](https://github.com/Kong/gateway-operator/pull/43)

## [v1.2.1]

> Release date: 2024-03-19
Expand Down
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=delete
pmalek marked this conversation as resolved.
Show resolved Hide resolved

// 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
}
Loading