Skip to content

Commit

Permalink
passing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
boonware committed Dec 11, 2024
1 parent f0c89f3 commit c736c4f
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 36 deletions.
5 changes: 4 additions & 1 deletion controllers/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import (
"github.com/instana/instana-agent-operator/pkg/k8s/operator/operator_utils"
"github.com/instana/instana-agent-operator/pkg/k8s/operator/status"
corev1 "k8s.io/api/core/v1"
v1 "k8s.io/client-go/kubernetes/typed/core/v1"
)

func getDaemonSetBuilders(
Expand Down Expand Up @@ -87,14 +88,16 @@ func (r *InstanaAgentReconciler) applyResources(
statusManager status.AgentStatusManager,
keysSecret *corev1.Secret,
k8SensorBackends []backends.K8SensorBackend,
apiClient v1.CoreV1Interface,
) reconcileReturn {
log := r.loggerFor(ctx, agent)
log.V(1).Info("applying Kubernetes resources for agent")
configMerger := agentsecrets.NewConfigMergerBuilder(apiClient)

builders := append(
getDaemonSetBuilders(agent, isOpenShift, statusManager),
headlessservice.NewHeadlessServiceBuilder(agent),
agentsecrets.NewConfigBuilder(agent, statusManager, keysSecret, k8SensorBackends),
agentsecrets.NewConfigBuilder(agent, statusManager, keysSecret, k8SensorBackends, configMerger),
agentsecrets.NewContainerBuilder(agent, keysSecret),
tlssecret.NewSecretBuilder(agent),
service.NewServiceBuilder(agent),
Expand Down
6 changes: 6 additions & 0 deletions controllers/instanaagent_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
corev1 "k8s.io/api/core/v1"
rbacv1 "k8s.io/api/rbac/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/record"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
Expand All @@ -38,6 +39,7 @@ import (
"github.com/instana/instana-agent-operator/pkg/k8s/operator/status"
"github.com/instana/instana-agent-operator/pkg/multierror"
"github.com/instana/instana-agent-operator/pkg/recovery"
"k8s.io/client-go/rest"
)

const (
Expand Down Expand Up @@ -132,6 +134,9 @@ func (r *InstanaAgentReconciler) reconcile(

k8SensorBackends := r.getK8SensorBackends(agent)

clientConfig, _ := rest.InClusterConfig()
clientSet, _ := kubernetes.NewForConfig(clientConfig)

if applyResourcesRes := r.applyResources(
ctx,
agent,
Expand All @@ -140,6 +145,7 @@ func (r *InstanaAgentReconciler) reconcile(
statusManager,
keysSecret,
k8SensorBackends,
clientSet.CoreV1(),
); applyResourcesRes.suppliesReconcileResult() {
return applyResourcesRes
}
Expand Down
6 changes: 4 additions & 2 deletions pkg/k8s/object/builders/agent/secrets/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,19 +30,21 @@ type configBuilder struct {
keysSecret *corev1.Secret
logger logr.Logger
backends []backends.K8SensorBackend
configMerger ConfigMerger
}

func NewConfigBuilder(
agent *instanav1.InstanaAgent,
statusManager status.AgentStatusManager,
keysSecret *corev1.Secret,
backends []backends.K8SensorBackend) commonbuilder.ObjectBuilder {
backends []backends.K8SensorBackend, configMerger ConfigMerger) commonbuilder.ObjectBuilder {
return &configBuilder{
InstanaAgent: agent,
statusManager: statusManager,
keysSecret: keysSecret,
logger: logf.Log.WithName("instana-agent-config-secret-builder"),
backends: backends,
configMerger: configMerger,
}
}

Expand Down Expand Up @@ -85,7 +87,7 @@ func (c *configBuilder) data() (map[string][]byte, error) {
data["cluster_name"] = []byte(c.Spec.Cluster.Name)
}
if c.Spec.Agent.ConfigurationYaml != "" {
data["configuration.yaml"] = []byte(c.Spec.Agent.ConfigurationYaml)
data["configuration.yaml"] = c.configMerger.MergeConfigurationYaml(c.Spec.Agent.ConfigurationYaml)
}
if otlp := c.Spec.OpenTelemetry; otlp.IsEnabled() {
mrshl, _ := yaml.Marshal(map[string]instanav1.OpenTelemetry{"com.instana.plugin.opentelemetry": otlp})
Expand Down
58 changes: 31 additions & 27 deletions pkg/k8s/object/builders/agent/secrets/config_merger.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,65 +19,69 @@ import (

const ConfigMapLabel = "instana.io/agent-config=true"

type ConfigMerger struct {
type ConfigMerger interface {
MergeConfigurationYaml(string) []byte
}

type DefaultConfigMerger struct {
logger logr.Logger
k8sClient v1.CoreV1Interface
}

func NewConfigMergerBuilder(client v1.CoreV1Interface) *ConfigMerger {
return &ConfigMerger{
func NewConfigMergerBuilder(client v1.CoreV1Interface) DefaultConfigMerger {
return DefaultConfigMerger{
logger: logf.Log.WithName("instana-agent-config-merger"),
k8sClient: client,
}
}

func (c *ConfigMerger) MergeConfigurationYaml(agentConfiguration string) []byte {
operator_data := make(map[string]interface{})
err := yaml.Unmarshal([]byte([]byte(agentConfiguration)), operator_data)
func (c *DefaultConfigMerger) MergeConfigurationYaml(agentConfiguration string) []byte {
agentData := make(map[string]interface{})
err := yaml.Unmarshal([]byte([]byte(agentConfiguration)), agentData)
config := []byte{}
if err != nil {
c.logger.Error(err, "Failed to load agent configuration")
} else {
config_maps := c.fetchConfigMaps()
for _, config_map := range config_maps {
config_map_data := make(map[string]interface{})
err := yaml.Unmarshal([]byte(config_map.Data["configuration_yaml"]), &config_map_data)
configMaps := c.fetchConfigMaps()
for _, configMap := range configMaps {
configMapData := make(map[string]interface{})
err := yaml.Unmarshal([]byte(configMap.Data["configuration_yaml"]), &configMapData)
if err != nil {
c.logger.Error(err, "Failed to parse agent configuration YAML")
} else {
operator_data = c.mergeConfig(operator_data, config_map_data)
agentData = c.mergeConfig(agentData, configMapData)
}
}
config, err = yaml.Marshal(operator_data)
config, err = yaml.Marshal(agentData)
}
return config
}

func (c *ConfigMerger) mergeConfig(operator_data, config_map_data map[string]interface{}) map[string]interface{} {
for key, cm_value := range config_map_data {
if op_value, ok := operator_data[key]; ok {
op_value_kind := reflect.TypeOf(op_value).Kind()
if op_value_kind == reflect.Array || op_value_kind == reflect.Slice {
operator_data[key] = append(op_value.([]interface{}), cm_value.([]interface{})...)
func (c *DefaultConfigMerger) mergeConfig(agentData, configMapData map[string]interface{}) map[string]interface{} {
for key, configMapValue := range configMapData {
if agentValue, ok := agentData[key]; ok {
agentValueKind := reflect.TypeOf(agentValue).Kind()
if agentValueKind == reflect.Array || agentValueKind == reflect.Slice {
agentData[key] = append(agentValue.([]interface{}), configMapValue.([]interface{})...)
} else {
c.mergeConfig(operator_data[key].(map[string]interface{}), cm_value.(map[string]interface{}))
c.mergeConfig(agentData[key].(map[string]interface{}), configMapValue.(map[string]interface{}))
}
} else {
operator_data[key] = cm_value
agentData[key] = configMapValue
}
}
return operator_data
return agentData
}

func (c *ConfigMerger) fetchConfigMaps() []apiV1.ConfigMap {
config_maps := []apiV1.ConfigMap{}
func (c *DefaultConfigMerger) fetchConfigMaps() []apiV1.ConfigMap {
configMaps := []apiV1.ConfigMap{}
c.logger.Info(fmt.Sprintf("Fetching agent configmaps with label '%s'", ConfigMapLabel))
config_map_list, err := c.k8sClient.ConfigMaps("").List(context.TODO(), metav1.ListOptions{LabelSelector: ConfigMapLabel})
configMapList, err := c.k8sClient.ConfigMaps("").List(context.TODO(), metav1.ListOptions{LabelSelector: ConfigMapLabel})
if err != nil {
c.logger.Error(err, fmt.Sprintf("Failed to fetch agent configmaps with label '%s'", ConfigMapLabel))
} else {
config_maps = config_map_list.Items
c.logger.Info(fmt.Sprintf("Found %d configmaps with label '%s'", len(config_maps), ConfigMapLabel))
configMaps = configMapList.Items
c.logger.Info(fmt.Sprintf("Found %d configmaps with label '%s'", len(configMaps), ConfigMapLabel))
}
return config_maps
return configMaps
}
30 changes: 24 additions & 6 deletions pkg/k8s/object/builders/agent/secrets/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,41 @@ import (
backend "github.com/instana/instana-agent-operator/pkg/k8s/object/builders/common/backends"
"github.com/instana/instana-agent-operator/pkg/pointer"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
gomock "go.uber.org/mock/gomock"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

const ConfigurationYamlValue = "configuration-yaml-value"

type ConfigMergerMock struct {
ConfigMerger
mock.Mock
}

func (mock *ConfigMergerMock) MergeConfigurationYaml(agentConfig string) []byte {
args := mock.Called()
return args.Get(0).([]byte)
}

func mockConfigMerger() ConfigMerger {
configMerger := new(ConfigMergerMock)
configMerger.On("MergeConfigurationYaml").Return([]byte(ConfigurationYamlValue))
return configMerger
}

func TestConfigBuilderComponentName(t *testing.T) {
ctrl := gomock.NewController(t)
statusManager := mocks.NewMockAgentStatusManager(ctrl)
s := NewConfigBuilder(&instanav1.InstanaAgent{}, statusManager, &corev1.Secret{}, make([]backend.K8SensorBackend, 0))

s := NewConfigBuilder(&instanav1.InstanaAgent{}, statusManager, &corev1.Secret{}, make([]backend.K8SensorBackend, 0), mockConfigMerger())
assert.True(t, s.IsNamespaced())
}

func TestConfigBuilderIsNamespaced(t *testing.T) {
ctrl := gomock.NewController(t)
statusManager := mocks.NewMockAgentStatusManager(ctrl)
s := NewConfigBuilder(&instanav1.InstanaAgent{}, statusManager, &corev1.Secret{}, make([]backend.K8SensorBackend, 0))
s := NewConfigBuilder(&instanav1.InstanaAgent{}, statusManager, &corev1.Secret{}, make([]backend.K8SensorBackend, 0), mockConfigMerger())

assert.Equal(t, "instana-agent", s.ComponentName())
}
Expand Down Expand Up @@ -93,7 +111,7 @@ func TestAgentSecretConfigBuild(t *testing.T) {
EndpointHost: "main-backend-host",
EndpointPort: "main-backend-port",
Key: "main-backend-key",
ConfigurationYaml: "configuration-yaml-value",
ConfigurationYaml: ConfigurationYamlValue,
ProxyHost: "proxy-host-value",
ProxyPort: "proxy-port-value",
ProxyUser: "proxy-user-value",
Expand All @@ -115,7 +133,7 @@ func TestAgentSecretConfigBuild(t *testing.T) {
keysSecret: &corev1.Secret{},
expected: map[string][]byte{
"cluster_name": []byte(objectMeta.Name),
"configuration.yaml": []byte("configuration-yaml-value"),
"configuration.yaml": []byte(ConfigurationYamlValue),
"configuration-opentelemetry.yaml": []byte("com.instana.plugin.opentelemetry:\n grpc: {}\n"),
"configuration-prometheus-remote-write.yaml": []byte("com.instana.plugin.prometheus:\n remote_write:\n enabled: true\n"),
"configuration-disable-kubernetes-sensor.yaml": []byte("com.instana.plugin.kubernetes:\n enabled: false\n"),
Expand Down Expand Up @@ -386,7 +404,7 @@ func TestAgentSecretConfigBuild(t *testing.T) {
statusManager := mocks.NewMockAgentStatusManager(ctrl)
statusManager.EXPECT().SetAgentSecretConfig(gomock.Any()).AnyTimes()

builder := NewConfigBuilder(&test.agent, statusManager, test.keysSecret, test.k8sBackends)
builder := NewConfigBuilder(&test.agent, statusManager, test.keysSecret, test.k8sBackends, mockConfigMerger())

actual := builder.Build().Get()

Expand Down

0 comments on commit c736c4f

Please sign in to comment.