|
| 1 | +package vectorized |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "testing" |
| 6 | + "time" |
| 7 | + |
| 8 | + "github.com/go-logr/logr/testr" |
| 9 | + "github.com/redpanda-data/common-go/rpadmin" |
| 10 | + "github.com/stretchr/testify/require" |
| 11 | + corev1 "k8s.io/api/core/v1" |
| 12 | + apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1" |
| 13 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 14 | + "k8s.io/utils/ptr" |
| 15 | + ctrl "sigs.k8s.io/controller-runtime" |
| 16 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 17 | + "sigs.k8s.io/controller-runtime/pkg/manager" |
| 18 | + "sigs.k8s.io/controller-runtime/pkg/metrics/server" |
| 19 | + |
| 20 | + redpanda "github.com/redpanda-data/redpanda-operator/charts/redpanda/v5/client" |
| 21 | + vectorizedv1alpha1 "github.com/redpanda-data/redpanda-operator/operator/api/vectorized/v1alpha1" |
| 22 | + crds "github.com/redpanda-data/redpanda-operator/operator/config/crd/bases" |
| 23 | + "github.com/redpanda-data/redpanda-operator/operator/internal/controller" |
| 24 | + "github.com/redpanda-data/redpanda-operator/operator/pkg/admin" |
| 25 | + "github.com/redpanda-data/redpanda-operator/operator/pkg/resources" |
| 26 | + "github.com/redpanda-data/redpanda-operator/operator/pkg/resources/types" |
| 27 | + "github.com/redpanda-data/redpanda-operator/operator/pkg/secrets" |
| 28 | + "github.com/redpanda-data/redpanda-operator/pkg/kube" |
| 29 | + "github.com/redpanda-data/redpanda-operator/pkg/kube/kubetest" |
| 30 | + "github.com/redpanda-data/redpanda-operator/pkg/otelutil/log" |
| 31 | +) |
| 32 | + |
| 33 | +func TestClusterControllerGolden(t *testing.T) { |
| 34 | + ctx := log.IntoContext(t.Context(), testr.New(t)) |
| 35 | + |
| 36 | + ctl := kubetest.NewEnv(t, kube.Options{ |
| 37 | + Options: client.Options{ |
| 38 | + Scheme: controller.V1Scheme, |
| 39 | + }, |
| 40 | + }) |
| 41 | + |
| 42 | + require.NoError(t, kube.ApplyAllAndWait(ctx, ctl, func(crd *apiextensionsv1.CustomResourceDefinition, err error) (bool, error) { |
| 43 | + if err != nil { |
| 44 | + return false, err |
| 45 | + } |
| 46 | + |
| 47 | + for _, cond := range crd.Status.Conditions { |
| 48 | + if cond.Type == apiextensionsv1.Established { |
| 49 | + return cond.Status == apiextensionsv1.ConditionTrue, nil |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + return false, nil |
| 54 | + }, crds.All()...)) |
| 55 | + |
| 56 | + manager, err := ctrl.NewManager(ctl.RestConfig(), manager.Options{ |
| 57 | + Logger: testr.New(t), |
| 58 | + Scheme: controller.V1Scheme, |
| 59 | + Metrics: server.Options{BindAddress: "0"}, |
| 60 | + }) |
| 61 | + require.NoError(t, err) |
| 62 | + |
| 63 | + adminAPIs := map[string]*admin.MockAdminAPI{} |
| 64 | + |
| 65 | + controller := &ClusterReconciler{ |
| 66 | + Log: testr.New(t), |
| 67 | + Client: manager.GetClient(), |
| 68 | + Scheme: manager.GetClient().Scheme(), |
| 69 | + configuratorSettings: resources.ConfiguratorSettings{ |
| 70 | + ImagePullPolicy: corev1.PullIfNotPresent, |
| 71 | + }, |
| 72 | + CloudSecretsExpander: &secrets.CloudExpander{}, |
| 73 | + AdminAPIClientFactory: func(ctx context.Context, k8sClient client.Reader, redpandaCluster *vectorizedv1alpha1.Cluster, fqdn string, adminTLSProvider types.AdminTLSConfigProvider, dialer redpanda.DialContextFunc, timeout time.Duration, pods ...string) (admin.AdminAPIClient, error) { |
| 74 | + if api, ok := adminAPIs[redpandaCluster.Name]; ok { |
| 75 | + return api, nil |
| 76 | + } |
| 77 | + |
| 78 | + api := &admin.MockAdminAPI{Log: testr.New(t)} |
| 79 | + |
| 80 | + api.RegisterPropertySchema("auto_create_topics_enabled", rpadmin.ConfigPropertyMetadata{NeedsRestart: false, Type: "boolean"}) |
| 81 | + api.RegisterPropertySchema("cloud_storage_segment_max_upload_interval_sec", rpadmin.ConfigPropertyMetadata{NeedsRestart: true, Type: "integer"}) |
| 82 | + api.RegisterPropertySchema("log_segment_size", rpadmin.ConfigPropertyMetadata{NeedsRestart: true, Type: "integer"}) |
| 83 | + api.RegisterPropertySchema("enable_rack_awareness", rpadmin.ConfigPropertyMetadata{NeedsRestart: false, Type: "boolean"}) |
| 84 | + |
| 85 | + adminAPIs[redpandaCluster.Name] = api |
| 86 | + |
| 87 | + return adminAPIs[redpandaCluster.Name], nil |
| 88 | + }, |
| 89 | + } |
| 90 | + |
| 91 | + require.NoError(t, controller.SetupWithManager(manager)) |
| 92 | + |
| 93 | + go func() { |
| 94 | + require.NoError(t, manager.Start(ctx)) |
| 95 | + }() |
| 96 | + |
| 97 | + require.NoError(t, kube.ApplyAndWait(ctx, ctl, &vectorizedv1alpha1.Cluster{ |
| 98 | + ObjectMeta: metav1.ObjectMeta{ |
| 99 | + Name: "cluster", |
| 100 | + Namespace: "default", |
| 101 | + }, |
| 102 | + Spec: vectorizedv1alpha1.ClusterSpec{ |
| 103 | + Replicas: ptr.To[int32](1), |
| 104 | + Configuration: vectorizedv1alpha1.RedpandaConfig{ |
| 105 | + AdminAPI: []vectorizedv1alpha1.AdminAPI{ |
| 106 | + {Port: 1234}, |
| 107 | + }, |
| 108 | + KafkaAPI: []vectorizedv1alpha1.KafkaAPI{ |
| 109 | + {Port: 4321}, |
| 110 | + }, |
| 111 | + }, |
| 112 | + }, |
| 113 | + }, func(cluster *vectorizedv1alpha1.Cluster, err error) (bool, error) { |
| 114 | + if err != nil { |
| 115 | + return false, err |
| 116 | + } |
| 117 | + |
| 118 | + for _, cond := range cluster.Status.Conditions { |
| 119 | + if cond.Type == vectorizedv1alpha1.ClusterConfiguredConditionType { |
| 120 | + return cond.Status == corev1.ConditionTrue, nil |
| 121 | + } |
| 122 | + } |
| 123 | + return false, nil |
| 124 | + })) |
| 125 | + |
| 126 | + cms, err := kube.List[corev1.SecretList](ctx, ctl) |
| 127 | + require.NoError(t, err) |
| 128 | + |
| 129 | + for _, cm := range cms.Items { |
| 130 | + t.Logf("%#v\n", cm) |
| 131 | + } |
| 132 | +} |
0 commit comments