From b4d5759a77e4aecb6d5a1d559f2fd9f66292a5e1 Mon Sep 17 00:00:00 2001 From: Daniel Stokes Date: Mon, 14 Oct 2024 14:23:08 -0500 Subject: [PATCH] feat: php support without annotations - 2 --- src/apm/php.go | 49 +++++++++++++++++++++++++++++---------------- src/apm/php_test.go | 8 ++++---- 2 files changed, 36 insertions(+), 21 deletions(-) diff --git a/src/apm/php.go b/src/apm/php.go index f0620d17..84a59974 100644 --- a/src/apm/php.go +++ b/src/apm/php.go @@ -18,6 +18,7 @@ package apm import ( "context" "errors" + "strings" corev1 "k8s.io/api/core/v1" @@ -25,7 +26,6 @@ import ( ) const ( - annotationPhpVersion = "instrumentation.newrelic.com/php-version" envIniScanDirKey = "PHP_INI_SCAN_DIR" envIniScanDirVal = "/newrelic-instrumentation/php-agent/ini" phpInitContainerName = initContainerName + "-php" @@ -34,10 +34,11 @@ const ( var _ Injector = (*PhpInjector)(nil) func init() { - DefaultInjectorRegistry.MustRegister(&PhpInjector{}) + for _, v := range phpAcceptVersions { + DefaultInjectorRegistry.MustRegister(&PhpInjector{acceptVersion: v}) + } } -// Deprecated: phpApiMap is deprecated. Do not use annotations. var phpApiMap = map[string]string{ "7.2": "20170718", "7.3": "20180731", @@ -48,16 +49,26 @@ var phpApiMap = map[string]string{ "8.3": "20230831", } -type PhpInjector struct { - baseInjector -} +var phpAcceptVersions = []acceptVersion{php72, php73, php74, php80, php81, php82, php83} + +const ( + php72 acceptVersion = "php-7.2" + php73 acceptVersion = "php-7.3" + php74 acceptVersion = "php-7.4" + php80 acceptVersion = "php-8.0" + php81 acceptVersion = "php-8.1" + php82 acceptVersion = "php-8.2" + php83 acceptVersion = "php-8.3" +) -func (i *PhpInjector) Language() string { - return "php" +type acceptVersion string + +func (al acceptVersion) Language() string { + return string(al) } -func (i *PhpInjector) acceptable(inst v1alpha2.Instrumentation, pod corev1.Pod) bool { - if inst.Spec.Agent.Language != i.Language() { +func (al acceptVersion) acceptable(inst v1alpha2.Instrumentation, pod corev1.Pod) bool { + if inst.Spec.Agent.Language != string(al) { return false } if len(pod.Spec.Containers) == 0 { @@ -66,8 +77,12 @@ func (i *PhpInjector) acceptable(inst v1alpha2.Instrumentation, pod corev1.Pod) return true } +type PhpInjector struct { + baseInjector + acceptVersion +} + // Inject is used to inject the PHP agent. -// @todo: Currently it uses annotations, which should be removed. This should either use a specific image for each php version or the k8s-agents-operator needs to add support for a language version func (i *PhpInjector) Inject(ctx context.Context, inst v1alpha2.Instrumentation, ns corev1.Namespace, pod corev1.Pod) (corev1.Pod, error) { if !i.acceptable(inst, pod) { return pod, nil @@ -78,14 +93,14 @@ func (i *PhpInjector) Inject(ctx context.Context, inst v1alpha2.Instrumentation, firstContainer := 0 - // exit early if we're missing mandatory annotations - // Deprecated: phpVer is deprecated. Do not use annotations. - phpVer, ok := pod.Annotations[annotationPhpVersion] - if !ok { - return pod, errors.New("missing php version annotation") + // - + lang := strings.SplitN(i.Language(), "-", 2) + if len(lang) != 2 { + // should never happen + return pod, errors.New("missing php version") } + phpVer := lang[1] - // Deprecated: apiNum is deprecated. Do not use annotations. apiNum, ok := phpApiMap[phpVer] if !ok { return pod, errors.New("invalid php version") diff --git a/src/apm/php_test.go b/src/apm/php_test.go index a1c178b9..6a152726 100644 --- a/src/apm/php_test.go +++ b/src/apm/php_test.go @@ -14,7 +14,7 @@ import ( ) func TestPhpInjector_Language(t *testing.T) { - require.Equal(t, "php", (&PhpInjector{}).Language()) + require.Equal(t, "php", (&PhpInjector{acceptVersion: acceptVersion("php")}).Language()) } func TestPhpInjector_Inject(t *testing.T) { @@ -58,7 +58,7 @@ func TestPhpInjector_Inject(t *testing.T) { {Name: "test"}, }}}, expectedErrStr: "licenseKeySecret must not be blank", - inst: v1alpha2.Instrumentation{Spec: v1alpha2.InstrumentationSpec{Agent: v1alpha2.Agent{Language: "php"}}}, + inst: v1alpha2.Instrumentation{Spec: v1alpha2.InstrumentationSpec{Agent: v1alpha2.Agent{Language: "php-8.3"}}}, }, { name: "a container, instrumentation", @@ -90,13 +90,13 @@ func TestPhpInjector_Inject(t *testing.T) { Volumes: []corev1.Volume{{Name: "newrelic-instrumentation", VolumeSource: corev1.VolumeSource{EmptyDir: &corev1.EmptyDirVolumeSource{}}}}, }, }, - inst: v1alpha2.Instrumentation{Spec: v1alpha2.InstrumentationSpec{Agent: v1alpha2.Agent{Language: "php"}, LicenseKeySecret: "newrelic-key-secret"}}, + inst: v1alpha2.Instrumentation{Spec: v1alpha2.InstrumentationSpec{Agent: v1alpha2.Agent{Language: "php-8.3"}, LicenseKeySecret: "newrelic-key-secret"}}, }, } for _, test := range tests { t.Run(test.name, func(t *testing.T) { ctx := context.Background() - i := &PhpInjector{} + i := &PhpInjector{acceptVersion: acceptVersion("php-8.3")} actualPod, err := i.Inject(ctx, test.inst, test.ns, test.pod) errStr := "" if err != nil {