|
| 1 | +package e2e |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "testing" |
| 7 | + "time" |
| 8 | + |
| 9 | + certv1 "github.com/cert-manager/cert-manager/pkg/apis/certmanager/v1" |
| 10 | + appsv1 "k8s.io/api/apps/v1" |
| 11 | + corev1 "k8s.io/api/core/v1" |
| 12 | + apiextensionsV1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1" |
| 13 | + k8serrors "k8s.io/apimachinery/pkg/api/errors" |
| 14 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 15 | + "sigs.k8s.io/e2e-framework/klient" |
| 16 | + "sigs.k8s.io/e2e-framework/klient/k8s" |
| 17 | + "sigs.k8s.io/e2e-framework/klient/wait" |
| 18 | + "sigs.k8s.io/e2e-framework/pkg/envconf" |
| 19 | + "sigs.k8s.io/e2e-framework/pkg/features" |
| 20 | + |
| 21 | + ecv1alpha1 "go.etcd.io/etcd-operator/api/v1alpha1" |
| 22 | + "go.etcd.io/etcd-operator/pkg/certificate/auto" |
| 23 | + interfaces "go.etcd.io/etcd-operator/pkg/certificate/interfaces" |
| 24 | +) |
| 25 | + |
| 26 | +const ( |
| 27 | + autoCertificateName = "sample-cert" |
| 28 | + autoCertificateNamespace = "default" |
| 29 | + autoCertificateValidity = 365 * 24 * time.Hour |
| 30 | +) |
| 31 | + |
| 32 | +func TestAutoProvider(t *testing.T) { |
| 33 | + feature := features.New("Auto Provider Certificate").WithLabel("app", "auto") |
| 34 | + |
| 35 | + cmConfig := &interfaces.Config{ |
| 36 | + CommonName: autoCertificateName, |
| 37 | + ValidityDuration: autoCertificateValidity, |
| 38 | + } |
| 39 | + |
| 40 | + feature.Setup( |
| 41 | + func(ctx context.Context, t *testing.T, cfg *envconf.Config) context.Context { |
| 42 | + client := cfg.Client() |
| 43 | + _ = appsv1.AddToScheme(client.Resources().GetScheme()) |
| 44 | + _ = corev1.AddToScheme(client.Resources().GetScheme()) |
| 45 | + _ = certv1.AddToScheme(client.Resources().GetScheme()) |
| 46 | + _ = apiextensionsV1.AddToScheme(client.Resources().GetScheme()) |
| 47 | + |
| 48 | + return ctx |
| 49 | + }) |
| 50 | + |
| 51 | + feature.Assess("Ensure certificate", |
| 52 | + func(ctx context.Context, t *testing.T, cfg *envconf.Config) context.Context { |
| 53 | + client := cfg.Client() |
| 54 | + acProvider := auto.New(client.Resources().GetControllerRuntimeClient()) |
| 55 | + err := acProvider.EnsureCertificateSecret(ctx, autoCertificateName, autoCertificateNamespace, cmConfig) |
| 56 | + if err != nil { |
| 57 | + t.Fatalf("Auto Provider Certificate could not be created: %v", err) |
| 58 | + } |
| 59 | + return ctx |
| 60 | + }) |
| 61 | + |
| 62 | + feature.Assess("Validate certificate secret", |
| 63 | + func(ctx context.Context, t *testing.T, cfg *envconf.Config) context.Context { |
| 64 | + client := cfg.Client() |
| 65 | + acProvider := auto.New(client.Resources().GetControllerRuntimeClient()) |
| 66 | + err := acProvider.ValidateCertificateSecret(ctx, autoCertificateName, autoCertificateNamespace, cmConfig) |
| 67 | + if err != nil { |
| 68 | + t.Fatalf("Failed to validate Auto Provider Certificate secret: %v", err) |
| 69 | + } |
| 70 | + return ctx |
| 71 | + }) |
| 72 | + |
| 73 | + feature.Assess("Delete certificate secret", |
| 74 | + func(ctx context.Context, t *testing.T, cfg *envconf.Config) context.Context { |
| 75 | + client := cfg.Client() |
| 76 | + acProvider := auto.New(client.Resources().GetControllerRuntimeClient()) |
| 77 | + err := acProvider.DeleteCertificateSecret(ctx, autoCertificateName, autoCertificateNamespace) |
| 78 | + if err != nil { |
| 79 | + t.Fatalf("Failed to delete Certificate secret: %v", err) |
| 80 | + } |
| 81 | + return ctx |
| 82 | + }) |
| 83 | + |
| 84 | + feature.Assess("Verify Delete certificate", |
| 85 | + func(ctx context.Context, t *testing.T, cfg *envconf.Config) context.Context { |
| 86 | + client := cfg.Client() |
| 87 | + acProvider := auto.New(client.Resources().GetControllerRuntimeClient()) |
| 88 | + _, err := acProvider.GetCertificateConfig(ctx, autoCertificateName, autoCertificateNamespace) |
| 89 | + if err == nil { |
| 90 | + t.Fatalf("Auto Provider Certificate found, deletion failed: %v", err) |
| 91 | + } |
| 92 | + return ctx |
| 93 | + }) |
| 94 | + |
| 95 | + _ = testEnv.Test(t, feature.Feature()) |
| 96 | +} |
| 97 | + |
| 98 | +func TestClusterAutoCertCreation(t *testing.T) { |
| 99 | + feature := features.New("cluster-auto-cert-creation") |
| 100 | + |
| 101 | + const etcdClusterName = "etcd-cluster-auto-cert" |
| 102 | + const size = 3 |
| 103 | + |
| 104 | + etcdCluster := &ecv1alpha1.EtcdCluster{ |
| 105 | + TypeMeta: metav1.TypeMeta{ |
| 106 | + APIVersion: "operator.etcd.io/v1alpha1", |
| 107 | + Kind: "EtcdCluster", |
| 108 | + }, |
| 109 | + ObjectMeta: metav1.ObjectMeta{ |
| 110 | + Name: etcdClusterName, |
| 111 | + Namespace: namespace, |
| 112 | + }, |
| 113 | + Spec: ecv1alpha1.EtcdClusterSpec{ |
| 114 | + Size: size, |
| 115 | + Version: etcdVersion, |
| 116 | + TLS: &ecv1alpha1.TLSCertificate{ |
| 117 | + Provider: "auto", |
| 118 | + ProviderCfg: ecv1alpha1.ProviderConfig{ |
| 119 | + AutoCfg: &ecv1alpha1.ProviderAutoConfig{ |
| 120 | + CommonConfig: ecv1alpha1.CommonConfig{ |
| 121 | + CommonName: "etcd-operator-system", |
| 122 | + ValidityDuration: "8760h", |
| 123 | + }, |
| 124 | + }, |
| 125 | + }, |
| 126 | + }, |
| 127 | + }, |
| 128 | + } |
| 129 | + |
| 130 | + feature.Setup(func(ctx context.Context, t *testing.T, cfg *envconf.Config) context.Context { |
| 131 | + client := cfg.Client() |
| 132 | + _ = appsv1.AddToScheme(client.Resources().GetScheme()) |
| 133 | + _ = corev1.AddToScheme(client.Resources().GetScheme()) |
| 134 | + _ = certv1.AddToScheme(client.Resources().GetScheme()) |
| 135 | + _ = apiextensionsV1.AddToScheme(client.Resources().GetScheme()) |
| 136 | + |
| 137 | + // create etcd cluster |
| 138 | + if err := client.Resources().Create(ctx, etcdCluster); err != nil { |
| 139 | + t.Fatalf("unable to create etcd cluster: %s", err) |
| 140 | + } |
| 141 | + |
| 142 | + // get etcd cluster object |
| 143 | + var ec ecv1alpha1.EtcdCluster |
| 144 | + if err := client.Resources().Get(ctx, etcdClusterName, namespace, &ec); err != nil { |
| 145 | + t.Fatalf("unable to fetch etcd cluster: %s", err) |
| 146 | + } |
| 147 | + |
| 148 | + return ctx |
| 149 | + }) |
| 150 | + |
| 151 | + feature.Assess("Check certificate secrets exist", |
| 152 | + func(ctx context.Context, t *testing.T, c *envconf.Config) context.Context { |
| 153 | + client := c.Client() |
| 154 | + // checks if corresponding client, server, peer secrets are created in the respective namespace |
| 155 | + if err := wait.For( |
| 156 | + func(context.Context) (bool, error) { |
| 157 | + return validateSecretExists(ctx, client, etcdClusterName, namespace, "secret") |
| 158 | + }, |
| 159 | + wait.WithTimeout(3*time.Minute), |
| 160 | + wait.WithInterval(10*time.Second), |
| 161 | + ); err != nil { |
| 162 | + t.Fatalf("timed out waiting for certificate: %s", err) |
| 163 | + } |
| 164 | + return ctx |
| 165 | + }, |
| 166 | + ) |
| 167 | + |
| 168 | + feature.Assess("Verify Data Operations", |
| 169 | + func(ctx context.Context, t *testing.T, c *envconf.Config) context.Context { |
| 170 | + // verify etcdCluster is accessible via client certificate with put and get |
| 171 | + verifyDataOperations(t, c, etcdClusterName) |
| 172 | + return ctx |
| 173 | + }, |
| 174 | + ) |
| 175 | + |
| 176 | + _ = testEnv.Test(t, feature.Feature()) |
| 177 | +} |
| 178 | + |
| 179 | +func validateSecretExists(ctx context.Context, client klient.Client, |
| 180 | + etcdClusterName, etcdClusterNamespace, resourceType string) (bool, error) { |
| 181 | + clientCertName := fmt.Sprintf("%s-client-tls", etcdClusterName) |
| 182 | + serverCertName := fmt.Sprintf("%s-server-tls", etcdClusterName) |
| 183 | + peerCertName := fmt.Sprintf("%s-peer-tls", etcdClusterName) |
| 184 | + |
| 185 | + var obj any |
| 186 | + |
| 187 | + switch resourceType { |
| 188 | + case "secret": |
| 189 | + var secretObj corev1.Secret |
| 190 | + obj = &secretObj |
| 191 | + default: |
| 192 | + return false, fmt.Errorf("invalid resource type: %v", resourceType) |
| 193 | + } |
| 194 | + |
| 195 | + runtimeObj, err := obj.(k8s.Object) |
| 196 | + if !err { |
| 197 | + return false, fmt.Errorf("object does not implement runtime.Object: %T", obj) |
| 198 | + } |
| 199 | + |
| 200 | + if err := client.Resources().Get(ctx, clientCertName, etcdClusterNamespace, runtimeObj); err != nil { |
| 201 | + if k8serrors.IsNotFound(err) { |
| 202 | + return false, nil |
| 203 | + } |
| 204 | + return false, fmt.Errorf("failed to get Client %s: %v", resourceType, err) |
| 205 | + } |
| 206 | + |
| 207 | + if err := client.Resources().Get(ctx, serverCertName, etcdClusterNamespace, runtimeObj); err != nil { |
| 208 | + if k8serrors.IsNotFound(err) { |
| 209 | + return false, nil |
| 210 | + } |
| 211 | + return false, fmt.Errorf("failed to get Server %s: %v", resourceType, err) |
| 212 | + } |
| 213 | + |
| 214 | + if err := client.Resources().Get(ctx, peerCertName, etcdClusterNamespace, runtimeObj); err != nil { |
| 215 | + if k8serrors.IsNotFound(err) { |
| 216 | + return false, nil |
| 217 | + } |
| 218 | + return false, fmt.Errorf("failed to get Peer %s: %v", resourceType, err) |
| 219 | + } |
| 220 | + return true, nil |
| 221 | +} |
0 commit comments