Skip to content

Commit

Permalink
refactor sentry settings
Browse files Browse the repository at this point in the history
Signed-off-by: Maksim Paskal <paskal.maksim@gmail.com>
  • Loading branch information
maksim-paskal committed May 10, 2024
1 parent ce5576a commit d1a5d19
Show file tree
Hide file tree
Showing 17 changed files with 366 additions and 76 deletions.
2 changes: 0 additions & 2 deletions e2e/e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,6 @@ func TestMutation(t *testing.T) { //nolint:funlen
}

for _, test := range tests {
test := test

t.Run(test.name, func(t *testing.T) {
t.Parallel()

Expand Down
2 changes: 1 addition & 1 deletion internal/internal.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func Start(ctx context.Context) error {

log.Info("Creating patch.json...")

if err := os.WriteFile("patch.json", patchBytes, 0o600); err != nil { //nolint:gomnd
if err := os.WriteFile("patch.json", patchBytes, 0o600); err != nil { //nolint:gomnd,mnd
log.WithError(err).Error()
}

Expand Down
11 changes: 11 additions & 0 deletions pkg/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -498,6 +498,7 @@ func GetImageInfo(image string) (*types.ContainerImage, error) {

result := types.ContainerImage{
Domain: reference.Domain(refName),
Path: reference.Path(refName),
Name: image,
Slug: strings.Trim(imageName, "-"),
Tag: "latest",
Expand All @@ -522,8 +523,18 @@ func TestPOD(ctx context.Context, namespace, podName string) ([]byte, error) {
}

input := &MutateInput{
Namespace: &corev1.Namespace{
ObjectMeta: metav1.ObjectMeta{
Name: namespace,
},
},
AdmissionReview: &admissionv1.AdmissionReview{
Request: &admissionv1.AdmissionRequest{
Resource: metav1.GroupVersionResource{
Group: "",
Version: "v1",
Resource: "pods",
},
Object: runtime.RawExtension{
Raw: podJSON,
},
Expand Down
8 changes: 7 additions & 1 deletion pkg/api/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,36 +130,42 @@ func TestGetImageInfo(t *testing.T) {

tests["10.10.10.10:5000/product/main/backend:release-20220516-1"] = &types.ContainerImage{
Domain: "10.10.10.10:5000",
Path: "product/main/backend",
Name: "10.10.10.10:5000/product/main/backend:release-20220516-1",
Slug: "product-main-backend",
Tag: "release-20220516-1",
}
tests["10.10.10.10:5000/product/main/front:release-20220516-1"] = &types.ContainerImage{
Domain: "10.10.10.10:5000",
Path: "product/main/front",
Name: "10.10.10.10:5000/product/main/front:release-20220516-1",
Slug: "product-main-front",
Tag: "release-20220516-1",
}
tests["domain.com/hipages/php-fpm_exporter:1"] = &types.ContainerImage{
Domain: "domain.com",
Path: "hipages/php-fpm_exporter",
Name: "domain.com/hipages/php-fpm_exporter:1",
Slug: "hipages-php-fpm-exporter",
Tag: "1",
}
tests["domain.com/paskalmaksim/envoy-docker-image:v0.3.8"] = &types.ContainerImage{
Domain: "domain.com",
Path: "paskalmaksim/envoy-docker-image",
Name: "domain.com/paskalmaksim/envoy-docker-image:v0.3.8",
Slug: "paskalmaksim-envoy-docker-image",
Tag: "v0.3.8",
}
tests["paskalmaksim/envoy-docker-image:v0.3.8"] = &types.ContainerImage{
Domain: "docker.io",
Path: "paskalmaksim/envoy-docker-image",
Name: "paskalmaksim/envoy-docker-image:v0.3.8",
Slug: "paskalmaksim-envoy-docker-image",
Tag: "v0.3.8",
}
tests["paskalmaksim/envoy-docker-image"] = &types.ContainerImage{
Domain: "docker.io",
Path: "paskalmaksim/envoy-docker-image",
Name: "paskalmaksim/envoy-docker-image",
Slug: "paskalmaksim-envoy-docker-image",
Tag: "latest",
Expand All @@ -172,7 +178,7 @@ func TestGetImageInfo(t *testing.T) {
}

if !reflect.DeepEqual(requre, formattedImage) {
t.Fatalf("must be %s, got %s", requre, formattedImage)
t.Fatalf("must be:\n%+v, got:\n%+v", requre, formattedImage)
}
}
}
2 changes: 0 additions & 2 deletions pkg/conditions/conditions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -440,8 +440,6 @@ func TestCheckConditions(t *testing.T) { //nolint:funlen,maintidx
}

for testID, test := range tests {
test := test

t.Run(strconv.Itoa(testID), func(t *testing.T) {
t.Parallel()

Expand Down
64 changes: 55 additions & 9 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,13 @@ import (
"encoding/json"
"flag"
"os"
"regexp"
"strings"
"time"

"github.com/maksim-paskal/pod-admission-controller/pkg/types"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
"k8s.io/apimachinery/pkg/util/yaml"
)

Expand All @@ -29,6 +32,50 @@ const (
defaultMetricsAddr = ":31080"
)

type SentryPrefix struct {
Pattern string
Name string
}

type Sentry struct {
Endpoint string
Token string
Organization string
Prefixes []*SentryPrefix
// replace sentry DSN with replay
Relay string
// project slug -> image path
Projects map[string]string
Cache map[string]string
}

func (s *Sentry) GetPrefixes(name string) []string {
result := make([]string, 0)

for _, prefix := range s.Prefixes {
if prefix.Pattern == "" {
continue
}

re2, err := regexp.Compile(prefix.Pattern)
if err != nil {
log.Warnf("error in %s regexp.Compile: %v", prefix.Pattern, err)

continue
}

if re2.MatchString(name) {
result = append(result, prefix.Name)
}
}

if prefix := os.Getenv("SENTRY_PROJECTS_PREFIX"); len(prefix) > 0 {
result = append(result, strings.Split(prefix, ",")...)
}

return result
}

type Params struct {
GracePeriodSeconds *int
ConfigFile *string
Expand All @@ -40,13 +87,10 @@ type Params struct {
CertFile *string
KeyFile *string
Rules []*types.Rule
// DefaultRequestCPU *string
// DefaultRequestMemory *string
SentryEndpoint *string
SentryToken *string
SentryDSN *string
CreateSecrets []*types.CreateSecret
IngressSuffix *string
Sentry *Sentry
SentryDSN *string
CreateSecrets []*types.CreateSecret
IngressSuffix *string
}

var param = Params{
Expand All @@ -59,8 +103,6 @@ var param = Params{
MetricsAddr: flag.String("metrics.listen", defaultMetricsAddr, "address to listen on metrics"),
CertFile: flag.String("cert", "server.crt", "certificate file"),
KeyFile: flag.String("key", "server.key", "key file"),
SentryEndpoint: flag.String("sentry.endpoint", "", "sentry endpoint"),
SentryToken: flag.String("sentry.token", "", "sentry token"),
SentryDSN: flag.String("sentry.dsn", os.Getenv("SENTRY_DSN"), "sentry DSN for error reporting"),
IngressSuffix: flag.String("ingress.suffix", os.Getenv("INGRESS_SUFFIX"), "default ingress suffix"),
}
Expand All @@ -73,6 +115,10 @@ func Get() *Params {
return &param
}

func Set(config Params) {
param = config
}

func Load() error {
if len(*param.ConfigFile) == 0 {
return nil
Expand Down
32 changes: 30 additions & 2 deletions pkg/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ package config_test

import (
"flag"
"reflect"
"testing"
"time"

Expand All @@ -33,11 +34,11 @@ func TestConfig(t *testing.T) {

t.Log("config:", config.Get().String())

if *config.Get().SentryEndpoint != "1" {
if *config.Get().CertFile != "1" {
t.Fatal("not valid SentryEndpoint")
}

if *config.Get().SentryToken != "2" {
if *config.Get().KeyFile != "2" {
t.Fatal("not valid SentryToken")
}

Expand Down Expand Up @@ -70,3 +71,30 @@ func TestVersion(t *testing.T) {
t.Fatal("version is not dev")
}
}

func TestSentryPrefix(t *testing.T) {
param := &config.Params{
Sentry: &config.Sentry{
Prefixes: []*config.SentryPrefix{
{
Pattern: "^test$",
Name: "testname",
},
},
},
}

if prefix := param.Sentry.GetPrefixes("aa"); len(prefix) != 0 {
t.Fatal("not valid prefix aa")
}

if prefix := param.Sentry.GetPrefixes("test"); len(prefix) != 1 || prefix[0] != "testname" {
t.Fatal("not valid prefix test")
}

t.Setenv("SENTRY_PROJECTS_PREFIX", "test2,test3")

if prefix := param.Sentry.GetPrefixes("test"); !reflect.DeepEqual(prefix, []string{"testname", "test2", "test3"}) {
t.Fatal("not valid prefix test2, not valid: ", prefix)
}
}
4 changes: 2 additions & 2 deletions pkg/config/testdata/test-config.yaml
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
sentryendpoint: "1"
sentrytoken: "2"
certfile: "1"
keyfile: "2"
2 changes: 0 additions & 2 deletions pkg/patch/custompatch/custompatch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,8 +193,6 @@ func TestCustompatch(t *testing.T) { //nolint:funlen
}

for _, test := range tests {
test := test

t.Run(fmt.Sprintf("%+v", test), func(t *testing.T) {
t.Parallel()

Expand Down
4 changes: 1 addition & 3 deletions pkg/patch/env/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,7 @@ func (p *Patch) FormatEnv(containerInfo *types.ContainerInfo, containersEnv []co

formattedEnv := make([]corev1.EnvVar, 0)

for _, containerEnv := range containersEnv {
item := containerEnv

for _, item := range containersEnv {
item.Value, err = template.Get(containerInfo, item.Value)
if err != nil {
return nil, errors.Wrap(err, "error template value")
Expand Down
2 changes: 0 additions & 2 deletions pkg/patch/patch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,6 @@ func TestIgnorePatch(t *testing.T) { //nolint:funlen
}

for _, test := range tests {
test := test

t.Run(fmt.Sprintf("%+v", test), func(t *testing.T) {
t.Parallel()

Expand Down
2 changes: 0 additions & 2 deletions pkg/patch/resources/resources_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,8 +158,6 @@ func TestGetDefaultResources(t *testing.T) { //nolint:funlen
}

for _, test := range tests {
test := test

t.Run(fmt.Sprintf("%+v", test), func(t *testing.T) {
t.Parallel()

Expand Down
Loading

0 comments on commit d1a5d19

Please sign in to comment.