Skip to content

Commit

Permalink
feat: php support without annotations - 2
Browse files Browse the repository at this point in the history
  • Loading branch information
danielstokes committed Oct 14, 2024
1 parent eb4880e commit b4d5759
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 21 deletions.
49 changes: 32 additions & 17 deletions src/apm/php.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@ package apm
import (
"context"
"errors"
"strings"

corev1 "k8s.io/api/core/v1"

"github.com/newrelic/k8s-agents-operator/src/api/v1alpha2"
)

const (
annotationPhpVersion = "instrumentation.newrelic.com/php-version"
envIniScanDirKey = "PHP_INI_SCAN_DIR"
envIniScanDirVal = "/newrelic-instrumentation/php-agent/ini"
phpInitContainerName = initContainerName + "-php"
Expand All @@ -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",
Expand All @@ -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 {
Expand All @@ -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
Expand All @@ -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:php>-<version:[0-9].[0-9]>
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")
Expand Down
8 changes: 4 additions & 4 deletions src/apm/php_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit b4d5759

Please sign in to comment.