Skip to content

Commit

Permalink
Merge branch 'crds_for_testing' into fix_expectations
Browse files Browse the repository at this point in the history
  • Loading branch information
atoulme committed Jan 10, 2025
2 parents 75a2405 + 831a1c6 commit cc733c2
Show file tree
Hide file tree
Showing 7 changed files with 13,079 additions and 72 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,6 @@ metadata:
release: default
heritage: Helm
app.kubernetes.io/component: otel-operator
annotations:
"helm.sh/hook": post-install,post-upgrade
"helm.sh/hook-weight": "5"
spec:
exporter:
endpoint: http://default-splunk-otel-collector-agent.default.svc.cluster.local:4317
Expand Down
187 changes: 121 additions & 66 deletions functional_tests/functional_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,103 @@ func deployChartsAndApps(t *testing.T) {
require.NoError(t, err)
dynamicClient, err := dynamic.NewForConfig(kubeConfig)
require.NoError(t, err)
decode := scheme.Codecs.UniversalDeserializer().Decode

// load up Prometheus PodMonitor and ServiceMonitor CRDs:
stream, err := os.ReadFile(filepath.Join(testDir, manifestsDir, "prometheus_operator_crds.yaml"))
require.NoError(t, err)
sch := k8sruntime.NewScheme()

for _, resourceYAML := range strings.Split(string(stream), "---") {
if len(resourceYAML) == 0 {
continue
}

obj, groupVersionKind, err := decode(
[]byte(resourceYAML),
nil,
nil)
require.NoError(t, err)
if groupVersionKind.Group == "apiextensions.k8s.io" &&
groupVersionKind.Version == "v1" &&
groupVersionKind.Kind == "CustomResourceDefinition" {
crd := obj.(*appextensionsv1.CustomResourceDefinition)
apiExtensions := extensionsClient.ApiextensionsV1().CustomResourceDefinitions()
crd, err := apiExtensions.Create(context.Background(), crd, metav1.CreateOptions{})
require.NoError(t, err)
t.Logf("Deployed CRD %s", crd.Name)
for _, version := range crd.Spec.Versions {
sch.AddKnownTypeWithName(
schema.GroupVersionKind{
Group: crd.Spec.Group,
Version: version.Name,
Kind: crd.Spec.Names.Kind,
},
&unstructured.Unstructured{},
)
}
}
}

// deploy the operator CRDs
stream, err = os.ReadFile(filepath.Join(testDir, manifestsDir, "operator_crds.yaml"))
require.NoError(t, err)

for _, resourceYAML := range strings.Split(string(stream), "---") {
if len(resourceYAML) == 0 {
continue
}

obj, groupVersionKind, err := decode(
[]byte(resourceYAML),
nil,
nil)
require.NoError(t, err)
if groupVersionKind.Group == "apiextensions.k8s.io" &&
groupVersionKind.Version == "v1" &&
groupVersionKind.Kind == "CustomResourceDefinition" {
crd := obj.(*appextensionsv1.CustomResourceDefinition)
apiExtensions := extensionsClient.ApiextensionsV1().CustomResourceDefinitions()
crd, err := apiExtensions.Create(context.Background(), crd, metav1.CreateOptions{})
require.NoError(t, err)
t.Logf("Deployed CRD %s", crd.Name)
for _, version := range crd.Spec.Versions {
sch.AddKnownTypeWithName(
schema.GroupVersionKind{
Group: crd.Spec.Group,
Version: version.Name,
Kind: crd.Spec.Names.Kind,
},
&unstructured.Unstructured{},
)
}
}
}

codecs := serializer.NewCodecFactory(sch)
crdDecode := codecs.UniversalDeserializer().Decode
// Prometheus pod monitor
stream, err = os.ReadFile(filepath.Join(testDir, manifestsDir, "pod_monitor.yaml"))
require.NoError(t, err)

podMonitor, _, err := crdDecode(stream, nil, nil)
require.NoError(t, err)
g := schema.GroupVersionResource{
Group: "monitoring.coreos.com",
Version: "v1",
Resource: "podmonitors",
}
// CRDs sometimes take time to register. We retry deploying the pod monitor until such a time all CRDs are deployed.
require.EventuallyWithT(t, func(tt *assert.CollectT) {
_, err = dynamicClient.Resource(g).Namespace("default").Create(context.Background(), podMonitor.(*unstructured.Unstructured), metav1.CreateOptions{})
if err != nil {
_, err2 := dynamicClient.Resource(g).Namespace("default").Update(context.Background(), podMonitor.(*unstructured.Unstructured), metav1.UpdateOptions{})
assert.NoError(tt, err2)
if err2 != nil {
assert.NoError(tt, err)
}
}
}, 1*time.Minute, 5*time.Second)

chartPath := filepath.Join("..", "helm-charts", "splunk-otel-collector")
chart, err := loader.Load(chartPath)
Expand Down Expand Up @@ -207,9 +304,8 @@ func deployChartsAndApps(t *testing.T) {

deployments := client.AppsV1().Deployments("default")

decode := scheme.Codecs.UniversalDeserializer().Decode
// NodeJS test app
stream, err := os.ReadFile(filepath.Join(testDir, "nodejs", "deployment.yaml"))
stream, err = os.ReadFile(filepath.Join(testDir, "nodejs", "deployment.yaml"))
require.NoError(t, err)
deployment, _, err := decode(stream, nil, nil)
require.NoError(t, err)
Expand Down Expand Up @@ -260,68 +356,6 @@ func deployChartsAndApps(t *testing.T) {
require.NoError(t, err)
}
}
// load up Prometheus PodMonitor and ServiceMonitor CRDs:
stream, err = os.ReadFile(filepath.Join(testDir, manifestsDir, "prometheus_operator_crds.yaml"))
require.NoError(t, err)
sch := k8sruntime.NewScheme()
var crds []*appextensionsv1.CustomResourceDefinition

for _, resourceYAML := range strings.Split(string(stream), "---") {
if len(resourceYAML) == 0 {
continue
}

obj, groupVersionKind, err := decode(
[]byte(resourceYAML),
nil,
nil)
require.NoError(t, err)
if groupVersionKind.Group == "apiextensions.k8s.io" &&
groupVersionKind.Version == "v1" &&
groupVersionKind.Kind == "CustomResourceDefinition" {
crd := obj.(*appextensionsv1.CustomResourceDefinition)
crds = append(crds, crd)
apiExtensions := extensionsClient.ApiextensionsV1().CustomResourceDefinitions()
crd, err := apiExtensions.Create(context.Background(), crd, metav1.CreateOptions{})
require.NoError(t, err)
t.Logf("Deployed CRD %s", crd.Name)
for _, version := range crd.Spec.Versions {
sch.AddKnownTypeWithName(
schema.GroupVersionKind{
Group: crd.Spec.Group,
Version: version.Name,
Kind: crd.Spec.Names.Kind,
},
&unstructured.Unstructured{},
)
}
}
}

codecs := serializer.NewCodecFactory(sch)
crdDecode := codecs.UniversalDeserializer().Decode
// Prometheus pod monitor
stream, err = os.ReadFile(filepath.Join(testDir, manifestsDir, "pod_monitor.yaml"))
require.NoError(t, err)

podMonitor, _, err := crdDecode(stream, nil, nil)
require.NoError(t, err)
g := schema.GroupVersionResource{
Group: "monitoring.coreos.com",
Version: "v1",
Resource: "podmonitors",
}
// CRDs sometimes take time to register. We retry deploying the pod monitor until such a time all CRDs are deployed.
require.EventuallyWithT(t, func(tt *assert.CollectT) {
_, err = dynamicClient.Resource(g).Namespace("default").Create(context.Background(), podMonitor.(*unstructured.Unstructured), metav1.CreateOptions{})
if err != nil {
_, err2 := dynamicClient.Resource(g).Namespace("default").Update(context.Background(), podMonitor.(*unstructured.Unstructured), metav1.UpdateOptions{})
assert.NoError(tt, err2)
if err2 != nil {
assert.NoError(tt, err)
}
}
}, 1*time.Minute, 5*time.Second)

// Service
stream, err = os.ReadFile(filepath.Join(testDir, manifestsDir, "service.yaml"))
Expand Down Expand Up @@ -481,7 +515,6 @@ func teardown(t *testing.T) {

crdstream, err := os.ReadFile(filepath.Join(testDir, manifestsDir, "prometheus_operator_crds.yaml"))
require.NoError(t, err)
var crds []*appextensionsv1.CustomResourceDefinition
for _, resourceYAML := range strings.Split(string(crdstream), "---") {
if len(resourceYAML) == 0 {
continue
Expand All @@ -496,7 +529,29 @@ func teardown(t *testing.T) {
groupVersionKind.Version == "v1" &&
groupVersionKind.Kind == "CustomResourceDefinition" {
crd := obj.(*appextensionsv1.CustomResourceDefinition)
crds = append(crds, crd)
apiExtensions := extensionsClient.ApiextensionsV1().CustomResourceDefinitions()
_ = apiExtensions.Delete(context.Background(), crd.Name, metav1.DeleteOptions{
GracePeriodSeconds: &waitTime,
})
}
}

crdstream, err = os.ReadFile(filepath.Join(testDir, manifestsDir, "operator_crds.yaml"))
require.NoError(t, err)
for _, resourceYAML := range strings.Split(string(crdstream), "---") {
if len(resourceYAML) == 0 {
continue
}

obj, groupVersionKind, err := decode(
[]byte(resourceYAML),
nil,
nil)
require.NoError(t, err)
if groupVersionKind.Group == "apiextensions.k8s.io" &&
groupVersionKind.Version == "v1" &&
groupVersionKind.Kind == "CustomResourceDefinition" {
crd := obj.(*appextensionsv1.CustomResourceDefinition)
apiExtensions := extensionsClient.ApiextensionsV1().CustomResourceDefinitions()
_ = apiExtensions.Delete(context.Background(), crd.Name, metav1.DeleteOptions{
GracePeriodSeconds: &waitTime,
Expand Down
Loading

0 comments on commit cc733c2

Please sign in to comment.