Skip to content

Commit

Permalink
Added sampling field to console frontend configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
OlivierCazade committed Feb 17, 2023
1 parent 6136a57 commit 0e1aa23
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 38 deletions.
7 changes: 7 additions & 0 deletions api/v1alpha1/flowcollector_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,13 @@ func (spec *FlowCollectorSpec) UseEBPF() bool { return spec.Agent.Type == Agent
func (spec *FlowCollectorSpec) UseIPFIX() bool { return spec.Agent.Type == AgentIPFIX }
func (spec *FlowCollectorSpec) UseKafka() bool { return spec.DeploymentModel == DeploymentModelKafka }

func (spec *FlowCollectorSpec) GetSampling() int {
if spec.UseEBPF() {
return int(*spec.Agent.EBPF.Sampling)
}
return int(spec.Agent.IPFIX.Sampling)
}

// Please notice that the FlowCollectorSpec's properties MUST redefine one of the default
// values to force the definition of the section when it is not provided by the manifest.
// This will cause that the remaining default fields will be set according to their definition.
Expand Down
7 changes: 7 additions & 0 deletions api/v1beta1/flowcollector_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,13 @@ func (spec *FlowCollectorSpec) UseEBPF() bool { return spec.Agent.Type == Agent
func (spec *FlowCollectorSpec) UseIPFIX() bool { return spec.Agent.Type == AgentIPFIX }
func (spec *FlowCollectorSpec) UseKafka() bool { return spec.DeploymentModel == DeploymentModelKafka }

func (spec *FlowCollectorSpec) GetSampling() int {
if spec.UseEBPF() {
return int(*spec.Agent.EBPF.Sampling)
}
return int(spec.Agent.IPFIX.Sampling)
}

// Please notice that the FlowCollectorSpec's properties MUST redefine one of the default
// values to force the definition of the section when it is not provided by the manifest.
// This will cause that the remaining default fields will be set according to their definition.
Expand Down
55 changes: 27 additions & 28 deletions controllers/consoleplugin/consoleplugin_objects.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,15 @@ const lokiCerts = "loki-certs"
const tokensPath = "/var/run/secrets/tokens/"

type builder struct {
namespace string
labels map[string]string
selector map[string]string
desired *flowslatest.FlowCollectorConsolePlugin
desiredLoki *flowslatest.FlowCollectorLoki
imageName string
cWatcher *watchers.CertificatesWatcher
namespace string
labels map[string]string
selector map[string]string
desired *flowslatest.FlowCollectorSpec
imageName string
cWatcher *watchers.CertificatesWatcher
}

func newBuilder(ns, imageName string, desired *flowslatest.FlowCollectorConsolePlugin, desiredLoki *flowslatest.FlowCollectorLoki, cWatcher *watchers.CertificatesWatcher) builder {
func newBuilder(ns, imageName string, desired *flowslatest.FlowCollectorSpec, cWatcher *watchers.CertificatesWatcher) builder {
version := helper.ExtractVersion(imageName)
return builder{
namespace: ns,
Expand All @@ -52,10 +51,9 @@ func newBuilder(ns, imageName string, desired *flowslatest.FlowCollectorConsoleP
selector: map[string]string{
"app": constants.PluginName,
},
desired: desired,
desiredLoki: desiredLoki,
imageName: imageName,
cWatcher: cWatcher,
desired: desired,
imageName: imageName,
cWatcher: cWatcher,
}
}

Expand All @@ -69,7 +67,7 @@ func (b *builder) consolePlugin() *osv1alpha1.ConsolePlugin {
Service: osv1alpha1.ConsolePluginService{
Name: constants.PluginName,
Namespace: b.namespace,
Port: b.desired.Port,
Port: b.desired.ConsolePlugin.Port,
BasePath: "/",
},
Proxy: []osv1alpha1.ConsolePluginProxy{{
Expand All @@ -79,7 +77,7 @@ func (b *builder) consolePlugin() *osv1alpha1.ConsolePlugin {
Service: osv1alpha1.ConsolePluginProxyServiceConfig{
Name: constants.PluginName,
Namespace: b.namespace,
Port: b.desired.Port,
Port: b.desired.ConsolePlugin.Port,
},
}},
},
Expand Down Expand Up @@ -134,7 +132,7 @@ func (b *builder) deployment(cmDigest string) *appsv1.Deployment {
Labels: b.labels,
},
Spec: appsv1.DeploymentSpec{
Replicas: &b.desired.Replicas,
Replicas: &b.desired.ConsolePlugin.Replicas,
Selector: &metav1.LabelSelector{
MatchLabels: b.selector,
},
Expand Down Expand Up @@ -216,12 +214,12 @@ func (b *builder) podTemplate(cmDigest string) *corev1.PodTemplateSpec {
},
}

args := buildArgs(b.desired, b.desiredLoki)
if b.desiredLoki != nil && b.desiredLoki.TLS.Enable && !b.desiredLoki.TLS.InsecureSkipVerify {
volumes, volumeMounts = helper.AppendCertVolumes(volumes, volumeMounts, &b.desiredLoki.TLS, lokiCerts, b.cWatcher)
args := buildArgs(&b.desired.ConsolePlugin, &b.desired.Loki)
if b.desired != nil && b.desired.Loki.TLS.Enable && !b.desired.Loki.TLS.InsecureSkipVerify {
volumes, volumeMounts = helper.AppendCertVolumes(volumes, volumeMounts, &b.desired.Loki.TLS, lokiCerts, b.cWatcher)
}

if b.desiredLoki.UseHostToken() {
if b.desired.Loki.UseHostToken() {
volumes, volumeMounts = helper.AppendTokenVolume(volumes, volumeMounts, constants.PluginName, constants.PluginName)
}

Expand All @@ -236,8 +234,8 @@ func (b *builder) podTemplate(cmDigest string) *corev1.PodTemplateSpec {
Containers: []corev1.Container{{
Name: constants.PluginName,
Image: b.imageName,
ImagePullPolicy: corev1.PullPolicy(b.desired.ImagePullPolicy),
Resources: *b.desired.Resources.DeepCopy(),
ImagePullPolicy: corev1.PullPolicy(b.desired.ConsolePlugin.ImagePullPolicy),
Resources: *b.desired.ConsolePlugin.Resources.DeepCopy(),
VolumeMounts: volumeMounts,
Args: args,
}},
Expand All @@ -260,9 +258,9 @@ func (b *builder) autoScaler() *ascv2.HorizontalPodAutoscaler {
Kind: "Deployment",
Name: constants.PluginName,
},
MinReplicas: b.desired.Autoscaler.MinReplicas,
MaxReplicas: b.desired.Autoscaler.MaxReplicas,
Metrics: b.desired.Autoscaler.Metrics,
MinReplicas: b.desired.ConsolePlugin.Autoscaler.MinReplicas,
MaxReplicas: b.desired.ConsolePlugin.Autoscaler.MaxReplicas,
Metrics: b.desired.ConsolePlugin.Autoscaler.Metrics,
},
}
}
Expand All @@ -281,7 +279,7 @@ func (b *builder) service(old *corev1.Service) *corev1.Service {
Spec: corev1.ServiceSpec{
Selector: b.selector,
Ports: []corev1.ServicePort{{
Port: b.desired.Port,
Port: b.desired.ConsolePlugin.Port,
Protocol: "TCP",
Name: "main",
}},
Expand All @@ -291,7 +289,7 @@ func (b *builder) service(old *corev1.Service) *corev1.Service {
// In case we're updating an existing service, we need to build from the old one to keep immutable fields such as clusterIP
newService := old.DeepCopy()
newService.Spec.Ports = []corev1.ServicePort{{
Port: b.desired.Port,
Port: b.desired.ConsolePlugin.Port,
Protocol: corev1.ProtocolUDP,
}}
return newService
Expand All @@ -313,9 +311,10 @@ func buildServiceAccount(ns string) *corev1.ServiceAccount {
// detect any configuration change
func (b *builder) configMap() (*corev1.ConfigMap, string) {
config := map[string]interface{}{
"portNaming": b.desired.PortNaming,
"quickFilters": b.desired.QuickFilters,
"portNaming": b.desired.ConsolePlugin.PortNaming,
"quickFilters": b.desired.ConsolePlugin.QuickFilters,
"alertNamespaces": []string{b.namespace},
"sampling": b.desired.GetSampling(),
}

configStr := "{}"
Expand Down
2 changes: 1 addition & 1 deletion controllers/consoleplugin/consoleplugin_reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ func (r *CPReconciler) Reconcile(ctx context.Context, desired *flowslatest.FlowC
}

// Create object builder
builder := newBuilder(ns, r.image, &desired.Spec.ConsolePlugin, &desired.Spec.Loki, r.CertWatcher)
builder := newBuilder(ns, r.image, &desired.Spec, r.CertWatcher)

if err = r.reconcilePlugin(ctx, builder, &desired.Spec); err != nil {
return err
Expand Down
25 changes: 16 additions & 9 deletions controllers/consoleplugin/consoleplugin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,15 +110,16 @@ func TestContainerUpdateCheck(t *testing.T) {
//equals specs
plugin := getPluginConfig()
loki := &flowslatest.FlowCollectorLoki{URL: "http://loki:3100/", TenantID: "netobserv"}
builder := newBuilder(testNamespace, testImage, &plugin, loki, &certWatcher)
spec := &flowslatest.FlowCollectorSpec{ConsolePlugin: plugin, Loki: *loki}
builder := newBuilder(testNamespace, testImage, spec, &certWatcher)
old := builder.deployment("digest")
new := builder.deployment("digest")
report := helper.NewChangeReport("")
assert.False(helper.PodChanged(&old.Spec.Template, &new.Spec.Template, constants.PluginName, &report))
assert.Contains(report.String(), "no change")

//wrong resources
plugin.Resources.Limits = map[corev1.ResourceName]resource.Quantity{
spec.ConsolePlugin.Resources.Limits = map[corev1.ResourceName]resource.Quantity{
corev1.ResourceCPU: resource.MustParse("500m"),
corev1.ResourceMemory: resource.MustParse("500Gi"),
}
Expand All @@ -137,15 +138,15 @@ func TestContainerUpdateCheck(t *testing.T) {
old = new

//new pull policy
plugin.ImagePullPolicy = string(corev1.PullAlways)
spec.ConsolePlugin.ImagePullPolicy = string(corev1.PullAlways)
new = builder.deployment("digest")
report = helper.NewChangeReport("")
assert.True(helper.PodChanged(&old.Spec.Template, &new.Spec.Template, constants.PluginName, &report))
assert.Contains(report.String(), "Pull policy changed")
old = new

//new log level
plugin.LogLevel = "debug"
spec.ConsolePlugin.LogLevel = "debug"
new = builder.deployment("digest")
report = helper.NewChangeReport("")
assert.True(helper.PodChanged(&old.Spec.Template, &new.Spec.Template, constants.PluginName, &report))
Expand All @@ -161,7 +162,8 @@ func TestContainerUpdateCheck(t *testing.T) {
CertFile: "ca.crt",
},
}}
builder = newBuilder(testNamespace, testImage, &plugin, loki, &certWatcher)
spec = &flowslatest.FlowCollectorSpec{ConsolePlugin: plugin, Loki: *loki}
builder = newBuilder(testNamespace, testImage, spec, &certWatcher)
new = builder.deployment("digest")
report = helper.NewChangeReport("")
assert.True(helper.PodChanged(&old.Spec.Template, &new.Spec.Template, constants.PluginName, &report))
Expand All @@ -170,7 +172,8 @@ func TestContainerUpdateCheck(t *testing.T) {

//new loki cert name
loki.TLS.CACert.Name = "cm-name-2"
builder = newBuilder(testNamespace, testImage, &plugin, loki, &certWatcher)
spec = &flowslatest.FlowCollectorSpec{ConsolePlugin: plugin, Loki: *loki}
builder = newBuilder(testNamespace, testImage, spec, &certWatcher)
new = builder.deployment("digest")
report = helper.NewChangeReport("")
assert.True(helper.PodChanged(&old.Spec.Template, &new.Spec.Template, constants.PluginName, &report))
Expand All @@ -179,7 +182,8 @@ func TestContainerUpdateCheck(t *testing.T) {

//test again no change
loki.TLS.CACert.Name = "cm-name-2"
builder = newBuilder(testNamespace, testImage, &plugin, loki, &certWatcher)
spec = &flowslatest.FlowCollectorSpec{ConsolePlugin: plugin, Loki: *loki}
builder = newBuilder(testNamespace, testImage, spec, &certWatcher)
new = builder.deployment("digest")
report = helper.NewChangeReport("")
assert.False(helper.PodChanged(&old.Spec.Template, &new.Spec.Template, constants.PluginName, &report))
Expand Down Expand Up @@ -215,7 +219,9 @@ func TestBuiltService(t *testing.T) {

//newly created service should not need update
plugin := getPluginConfig()
builder := newBuilder(testNamespace, testImage, &plugin, nil, &certWatcher)
loki := &flowslatest.FlowCollectorLoki{URL: "http://foo:1234"}
spec := &flowslatest.FlowCollectorSpec{ConsolePlugin: plugin, Loki: *loki}
builder := newBuilder(testNamespace, testImage, spec, &certWatcher)
newService := builder.service(nil)
report := helper.NewChangeReport("")
assert.Equal(serviceNeedsUpdate(newService, &plugin, &report), false)
Expand All @@ -227,7 +233,8 @@ func TestLabels(t *testing.T) {

plugin := getPluginConfig()
loki := &flowslatest.FlowCollectorLoki{URL: "http://foo:1234"}
builder := newBuilder(testNamespace, testImage, &plugin, loki, &certWatcher)
spec := &flowslatest.FlowCollectorSpec{ConsolePlugin: plugin, Loki: *loki}
builder := newBuilder(testNamespace, testImage, spec, &certWatcher)

// Deployment
depl := builder.deployment("digest")
Expand Down

0 comments on commit 0e1aa23

Please sign in to comment.