Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: remove easyjson dependency #34

Merged
merged 5 commits into from
Jul 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
13 changes: 2 additions & 11 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,14 @@ SOURCE_FILES := $(shell find . -type f -name '*.go')
# starting with v.
VERSION ?= $(shell git describe | cut -c2-)

policy.wasm: $(SOURCE_FILES) go.mod go.sum types_easyjson.go
policy.wasm: $(SOURCE_FILES) go.mod go.sum
docker run \
--rm \
-e GOFLAGS="-buildvcs=false" \
-v ${PWD}:/src \
-w /src tinygo/tinygo:0.27.0 \
-w /src tinygo/tinygo:0.28.1 \
tinygo build -o policy.wasm -target=wasi -no-debug .


artifacthub-pkg.yml: metadata.yml go.mod
$(warning If you are updating the artifacthub-pkg.yml file for a release, \
remember to set the VERSION variable with the proper value. \
Expand All @@ -24,14 +23,6 @@ artifacthub-pkg.yml: metadata.yml go.mod
annotated-policy.wasm: policy.wasm metadata.yml artifacthub-pkg.yml
kwctl annotate -m metadata.yml -u README.md -o annotated-policy.wasm policy.wasm

.PHONY: generate-easyjson
types_easyjson.go: types.go
docker run \
--rm \
-v ${PWD}:/src \
-w /src \
golang:1.20-alpine ./hack/generate-easyjson.sh

.PHONY: test
test:
go test -v
Expand Down
10 changes: 5 additions & 5 deletions artifacthub-pkg.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,27 @@
#
# This config can be saved to its default location with:
# kwctl scaffold artifacthub > artifacthub-pkg.yml
version: 0.1.13
version: 0.1.14
name: safe-labels
displayName: Safe Labels
createdAt: 2023-07-07T18:45:46.064822442Z
createdAt: 2023-07-28T08:50:55.333527563Z
description: A policy that validates Kubernetes' resource labels
license: Apache-2.0
homeURL: https://github.com/kubewarden/safe-labels-policy
containersImages:
- name: policy
image: ghcr.io/kubewarden/policies/safe-labels:v0.1.13
image: ghcr.io/kubewarden/policies/safe-labels:v0.1.14
keywords:
- labels
links:
- name: policy
url: https://github.com/kubewarden/safe-labels-policy/releases/download/v0.1.13/policy.wasm
url: https://github.com/kubewarden/safe-labels-policy/releases/download/v0.1.14/policy.wasm
- name: source
url: https://github.com/kubewarden/safe-labels-policy
install: |
The policy can be obtained using [`kwctl`](https://github.com/kubewarden/kwctl):
```console
kwctl pull ghcr.io/kubewarden/policies/safe-labels:v0.1.13
kwctl pull ghcr.io/kubewarden/policies/safe-labels:v0.1.14
```
maintainers:
- name: Kubewarden developers
Expand Down
8 changes: 3 additions & 5 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,18 @@ module github.com/kubewarden/safe-labels-policy

go 1.20

replace github.com/go-openapi/strfmt => github.com/kubewarden/strfmt v0.1.2
replace github.com/go-openapi/strfmt => github.com/kubewarden/strfmt v0.1.3

require (
github.com/deckarep/golang-set/v2 v2.3.0
github.com/kubewarden/gjson v1.7.2
github.com/kubewarden/policy-sdk-go v0.4.1
github.com/mailru/easyjson v0.7.7
github.com/kubewarden/policy-sdk-go v0.5.0
github.com/wapc/wapc-guest-tinygo v0.3.3
)

require (
github.com/go-openapi/strfmt v0.21.3 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/kubewarden/k8s-objects v1.24.0-kw7 // indirect
github.com/kubewarden/k8s-objects v1.27.0-kw2 // indirect
github.com/tidwall/match v1.0.3 // indirect
github.com/tidwall/pretty v1.0.2 // indirect
)
26 changes: 10 additions & 16 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,26 +1,20 @@
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/deckarep/golang-set/v2 v2.3.0 h1:qs18EKUfHm2X9fA50Mr/M5hccg2tNnVqsiBImnyDs0g=
github.com/deckarep/golang-set/v2 v2.3.0/go.mod h1:VAky9rY/yGXJOLEDv3OMci+7wtDpOF4IN+y82NBOac4=
github.com/josharian/intern v1.0.0 h1:vlS4z54oSdjm0bgjRigI+G1HpF+tI+9rE5LLzOg8HmY=
github.com/josharian/intern v1.0.0/go.mod h1:5DoeVV0s6jJacbCEi61lwdGj/aVlrQvzHFFd8Hwg//Y=
github.com/kubewarden/gjson v1.7.2 h1:+cLRfPqyvjwrkgE68cGdNfUy1Z6L45DjvuFH9/ofwQ0=
github.com/kubewarden/gjson v1.7.2/go.mod h1:jSlxpubGqBG5HHe4v8kwcc8z1JF2reJ7AJAUojw66AY=
github.com/kubewarden/k8s-objects v1.24.0-kw4 h1:/XAEdK8uHUz+asM/PgUs1T4vgCN7d+tiDDb5azMOimo=
github.com/kubewarden/k8s-objects v1.24.0-kw4/go.mod h1:0d8vhSnO2G4bwo58G9ncQYhsgBggGzgh+V0Wqc3CLe8=
github.com/kubewarden/k8s-objects v1.24.0-kw7 h1:uXGSLyyp/qnipq7yNxFy5zebFRy8utoO+wn1fW6QkkU=
github.com/kubewarden/k8s-objects v1.24.0-kw7/go.mod h1:IuIHLG1JtxjC1JnY7SyEEA9MukCh/FACcwpzaBjgdLQ=
github.com/kubewarden/policy-sdk-go v0.3.0 h1:5WqhrC3eJP+gRti14d4vCyXCITeM95X4hob6QA96eX8=
github.com/kubewarden/policy-sdk-go v0.3.0/go.mod h1:zJLxhZkoFVKOlHJ3mlbG8qTCvVbkZprZ7odVwVsunUo=
github.com/kubewarden/policy-sdk-go v0.4.0 h1:qxYHarQ3fHD90QErZEjXRzErEOXLQVtTYKmtyv0rtMQ=
github.com/kubewarden/policy-sdk-go v0.4.0/go.mod h1:pY1FrcuGdhnzzN31wNieAimI4+7rYWbtkP+tYGna0Ug=
github.com/kubewarden/policy-sdk-go v0.4.1 h1:MTGxJaWWH6dZBwCdZ+FYVUclxveGzW3p4kuUJiZw+7M=
github.com/kubewarden/policy-sdk-go v0.4.1/go.mod h1:pY1FrcuGdhnzzN31wNieAimI4+7rYWbtkP+tYGna0Ug=
github.com/kubewarden/strfmt v0.1.2 h1:S0YUVkPeyUMikz8QssbMzfd1MC5K8ZqxLI2zfF8euMs=
github.com/kubewarden/strfmt v0.1.2/go.mod h1:sqLlis8qlm4A4pnZsRyRjNxyH86fiM+7Ee7bO+uJk94=
github.com/mailru/easyjson v0.7.7 h1:UGYAvKxe3sBsEDzO8ZeWOSlIQfWFlxbzLZe7hwFURr0=
github.com/mailru/easyjson v0.7.7/go.mod h1:xzfreul335JAWq5oZzymOObrkdz5UnU4kGfJJLY9Nlc=
github.com/kubewarden/k8s-objects v1.27.0-kw2 h1:6ZA72SFtDSbCupwxlIyJimUzN0nSweMCUx5jUEnoxkw=
github.com/kubewarden/k8s-objects v1.27.0-kw2/go.mod h1:wVx4Rg1HKml8yewPwAHUWp1wYSAtBlKV/tsmteDuK1g=
github.com/kubewarden/policy-sdk-go v0.5.0 h1:JnSRf5pHjFzTNNp6jJbSP5a4cwzFzkUBjLujqJd+Z+w=
github.com/kubewarden/policy-sdk-go v0.5.0/go.mod h1:1IZXauwI5iCuOZj7tU58nE/SZFb/HsCmj3ZpDVStVQs=
github.com/kubewarden/strfmt v0.1.3 h1:bb+2rbotioROjCkziSt+hqnHXzOlumN94NxDKdV2kPI=
github.com/kubewarden/strfmt v0.1.3/go.mod h1:DXoaaIYwqW1LyyRoMeyxfHUU+VUSTNFdj38juCXfRzs=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/tidwall/match v1.0.3 h1:FQUVvBImDutD8wJLN6c5eMzWtjgONK9MwIBCOrUJKeE=
github.com/tidwall/match v1.0.3/go.mod h1:eRSPERbgtNPcGhD8UCthc6PmLEQXEWd3PRB5JTxsfmM=
github.com/tidwall/pretty v1.0.2 h1:Z7S3cePv9Jwm1KwS0513MRaoUe3S01WPbLNV40pwWZU=
github.com/tidwall/pretty v1.0.2/go.mod h1:XNkn88O1ChpSDQmQeStsy+sBenx6DDtFZJxhVysOjyk=
github.com/wapc/wapc-guest-tinygo v0.3.3 h1:jLebiwjVSHLGnS+BRabQ6+XOV7oihVWAc05Hf1SbeR0=
github.com/wapc/wapc-guest-tinygo v0.3.3/go.mod h1:mzM3CnsdSYktfPkaBdZ8v88ZlfUDEy5Jh5XBOV3fYcw=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
33 changes: 0 additions & 33 deletions hack/generate-easyjson.sh

This file was deleted.

61 changes: 34 additions & 27 deletions settings.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package main

import (
"encoding/json"
"fmt"
"regexp"
"strings"

mapset "github.com/deckarep/golang-set/v2"
"github.com/kubewarden/gjson"
kubewarden "github.com/kubewarden/policy-sdk-go"
easyjson "github.com/mailru/easyjson"
)

// A wrapper around the standard regexp.Regexp struct
Expand Down Expand Up @@ -53,26 +53,6 @@ type Settings struct {
ConstrainedLabels map[string]*RegularExpression `json:"constrained_labels"`
}

func NewSettingsFromRaw(rawSettings *RawSettings) (Settings, error) {
deniedLabels := mapset.NewThreadUnsafeSet[string](rawSettings.DeniedLabels...)
mandatoryLabels := mapset.NewThreadUnsafeSet[string](rawSettings.MandatoryLabels...)

constrainedLabels := make(map[string]*RegularExpression)
for key, value := range rawSettings.ConstrainedLabels {
re, err := CompileRegularExpression(value)
if err != nil {
return Settings{}, err
}
constrainedLabels[key] = re
}

return Settings{
DeniedLabels: deniedLabels,
MandatoryLabels: mandatoryLabels,
ConstrainedLabels: constrainedLabels,
}, nil
}

// Builds a new Settings instance starting from a validation
// request payload:
//
Expand All @@ -87,13 +67,13 @@ func NewSettingsFromRaw(rawSettings *RawSettings) (Settings, error) {
func NewSettingsFromValidationReq(payload []byte) (Settings, error) {
settingsJson := gjson.GetBytes(payload, "settings")

rawSettings := RawSettings{}
err := easyjson.Unmarshal([]byte(settingsJson.Raw), &rawSettings)
settings := Settings{}
err := json.Unmarshal([]byte(settingsJson.Raw), &settings)
if err != nil {
return Settings{}, err
}

return NewSettingsFromRaw(&rawSettings)
return settings, nil
}

// Builds a new Settings instance starting from a Settings
Expand All @@ -104,13 +84,13 @@ func NewSettingsFromValidationReq(payload []byte) (Settings, error) {
// "constrained_labels": { ... }
// }
func NewSettingsFromValidateSettingsPayload(payload []byte) (Settings, error) {
rawSettings := RawSettings{}
err := easyjson.Unmarshal(payload, &rawSettings)
settings := Settings{}
err := json.Unmarshal(payload, &settings)
if err != nil {
return Settings{}, err
}

return NewSettingsFromRaw(&rawSettings)
return settings, nil
}

func (s *Settings) Valid() (bool, error) {
Expand Down Expand Up @@ -152,6 +132,33 @@ func (s *Settings) Valid() (bool, error) {
return true, nil
}

func (s *Settings) UnmarshalJSON(data []byte) error {
rawSettings := struct {
DeniedLabels []string `json:"denied_labels"`
MandatoryLabels []string `json:"mandatory_labels"`
ConstrainedLabels map[string]string `json:"constrained_labels"`
}{}

err := json.Unmarshal(data, &rawSettings)
if err != nil {
return err
}

s.DeniedLabels = mapset.NewThreadUnsafeSet[string](rawSettings.DeniedLabels...)
s.MandatoryLabels = mapset.NewThreadUnsafeSet[string](rawSettings.MandatoryLabels...)

s.ConstrainedLabels = make(map[string]*RegularExpression)
for key, value := range rawSettings.ConstrainedLabels {
re, err := CompileRegularExpression(value)
if err != nil {
return err
}
s.ConstrainedLabels[key] = re
}

return nil
}

func validateSettings(payload []byte) ([]byte, error) {
settings, err := NewSettingsFromValidateSettingsPayload(payload)
if err != nil {
Expand Down
7 changes: 0 additions & 7 deletions types.go

This file was deleted.

Loading
Loading