Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
Signed-off-by: Kristof Gyuracz <[email protected]>
  • Loading branch information
kristofgyuracz committed Feb 22, 2024
1 parent ee8f91a commit 30cdbd2
Show file tree
Hide file tree
Showing 7 changed files with 327 additions and 40 deletions.
8 changes: 8 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,11 @@ Dockerfile.cross
*.swp
*.swo
*~

crddir

.licensei.cache

.DS_Store

go.work.sum
10 changes: 7 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,11 @@ help: ## Display this help.

.PHONY: manifests
manifests: controller-gen ## Generate WebhookConfiguration, ClusterRole and CustomResourceDefinition objects.
$(CONTROLLER_GEN) rbac:roleName=manager-role crd webhook paths="./..." output:crd:artifacts:config=config/crd/bases
$(CONTROLLER_GEN) rbac:roleName=manager-role crd webhook paths="./api/..." output:crd:artifacts:config=config/crd/bases

.PHONY: generate
generate: controller-gen ## Generate code containing DeepCopy, DeepCopyInto, and DeepCopyObject method implementations.
$(CONTROLLER_GEN) object:headerFile="hack/boilerplate.go.txt" paths="./..."
$(CONTROLLER_GEN) object:headerFile="hack/boilerplate.go.txt" paths="./api/..."

.PHONY: fmt
fmt: ## Run go fmt against code.
Expand Down Expand Up @@ -190,8 +190,12 @@ $(CONTROLLER_GEN): $(LOCALBIN)
test -s $(LOCALBIN)/controller-gen && $(LOCALBIN)/controller-gen --version | grep -q $(CONTROLLER_TOOLS_VERSION) || \
GOBIN=$(LOCALBIN) go install sigs.k8s.io/controller-tools/cmd/controller-gen@$(CONTROLLER_TOOLS_VERSION)

# Download CRDs for envtest
crddir/github.com/open-telemetry/opentelemetry-operator:
git clone --depth 1 --branch v0.93.0 https://github.com/open-telemetry/opentelemetry-operator.git crddir/github.com/open-telemetry/opentelemetry-operator

.PHONY: envtest
envtest: $(ENVTEST) ## Download envtest-setup locally if necessary.
envtest: $(ENVTEST) crddir/github.com/open-telemetry/opentelemetry-operator ## Download envtest-setup locally if necessary.
$(ENVTEST): $(LOCALBIN)
test -s $(LOCALBIN)/setup-envtest || GOBIN=$(LOCALBIN) go install sigs.k8s.io/controller-runtime/tools/setup-envtest@latest

Expand Down
2 changes: 1 addition & 1 deletion hack/boilerplate.go.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright © 2023 Kube logging authors
// Copyright © 2024 Kube logging authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down
9 changes: 5 additions & 4 deletions internal/controller/telemetry/collector_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,10 +209,11 @@ func (r *CollectorReconciler) Reconcile(ctx context.Context, req ctrl.Request) (
Namespace: collector.Spec.ControlNamespace,
},
Spec: otelv1alpha1.OpenTelemetryCollectorSpec{
Config: otelConfig,
Mode: otelv1alpha1.ModeDaemonSet,
Image: "ghcr.io/axoflow/axoflow-otel-collector/axoflow-otel-collector:0.94.0",
ServiceAccount: saName.Name,
UpgradeStrategy: "none",
Config: otelConfig,
Mode: otelv1alpha1.ModeDaemonSet,
Image: "ghcr.io/axoflow/axoflow-otel-collector/axoflow-otel-collector:0.94.0",
ServiceAccount: saName.Name,
VolumeMounts: []apiv1.VolumeMount{
{
Name: "varlog",
Expand Down
193 changes: 166 additions & 27 deletions internal/controller/telemetry/controller_integration_test.go
Original file line number Diff line number Diff line change
@@ -1,46 +1,87 @@
// Copyright © 2024 Kube logging authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package telemetry

import (
"context"
"os"
"path"
"time"

"github.com/kube-logging/telemetry-controller/api/telemetry/v1alpha1"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
otelv1alpha1 "github.com/open-telemetry/opentelemetry-operator/apis/v1alpha1"
v1 "k8s.io/api/core/v1"
rbacv1 "k8s.io/api/rbac/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
//+kubebuilder:scaffold:imports
)

var _ = Describe("IMPS controller", func() {
// Define utility constants for object names and testing timeouts/durations and intervals.
var _ = Describe("Telemetry controller integration test", func() {
const (
ImpsName = "test-imps"
ImpsNamespace = "source-ns"
SourceSecretName = "source-secret-1"
TargetSecretName = "target-secret"
TargetSecretNamespace = "target-ns"

timeout = time.Second * 20
timeout = time.Second * 5
interval = time.Millisecond * 250
)

Describe("When creating an IMPS resource", Ordered, func() {
It("Secret should be created in the annotated namespace", func() {
Context("Deploying a telemetry pipeline", Ordered, func() {
It("Namespace should exist beforehand ", func() {
namespaces := []v1.Namespace{
{
ObjectMeta: metav1.ObjectMeta{
Name: "tenant-1-workload",
},
},
{
ObjectMeta: metav1.ObjectMeta{
Name: "tenant-1-ctrl",
},
},
{
ObjectMeta: metav1.ObjectMeta{
Name: "tenant-2-all",
},
},
{
ObjectMeta: metav1.ObjectMeta{
Name: "collector",
},
},
}

for _, namespace := range namespaces {
Expect(k8sClient.Create(ctx, &namespace)).Should(Succeed())
}

})

It("Subscriptions should be created in the annotated namespaces", func() {
ctx := context.Background()

subscriptions := []v1alpha1.Subscription{
{
ObjectMeta: metav1.ObjectMeta{
Name: "subscription-example-1",
Namespace: "example-tenant-ns",
Namespace: "tenant-1-ctrl",
},
Spec: v1alpha1.SubscriptionSpec{
OTTL: "route()",
Outputs: []v1alpha1.NamespacedName{
{
Name: "otlp-test-output",
Name: "otlp-test-output-1",
Namespace: "collector",
},
},
Expand All @@ -49,7 +90,7 @@ var _ = Describe("IMPS controller", func() {
{
ObjectMeta: metav1.ObjectMeta{
Name: "subscription-example-2",
Namespace: "example-tenant-ns",
Namespace: "tenant-2-all",
},
Spec: v1alpha1.SubscriptionSpec{
OTTL: "route()",
Expand All @@ -66,34 +107,70 @@ var _ = Describe("IMPS controller", func() {
for _, subscription := range subscriptions {
Expect(k8sClient.Create(ctx, &subscription)).Should(Succeed())
}
})

It("Tenants should be created", func() {
tenants := []v1alpha1.Tenant{
{
ObjectMeta: metav1.ObjectMeta{
Name: "example-tenant",
Name: "tenant-1",
},
Spec: v1alpha1.TenantSpec{
SubscriptionNamespaceSelectors: []metav1.LabelSelector{
{
MatchLabels: map[string]string{
"nsSelector": "tenant-1-ctrl",
},
},
},
LogSourceNamespaceSelectors: []metav1.LabelSelector{
{
MatchLabels: map[string]string{
"nsSelector": "tenant-1-workload",
},
},
},
},
Status: v1alpha1.TenantStatus{
Subscriptions: []string{"asd", "bsd"},
LogSourceNamespaces: []string{},
Collector: "asd",
},
},
{
ObjectMeta: metav1.ObjectMeta{
Name: "tenant-2",
},
Spec: v1alpha1.TenantSpec{
SubscriptionNamespaceSelectors: []metav1.LabelSelector{
{
MatchLabels: map[string]string{
"nsSelector": "example-tenant",
"nsSelector": "tenant-2-all",
},
},
},
LogSourceNamespaceSelectors: []metav1.LabelSelector{
{
MatchLabels: map[string]string{
"nsSelector": "example-tenant",
"nsSelector": "tenant-2-all",
},
},
},
},
Status: v1alpha1.TenantStatus{
Subscriptions: []string{"asd", "bsd"},
LogSourceNamespaces: []string{},
Collector: "asd",
},
},
}

for _, tenant := range tenants {
Expect(k8sClient.Create(ctx, &tenant)).Should(Succeed())
}
})

It("Outputs should be created", func() {

outputs := []v1alpha1.OtelOutput{
{
Expand All @@ -103,7 +180,7 @@ var _ = Describe("IMPS controller", func() {
},
Spec: v1alpha1.OtelOutputSpec{
OTLP: v1alpha1.OTLPgrpc{
GRPCClientSettings: v1alpha1.GRPCClientSettings{
ClientConfig: v1alpha1.ClientConfig{
Endpoint: "receiver-collector.example-tenant-ns.svc.cluster.local:4317",
TLSSetting: v1alpha1.TLSClientSetting{
Insecure: true,
Expand All @@ -119,7 +196,7 @@ var _ = Describe("IMPS controller", func() {
},
Spec: v1alpha1.OtelOutputSpec{
OTLP: v1alpha1.OTLPgrpc{
GRPCClientSettings: v1alpha1.GRPCClientSettings{
ClientConfig: v1alpha1.ClientConfig{
Endpoint: "receiver-collector.example-tenant-ns.svc.cluster.local:4317",
TLSSetting: v1alpha1.TLSClientSetting{
Insecure: true,
Expand All @@ -133,19 +210,81 @@ var _ = Describe("IMPS controller", func() {
for _, output := range outputs {
Expect(k8sClient.Create(ctx, &output)).Should(Succeed())
}
})

It("Collector should be created", func() {
collector := v1alpha1.Collector{
ObjectMeta: metav1.ObjectMeta{
Name: "example-collector",
},
Spec: v1alpha1.CollectorSpec{
TenantSelector: metav1.LabelSelector{
MatchLabels: map[string]string{},
MatchExpressions: []metav1.LabelSelectorRequirement{},
},
ControlNamespace: "collector",
},
}
Expect(k8sClient.Create(ctx, &collector)).Should(Succeed())
})

})
When("The controller reconciles based on deployed resources", Ordered, func() {

It("RBAC resources should be reconciled by controller", func() {

createdServiceAccount := &v1.ServiceAccount{}

Eventually(func() bool {
err := k8sClient.Get(ctx, types.NamespacedName{Namespace: "collector", Name: "example-collector-sa"}, createdServiceAccount)
return err == nil
}, timeout, interval).Should(BeTrue())

Expect(createdServiceAccount.OwnerReferences[0].Name).To(Equal("example-collector"))

createdClusterRole := &rbacv1.ClusterRole{}

Eventually(func() bool {
err := k8sClient.Get(ctx, types.NamespacedName{Name: "example-collector-pod-association-reader"}, createdClusterRole)
return err == nil
}, timeout, interval).Should(BeTrue())

Expect(createdClusterRole.OwnerReferences[0].Name).To(Equal("example-collector"))

secretLookupKey := types.NamespacedName{Name: TargetSecretName, Namespace: TargetSecretNamespace}
createdSecret := &v1.Secret{}
createdClusterRoleBinding := &rbacv1.ClusterRoleBinding{}

By("TODO")
Eventually(func() bool {
err := k8sClient.Get(ctx, secretLookupKey, createdSecret)
if err != nil { //nolint:gosimple
return false
}
err := k8sClient.Get(ctx, types.NamespacedName{Name: "example-collector-crb"}, createdClusterRoleBinding)
return err == nil
}, timeout, interval).Should(BeTrue())

Expect(createdClusterRoleBinding.OwnerReferences[0].Name).To(Equal("example-collector"))

})

It("OpentelemetryCollector resource should be reconciled by controller", func() {

createdOtelCollector := &otelv1alpha1.OpenTelemetryCollector{}

expectedConfig, err := os.ReadFile(path.Join("envtest_testdata", "config.yaml"))
Expect(err).NotTo(HaveOccurred())
exptectedOtelCollector := &otelv1alpha1.OpenTelemetryCollector{
ObjectMeta: metav1.ObjectMeta{
Name: "otelcollector-example-collector",
Namespace: "collector",
},
Spec: otelv1alpha1.OpenTelemetryCollectorSpec{
Config: string(expectedConfig),
},
}

return true
Eventually(func() bool {
err := k8sClient.Get(ctx, types.NamespacedName{Namespace: "collector", Name: "otelcollector-example-collector"}, createdOtelCollector)
return err == nil
}, timeout, interval).Should(BeTrue())

Expect(createdOtelCollector.Spec.Config).To(Equal(exptectedOtelCollector.Spec.Config))

})
})
})
Loading

0 comments on commit 30cdbd2

Please sign in to comment.