Skip to content

Commit

Permalink
chore(store) expand TypeMeta population
Browse files Browse the repository at this point in the history
Add a type-agnostic helper to populate TypeMeta using a scheme with
registered types.

Populate TypeMeta throughout all controllers and reference filter
predicates.

Remove feature gate limitations on types added to the manager scheme to
simplify its use where config is not readily available.
  • Loading branch information
rainest committed Sep 28, 2023
1 parent 7f5c264 commit f9e1d91
Show file tree
Hide file tree
Showing 18 changed files with 231 additions and 149 deletions.
11 changes: 5 additions & 6 deletions hack/generators/controllers/networking/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,6 @@ import (
discoveryv1 "k8s.io/api/discovery/v1"
netv1 "k8s.io/api/networking/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
k8stypes "k8s.io/apimachinery/pkg/types"
Expand Down Expand Up @@ -550,11 +549,6 @@ func (r *{{.PackageAlias}}{{.Kind}}Reconciler) Reconcile(ctx context.Context, re
// get the relevant object
obj := new({{.PackageImportAlias}}.{{.Kind}})
// set type meta to the object
obj.TypeMeta = metav1.TypeMeta{
APIVersion: {{.PackageImportAlias}}.SchemeGroupVersion.String(),
Kind: "{{.Kind}}",
}
if err := r.Get(ctx, req.NamespacedName, obj); err != nil {
if apierrors.IsNotFound(err) {
Expand All @@ -572,6 +566,11 @@ func (r *{{.PackageAlias}}{{.Kind}}Reconciler) Reconcile(ctx context.Context, re
}
log.V(util.DebugLevel).Info("reconciling resource", "namespace", req.Namespace, "name", req.Name)
err := util.PopulateTypeMeta(obj)
if err != nil {
log.WithValues().Error(err, "could not set resource TypeMeta", "namespace", obj.GetNamespace(), "name", obj.GetName())
}
// clean the object up if it's being deleted
if !obj.DeletionTimestamp.IsZero() && time.Now().After(obj.DeletionTimestamp.Time) {
log.V(util.DebugLevel).Info("resource is being deleted, its configuration will be removed", "type", "{{.Kind}}", "namespace", req.Namespace, "name", req.Name)
Expand Down
5 changes: 5 additions & 0 deletions internal/controllers/configuration/kongadminapi_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,11 @@ func (r *KongAdminAPIServiceReconciler) SetLogger(l logr.Logger) {
}

func (r *KongAdminAPIServiceReconciler) shouldReconcileEndpointSlice(obj client.Object) bool {
err := util.PopulateTypeMeta(obj)
if err != nil {
r.Log.Error(err, "could not set resource TypeMeta",
"namespace", obj.GetNamespace(), "name", obj.GetName())
}
endpoints, ok := obj.(*discoveryv1.EndpointSlice)
if !ok {
return false
Expand Down
12 changes: 12 additions & 0 deletions internal/controllers/configuration/secret_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,11 @@ func (r *CoreV1SecretReconciler) SetLogger(l logr.Logger) {
// - the secret has label: konghq.com/ca-cert:true
// - or the secret is referred by objects we care (service, ingress, gateway, ...)
func (r *CoreV1SecretReconciler) shouldReconcileSecret(obj client.Object) bool {
err := util.PopulateTypeMeta(obj)
if err != nil {
r.Log.Error(err, "could not set resource TypeMeta",
"namespace", obj.GetNamespace(), "name", obj.GetName())
}
secret, ok := obj.(*corev1.Secret)
if !ok {
return false
Expand Down Expand Up @@ -113,6 +118,13 @@ func (r *CoreV1SecretReconciler) Reconcile(ctx context.Context, req ctrl.Request
}
return ctrl.Result{}, err
}

err := util.PopulateTypeMeta(secret)
if err != nil {
r.Log.Error(err, "could not set resource TypeMeta",
"namespace", secret.GetNamespace(), "name", secret.GetName())
}

log.V(util.DebugLevel).Info("reconciling resource", "namespace", req.Namespace, "name", req.Name)

// clean the object up if it's being deleted
Expand Down
121 changes: 60 additions & 61 deletions internal/controllers/configuration/zz_generated_controllers.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 7 additions & 5 deletions internal/controllers/gateway/gateway_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,6 @@ import (
var (
ErrUnmanagedAnnotation = errors.New("invalid unmanaged annotation value")
gatewayV1beta1Group = gatewayv1beta1.Group(gatewayv1beta1.GroupName)
gatewayTypeMeta = metav1.TypeMeta{
APIVersion: gatewayv1beta1.GroupVersion.String(),
Kind: "Gateway",
}
)

// -----------------------------------------------------------------------------
Expand Down Expand Up @@ -343,7 +339,6 @@ func (r *GatewayReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ct
// gather the gateway object based on the reconciliation trigger. It's possible for the object
// to be gone at this point in which case it will be ignored.
gateway := new(gatewayv1beta1.Gateway)
gateway.TypeMeta = gatewayTypeMeta
if err := r.Get(ctx, req.NamespacedName, gateway); err != nil {
if apierrors.IsNotFound(err) {
gateway.Namespace = req.Namespace
Expand All @@ -358,6 +353,13 @@ func (r *GatewayReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ct
}
return ctrl.Result{Requeue: true}, err
}

err := util.PopulateTypeMeta(gateway)
if err != nil {
r.Log.Error(err, "could not set resource TypeMeta",
"namespace", gateway.GetNamespace(), "name", gateway.GetName())
}

debug(log, gateway, "processing gateway")

// though our watch configuration eliminates reconciliation of unsupported gateways it's
Expand Down
Loading

0 comments on commit f9e1d91

Please sign in to comment.