|
| 1 | +package clusteraccess |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "time" |
| 7 | + |
| 8 | + corev1 "k8s.io/api/core/v1" |
| 9 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 10 | + "k8s.io/apimachinery/pkg/runtime" |
| 11 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 12 | + "sigs.k8s.io/controller-runtime/pkg/client/fake" |
| 13 | + "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil" |
| 14 | + |
| 15 | + "github.com/openmcp-project/controller-utils/pkg/clusters" |
| 16 | + "github.com/openmcp-project/controller-utils/pkg/resources" |
| 17 | + |
| 18 | + clustersv1alpha1 "github.com/openmcp-project/openmcp-operator/api/clusters/v1alpha1" |
| 19 | + commonapi "github.com/openmcp-project/openmcp-operator/api/common" |
| 20 | + openmcpconst "github.com/openmcp-project/openmcp-operator/api/constants" |
| 21 | +) |
| 22 | + |
| 23 | +func NewTestClusterAccessManager(platformClusterClient client.Client, controllerName, controllerNamespace string, fakeClientMapping map[string]client.Client) Manager { |
| 24 | + return &testManagerImpl{ |
| 25 | + managerImpl: managerImpl{ |
| 26 | + platformClusterClient: platformClusterClient, |
| 27 | + controllerName: controllerName, |
| 28 | + controllerNamespace: controllerNamespace, |
| 29 | + timeout: 5 * time.Minute, |
| 30 | + interval: 10 * time.Second, |
| 31 | + log: nil, // Default to no logging |
| 32 | + }, |
| 33 | + fakeClientMapping: fakeClientMapping, |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +type testManagerImpl struct { |
| 38 | + managerImpl |
| 39 | + fakeClientMapping map[string]client.Client |
| 40 | +} |
| 41 | + |
| 42 | +var _ Manager = &testManagerImpl{} |
| 43 | + |
| 44 | +// CreateAndWaitForCluster implements Manager. |
| 45 | +func (m *testManagerImpl) CreateAndWaitForCluster(ctx context.Context, localName, purpose string, scheme *runtime.Scheme, permissions []clustersv1alpha1.PermissionsRequest) (*clusters.Cluster, error) { |
| 46 | + cr := &clustersv1alpha1.ClusterRequest{ |
| 47 | + ObjectMeta: metav1.ObjectMeta{ |
| 48 | + Name: StableRequestNameFromLocalName(m.controllerName, localName), |
| 49 | + Namespace: m.controllerNamespace, |
| 50 | + }, |
| 51 | + } |
| 52 | + |
| 53 | + clusterRequestMutator := newClusterRequestMutator(cr.Name, cr.Namespace, purpose) |
| 54 | + clusterRequestMutator.WithMetadata(resources.NewMetadataMutator().WithLabels(map[string]string{ |
| 55 | + openmcpconst.ManagedByLabel: m.controllerName, |
| 56 | + })) |
| 57 | + |
| 58 | + if err := resources.CreateOrUpdateResource(ctx, m.platformClusterClient, clusterRequestMutator); err != nil { |
| 59 | + return nil, fmt.Errorf("failed to create/update ClusterRequest: %w", err) |
| 60 | + } |
| 61 | + |
| 62 | + // TESTING MODIFICATIONS |
| 63 | + // fake ClusterRequest readiness |
| 64 | + old := cr.DeepCopy() |
| 65 | + cr.Status.Phase = clustersv1alpha1.REQUEST_GRANTED |
| 66 | + if err := m.platformClusterClient.Status().Patch(ctx, cr, client.MergeFrom(old)); err != nil { |
| 67 | + return nil, fmt.Errorf("failed to update ClusterRequest status: %w", err) |
| 68 | + } |
| 69 | + |
| 70 | + cl, _, err := m.WaitForClusterAccess(ctx, localName, scheme, &commonapi.ObjectReference{ |
| 71 | + Name: cr.Name, |
| 72 | + Namespace: cr.Namespace, |
| 73 | + }, ReferenceToClusterRequest, permissions) |
| 74 | + return cl, err |
| 75 | +} |
| 76 | + |
| 77 | +// WaitForClusterAccess implements Manager. |
| 78 | +func (m *testManagerImpl) WaitForClusterAccess(ctx context.Context, localName string, scheme *runtime.Scheme, ref *commonapi.ObjectReference, refType ClusterReferenceType, permissions []clustersv1alpha1.PermissionsRequest) (*clusters.Cluster, *clustersv1alpha1.AccessRequest, error) { |
| 79 | + ar := &clustersv1alpha1.AccessRequest{ |
| 80 | + ObjectMeta: metav1.ObjectMeta{ |
| 81 | + Name: StableRequestNameFromLocalName(m.controllerName, localName), |
| 82 | + Namespace: m.controllerNamespace, |
| 83 | + }, |
| 84 | + } |
| 85 | + |
| 86 | + accessRequestMutator := newAccessRequestMutator(ar.Name, ar.Namespace) |
| 87 | + switch refType { |
| 88 | + case ReferenceToCluster: |
| 89 | + accessRequestMutator.WithClusterRef(ref) |
| 90 | + case ReferenceToClusterRequest: |
| 91 | + accessRequestMutator.WithRequestRef(ref) |
| 92 | + default: |
| 93 | + return nil, nil, fmt.Errorf("invalid ClusterReferenceType: %s", refType) |
| 94 | + } |
| 95 | + accessRequestMutator.WithTokenPermissions(permissions) |
| 96 | + accessRequestMutator.WithMetadata(resources.NewMetadataMutator().WithLabels(map[string]string{ |
| 97 | + openmcpconst.ManagedByLabel: m.controllerName, |
| 98 | + })) |
| 99 | + |
| 100 | + if err := resources.CreateOrUpdateResource(ctx, m.platformClusterClient, accessRequestMutator); err != nil { |
| 101 | + return nil, nil, fmt.Errorf("failed to create/update AccessRequest: %w", err) |
| 102 | + } |
| 103 | + |
| 104 | + // TESTING MODIFICATIONS |
| 105 | + // fake AccessRequest readiness and return cluster from fake client mapping |
| 106 | + fakeClient, ok := m.fakeClientMapping[localName] |
| 107 | + if !ok { |
| 108 | + return nil, nil, fmt.Errorf("no fake client found for local cluster name %q", localName) // simulate AccessRequest not being granted |
| 109 | + } |
| 110 | + old := ar.DeepCopy() |
| 111 | + ar.Status.Phase = clustersv1alpha1.REQUEST_GRANTED |
| 112 | + ar.Status.SecretRef = &commonapi.ObjectReference{ |
| 113 | + Name: ar.Name, |
| 114 | + Namespace: ar.Namespace, |
| 115 | + } |
| 116 | + if err := m.platformClusterClient.Status().Patch(ctx, ar, client.MergeFrom(old)); err != nil { |
| 117 | + return nil, nil, fmt.Errorf("failed to update AccessRequest status: %w", err) |
| 118 | + } |
| 119 | + sec := &corev1.Secret{} |
| 120 | + sec.Name = ar.Status.SecretRef.Name |
| 121 | + sec.Namespace = ar.Status.SecretRef.Namespace |
| 122 | + if _, err := controllerutil.CreateOrUpdate(ctx, m.platformClusterClient, sec, func() error { |
| 123 | + sec.Data = map[string][]byte{ |
| 124 | + clustersv1alpha1.SecretKeyKubeconfig: []byte("fake:" + localName), |
| 125 | + } |
| 126 | + return nil |
| 127 | + }); err != nil { |
| 128 | + return nil, nil, fmt.Errorf("failed to create/update fake kubeconfig secret for AccessRequest: %w", err) |
| 129 | + } |
| 130 | + if fakeClient == nil { |
| 131 | + // create new default fake client if none was provided |
| 132 | + fakeClient = fake.NewClientBuilder().WithScheme(scheme).Build() |
| 133 | + } |
| 134 | + return clusters.NewTestClusterFromClient(localName, fakeClient), ar, nil |
| 135 | +} |
0 commit comments