Skip to content

Commit

Permalink
feat: add otel collector uri flag
Browse files Browse the repository at this point in the history
Signed-off-by: Leopold Jacquot <[email protected]>
  • Loading branch information
L3o-pold committed Aug 3, 2023
1 parent e740068 commit e4d9fde
Show file tree
Hide file tree
Showing 13 changed files with 155 additions and 8 deletions.
8 changes: 8 additions & 0 deletions apis/core/v1alpha1/flagsourceconfiguration_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,10 @@ type FlagSourceConfigurationSpec struct {
// DebugLogging defines whether to enable --debug flag of flagd sidecar. Default false (disabled).
// +optional
DebugLogging *bool `json:"debugLogging"`

// OtelCollectorUri defines whether to enable --otel-collector-uri flag of flagd sidecar. Default false (disabled).
// +optional
OtelCollectorUri string `json:"otelCollectorUri"`
}

type Source struct {
Expand Down Expand Up @@ -204,6 +208,7 @@ func NewFlagSourceConfigurationSpec() (*FlagSourceConfigurationSpec, error) {
LogFormat: defaultLogFormat,
RolloutOnChange: nil,
DebugLogging: utils.FalseVal(),
OtelCollectorUri: "",
}

// set default value derived from constant default
Expand Down Expand Up @@ -318,6 +323,9 @@ func (fc *FlagSourceConfigurationSpec) Merge(new *FlagSourceConfigurationSpec) {
if new.DebugLogging != nil {
fc.DebugLogging = new.DebugLogging
}
if new.OtelCollectorUri != "" {
fc.OtelCollectorUri = new.OtelCollectorUri
}
}

func (fc *FlagSourceConfigurationSpec) ToEnvVars() []corev1.EnvVar {
Expand Down
5 changes: 5 additions & 0 deletions apis/core/v1alpha1/flagsourceconfiguration_types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ func Test_FLagSourceConfiguration_Merge(t *testing.T) {
RolloutOnChange: utils.TrueVal(),
ProbesEnabled: utils.TrueVal(),
DebugLogging: utils.TrueVal(),
OtelCollectorUri: "",
},
}

Expand Down Expand Up @@ -159,6 +160,7 @@ func Test_FLagSourceConfiguration_Merge(t *testing.T) {
RolloutOnChange: utils.TrueVal(),
ProbesEnabled: utils.TrueVal(),
DebugLogging: utils.TrueVal(),
OtelCollectorUri: "",
},
}, ff_old)

Expand Down Expand Up @@ -193,6 +195,7 @@ func Test_FLagSourceConfiguration_Merge(t *testing.T) {
RolloutOnChange: utils.FalseVal(),
ProbesEnabled: utils.FalseVal(),
DebugLogging: utils.FalseVal(),
OtelCollectorUri: "",
},
}

Expand Down Expand Up @@ -245,6 +248,7 @@ func Test_FLagSourceConfiguration_Merge(t *testing.T) {
RolloutOnChange: utils.FalseVal(),
ProbesEnabled: utils.FalseVal(),
DebugLogging: utils.FalseVal(),
OtelCollectorUri: "",
},
}, ff_old)
}
Expand Down Expand Up @@ -281,6 +285,7 @@ func Test_FLagSourceConfiguration_NewFlagSourceConfigurationSpec(t *testing.T) {
LogFormat: "val5",
ProbesEnabled: utils.TrueVal(),
DebugLogging: utils.FalseVal(),
OtelCollectorUri: "",
}, fs)

//error paths
Expand Down
1 change: 1 addition & 0 deletions apis/core/v1alpha2/flagsourceconfiguration_conversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ func (src *FlagSourceConfiguration) ConvertTo(dstRaw conversion.Hub) error {
DefaultSyncProvider: v1alpha1.SyncProviderType(src.Spec.DefaultSyncProvider),
ProbesEnabled: src.Spec.ProbesEnabled,
DebugLogging: utils.FalseVal(),
OtelCollectorUri: src.Spec.OtelCollectorUri,
}
return nil
}
Expand Down
2 changes: 2 additions & 0 deletions apis/core/v1alpha2/flagsourceconfiguration_conversion_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ func TestFlagSourceConfiguration_ConvertFrom(t *testing.T) {
RolloutOnChange: &tt,
SyncProviderArgs: []string{"provider", "arg"},
DebugLogging: utils.FalseVal(),
OtelCollectorUri: "",
},
},
wantErr: false,
Expand Down Expand Up @@ -155,6 +156,7 @@ func TestFlagSourceConfiguration_ConvertTo(t *testing.T) {
RolloutOnChange: nil,
SyncProviderArgs: []string{"provider", "arg"},
DebugLogging: utils.FalseVal(),
OtelCollectorUri: "",
},
},
},
Expand Down
4 changes: 4 additions & 0 deletions apis/core/v1alpha2/flagsourceconfiguration_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ type FlagSourceConfigurationSpec struct {
// ProbesEnabled defines whether to enable liveness and readiness probes of flagd sidecar. Default true (enabled).
// +optional
ProbesEnabled *bool `json:"probesEnabled"`

// OtelCollectorUri defines whether to enable --otel-collector-uri flag of flagd sidecar. Default false (disabled).
// +optional
OtelCollectorUri string `json:"otelCollectorUri"`
}

// FlagSourceConfigurationStatus defines the observed state of FlagSourceConfiguration
Expand Down
2 changes: 2 additions & 0 deletions apis/core/v1alpha3/flagsourceconfiguration_conversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ func (src *FlagSourceConfiguration) ConvertTo(dstRaw conversion.Hub) error {
RolloutOnChange: src.Spec.RolloutOnChange,
ProbesEnabled: src.Spec.ProbesEnabled,
DebugLogging: src.Spec.DebugLogging,
OtelCollectorUri: src.Spec.OtelCollectorUri,
}
return nil
}
Expand Down Expand Up @@ -106,6 +107,7 @@ func (dst *FlagSourceConfiguration) ConvertFrom(srcRaw conversion.Hub) error {
RolloutOnChange: src.Spec.RolloutOnChange,
ProbesEnabled: src.Spec.ProbesEnabled,
DebugLogging: src.Spec.DebugLogging,
OtelCollectorUri: src.Spec.OtelCollectorUri,
}
return nil
}
2 changes: 2 additions & 0 deletions apis/core/v1alpha3/flagsourceconfiguration_conversion_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ func TestFlagSourceConfiguration_ConvertFrom(t *testing.T) {
EnvVarPrefix: "pre",
RolloutOnChange: &tt,
DebugLogging: utils.FalseVal(),
OtelCollectorUri: "",
},
},
wantErr: false,
Expand Down Expand Up @@ -220,6 +221,7 @@ func TestFlagSourceConfiguration_ConvertTo(t *testing.T) {
EnvVarPrefix: "pre",
RolloutOnChange: &tt,
DebugLogging: utils.FalseVal(),
OtelCollectorUri: "",
},
},
},
Expand Down
4 changes: 4 additions & 0 deletions apis/core/v1alpha3/flagsourceconfiguration_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,10 @@ type FlagSourceConfigurationSpec struct {
// DebugLogging defines whether to enable --debug flag of flagd sidecar. Default false (disabled).
// +optional
DebugLogging *bool `json:"debugLogging"`

// OtelCollectorUri defines whether to enable --otel-collector-uri flag of flagd sidecar. Default false (disabled).
// +optional
OtelCollectorUri string `json:"otelCollectorUri"`
}

type Source struct {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,15 @@ spec:
C_IDENTIFIER.
type: string
value:
description: 'Variable references. More info: https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.20/#envvar-v1-core '
description: 'Variable references $(VAR_NAME) are expanded
using the previously defined environment variables in
the container and any service environment variables. If
a variable cannot be resolved, the reference in the input
string will be unchanged. Double $$ are reduced to a single
$, which allows for escaping the $(VAR_NAME) syntax: i.e.
"$$(VAR_NAME)" will produce the string literal "$(VAR_NAME)".
Escaped references will never be expanded, regardless
of whether the variable exists or not. Defaults to "".'
type: string
valueFrom:
description: Source for the environment variable's value.
Expand Down Expand Up @@ -330,7 +338,15 @@ spec:
C_IDENTIFIER.
type: string
value:
description: 'Variable references. More info: https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.20/#envvar-v1-core'
description: 'Variable references $(VAR_NAME) are expanded
using the previously defined environment variables in
the container and any service environment variables. If
a variable cannot be resolved, the reference in the input
string will be unchanged. Double $$ are reduced to a single
$, which allows for escaping the $(VAR_NAME) syntax: i.e.
"$$(VAR_NAME)" will produce the string literal "$(VAR_NAME)".
Escaped references will never be expanded, regardless
of whether the variable exists or not. Defaults to "".'
type: string
valueFrom:
description: Source for the environment variable's value.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,15 @@ spec:
description: Name of the environment variable. Must be a C_IDENTIFIER.
type: string
value:
description: 'Variable references. More info: https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.20/#envvar-v1-core'
description: 'Variable references $(VAR_NAME) are expanded using
the previously defined environment variables in the container
and any service environment variables. If a variable cannot
be resolved, the reference in the input string will be unchanged.
Double $$ are reduced to a single $, which allows for escaping
the $(VAR_NAME) syntax: i.e. "$$(VAR_NAME)" will produce the
string literal "$(VAR_NAME)". Escaped references will never
be expanded, regardless of whether the variable exists or
not. Defaults to "".'
type: string
valueFrom:
description: Source for the environment variable's value. Cannot
Expand Down Expand Up @@ -169,6 +177,10 @@ spec:
to 8014
format: int32
type: integer
otelCollectorUri:
description: OtelCollectorUri defines whether to enable --otel-collector-uri
flag of flagd sidecar. Default false (disabled).
type: string
port:
description: Port defines the port to listen on, defaults to 8013
format: int32
Expand Down Expand Up @@ -284,6 +296,10 @@ spec:
to 8013
format: int32
type: integer
otelCollectorUri:
description: OtelCollectorUri defines whether to enable --otel-collector-uri
flag of flagd sidecar. Default false (disabled).
type: string
port:
description: Port defines the port to listen on, defaults to 8014
format: int32
Expand Down Expand Up @@ -360,7 +376,15 @@ spec:
description: Name of the environment variable. Must be a C_IDENTIFIER.
type: string
value:
description: 'Variable references. More info: https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.20/#envvar-v1-core'
description: 'Variable references $(VAR_NAME) are expanded using
the previously defined environment variables in the container
and any service environment variables. If a variable cannot
be resolved, the reference in the input string will be unchanged.
Double $$ are reduced to a single $, which allows for escaping
the $(VAR_NAME) syntax: i.e. "$$(VAR_NAME)" will produce the
string literal "$(VAR_NAME)". Escaped references will never
be expanded, regardless of whether the variable exists or
not. Defaults to "".'
type: string
valueFrom:
description: Source for the environment variable's value. Cannot
Expand Down Expand Up @@ -467,6 +491,10 @@ spec:
to 8014
format: int32
type: integer
otelCollectorUri:
description: OtelCollectorUri defines whether to enable --otel-collector-uri
flag of flagd sidecar. Default false (disabled).
type: string
port:
description: Port defines the port to listen on, defaults to 8013
format: int32
Expand Down
15 changes: 15 additions & 0 deletions controllers/common/flagd-injector.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,21 @@ func (fi *FlagdContainerInjector) InjectFlagd(
)
}

// set --otel-collector-uri flag if enabled
if flagSourceConfig.OtelCollectorUri != "" {
flagdContainer.Args = append(
flagdContainer.Args,
"--metrics-exporter",
"otel",
)

flagdContainer.Args = append(
flagdContainer.Args,
"--otel-collector-uri",
flagSourceConfig.OtelCollectorUri,
)
}

addFlagdContainer(podSpec, flagdContainer)

return nil
Expand Down
39 changes: 39 additions & 0 deletions controllers/common/flagd-injector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,45 @@ func TestFlagdContainerInjector_InjectDefaultSyncProvider_WithDebugLogging(t *te
require.Equal(t, expectedDeployment, deployment)
}

func TestFlagdContainerInjector_InjectDefaultSyncProvider_WithOtelCollectorUri(t *testing.T) {

namespace, fakeClient := initContainerInjectionTestEnv()

fi := &FlagdContainerInjector{
Client: fakeClient,
Logger: testr.New(t),
FlagdProxyConfig: getProxyConfig(),
FlagDResourceRequirements: getResourceRequirements(),
}

deployment := appsV1.Deployment{
ObjectMeta: metav1.ObjectMeta{
Name: "my-deployment",
Namespace: namespace,
},
Spec: appsV1.DeploymentSpec{},
}

flagSourceConfig := getFlagSourceConfigSpec()

flagSourceConfig.DefaultSyncProvider = v1alpha1.SyncProviderGrpc

flagSourceConfig.OtelCollectorUri = "localhost:4317"

flagSourceConfig.Sources = []v1alpha1.Source{{}}

err := fi.InjectFlagd(context.Background(), &deployment.ObjectMeta, &deployment.Spec.Template.Spec, flagSourceConfig)
require.Nil(t, err)

expectedDeployment := getExpectedDeployment(namespace)

expectedDeployment.Annotations = nil

expectedDeployment.Spec.Template.Spec.Containers[0].Args = []string{"start", "--sources", "[{\"uri\":\"\",\"provider\":\"grpc\"}]", "--metrics-exporter", "otel", "--otel-collector-uri", "localhost:4317"}

require.Equal(t, expectedDeployment, deployment)
}

func TestFlagdContainerInjector_InjectDefaultSyncProvider_WithSyncProviderArgs(t *testing.T) {

namespace, fakeClient := initContainerInjectionTestEnv()
Expand Down
29 changes: 25 additions & 4 deletions docs/crds.md
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ EnvVar represents an environment variable present in a Container.
<td><b>value</b></td>
<td>string</td>
<td>
Variable references. More info: https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.20/#envvar-v1-core <br/>
Variable references $(VAR&lowbar;NAME) are expanded using the previously defined environment variables in the container and any service environment variables. If a variable cannot be resolved, the reference in the input string will be unchanged. Double $$ are reduced to a single $, which allows for escaping the $(VAR&lowbar;NAME) syntax: i.e. "$$(VAR&lowbar;NAME)" will produce the string literal "$(VAR&lowbar;NAME)". Escaped references will never be expanded, regardless of whether the variable exists or not. Defaults to "".<br/>
</td>
<td>false</td>
</tr><tr>
Expand Down Expand Up @@ -710,6 +710,13 @@ FlagSourceConfigurationSpec defines the desired state of FlagSourceConfiguration
<i>Format</i>: int32<br/>
</td>
<td>false</td>
</tr><tr>
<td><b>otelCollectorUri</b></td>
<td>string</td>
<td>
OtelCollectorUri defines whether to enable --otel-collector-uri flag of flagd sidecar. Default false (disabled).<br/>
</td>
<td>false</td>
</tr><tr>
<td><b>port</b></td>
<td>integer</td>
Expand Down Expand Up @@ -854,7 +861,7 @@ EnvVar represents an environment variable present in a Container.
<td><b>value</b></td>
<td>string</td>
<td>
Variable references. More info: https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.20/#envvar-v1-core<br/>
Variable references $(VAR&lowbar;NAME) are expanded using the previously defined environment variables in the container and any service environment variables. If a variable cannot be resolved, the reference in the input string will be unchanged. Double $$ are reduced to a single $, which allows for escaping the $(VAR&lowbar;NAME) syntax: i.e. "$$(VAR&lowbar;NAME)" will produce the string literal "$(VAR&lowbar;NAME)". Escaped references will never be expanded, regardless of whether the variable exists or not. Defaults to "".<br/>
</td>
<td>false</td>
</tr><tr>
Expand Down Expand Up @@ -1323,7 +1330,7 @@ EnvVar represents an environment variable present in a Container.
<td><b>value</b></td>
<td>string</td>
<td>
Variable references. More info: https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.20/#envvar-v1-core<br/>
Variable references $(VAR&lowbar;NAME) are expanded using the previously defined environment variables in the container and any service environment variables. If a variable cannot be resolved, the reference in the input string will be unchanged. Double $$ are reduced to a single $, which allows for escaping the $(VAR&lowbar;NAME) syntax: i.e. "$$(VAR&lowbar;NAME)" will produce the string literal "$(VAR&lowbar;NAME)". Escaped references will never be expanded, regardless of whether the variable exists or not. Defaults to "".<br/>
</td>
<td>false</td>
</tr><tr>
Expand Down Expand Up @@ -1823,6 +1830,13 @@ FlagSourceConfigurationSpec defines the desired state of FlagSourceConfiguration
<i>Format</i>: int32<br/>
</td>
<td>false</td>
</tr><tr>
<td><b>otelCollectorUri</b></td>
<td>string</td>
<td>
OtelCollectorUri defines whether to enable --otel-collector-uri flag of flagd sidecar. Default false (disabled).<br/>
</td>
<td>false</td>
</tr><tr>
<td><b>port</b></td>
<td>integer</td>
Expand Down Expand Up @@ -2007,6 +2021,13 @@ FlagSourceConfigurationSpec defines the desired state of FlagSourceConfiguration
<i>Format</i>: int32<br/>
</td>
<td>false</td>
</tr><tr>
<td><b>otelCollectorUri</b></td>
<td>string</td>
<td>
OtelCollectorUri defines whether to enable --otel-collector-uri flag of flagd sidecar. Default false (disabled).<br/>
</td>
<td>false</td>
</tr><tr>
<td><b>port</b></td>
<td>integer</td>
Expand Down Expand Up @@ -2151,7 +2172,7 @@ EnvVar represents an environment variable present in a Container.
<td><b>value</b></td>
<td>string</td>
<td>
Variable references. More info: https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.20/#envvar-v1-core<br/>
Variable references $(VAR&lowbar;NAME) are expanded using the previously defined environment variables in the container and any service environment variables. If a variable cannot be resolved, the reference in the input string will be unchanged. Double $$ are reduced to a single $, which allows for escaping the $(VAR&lowbar;NAME) syntax: i.e. "$$(VAR&lowbar;NAME)" will produce the string literal "$(VAR&lowbar;NAME)". Escaped references will never be expanded, regardless of whether the variable exists or not. Defaults to "".<br/>
</td>
<td>false</td>
</tr><tr>
Expand Down

0 comments on commit e4d9fde

Please sign in to comment.