Skip to content
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
13 changes: 13 additions & 0 deletions Taskfile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,22 @@ tasks:
MOD:
sh: go work edit -json | jq -r '.Use.[].DiskPath'
cmds:
# go.works make things a bit funky. We have to sync twice to avoid
# accidentally upgrading deps everywhere which can be quite disruptive.
#
# The first sync will ensure that any newly added transient deps get pulled
# in at the same version as our other modules, if applicable.
# If we have Mod A and B and a 3rd party dep X such that:
# A -> B -> X
# if A imports X:
# `go mod tidy` will pull in the most recent version of X.
# `go work sync` will pull in the version that B is using.
- go work sync
- for:
var: MOD
cmd: go mod tidy -C {{.ITEM}}
# The second go work sync, is the more standard go work sync. Any newly
# added deps will get added to the go.work.sum file.
- go work sync

fmt:
Expand Down
2 changes: 2 additions & 0 deletions harpoon/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,8 @@ github.com/power-devops/perfstat v0.0.0-20221212215047-62379fc7944b h1:0LFwY6Q3g
github.com/power-devops/perfstat v0.0.0-20221212215047-62379fc7944b/go.mod h1:OmDBASR4679mdNQnz2pUhc2G8CO2JrUAVFDRBDP/hJE=
github.com/poy/onpar v1.1.2 h1:QaNrNiZx0+Nar5dLgTVp5mXkyoVFIbepjyEoGSnhbAY=
github.com/poy/onpar v1.1.2/go.mod h1:6X8FLNoxyr9kkmnlqpK6LSoiOtrO6MICtWwEuWkLjzg=
github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring v0.76.2 h1:BpGDC87A2SaxbKgONsFLEX3kRcRJee2aLQbjXsuz0hA=
github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring v0.76.2/go.mod h1:Rd8YnCqz+2FYsiGmE2DMlaLjQRB4v2jFNnzCt9YY4IM=
github.com/prometheus/client_golang v0.9.1/go.mod h1:7SWBe2y4D6OKWSNQJUaRYU/AaXPKyh/dDVn+NZz0KFw=
github.com/prometheus/client_golang v1.0.0/go.mod h1:db9x61etRT2tGnBNRi70OPL5FsnadC4Ky3P0J6CfImo=
github.com/prometheus/client_golang v1.1.0/go.mod h1:I1FGZT9+L76gKKOs5djB6ezCbFQP1xR9D75/vuwEF3g=
Expand Down
16 changes: 9 additions & 7 deletions operator/internal/lifecycle/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
"context"
"errors"
"fmt"
"reflect"
"slices"

"github.com/go-logr/logr"
Expand All @@ -30,6 +29,8 @@ import (
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/handler"
"sigs.k8s.io/controller-runtime/pkg/reconcile"

"github.com/redpanda-data/redpanda-operator/pkg/kube"
)

// Cluster is a generic interface for a pointer to a Kubernetes object
Expand Down Expand Up @@ -272,15 +273,16 @@ func (r *ResourceClient[T, U]) listResources(ctx context.Context, object client.
}
}

converted := []client.Object{}
items := reflect.ValueOf(list).Elem().FieldByName("Items")
for i := 0; i < items.Len(); i++ {
item := items.Index(i).Addr().Interface().(client.Object)
items, err := kube.Items[client.Object](list)
if err != nil {
return nil, err
}

for _, item := range items {
item.GetObjectKind().SetGroupVersionKind(*kind)
converted = append(converted, item)
}

return sortCreation(converted), nil
return sortCreation(items), nil
}

// listAllOwnedResources lists all resources owned by a given cluster, optionally including node pools.
Expand Down
1 change: 1 addition & 0 deletions pkg/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ require (
github.com/homeport/dyff v1.7.1
github.com/invopop/jsonschema v0.12.0
github.com/lucasjones/reggen v0.0.0-20200904144131-37ba4fa293bb
github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring v0.76.2
github.com/santhosh-tekuri/jsonschema/v5 v5.3.1
github.com/stretchr/testify v1.10.0
github.com/testcontainers/testcontainers-go/modules/k3s v0.32.0
Expand Down
2 changes: 2 additions & 0 deletions pkg/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,8 @@ github.com/power-devops/perfstat v0.0.0-20221212215047-62379fc7944b h1:0LFwY6Q3g
github.com/power-devops/perfstat v0.0.0-20221212215047-62379fc7944b/go.mod h1:OmDBASR4679mdNQnz2pUhc2G8CO2JrUAVFDRBDP/hJE=
github.com/poy/onpar v1.1.2 h1:QaNrNiZx0+Nar5dLgTVp5mXkyoVFIbepjyEoGSnhbAY=
github.com/poy/onpar v1.1.2/go.mod h1:6X8FLNoxyr9kkmnlqpK6LSoiOtrO6MICtWwEuWkLjzg=
github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring v0.76.2 h1:BpGDC87A2SaxbKgONsFLEX3kRcRJee2aLQbjXsuz0hA=
github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring v0.76.2/go.mod h1:Rd8YnCqz+2FYsiGmE2DMlaLjQRB4v2jFNnzCt9YY4IM=
github.com/prometheus/client_golang v0.9.1/go.mod h1:7SWBe2y4D6OKWSNQJUaRYU/AaXPKyh/dDVn+NZz0KFw=
github.com/prometheus/client_golang v1.0.0/go.mod h1:db9x61etRT2tGnBNRi70OPL5FsnadC4Ky3P0J6CfImo=
github.com/prometheus/client_golang v1.1.0/go.mod h1:I1FGZT9+L76gKKOs5djB6ezCbFQP1xR9D75/vuwEF3g=
Expand Down
27 changes: 27 additions & 0 deletions pkg/kube/generics.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ package kube

import (
"context"
"reflect"

"github.com/cockroachdb/errors"
"sigs.k8s.io/controller-runtime/pkg/client"
)

Expand Down Expand Up @@ -66,3 +68,28 @@ func Delete[T any, PT AddrofObject[T]](ctx context.Context, ctl *Ctl, key Object
func AsKey(obj Object) ObjectKey {
return ObjectKey{Namespace: obj.GetNamespace(), Name: obj.GetName()}
}

// Items is a generic aware accessor for [client.ObjectList] that handles
// non-standard list implementations.
func Items[T Object](list client.ObjectList) ([]T, error) {
items := reflect.ValueOf(list).Elem().FieldByName("Items")

out := make([]T, items.Len())
for i := 0; i < items.Len(); i++ {
item := items.Index(i)
if item.Kind() != reflect.Pointer {
item = item.Addr()
}

converted, ok := item.Interface().(T)
if !ok {
to := reflect.TypeFor[T]()
from := items.Index(i).Type()
return nil, errors.Newf("can't convert %v to %v", from, to)
}

out[i] = converted
}

return out, nil
}
49 changes: 49 additions & 0 deletions pkg/kube/generics_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package kube

import (
"testing"

monitoringv1 "github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring/v1"
"github.com/stretchr/testify/require"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"sigs.k8s.io/controller-runtime/pkg/client"
)

func TestItems(t *testing.T) {
// Unlike other types, ServiceMonitorList's Items is []*T instead of []T.
// As we need to use reflection to get Items out, we need to be careful to
// not panic.
_, err := Items[Object](&monitoringv1.ServiceMonitorList{Items: []*monitoringv1.ServiceMonitor{{}}})
require.NoError(t, err)

_, err = Items[*monitoringv1.ServiceMonitor](&monitoringv1.ServiceMonitorList{Items: []*monitoringv1.ServiceMonitor{{}}})
require.NoError(t, err)

_, err = Items[Object](&corev1.PodList{Items: []corev1.Pod{{}}})
require.NoError(t, err)

_, err = Items[*corev1.Pod](&corev1.PodList{Items: []corev1.Pod{{}}})
require.NoError(t, err)

_, err = Items[Object](&BogusList{Items: []struct{}{{}}})
require.EqualError(t, err, "can't convert struct {} to client.Object")
}

type BogusList struct {
Items []struct{}
}

var _ client.ObjectList = (*BogusList)(nil)

func (*BogusList) GetObjectKind() schema.ObjectKind { return schema.EmptyObjectKind }
func (*BogusList) DeepCopyObject() runtime.Object { return nil }
func (*BogusList) GetResourceVersion() string { return "" }
func (*BogusList) SetResourceVersion(version string) {}
func (*BogusList) GetSelfLink() string { return "" }
func (*BogusList) SetSelfLink(selfLink string) {}
func (*BogusList) GetContinue() string { return "" }
func (*BogusList) SetContinue(c string) {}
func (*BogusList) GetRemainingItemCount() *int64 { return nil }
func (*BogusList) SetRemainingItemCount(c *int64) {}