From 67b6168d49ae2bb1581f18052a7723e34013aad6 Mon Sep 17 00:00:00 2001 From: Jefiya-MJ <141906581+Jefiya-MJ@users.noreply.github.com> Date: Mon, 24 Jun 2024 13:52:53 +0530 Subject: [PATCH] Add new flag serviceMesh.enabled (#168) * Add new flag serviceMesh.enabled Signed-off-by: Jefiya M J * Add serviceMesh.enabled field Signed-off-by: Jefiya M J * feat(servicemesh): Propagate serviceMesh.enabled The serviceMesh.enabled boolean is translated into a ENABLE_AGENT_SOCKET environment variable on the agent DaemonSet which schedules the pods with the environment variable exposed to the agent container. * chore(fix): Fix unit test to include new env var * add unit test to include new env var Signed-off-by: Jefiya M J --------- Signed-off-by: Jefiya M J Co-authored-by: Konrad Ohms --- api/v1/inline_types.go | 9 ++++++++ api/v1/instanaagent_types.go | 3 +++ api/v1/zz_generated.deepcopy.go | 17 ++++++++++++++ .../instana_v1_extended_instanaagent.yaml | 5 +++++ .../builders/agent/daemonset/daemonset.go | 1 + .../agent/daemonset/daemonset_test.go | 1 + pkg/k8s/object/builders/common/env/env.go | 16 ++++++++++++++ .../object/builders/common/env/env_builder.go | 21 ++++++++++++++++++ .../builders/common/env/env_builder_test.go | 18 ++++++++++++++- .../object/builders/common/env/env_test.go | 16 ++++++++++++++ pkg/k8s/object/builders/common/env/vars.go | 22 +++++++++++++++++++ .../object/builders/common/env/vars_test.go | 22 +++++++++++++++++++ 12 files changed, 150 insertions(+), 1 deletion(-) diff --git a/api/v1/inline_types.go b/api/v1/inline_types.go index 3317c46a..dfd72b53 100644 --- a/api/v1/inline_types.go +++ b/api/v1/inline_types.go @@ -151,6 +151,10 @@ type BaseAgentSpec struct { // +kubebuilder:validation:Optional Host HostSpec `json:"host,omitempty"` + // ServiceMesh sets the ENABLE_AGENT_SOCKET environment variable. + // +kubebuilder:validation:Optional + ServiceMesh ServiceMeshSpec `json:"serviceMesh,omitempty"` + // Override for the Maven repository URL when the Agent needs to connect to a locally provided Maven repository 'proxy' // Alternative to `Host` for referencing a different Maven repo. // +kubebuilder:validation:Optional @@ -263,6 +267,11 @@ type HostSpec struct { Repository string `json:"repository,omitempty"` } +type ServiceMeshSpec struct { + // +kubebuilder:validation:Optional + Enabled bool `json:"enabled,omitempty"` +} + type Prometheus struct { // +kubebuilder:validation:Optional RemoteWrite Enabled `json:"remoteWrite,omitempty"` diff --git a/api/v1/instanaagent_types.go b/api/v1/instanaagent_types.go index c5bf5826..e3843f53 100644 --- a/api/v1/instanaagent_types.go +++ b/api/v1/instanaagent_types.go @@ -79,6 +79,9 @@ type InstanaAgentSpec struct { // Zones can be used to specify agents in multiple zones split across different nodes in the cluster // +kubebuilder:validation:Optional Zones []Zone `json:"zones,omitempty"` + + // +kubebuilder:validation:Optional + ServiceMesh ServiceMeshSpec `json:"serviceMesh,omitempty"` } // +k8s:openapi-gen=true diff --git a/api/v1/zz_generated.deepcopy.go b/api/v1/zz_generated.deepcopy.go index 79f1fb78..4fef8837 100644 --- a/api/v1/zz_generated.deepcopy.go +++ b/api/v1/zz_generated.deepcopy.go @@ -107,6 +107,7 @@ func (in *BaseAgentSpec) DeepCopyInto(out *BaseAgentSpec) { } } out.Host = in.Host + out.ServiceMesh = in.ServiceMesh } // DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new BaseAgentSpec. @@ -320,6 +321,7 @@ func (in *InstanaAgentSpec) DeepCopyInto(out *InstanaAgentSpec) { (*in)[i].DeepCopyInto(&(*out)[i]) } } + out.ServiceMesh = in.ServiceMesh } // DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new InstanaAgentSpec. @@ -610,6 +612,21 @@ func (in *ServiceAccountSpec) DeepCopy() *ServiceAccountSpec { return out } +// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. +func (in *ServiceMeshSpec) DeepCopyInto(out *ServiceMeshSpec) { + *out = *in +} + +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new ServiceMeshSpec. +func (in *ServiceMeshSpec) DeepCopy() *ServiceMeshSpec { + if in == nil { + return nil + } + out := new(ServiceMeshSpec) + in.DeepCopyInto(out) + return out +} + // DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. func (in *TlsSpec) DeepCopyInto(out *TlsSpec) { *out = *in diff --git a/config/samples/instana_v1_extended_instanaagent.yaml b/config/samples/instana_v1_extended_instanaagent.yaml index db67ca52..9f3cabfd 100644 --- a/config/samples/instana_v1_extended_instanaagent.yaml +++ b/config/samples/instana_v1_extended_instanaagent.yaml @@ -92,6 +92,11 @@ spec: # agent.pod.limits.cpu sets the CPU units allocation limits for the agent pods. cpu: "1.5" + serviceMesh: + # agent.serviceMesh.enabled sets the Instana agent's communication direction with JVMs. + # To allow communication to be directed to the agent, set this variable to true. + enabled: true + # agent.host.repository sets a host path to be mounted as the agent maven repository (for debugging or development purposes) # host: # repository: "" diff --git a/pkg/k8s/object/builders/agent/daemonset/daemonset.go b/pkg/k8s/object/builders/agent/daemonset/daemonset.go index e2377e4e..f75c335f 100644 --- a/pkg/k8s/object/builders/agent/daemonset/daemonset.go +++ b/pkg/k8s/object/builders/agent/daemonset/daemonset.go @@ -85,6 +85,7 @@ func (d *daemonSetBuilder) getEnvVars() []corev1.EnvVar { env.InstanaAgentPodNameEnv, env.PodIPEnv, env.K8sServiceDomainEnv, + env.EnableAgentSocketEnv, ) } diff --git a/pkg/k8s/object/builders/agent/daemonset/daemonset_test.go b/pkg/k8s/object/builders/agent/daemonset/daemonset_test.go index 58016398..e21648b0 100644 --- a/pkg/k8s/object/builders/agent/daemonset/daemonset_test.go +++ b/pkg/k8s/object/builders/agent/daemonset/daemonset_test.go @@ -165,6 +165,7 @@ func TestDaemonSetBuilder_getEnvVars(t *testing.T) { env.InstanaAgentPodNameEnv, env.PodIPEnv, env.K8sServiceDomainEnv, + env.EnableAgentSocketEnv, ). Return(expected) diff --git a/pkg/k8s/object/builders/common/env/env.go b/pkg/k8s/object/builders/common/env/env.go index da93dd09..ad54f367 100644 --- a/pkg/k8s/object/builders/common/env/env.go +++ b/pkg/k8s/object/builders/common/env/env.go @@ -1,3 +1,19 @@ +// /* +// (c) Copyright IBM Corp. 2024 +// (c) Copyright Instana Inc. +// +// 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 env import ( diff --git a/pkg/k8s/object/builders/common/env/env_builder.go b/pkg/k8s/object/builders/common/env/env_builder.go index 904d9267..557b7ae0 100644 --- a/pkg/k8s/object/builders/common/env/env_builder.go +++ b/pkg/k8s/object/builders/common/env/env_builder.go @@ -1,3 +1,21 @@ +// /* +// (c) Copyright IBM Corp. 2024 +// (c) Copyright Instana Inc. +// +// 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 env import ( @@ -45,6 +63,7 @@ const ( PodUIDEnv PodNamespaceEnv K8sServiceDomainEnv + EnableAgentSocketEnv ) type EnvBuilder interface { @@ -122,6 +141,8 @@ func (e *envBuilder) getBuilder(envVar EnvVar) func() optional.Optional[corev1.E return e.podNamespaceEnv case K8sServiceDomainEnv: return e.k8sServiceDomainEnv + case EnableAgentSocketEnv: + return e.enableAgentSocketEnv default: panic(errors.New("unknown environment variable requested")) } diff --git a/pkg/k8s/object/builders/common/env/env_builder_test.go b/pkg/k8s/object/builders/common/env/env_builder_test.go index fbe624f0..b3972e2f 100644 --- a/pkg/k8s/object/builders/common/env/env_builder_test.go +++ b/pkg/k8s/object/builders/common/env/env_builder_test.go @@ -1,3 +1,19 @@ +// /* +// (c) Copyright IBM Corp. 2024 +// (c) Copyright Instana Inc. +// +// 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 env import ( @@ -32,7 +48,7 @@ func assertAllElementsUnique[T comparable](assertions *require.Assertions, list } func TestEnvBuilder_getBuilder(t *testing.T) { - const numDefinedEnvVars = 31 + const numDefinedEnvVars = 32 t.Run( "each_defined_var_has_unique_function", func(t *testing.T) { diff --git a/pkg/k8s/object/builders/common/env/env_test.go b/pkg/k8s/object/builders/common/env/env_test.go index 9e1e6e68..2b8f94fa 100644 --- a/pkg/k8s/object/builders/common/env/env_test.go +++ b/pkg/k8s/object/builders/common/env/env_test.go @@ -1,3 +1,19 @@ +// /* +// (c) Copyright IBM Corp. 2024 +// (c) Copyright Instana Inc. +// +// 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 env import ( diff --git a/pkg/k8s/object/builders/common/env/vars.go b/pkg/k8s/object/builders/common/env/vars.go index 59ebb377..e5f786f7 100644 --- a/pkg/k8s/object/builders/common/env/vars.go +++ b/pkg/k8s/object/builders/common/env/vars.go @@ -1,3 +1,21 @@ +// /* +// (c) Copyright IBM Corp. 2024 +// (c) Copyright Instana Inc. +// +// 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 env import ( @@ -137,6 +155,10 @@ func (e *envBuilder) httpsProxyEnv() optional.Optional[corev1.EnvVar] { ) } +func (e *envBuilder) enableAgentSocketEnv() optional.Optional[corev1.EnvVar] { + return fromCRField("ENABLE_AGENT_SOCKET", e.agent.Spec.Agent.ServiceMesh.Enabled) +} + // Static func (e *envBuilder) backendURLEnv() optional.Optional[corev1.EnvVar] { diff --git a/pkg/k8s/object/builders/common/env/vars_test.go b/pkg/k8s/object/builders/common/env/vars_test.go index 02578773..c36b5e0b 100644 --- a/pkg/k8s/object/builders/common/env/vars_test.go +++ b/pkg/k8s/object/builders/common/env/vars_test.go @@ -573,6 +573,28 @@ func TestEnvBuilder_httpsProxyEnv(t *testing.T) { } } +func TestEnvBuilder_enableAgentSocketEnv(t *testing.T) { + const expectedValue = true + + testFromCRField( + &crFieldVarTest{ + t: t, + getMethod: func(builder *envBuilder) func() optional.Optional[corev1.EnvVar] { + return builder.enableAgentSocketEnv + }, + expectedName: "ENABLE_AGENT_SOCKET", + expectedValue: strconv.FormatBool(expectedValue), + agentSpec: instanav1.InstanaAgentSpec{ + Agent: instanav1.BaseAgentSpec{ + ServiceMesh: instanav1.ServiceMeshSpec{ + Enabled: expectedValue, + }, + }, + }, + }, + ) +} + func TestEnvBuilder_backendURLEnv(t *testing.T) { testLiteralAlways( &literalAlwaysTest{