Skip to content

Commit

Permalink
feat: fix-license-key-injection
Browse files Browse the repository at this point in the history
  • Loading branch information
danielstokes committed Oct 15, 2024
1 parent b4d5759 commit c664a75
Show file tree
Hide file tree
Showing 13 changed files with 39 additions and 27 deletions.
3 changes: 2 additions & 1 deletion src/apm/dotnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,8 @@ func (i DotnetInjector) Inject(ctx context.Context, inst v1alpha2.Instrumentatio
})
}

pod = i.injectNewrelicConfig(ctx, inst.Spec.Resource, ns, pod, firstContainer, inst.Spec.LicenseKeySecret)
pod = i.injectNewrelicConfig(ctx, inst.Spec.Resource, ns, pod, firstContainer)
pod.Spec.Containers[firstContainer] = i.injectNewrelicLicenseKeyIntoContainer(*container, inst.Spec.LicenseKeySecret)

return pod, nil
}
2 changes: 1 addition & 1 deletion src/apm/dotnet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,9 @@ func TestDotnetInjector_Inject(t *testing.T) {
{Name: "CORECLR_PROFILER_PATH", Value: "/newrelic-instrumentation/libNewRelicProfiler.so"},
{Name: "CORECLR_NEWRELIC_HOME", Value: "/newrelic-instrumentation"},
{Name: "NEW_RELIC_APP_NAME", Value: "test"},
{Name: "NEW_RELIC_LICENSE_KEY", ValueFrom: &corev1.EnvVarSource{SecretKeyRef: &corev1.SecretKeySelector{LocalObjectReference: corev1.LocalObjectReference{Name: "newrelic-key-secret"}, Key: "new_relic_license_key", Optional: &vtrue}}},
{Name: "NEW_RELIC_LABELS", Value: "operator:auto-injection"},
{Name: "NEW_RELIC_K8S_OPERATOR_ENABLED", Value: "true"},
{Name: "NEW_RELIC_LICENSE_KEY", ValueFrom: &corev1.EnvVarSource{SecretKeyRef: &corev1.SecretKeySelector{LocalObjectReference: corev1.LocalObjectReference{Name: "newrelic-key-secret"}, Key: "new_relic_license_key", Optional: &vtrue}}},
},
VolumeMounts: []corev1.VolumeMount{{Name: "newrelic-instrumentation", MountPath: "/newrelic-instrumentation"}},
}},
Expand Down
25 changes: 14 additions & 11 deletions src/apm/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,17 +214,7 @@ func (i *baseInjector) validate(inst v1alpha2.Instrumentation) error {
return nil
}

func (i *baseInjector) injectNewrelicConfig(ctx context.Context, resource v1alpha2.Resource, ns corev1.Namespace, pod corev1.Pod, index int, licenseKeySecretName string) corev1.Pod {
container := &pod.Spec.Containers[index]

if idx := getIndexOfEnv(container.Env, EnvNewRelicAppName); idx == -1 {
//@todo: how can we do this if multiple injectors need this?
resourceMap := i.createResourceMap(ctx, resource, ns, pod, index)
container.Env = append(container.Env, corev1.EnvVar{
Name: EnvNewRelicAppName,
Value: chooseServiceName(pod, resourceMap, index),
})
}
func (i *baseInjector) injectNewrelicLicenseKeyIntoContainer(container corev1.Container, licenseKeySecretName string) corev1.Container {
if idx := getIndexOfEnv(container.Env, EnvNewRelicLicenseKey); idx == -1 {
optional := true
container.Env = append(container.Env, corev1.EnvVar{
Expand All @@ -238,6 +228,19 @@ func (i *baseInjector) injectNewrelicConfig(ctx context.Context, resource v1alph
},
})
}
return container
}

func (i *baseInjector) injectNewrelicConfig(ctx context.Context, resource v1alpha2.Resource, ns corev1.Namespace, pod corev1.Pod, index int) corev1.Pod {
container := &pod.Spec.Containers[index]
if idx := getIndexOfEnv(container.Env, EnvNewRelicAppName); idx == -1 {
//@todo: how can we do this if multiple injectors need this?
resourceMap := i.createResourceMap(ctx, resource, ns, pod, index)
container.Env = append(container.Env, corev1.EnvVar{
Name: EnvNewRelicAppName,
Value: chooseServiceName(pod, resourceMap, index),
})
}
if idx := getIndexOfEnv(container.Env, EnvNewRelicLabels); idx == -1 {
container.Env = append(container.Env, corev1.EnvVar{
Name: EnvNewRelicLabels,
Expand Down
3 changes: 2 additions & 1 deletion src/apm/java.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,8 @@ func (i *JavaInjector) Inject(ctx context.Context, inst v1alpha2.Instrumentation
})
}

pod = i.injectNewrelicConfig(ctx, inst.Spec.Resource, ns, pod, firstContainer, inst.Spec.LicenseKeySecret)
pod = i.injectNewrelicConfig(ctx, inst.Spec.Resource, ns, pod, firstContainer)
pod.Spec.Containers[firstContainer] = i.injectNewrelicLicenseKeyIntoContainer(*container, inst.Spec.LicenseKeySecret)

return pod, nil
}
2 changes: 1 addition & 1 deletion src/apm/java_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,9 @@ func TestJavaInjector_Inject(t *testing.T) {
Env: []corev1.EnvVar{
{Name: "JAVA_TOOL_OPTIONS", Value: " -javaagent:/newrelic-instrumentation/newrelic-agent.jar"},
{Name: "NEW_RELIC_APP_NAME", Value: "test"},
{Name: "NEW_RELIC_LICENSE_KEY", ValueFrom: &corev1.EnvVarSource{SecretKeyRef: &corev1.SecretKeySelector{LocalObjectReference: corev1.LocalObjectReference{Name: "newrelic-key-secret"}, Key: "new_relic_license_key", Optional: &vtrue}}},
{Name: "NEW_RELIC_LABELS", Value: "operator:auto-injection"},
{Name: "NEW_RELIC_K8S_OPERATOR_ENABLED", Value: "true"},
{Name: "NEW_RELIC_LICENSE_KEY", ValueFrom: &corev1.EnvVarSource{SecretKeyRef: &corev1.SecretKeySelector{LocalObjectReference: corev1.LocalObjectReference{Name: "newrelic-key-secret"}, Key: "new_relic_license_key", Optional: &vtrue}}},
},
VolumeMounts: []corev1.VolumeMount{{Name: "newrelic-instrumentation", MountPath: "/newrelic-instrumentation"}},
}},
Expand Down
3 changes: 2 additions & 1 deletion src/apm/nodejs.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,8 @@ func (i *NodejsInjector) Inject(ctx context.Context, inst v1alpha2.Instrumentati
})
}

pod = i.injectNewrelicConfig(ctx, inst.Spec.Resource, ns, pod, firstContainer, inst.Spec.LicenseKeySecret)
pod = i.injectNewrelicConfig(ctx, inst.Spec.Resource, ns, pod, firstContainer)
pod.Spec.Containers[firstContainer] = i.injectNewrelicLicenseKeyIntoContainer(*container, inst.Spec.LicenseKeySecret)

return pod, nil
}
2 changes: 1 addition & 1 deletion src/apm/nodejs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,9 @@ func TestNodejsInjector_Inject(t *testing.T) {
Env: []corev1.EnvVar{
{Name: "NODE_OPTIONS", Value: " --require /newrelic-instrumentation/newrelicinstrumentation.js"},
{Name: "NEW_RELIC_APP_NAME", Value: "test"},
{Name: "NEW_RELIC_LICENSE_KEY", ValueFrom: &corev1.EnvVarSource{SecretKeyRef: &corev1.SecretKeySelector{LocalObjectReference: corev1.LocalObjectReference{Name: "newrelic-key-secret"}, Key: "new_relic_license_key", Optional: &vtrue}}},
{Name: "NEW_RELIC_LABELS", Value: "operator:auto-injection"},
{Name: "NEW_RELIC_K8S_OPERATOR_ENABLED", Value: "true"},
{Name: "NEW_RELIC_LICENSE_KEY", ValueFrom: &corev1.EnvVarSource{SecretKeyRef: &corev1.SecretKeySelector{LocalObjectReference: corev1.LocalObjectReference{Name: "newrelic-key-secret"}, Key: "new_relic_license_key", Optional: &vtrue}}},
},
VolumeMounts: []corev1.VolumeMount{{Name: "newrelic-instrumentation", MountPath: "/newrelic-instrumentation"}},
}},
Expand Down
8 changes: 5 additions & 3 deletions src/apm/php.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ func (i *PhpInjector) Inject(ctx context.Context, inst v1alpha2.Instrumentation,
}})
}

pod.Spec.InitContainers = append(pod.Spec.InitContainers, corev1.Container{
initContainer := corev1.Container{
Name: phpInitContainerName,
Image: inst.Spec.Agent.Image,
Command: []string{"/bin/sh"},
Expand All @@ -148,10 +148,12 @@ func (i *PhpInjector) Inject(ctx context.Context, inst v1alpha2.Instrumentation,
Name: volumeName,
MountPath: "/newrelic-instrumentation",
}},
})
}
initContainer = i.injectNewrelicLicenseKeyIntoContainer(initContainer, inst.Spec.LicenseKeySecret)
pod.Spec.InitContainers = append(pod.Spec.InitContainers, initContainer)
}

pod = i.injectNewrelicConfig(ctx, inst.Spec.Resource, ns, pod, firstContainer, inst.Spec.LicenseKeySecret)
pod = i.injectNewrelicConfig(ctx, inst.Spec.Resource, ns, pod, firstContainer)

return pod, nil
}
8 changes: 5 additions & 3 deletions src/apm/php_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,15 +74,17 @@ func TestPhpInjector_Inject(t *testing.T) {
Env: []corev1.EnvVar{
{Name: "PHP_INI_SCAN_DIR", Value: "/newrelic-instrumentation/php-agent/ini"},
{Name: "NEW_RELIC_APP_NAME", Value: "test"},
{Name: "NEW_RELIC_LICENSE_KEY", ValueFrom: &corev1.EnvVarSource{SecretKeyRef: &corev1.SecretKeySelector{LocalObjectReference: corev1.LocalObjectReference{Name: "newrelic-key-secret"}, Key: "new_relic_license_key", Optional: &vtrue}}},
{Name: "NEW_RELIC_LABELS", Value: "operator:auto-injection"},
{Name: "NEW_RELIC_K8S_OPERATOR_ENABLED", Value: "true"},
},
VolumeMounts: []corev1.VolumeMount{{Name: "newrelic-instrumentation", MountPath: "/newrelic-instrumentation"}},
}},
InitContainers: []corev1.Container{{
Name: "newrelic-instrumentation-php",
Env: []corev1.EnvVar{{Name: "PHP_INI_SCAN_DIR", Value: "/newrelic-instrumentation/php-agent/ini"}},
Name: "newrelic-instrumentation-php",
Env: []corev1.EnvVar{
{Name: "PHP_INI_SCAN_DIR", Value: "/newrelic-instrumentation/php-agent/ini"},
{Name: "NEW_RELIC_LICENSE_KEY", ValueFrom: &corev1.EnvVarSource{SecretKeyRef: &corev1.SecretKeySelector{LocalObjectReference: corev1.LocalObjectReference{Name: "newrelic-key-secret"}, Key: "new_relic_license_key", Optional: &vtrue}}},
},
Command: []string{"/bin/sh"},
Args: []string{"-c", "cp -a /instrumentation/. /newrelic-instrumentation/ && /newrelic-instrumentation/k8s-php-install.sh 20230831 && /newrelic-instrumentation/nr_env_to_ini.sh"},
VolumeMounts: []corev1.VolumeMount{{Name: "newrelic-instrumentation", MountPath: "/newrelic-instrumentation"}},
Expand Down
3 changes: 2 additions & 1 deletion src/apm/python.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,8 @@ func (i *PythonInjector) Inject(ctx context.Context, inst v1alpha2.Instrumentati
})
}

pod = i.injectNewrelicConfig(ctx, inst.Spec.Resource, ns, pod, firstContainer, inst.Spec.LicenseKeySecret)
pod = i.injectNewrelicConfig(ctx, inst.Spec.Resource, ns, pod, firstContainer)
pod.Spec.Containers[firstContainer] = i.injectNewrelicLicenseKeyIntoContainer(*container, inst.Spec.LicenseKeySecret)

return pod, nil
}
2 changes: 1 addition & 1 deletion src/apm/python_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,9 @@ func TestPythonInjector_Inject(t *testing.T) {
Env: []corev1.EnvVar{
{Name: "PYTHONPATH", Value: "/newrelic-instrumentation"},
{Name: "NEW_RELIC_APP_NAME", Value: "test"},
{Name: "NEW_RELIC_LICENSE_KEY", ValueFrom: &corev1.EnvVarSource{SecretKeyRef: &corev1.SecretKeySelector{LocalObjectReference: corev1.LocalObjectReference{Name: "newrelic-key-secret"}, Key: "new_relic_license_key", Optional: &vtrue}}},
{Name: "NEW_RELIC_LABELS", Value: "operator:auto-injection"},
{Name: "NEW_RELIC_K8S_OPERATOR_ENABLED", Value: "true"},
{Name: "NEW_RELIC_LICENSE_KEY", ValueFrom: &corev1.EnvVarSource{SecretKeyRef: &corev1.SecretKeySelector{LocalObjectReference: corev1.LocalObjectReference{Name: "newrelic-key-secret"}, Key: "new_relic_license_key", Optional: &vtrue}}},
},
VolumeMounts: []corev1.VolumeMount{{Name: "newrelic-instrumentation", MountPath: "/newrelic-instrumentation"}},
}},
Expand Down
3 changes: 2 additions & 1 deletion src/apm/ruby.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,8 @@ func (i *RubyInjector) Inject(ctx context.Context, inst v1alpha2.Instrumentation
})
}

pod = i.injectNewrelicConfig(ctx, inst.Spec.Resource, ns, pod, firstContainer, inst.Spec.LicenseKeySecret)
pod = i.injectNewrelicConfig(ctx, inst.Spec.Resource, ns, pod, firstContainer)
pod.Spec.Containers[firstContainer] = i.injectNewrelicLicenseKeyIntoContainer(*container, inst.Spec.LicenseKeySecret)

return pod, nil
}
2 changes: 1 addition & 1 deletion src/apm/ruby_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,9 @@ func TestRubyInjector_Inject(t *testing.T) {
Env: []corev1.EnvVar{
{Name: "RUBYOPT", Value: "-r /newrelic-instrumentation/lib/boot/strap"},
{Name: "NEW_RELIC_APP_NAME", Value: "test"},
{Name: "NEW_RELIC_LICENSE_KEY", ValueFrom: &corev1.EnvVarSource{SecretKeyRef: &corev1.SecretKeySelector{LocalObjectReference: corev1.LocalObjectReference{Name: "newrelic-key-secret"}, Key: "new_relic_license_key", Optional: &vtrue}}},
{Name: "NEW_RELIC_LABELS", Value: "operator:auto-injection"},
{Name: "NEW_RELIC_K8S_OPERATOR_ENABLED", Value: "true"},
{Name: "NEW_RELIC_LICENSE_KEY", ValueFrom: &corev1.EnvVarSource{SecretKeyRef: &corev1.SecretKeySelector{LocalObjectReference: corev1.LocalObjectReference{Name: "newrelic-key-secret"}, Key: "new_relic_license_key", Optional: &vtrue}}},
},
VolumeMounts: []corev1.VolumeMount{{Name: "newrelic-instrumentation", MountPath: "/newrelic-instrumentation"}},
}},
Expand Down

0 comments on commit c664a75

Please sign in to comment.