Skip to content

Commit 14a05cf

Browse files
committed
replaces grpcurl job struct with loader file
1 parent b7dc226 commit 14a05cf

File tree

4 files changed

+90
-126
lines changed

4 files changed

+90
-126
lines changed

test/e2e/config_template.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ package e2e
55

66
import (
77
"bytes"
8+
"github.com/openshift/cert-manager-operator/api/operator/v1alpha1"
89
"text/template"
910
)
1011

@@ -19,6 +20,12 @@ type CertificateConfig struct {
1920
DNSName string
2021
}
2122

23+
// IstioCSRConfig customizes the fields in a job spec
24+
type IstioCSRGRPCurlJobConfig struct {
25+
CertificateSigningRequest string
26+
IstioCSRStatus v1alpha1.IstioCSRStatus
27+
}
28+
2229
// replaceWithTemplate puts field values from a template struct
2330
func replaceWithTemplate(sourceFileContents string, templatedValues any) ([]byte, error) {
2431
tmpl, err := template.New("template").Parse(sourceFileContents)

test/e2e/istio_csr_test.go

Lines changed: 8 additions & 104 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,11 @@ import (
1313
"net/url"
1414
"path/filepath"
1515

16-
batchv1 "k8s.io/api/batch/v1"
16+
"github.com/openshift/cert-manager-operator/test/library"
1717
corev1 "k8s.io/api/core/v1"
1818
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1919
"k8s.io/client-go/dynamic"
2020
"k8s.io/client-go/kubernetes"
21-
"k8s.io/utils/ptr"
22-
23-
"github.com/openshift/cert-manager-operator/test/library"
2421

2522
. "github.com/onsi/ginkgo/v2"
2623
. "github.com/onsi/gomega"
@@ -115,7 +112,7 @@ var _ = Describe("Istio-CSR", Ordered, Label("TechPreview", "Feature:IstioCSR"),
115112
err = pollTillDeploymentAvailable(ctx, clientset, ns.Name, "cert-manager-istio-csr")
116113
Expect(err).Should(BeNil())
117114

118-
istioCSRGRPCEndpoint, err := pollTillIstioCSRAvailable(ctx, dynamicClient, ns.Name, "default")
115+
istioCSRStatus, err := pollTillIstioCSRAvailable(ctx, dynamicClient, ns.Name, "default")
119116
Expect(err).Should(BeNil())
120117

121118
By("poll till the service account is available")
@@ -142,106 +139,13 @@ var _ = Describe("Istio-CSR", Ordered, Label("TechPreview", "Feature:IstioCSR"),
142139
Expect(err).Should(BeNil())
143140

144141
By("creating an grpcurl job")
145-
job := &batchv1.Job{
146-
ObjectMeta: metav1.ObjectMeta{
147-
Name: "grpcurl-job",
142+
loader.CreateFromFile(AssetFunc(testassets.ReadFile).WithTemplateValues(
143+
IstioCSRGRPCurlJobConfig{
144+
CertificateSigningRequest: csr,
145+
IstioCSRStatus: istioCSRStatus,
148146
},
149-
Spec: batchv1.JobSpec{
150-
Completions: ptr.To(int32(1)),
151-
BackoffLimit: ptr.To(backOffLimit),
152-
Template: corev1.PodTemplateSpec{
153-
ObjectMeta: metav1.ObjectMeta{
154-
Name: grpcAppName,
155-
Labels: map[string]string{
156-
"app": grpcAppName,
157-
},
158-
},
159-
Spec: corev1.PodSpec{
160-
ServiceAccountName: serviceAccountName,
161-
AutomountServiceAccountToken: ptr.To(false),
162-
RestartPolicy: corev1.RestartPolicyOnFailure,
163-
Containers: []corev1.Container{
164-
{
165-
Name: grpcAppName,
166-
Image: "registry.redhat.io/rhel9/go-toolset",
167-
Command: []string{
168-
"/bin/sh",
169-
"-c",
170-
},
171-
Env: []corev1.EnvVar{
172-
{
173-
Name: "GOCACHE",
174-
Value: "/tmp/go-cache",
175-
},
176-
{
177-
Name: "GOPATH",
178-
Value: "/tmp/go",
179-
},
180-
},
181-
Args: []string{
182-
"go install github.com/fullstorydev/grpcurl/cmd/[email protected] >/dev/null 2>&1 && " +
183-
"TOKEN=$(cat /var/run/secrets/istio-ca/token) && " +
184-
"/tmp/go/bin/grpcurl " +
185-
"-import-path /proto " +
186-
"-proto /proto/ca.proto " +
187-
"-H \"Authorization: Bearer $TOKEN\" " +
188-
fmt.Sprintf("-d '{\"csr\": \"%s\", \"validity_duration\": 3600}' ", csr) +
189-
"-cacert /etc/root-secret/ca.crt " +
190-
"-key /etc/root-secret/tls.key " +
191-
"-cert /etc/root-secret/tls.crt " +
192-
fmt.Sprintf("%s istio.v1.auth.IstioCertificateService/CreateCertificate", istioCSRGRPCEndpoint),
193-
},
194-
VolumeMounts: []corev1.VolumeMount{
195-
{Name: "root-secret", MountPath: "/etc/root-secret"},
196-
{Name: "proto", MountPath: "/proto"},
197-
{Name: "sa-token", MountPath: "/var/run/secrets/istio-ca"},
198-
},
199-
},
200-
},
201-
Volumes: []corev1.Volume{
202-
{
203-
Name: "sa-token",
204-
VolumeSource: corev1.VolumeSource{
205-
Projected: &corev1.ProjectedVolumeSource{
206-
DefaultMode: ptr.To(int32(420)),
207-
Sources: []corev1.VolumeProjection{
208-
{
209-
ServiceAccountToken: &corev1.ServiceAccountTokenProjection{
210-
Audience: "istio-ca",
211-
ExpirationSeconds: ptr.To(int64(3600)),
212-
Path: "token",
213-
},
214-
},
215-
},
216-
},
217-
},
218-
},
219-
{
220-
Name: "root-secret",
221-
VolumeSource: corev1.VolumeSource{
222-
Secret: &corev1.SecretVolumeSource{
223-
SecretName: "istiod-tls",
224-
},
225-
},
226-
},
227-
{
228-
Name: "proto",
229-
VolumeSource: corev1.VolumeSource{
230-
ConfigMap: &corev1.ConfigMapVolumeSource{
231-
LocalObjectReference: corev1.LocalObjectReference{
232-
Name: "proto-cm",
233-
},
234-
},
235-
},
236-
},
237-
},
238-
},
239-
},
240-
},
241-
}
242-
_, err = clientset.BatchV1().Jobs(ns.Name).Create(context.TODO(), job, metav1.CreateOptions{})
243-
Expect(err).Should(BeNil())
244-
defer clientset.BatchV1().Jobs(ns.Name).Delete(ctx, job.Name, metav1.DeleteOptions{})
147+
), filepath.Join("testdata", "istio", "grpcurl_job.yaml"), ns.Name)
148+
defer loader.DeleteFromFile(testassets.ReadFile, filepath.Join("testdata", "istio", "grpcurl_job.yaml"), ns.Name)
245149

246150
By("waiting for the job to be completed")
247151
err = pollTillJobCompleted(ctx, clientset, ns.Name, "grpcurl-job")
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
apiVersion: batch/v1
2+
kind: Job
3+
metadata:
4+
name: grpcurl-istio-csr
5+
spec:
6+
backoffLimit: 10
7+
completions: 1
8+
template:
9+
metadata:
10+
labels:
11+
app: grpcurl-istio-csr
12+
name: grpcurl-istio-csr
13+
spec:
14+
automountServiceAccountToken: false
15+
containers:
16+
- args:
17+
- |
18+
go install github.com/fullstorydev/grpcurl/cmd/[email protected] >/dev/null 2>&1 && \
19+
TOKEN=$(cat /var/run/secrets/istio-ca/token) && \
20+
/tmp/go/bin/grpcurl \
21+
-import-path /proto \
22+
-proto /proto/ca.proto \
23+
-H "Authorization: Bearer $TOKEN" \
24+
-d '{"csr": "{{.CertificateSigningRequest}}", "validity_duration": 3600}' \
25+
-cacert /etc/root-secret/ca.crt \
26+
-key /etc/root-secret/tls.key \
27+
-cert /etc/root-secret/tls.crt \
28+
{{.IstioCSRStatus.IstioCSRGRPCEndpoint}} istio.v1.auth.IstioCertificateService/CreateCertificate
29+
command:
30+
- /bin/sh
31+
- -c
32+
env:
33+
- name: GOCACHE
34+
value: /tmp/go-cache
35+
- name: GOPATH
36+
value: /tmp/go
37+
image: registry.redhat.io/rhel9/go-toolset
38+
name: grpcurl
39+
volumeMounts:
40+
- mountPath: /etc/root-secret
41+
name: root-secret
42+
- mountPath: /proto
43+
name: proto
44+
- mountPath: /var/run/secrets/istio-ca
45+
name: sa-token
46+
restartPolicy: OnFailure
47+
serviceAccountName: '{{.IstioCSRStatus.ServiceAccount}}'
48+
volumes:
49+
- name: sa-token
50+
projected:
51+
defaultMode: 420
52+
sources:
53+
- serviceAccountToken:
54+
audience: istio-ca
55+
expirationSeconds: 3600
56+
path: token
57+
- name: root-secret
58+
secret:
59+
secretName: istiod-tls
60+
- configMap:
61+
name: proto-cm
62+
name: proto

test/e2e/utils_test.go

Lines changed: 13 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"context"
99
"encoding/json"
1010
"fmt"
11-
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
1211
"math/rand"
1312
"regexp"
1413
"strings"
@@ -33,6 +32,8 @@ import (
3332
"k8s.io/apimachinery/pkg/api/equality"
3433
apierrors "k8s.io/apimachinery/pkg/api/errors"
3534
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
35+
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
36+
"k8s.io/apimachinery/pkg/runtime"
3637
"k8s.io/apimachinery/pkg/runtime/schema"
3738
"k8s.io/apimachinery/pkg/types"
3839
"k8s.io/apimachinery/pkg/util/errors"
@@ -611,10 +612,10 @@ func pollTillServiceAccountAvailable(ctx context.Context, clientset *kubernetes.
611612
return err
612613
}
613614

614-
// pollTillIstioCSRAvailable poll the istioCSR object and returns non-nil error and istio-grpc-endpoint
615+
// pollTillIstioCSRAvailable poll the istioCSR object and returns non-nil error and istioCSRStatus
615616
// once the istiocsr is available, otherwise should return a time-out error
616-
func pollTillIstioCSRAvailable(ctx context.Context, dynamicClient *dynamic.DynamicClient, namespace, istioCsrName string) (string, error) {
617-
var istioCSRGRPCEndpoint string
617+
func pollTillIstioCSRAvailable(ctx context.Context, dynamicClient *dynamic.DynamicClient, namespace, istioCsrName string) (v1alpha1.IstioCSRStatus, error) {
618+
var istioCSRStatus v1alpha1.IstioCSRStatus
618619
err := wait.PollUntilContextTimeout(ctx, PollInterval, TestTimeout, true, func(ctx context.Context) (bool, error) {
619620
gvr := schema.GroupVersionResource{
620621
Group: "operator.openshift.io",
@@ -636,44 +637,34 @@ func pollTillIstioCSRAvailable(ctx context.Context, dynamicClient *dynamic.Dynam
636637
return false, nil
637638
}
638639

639-
conditions, found, err := unstructured.NestedSlice(customResource.Object, "status", "conditions")
640+
err = runtime.DefaultUnstructuredConverter.FromUnstructured(status, &istioCSRStatus)
640641
if err != nil {
641642
return false, nil
642643
}
643644

644-
if !found {
645-
return false, nil
646-
}
647-
648-
for _, condition := range conditions {
649-
condMap, ok := condition.(map[string]interface{})
650-
if !ok {
651-
continue
652-
}
653-
654-
condType, _ := condMap["type"].(string)
655-
condStatus, _ := condMap["status"].(string)
645+
for _, condition := range istioCSRStatus.Conditions {
646+
condType := condition.Type
647+
condStatus := condition.Status
656648

657-
if condType != "Ready" {
649+
if condType != v1alpha1.Ready {
658650
continue
659651
}
660652

661-
if condStatus == string(metav1.ConditionTrue) {
653+
if condStatus == metav1.ConditionTrue {
662654
break
663655
} else {
664656
return false, nil
665657
}
666658

667659
}
668660

669-
if !library.IsEmptyString(status["istioCSRGRPCEndpoint"]) && !library.IsEmptyString(status["clusterRoleBinding"]) && !library.IsEmptyString(status["istioCSRImage"]) && !library.IsEmptyString(status["serviceAccount"]) {
670-
istioCSRGRPCEndpoint = status["istioCSRGRPCEndpoint"].(string)
661+
if !library.IsEmptyString(istioCSRStatus.IstioCSRGRPCEndpoint) && !library.IsEmptyString(istioCSRStatus.ClusterRoleBinding) && !library.IsEmptyString(istioCSRStatus.IstioCSRImage) && !library.IsEmptyString(istioCSRStatus.ServiceAccount) {
671662
return true, nil
672663
}
673664
return false, nil
674665
})
675666

676-
return istioCSRGRPCEndpoint, err
667+
return istioCSRStatus, err
677668
}
678669

679670
func pollTillDeploymentAvailable(ctx context.Context, clientSet *kubernetes.Clientset, namespace, deploymentName string) error {

0 commit comments

Comments
 (0)