Skip to content

Commit

Permalink
Add a compatibility kludge for v0.17.1 clients
Browse files Browse the repository at this point in the history
For some reason I don't remember I didn't send the whole GTW object
inside our GWProxy. Instead I made a "ports" struct but that quickly
became insufficient.

Out Gateway client v0.18.0 and later send the full GTW inside the
GWProxy but for compatibility with v0.17.1 clients we need to fill in
a few items to pass the CRD validation rules.
  • Loading branch information
caboteria committed Jul 8, 2022
1 parent 309dae0 commit e1e19da
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 0 deletions.
15 changes: 15 additions & 0 deletions api/v1/gwproxy_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
logf "sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/webhook"
"sigs.k8s.io/gateway-api/apis/v1alpha2"
)

// log is for logging in this package.
Expand Down Expand Up @@ -119,6 +120,20 @@ func (r *GWProxy) Default() {
}
}

// Back-compatibility with v0.17.1. This is the minimum necessary to
// get the CRD validation to succeed. FIXME: We can remove this when
// the couple of users who currently have v0.17.1 upgrade.
if r.Spec.Gateway.GatewayClassName == "" {
r.Spec.Gateway.GatewayClassName = "compatibility"
}
if r.Spec.Gateway.Listeners == nil {
r.Spec.Gateway.Listeners = []v1alpha2.Listener{{
Name: "compatibility",
Port: v1alpha2.PortNumber(r.Spec.PublicPorts[0].Port),
Protocol: v1alpha2.HTTPProtocolType,
}}
}

gwLog.V(1).Info("defaulted", "proxyName", r.Name, "contents", r)
}

Expand Down
47 changes: 47 additions & 0 deletions controllers/gwproxy_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/gateway-api/apis/v1alpha2"

epicv1 "gitlab.com/acnodal/epic/resource-model/api/v1"
"gitlab.com/acnodal/epic/resource-model/internal/allocator"
Expand Down Expand Up @@ -56,6 +57,13 @@ func (r *GWProxyReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ct
l := log.FromContext(ctx)
l.V(1).Info("Reconciling")

// Back-compatibility with v0.17.1. This is the minimum necessary to
// get the CRD validation to succeed. FIXME: We can remove this when
// the couple of users who currently have v0.17.1 upgrade.
if err := kludgeProxy(ctx, r.Client, req.NamespacedName); err != nil {
return done, err
}

prefix := &epicv1.ServicePrefix{}
account := &epicv1.Account{}
sg := &epicv1.LBServiceGroup{}
Expand Down Expand Up @@ -414,3 +422,42 @@ func updateDeployment(ctx context.Context, cl client.Client, updated *marin3rope
return cl.Update(ctx, existing)
})
}

func kludgeProxy(ctx context.Context, cl client.Client, proxy types.NamespacedName) error {
key := client.ObjectKey{Namespace: proxy.Namespace, Name: proxy.Name}

return retry.RetryOnConflict(retry.DefaultRetry, func() error {
needsUpdate := false
existing := epicv1.GWProxy{}

// Fetch the resource here; you need to refetch it on every try,
// since if you got a conflict on the last update attempt then
// you need to get the current version before making your own
// changes.
if err := cl.Get(ctx, key, &existing); err != nil {
return err
}
// Back-compatibility with v0.17.1. This is the minimum necessary to
// get the CRD validation to succeed. FIXME: We can remove this when
// the couple of users who currently have v0.17.1 upgrade.
if existing.Spec.Gateway.GatewayClassName == "" {
existing.Spec.Gateway.GatewayClassName = "compatibility"
needsUpdate = true
}
if existing.Spec.Gateway.Listeners == nil {
existing.Spec.Gateway.Listeners = []v1alpha2.Listener{{
Name: "compatibility",
Port: v1alpha2.PortNumber(existing.Spec.PublicPorts[0].Port),
Protocol: v1alpha2.HTTPProtocolType,
}}
needsUpdate = true
}

if !needsUpdate {
return nil
}

// Try to update
return cl.Update(ctx, &existing)
})
}

0 comments on commit e1e19da

Please sign in to comment.