|
| 1 | +// Copyright 2025 MongoDB Inc |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package kube |
| 16 | + |
| 17 | +import ( |
| 18 | + "context" |
| 19 | + "errors" |
| 20 | + "fmt" |
| 21 | + "time" |
| 22 | + |
| 23 | + corev1 "k8s.io/api/core/v1" |
| 24 | + apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1" |
| 25 | + apierrors "k8s.io/apimachinery/pkg/api/errors" |
| 26 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 27 | + "k8s.io/apimachinery/pkg/runtime" |
| 28 | + ctrl "sigs.k8s.io/controller-runtime" |
| 29 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 30 | + |
| 31 | + akov2 "github.com/mongodb/mongodb-atlas-kubernetes/v2/api/v1" |
| 32 | + akov2next "github.com/mongodb/mongodb-atlas-kubernetes/v2/internal/nextapi/v1" |
| 33 | +) |
| 34 | + |
| 35 | +const ( |
| 36 | + Pause = time.Second |
| 37 | +) |
| 38 | + |
| 39 | +type ObjectWithStatus interface { |
| 40 | + client.Object |
| 41 | + GetConditions() []metav1.Condition |
| 42 | +} |
| 43 | + |
| 44 | +// NewK8sTest initializes a test environment on Kubernetes. |
| 45 | +// It requires: |
| 46 | +// - A running Kubernetes cluster with a local configuration bound to it. |
| 47 | +// - The given set CRDs installed in that cluster |
| 48 | +func NewK8sTest(ctx context.Context, crds ...string) (client.Client, error) { |
| 49 | + kubeClient, err := TestKubeClient() |
| 50 | + if err != nil { |
| 51 | + return nil, fmt.Errorf("failed to setup Kubernetes test env client: %w", err) |
| 52 | + } |
| 53 | + |
| 54 | + for _, targetCRD := range crds { |
| 55 | + if err := assertCRD(ctx, kubeClient, targetCRD); err != nil { |
| 56 | + return nil, fmt.Errorf("failed to asert for test-required CRD: %w", err) |
| 57 | + } |
| 58 | + } |
| 59 | + return kubeClient, nil |
| 60 | +} |
| 61 | + |
| 62 | +// TestKubeClient returns a Kubernetes client for tests. |
| 63 | +// It requires a running Kubernetes cluster and a local configuration to it. |
| 64 | +// It supports core Kubernetes types, production and experimental CRDs. |
| 65 | +func TestKubeClient() (client.Client, error) { |
| 66 | + testScheme, err := getTestScheme( |
| 67 | + corev1.AddToScheme, |
| 68 | + apiextensionsv1.AddToScheme, |
| 69 | + akov2.AddToScheme, |
| 70 | + akov2next.AddToScheme) |
| 71 | + if err != nil { |
| 72 | + return nil, fmt.Errorf("failed to setup Kubernetes test env scheme: %w", err) |
| 73 | + } |
| 74 | + return getKubeClient(testScheme) |
| 75 | +} |
| 76 | + |
| 77 | +func Apply(ctx context.Context, kubeClient client.Client, defaultNamespace string, objs ...client.Object) error { |
| 78 | + for i, obj := range objs { |
| 79 | + if obj.GetNamespace() == "" { |
| 80 | + obj = obj.DeepCopyObject().(client.Object) |
| 81 | + obj.SetNamespace(defaultNamespace) |
| 82 | + } |
| 83 | + if err := apply(ctx, kubeClient, obj); err != nil { |
| 84 | + return fmt.Errorf("failed to apply object %d: %w", (i + 1), err) |
| 85 | + } |
| 86 | + } |
| 87 | + return nil |
| 88 | +} |
| 89 | + |
| 90 | +func apply(ctx context.Context, kubeClient client.Client, obj client.Object) error { |
| 91 | + key := client.ObjectKeyFromObject(obj) |
| 92 | + old := obj.DeepCopyObject().(client.Object) |
| 93 | + err := kubeClient.Get(ctx, key, old) |
| 94 | + switch { |
| 95 | + case err == nil: |
| 96 | + obj = obj.DeepCopyObject().(client.Object) |
| 97 | + obj.SetResourceVersion(old.GetResourceVersion()) |
| 98 | + if err := kubeClient.Update(ctx, obj); err != nil { |
| 99 | + return fmt.Errorf("failed to update %s: %w", key, err) |
| 100 | + } |
| 101 | + case apierrors.IsNotFound(err): |
| 102 | + if err := kubeClient.Create(ctx, obj); err != nil { |
| 103 | + return fmt.Errorf("failed to create %s: %w", key, err) |
| 104 | + } |
| 105 | + default: |
| 106 | + return fmt.Errorf("failed to apply %s: %w", key, err) |
| 107 | + } |
| 108 | + return nil |
| 109 | +} |
| 110 | + |
| 111 | +type OKOrFailureFunc func() (bool, error) |
| 112 | + |
| 113 | +func WaitConditionOrFailure(timeout time.Duration, okOrFailFn OKOrFailureFunc) error { |
| 114 | + start := time.Now() |
| 115 | + for { |
| 116 | + ok, err := okOrFailFn() |
| 117 | + if ok { |
| 118 | + return nil |
| 119 | + } |
| 120 | + if err != nil { |
| 121 | + return fmt.Errorf("failed to check condition: %w", err) |
| 122 | + } |
| 123 | + if time.Since(start) > timeout { |
| 124 | + return errors.New("wait condition timed out") |
| 125 | + } |
| 126 | + time.Sleep(Pause) |
| 127 | + } |
| 128 | +} |
| 129 | + |
| 130 | +func AssertObjReady(ctx context.Context, kubeClient client.Client, key client.ObjectKey, obj ObjectWithStatus) (bool, error) { |
| 131 | + err := kubeClient.Get(ctx, key, obj) |
| 132 | + if err != nil { |
| 133 | + return false, fmt.Errorf("failed to get object %v: %w", key, err) |
| 134 | + } |
| 135 | + for _, condition := range obj.GetConditions() { |
| 136 | + if condition.Type == "Ready" && condition.Status == metav1.ConditionTrue { |
| 137 | + return true, nil |
| 138 | + } |
| 139 | + } |
| 140 | + return false, nil |
| 141 | +} |
| 142 | + |
| 143 | +func getTestScheme(addToSchemeFunctions ...func(*runtime.Scheme) error) (*runtime.Scheme, error) { |
| 144 | + testScheme := runtime.NewScheme() |
| 145 | + for _, addToSchemeFn := range addToSchemeFunctions { |
| 146 | + if err := addToSchemeFn(testScheme); err != nil { |
| 147 | + return nil, fmt.Errorf("failed to add to testScheme: %w", err) |
| 148 | + } |
| 149 | + } |
| 150 | + return testScheme, nil |
| 151 | +} |
| 152 | + |
| 153 | +func getKubeClient(scheme *runtime.Scheme) (client.Client, error) { |
| 154 | + restCfg, err := ctrl.GetConfig() |
| 155 | + if err != nil { |
| 156 | + return nil, fmt.Errorf("failed to get Kubernetes config (is cluster configured?): %w", err) |
| 157 | + } |
| 158 | + kubeClient, err := client.New(restCfg, client.Options{Scheme: scheme}) |
| 159 | + if err != nil { |
| 160 | + return nil, fmt.Errorf("failed to get Kubernetes client (is cluster up?): %w", err) |
| 161 | + } |
| 162 | + return kubeClient, nil |
| 163 | +} |
| 164 | + |
| 165 | +func assertCRD(ctx context.Context, kubeClient client.Client, targetCRD string) error { |
| 166 | + crds := apiextensionsv1.CustomResourceDefinitionList{} |
| 167 | + if err := kubeClient.List(ctx, &crds, &client.ListOptions{}); err != nil { |
| 168 | + return fmt.Errorf("failed to list CRDs: %w", err) |
| 169 | + } |
| 170 | + for _, crd := range crds.Items { |
| 171 | + if crd.Name == targetCRD { |
| 172 | + return nil |
| 173 | + } |
| 174 | + } |
| 175 | + return fmt.Errorf("%s not found", targetCRD) |
| 176 | +} |
0 commit comments