Skip to content

Commit

Permalink
create dedicated ingress type
Browse files Browse the repository at this point in the history
Signed-off-by: Benedikt Bongartz <bongartz@klimlive.de>
  • Loading branch information
frzifus committed Oct 10, 2022
1 parent 5cd9c95 commit 5689a0d
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 10 deletions.
26 changes: 26 additions & 0 deletions apis/v1alpha1/ingress_type.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Copyright The OpenTelemetry 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 v1alpha1

type (
// IngressType represents how a collector should be exposed (ingress vs route).
// +kubebuilder:validation:Enum=ingress
IngressType string
)

const (
// IngressTypeNginx specifies that an ingress entry should be created.
IngressTypeNginx IngressType = "ingress"
)
3 changes: 2 additions & 1 deletion apis/v1alpha1/opentelemetrycollector_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,10 @@ import (
type Ingress struct {
// Type default value is: none
// Supported types are: ingress
Type string `json:"type,omitempty"`
Type IngressType `json:"type,omitempty"`

// Hostname by which the ingress proxy can be reached.
// +optional
Hostname string `json:"hostname,omitempty"`

// Annotations to add to ingress.
Expand Down
6 changes: 2 additions & 4 deletions apis/v1alpha1/opentelemetrycollector_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ package v1alpha1

import (
"fmt"
"strings"

"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/util/validation"
Expand Down Expand Up @@ -167,10 +166,9 @@ func (r *OpenTelemetryCollector) validateCRDSpec() error {

}

mode := strings.ToLower(string(r.Spec.Mode))
if r.Spec.Ingress.Type != "" && (mode == "deployment" || mode == "daemonset" || mode == "statefulset") {
if r.Spec.Ingress.Type == IngressTypeNginx && (r.Spec.Mode != ModeDeployment || r.Spec.Mode == ModeDaemonSet || r.Spec.Mode == ModeStatefulSet) {
return fmt.Errorf("the OptenTelemetry Spec Ingress configuiration is incorrect. Ingress can only be used in combination with the modes: %s, %s, %s",
"deployment", "daemenset", "statefulset",
ModeDeployment, ModeDaemonSet, ModeStatefulSet,
)
}

Expand Down
15 changes: 15 additions & 0 deletions apis/v1alpha1/opentelemetrycollector_webhook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package v1alpha1

import (
"fmt"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -317,6 +318,20 @@ func TestOTELColValidatingWebhook(t *testing.T) {
},
expectedErr: "targetCPUUtilization should be greater than 0 and less than 100",
},
{
name: "invalid deployment mode incompabible with ingress settings",
otelcol: OpenTelemetryCollector{
Spec: OpenTelemetryCollectorSpec{
Mode: ModeSidecar,
Ingress: Ingress{
Type: IngressTypeNginx,
},
},
},
expectedErr: fmt.Sprintf("Ingress can only be used in combination with the modes: %s, %s, %s",
ModeDeployment, ModeDaemonSet, ModeStatefulSet,
),
},
}

for _, test := range tests {
Expand Down
2 changes: 1 addition & 1 deletion pkg/collector/reconcile/ingress.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import (
)

func desiredIngresses(_ context.Context, params Params) *networkingv1.Ingress {
if params.Instance.Spec.Ingress.Type != "ingress" {
if params.Instance.Spec.Ingress.Type != v1alpha1.IngressTypeNginx {
return nil
}
svcTarget := naming.Service(params.Instance)
Expand Down
8 changes: 4 additions & 4 deletions pkg/collector/reconcile/ingress_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func TestDesiredIngresses(t *testing.T) {
Instance: v1alpha1.OpenTelemetryCollector{
Spec: v1alpha1.OpenTelemetryCollectorSpec{
Ingress: v1alpha1.Ingress{
Type: "unknown",
Type: v1alpha1.IngressType("unknown"),
},
},
},
Expand All @@ -62,7 +62,7 @@ func TestDesiredIngresses(t *testing.T) {
Spec: v1alpha1.OpenTelemetryCollectorSpec{
Config: "!!!",
Ingress: v1alpha1.Ingress{
Type: "ingress",
Type: v1alpha1.IngressTypeNginx,
},
},
},
Expand All @@ -81,7 +81,7 @@ func TestDesiredIngresses(t *testing.T) {
Spec: v1alpha1.OpenTelemetryCollectorSpec{
Config: "---",
Ingress: v1alpha1.Ingress{
Type: "ingress",
Type: v1alpha1.IngressTypeNginx,
},
},
},
Expand All @@ -104,7 +104,7 @@ func TestDesiredIngresses(t *testing.T) {

params.Instance.Namespace = ns
params.Instance.Spec.Ingress = v1alpha1.Ingress{
Type: "ingress",
Type: v1alpha1.IngressTypeNginx,
Hostname: hostname,
Annotations: map[string]string{"some.key": "some.value"},
}
Expand Down

0 comments on commit 5689a0d

Please sign in to comment.