|
| 1 | +package controllers |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "errors" |
| 7 | + "fmt" |
| 8 | + "testing" |
| 9 | + |
| 10 | + "github.com/stretchr/testify/assert" |
| 11 | + "github.com/stretchr/testify/require" |
| 12 | + appsv1 "k8s.io/api/apps/v1" |
| 13 | + corev1 "k8s.io/api/core/v1" |
| 14 | + apierrors "k8s.io/apimachinery/pkg/api/errors" |
| 15 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 16 | + "k8s.io/apimachinery/pkg/runtime" |
| 17 | + "k8s.io/apimachinery/pkg/types" |
| 18 | + clientgoscheme "k8s.io/client-go/kubernetes/scheme" |
| 19 | + ctrl "sigs.k8s.io/controller-runtime" |
| 20 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 21 | + "sigs.k8s.io/controller-runtime/pkg/client/fake" |
| 22 | +) |
| 23 | + |
| 24 | +func Test_MachineAPIControllersReconciler_Reconcile(t *testing.T) { |
| 25 | + t.Parallel() |
| 26 | + |
| 27 | + ctx := context.Background() |
| 28 | + |
| 29 | + const namespace = "openshift-machine-api" |
| 30 | + |
| 31 | + scheme := runtime.NewScheme() |
| 32 | + require.NoError(t, clientgoscheme.AddToScheme(scheme)) |
| 33 | + |
| 34 | + images := map[string]string{ |
| 35 | + "machineAPIOperator": "registry.io/machine-api-operator:v1.0.0", |
| 36 | + "kubeRBACProxy": "registry.io/kube-rbac-proxy:v1.0.0", |
| 37 | + } |
| 38 | + imagesJSON, err := json.Marshal(images) |
| 39 | + require.NoError(t, err) |
| 40 | + |
| 41 | + ucm := &corev1.ConfigMap{ |
| 42 | + ObjectMeta: metav1.ObjectMeta{ |
| 43 | + Name: imagesConfigMapName, |
| 44 | + Namespace: namespace, |
| 45 | + }, |
| 46 | + Data: map[string]string{ |
| 47 | + imageKey: string(imagesJSON), |
| 48 | + }, |
| 49 | + } |
| 50 | + |
| 51 | + c := &fakeSSA{ |
| 52 | + fake.NewClientBuilder(). |
| 53 | + WithScheme(scheme). |
| 54 | + WithRuntimeObjects(ucm). |
| 55 | + Build(), |
| 56 | + } |
| 57 | + |
| 58 | + r := &MachineAPIControllersReconciler{ |
| 59 | + Client: c, |
| 60 | + Scheme: scheme, |
| 61 | + |
| 62 | + Namespace: namespace, |
| 63 | + } |
| 64 | + |
| 65 | + _, err = r.Reconcile(ctx, ctrl.Request{NamespacedName: client.ObjectKeyFromObject(ucm)}) |
| 66 | + require.NoError(t, err) |
| 67 | + |
| 68 | + var deployment appsv1.Deployment |
| 69 | + require.NoError(t, c.Get(ctx, client.ObjectKey{Namespace: namespace, Name: "appuio-" + originalUpstreamDeploymentName}, &deployment)) |
| 70 | + |
| 71 | + assert.Equal(t, "system-node-critical", deployment.Spec.Template.Spec.PriorityClassName) |
| 72 | + for _, c := range deployment.Spec.Template.Spec.Containers { |
| 73 | + if c.Image == images["machineAPIOperator"] || c.Image == images["kubeRBACProxy"] { |
| 74 | + continue |
| 75 | + } |
| 76 | + t.Errorf("expected image %q or %q, got %q", images["machineAPIOperator"], images["kubeRBACProxy"], c.Image) |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +func Test_MachineAPIControllersReconciler_OriginalDeploymentExists(t *testing.T) { |
| 81 | + t.Parallel() |
| 82 | + |
| 83 | + ctx := context.Background() |
| 84 | + |
| 85 | + const namespace = "openshift-machine-api" |
| 86 | + |
| 87 | + scheme := runtime.NewScheme() |
| 88 | + require.NoError(t, clientgoscheme.AddToScheme(scheme)) |
| 89 | + |
| 90 | + images := map[string]string{ |
| 91 | + "machineAPIOperator": "registry.io/machine-api-operator:v1.0.0", |
| 92 | + "kubeRBACProxy": "registry.io/kube-rbac-proxy:v1.0.0", |
| 93 | + } |
| 94 | + imagesJSON, err := json.Marshal(images) |
| 95 | + require.NoError(t, err) |
| 96 | + |
| 97 | + ucm := &corev1.ConfigMap{ |
| 98 | + ObjectMeta: metav1.ObjectMeta{ |
| 99 | + Name: imagesConfigMapName, |
| 100 | + Namespace: namespace, |
| 101 | + }, |
| 102 | + Data: map[string]string{ |
| 103 | + imageKey: string(imagesJSON), |
| 104 | + }, |
| 105 | + } |
| 106 | + |
| 107 | + origDeploy := &appsv1.Deployment{ |
| 108 | + ObjectMeta: metav1.ObjectMeta{ |
| 109 | + Name: originalUpstreamDeploymentName, |
| 110 | + Namespace: namespace, |
| 111 | + }, |
| 112 | + } |
| 113 | + |
| 114 | + c := &fakeSSA{ |
| 115 | + fake.NewClientBuilder(). |
| 116 | + WithScheme(scheme). |
| 117 | + WithRuntimeObjects(ucm, origDeploy). |
| 118 | + Build(), |
| 119 | + } |
| 120 | + |
| 121 | + r := &MachineAPIControllersReconciler{ |
| 122 | + Client: c, |
| 123 | + Scheme: scheme, |
| 124 | + |
| 125 | + Namespace: namespace, |
| 126 | + } |
| 127 | + |
| 128 | + _, err = r.Reconcile(ctx, ctrl.Request{NamespacedName: client.ObjectKeyFromObject(ucm)}) |
| 129 | + require.ErrorContains(t, err, "machine-api-controllers already exists") |
| 130 | +} |
| 131 | + |
| 132 | +// fakeSSA is a fake client that approximates SSA. |
| 133 | +// It creates objects that don't exist yet and _updates_ them if they exist. |
| 134 | +// This is completely kaputt since the object is overwritten with the new object. |
| 135 | +// See https://github.com/kubernetes-sigs/controller-runtime/issues/2341 |
| 136 | +type fakeSSA struct { |
| 137 | + client.WithWatch |
| 138 | +} |
| 139 | + |
| 140 | +// Patch approximates SSA by creating objects that don't exist yet. |
| 141 | +func (f *fakeSSA) Patch(ctx context.Context, obj client.Object, patch client.Patch, opts ...client.PatchOption) error { |
| 142 | + // Apply patches are supposed to upsert, but fake client fails if the object doesn't exist, |
| 143 | + // if an apply patch occurs for an object that doesn't yet exist, create it. |
| 144 | + if patch.Type() != types.ApplyPatchType { |
| 145 | + return f.WithWatch.Patch(ctx, obj, patch, opts...) |
| 146 | + } |
| 147 | + check, ok := obj.DeepCopyObject().(client.Object) |
| 148 | + if !ok { |
| 149 | + return errors.New("could not check for object in fake client") |
| 150 | + } |
| 151 | + if err := f.WithWatch.Get(ctx, client.ObjectKeyFromObject(obj), check); apierrors.IsNotFound(err) { |
| 152 | + if err := f.WithWatch.Create(ctx, check); err != nil { |
| 153 | + return fmt.Errorf("could not inject object creation for fake: %w", err) |
| 154 | + } |
| 155 | + } else if err != nil { |
| 156 | + return fmt.Errorf("could not check for object in fake client: %w", err) |
| 157 | + } |
| 158 | + return f.WithWatch.Update(ctx, obj) |
| 159 | +} |
0 commit comments