Skip to content

Commit

Permalink
fix: update to latest version of tinygo and KW's Go SDK
Browse files Browse the repository at this point in the history
Updates to the latest version of TinyGo. Furthermore, the Kubewarden SDK
has been updated to the latest stable release. This version provides a
different way to mock the waPC client, hence some changes had to be done
to our codebase.

Signed-off-by: José Guilherme Vanz <jguilhermevanz@suse.com>
  • Loading branch information
jvanz committed Jun 27, 2024
1 parent 5ebefed commit 2c7b125
Show file tree
Hide file tree
Showing 65 changed files with 25,072 additions and 63 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ policy.wasm: $(SOURCE_FILES) go.mod go.sum
--rm \
-e GOFLAGS="-buildvcs=false" \
-v ${PWD}:/src \
-w /src tinygo/tinygo:0.30.0 \
tinygo build -o policy.wasm -target=wasi -no-debug .
-w /src tinygo/tinygo:0.32.0 \
tinygo build -o policy.wasm -target=wasip1 -no-debug .

artifacthub-pkg.yml: metadata.yml go.mod
$(warning If you are updating the artifacthub-pkg.yml file for a release, \
Expand Down
5 changes: 5 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,13 @@ require (
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/francoispqt/gojay v0.0.0-20181220093123-f2cc13a668ca // indirect
github.com/go-openapi/strfmt v0.21.3 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/stretchr/objx v0.5.2 // indirect
github.com/stretchr/testify v1.9.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)

replace github.com/go-openapi/strfmt => github.com/kubewarden/strfmt v0.1.3
16 changes: 6 additions & 10 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,22 @@ github.com/francoispqt/gojay v0.0.0-20181220093123-f2cc13a668ca h1:F2BD6Vhei4w0r
github.com/francoispqt/gojay v0.0.0-20181220093123-f2cc13a668ca/go.mod h1:H8Wgri1Asi1VevY3ySdpIK5+KCpqzToVswNq8g2xZj4=
github.com/francoispqt/onelog v0.0.0-20190306043706-8c2bb31b10a4 h1:N9eG+1y9e3tnNPXKjssLMa8MumIBDWWoJQWM7htGWUc=
github.com/francoispqt/onelog v0.0.0-20190306043706-8c2bb31b10a4/go.mod h1:v1Il1fkBpjiYPpEJcGxqgrPUPcHuTC7eHh9zBV3CLBE=
github.com/golang/mock v1.6.0 h1:ErTB+efbowRARo13NNdxyJji2egdxLGQhRaY+DUumQc=
github.com/golang/mock v1.6.0/go.mod h1:p6yTPP+5HYm5mzsMV8JkE6ZKdX+/wYM6Hr+LicevLPs=
github.com/kubewarden/k8s-objects v1.29.0-kw1 h1:bVQ2WL1ROqApYmHQJ/yxrs3tssfzzalblE2txChcHxY=
github.com/kubewarden/k8s-objects v1.29.0-kw1/go.mod h1:EMF+Hr26oDR4yQkWJAQpl0M0Ek5ioNXlCswjGZO0G2U=
github.com/kubewarden/policy-sdk-go v0.6.1 h1:MNCwo9tZ2lmumv/7/0h7NczwHlazueSnAJYQEmtDApk=
github.com/kubewarden/policy-sdk-go v0.6.1/go.mod h1:wq/jwnVOpSaETu/n9DW0ePbeoUdoqYTEzlNKmZGaPIM=
github.com/kubewarden/policy-sdk-go v0.7.0 h1:quNOrqtZRgLHpknoIWl0yDWDK8xJ/hDtP8M40HVDPmc=
github.com/kubewarden/policy-sdk-go v0.7.0/go.mod h1:wq/jwnVOpSaETu/n9DW0ePbeoUdoqYTEzlNKmZGaPIM=
github.com/kubewarden/policy-sdk-go v0.8.0 h1:4SR6UeKLBQ+UkwohuMqYw2lPKgqgF5Ifdw7tFNjQwiI=
github.com/kubewarden/policy-sdk-go v0.8.0/go.mod h1:gjYdcErABXti/dxoNW2PceSwy4+/X+o/wuLwWHZCoNU=
github.com/kubewarden/policy-sdk-go v0.11.0 h1:qW2UqARixH2r8KyitSistiAg0Ex0LA+HJr/xaBwiDcg=
github.com/kubewarden/policy-sdk-go v0.11.0/go.mod h1:4Yg/Wpxnt7p4Ps68hBfnK8qoGURM5MJaq67Kjao2smY=
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/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.5.2 h1:xuMeJ0Sdp5ZMRXx/aWO6RZxdr3beISkG5/G/aIRr3pY=
github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
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/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
16 changes: 0 additions & 16 deletions host_native.go

This file was deleted.

12 changes: 0 additions & 12 deletions host_wasi.go

This file was deleted.

5 changes: 3 additions & 2 deletions validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
corev1 "github.com/kubewarden/k8s-objects/api/core/v1"
metav1 "github.com/kubewarden/k8s-objects/apimachinery/pkg/apis/meta/v1"
kubewarden "github.com/kubewarden/policy-sdk-go"
"github.com/kubewarden/policy-sdk-go/pkg/capabilities"
kubernetes "github.com/kubewarden/policy-sdk-go/pkg/capabilities/kubernetes"
kubewarden_protocol "github.com/kubewarden/policy-sdk-go/protocol"
)
Expand All @@ -23,14 +24,14 @@ const CRONJOB_KIND = "cronjob"
const JOB_KIND = "job"
const POD_KIND = "pod"

var host = capabilities.NewHost()

func getNamespace(validationRequest kubewarden_protocol.ValidationRequest) (*corev1.Namespace, error) {

if len(validationRequest.Request.Namespace) == 0 {
return nil, fmt.Errorf("Admission request is missing namespace")
}

host := getWapcHost()

resourceRequest := kubernetes.GetResourceRequest{
APIVersion: "v1",
Kind: "Namespace",
Expand Down
91 changes: 70 additions & 21 deletions validate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ import (
batchv1 "github.com/kubewarden/k8s-objects/api/batch/v1"
corev1 "github.com/kubewarden/k8s-objects/api/core/v1"
metav1 "github.com/kubewarden/k8s-objects/apimachinery/pkg/apis/meta/v1"
capabilities "github.com/kubewarden/policy-sdk-go/pkg/capabilities"

"github.com/kubewarden/policy-sdk-go/pkg/capabilities/kubernetes"
"github.com/kubewarden/policy-sdk-go/pkg/capabilities/mocks"
kubewarden_protocol "github.com/kubewarden/policy-sdk-go/protocol"
kubewarden_testing "github.com/kubewarden/policy-sdk-go/testing"
)
Expand All @@ -34,20 +36,6 @@ func buildValidationRequest(propagatedLabels []string, resource interface{}, kin
}
return payload, nil

}
func buildWapcClient(namespaceLabels map[string]string) error {
clientResponse := corev1.Namespace{
Metadata: &metav1.ObjectMeta{
Labels: namespaceLabels,
},
}
var err error
wapcClient, err = capabilities.NewSuccessfulMockWapcClient(clientResponse)
if err != nil {
return err
}
return nil

}

func basicResposeValidation(responsePayload []byte, accepted, should_mutate bool) (*kubewarden_protocol.ValidationResponse, error) {
Expand Down Expand Up @@ -118,11 +106,32 @@ func TestPodWithNoLabels(t *testing.T) {
t.Errorf("Unexpected error: %+v", err)
}

err = buildWapcClient(namespaceLabels)

wapcRequest, err := json.Marshal(&kubernetes.GetResourceRequest{
APIVersion: "v1",
Kind: "Namespace",
Name: "default",
DisableCache: false,
})
if err != nil {
t.Errorf("Unexpected error: %+v", err)
t.Errorf("Cannot create wapcRequest payload: %+v", err)
}

wapcResponse, err := json.Marshal(&corev1.Namespace{
Metadata: &metav1.ObjectMeta{
Labels: namespaceLabels,
},
})
if err != nil {
t.Errorf("Cannot create wapcResponse payload: %+v", err)
}

wapcClient := mocks.NewMockWapcClient(t)
wapcClient.On("HostCall", "kubewarden", "kubernetes", "get_resource",
wapcRequest).Return(wapcResponse, nil)

host.Client = wapcClient

responsePayload, err := validate(payload)
if err != nil {
t.Errorf("Unexpected error: %+v", err)
Expand Down Expand Up @@ -170,11 +179,31 @@ func TestPodLabelsShouldNotMutateWithItHasTheExpectedValue(t *testing.T) {
t.Errorf("Unexpected error: %+v", err)
}

err = buildWapcClient(namespaceLabels)
wapcRequest, err := json.Marshal(&kubernetes.GetResourceRequest{
APIVersion: "v1",
Kind: "Namespace",
Name: "default",
DisableCache: false,
})
if err != nil {
t.Errorf("Unexpected error: %+v", err)
t.Errorf("Cannot create wapcRequest payload: %+v", err)
}

wapcResponse, err := json.Marshal(&corev1.Namespace{
Metadata: &metav1.ObjectMeta{
Labels: namespaceLabels,
},
})
if err != nil {
t.Errorf("Cannot create wapcResponse payload: %+v", err)
}

wapcClient := mocks.NewMockWapcClient(t)
wapcClient.On("HostCall", "kubewarden", "kubernetes", "get_resource",
wapcRequest).Return(wapcResponse, nil)

host.Client = wapcClient

responsePayload, err := validate(payload)
if err != nil {
t.Errorf("Unexpected error: %+v", err)
Expand Down Expand Up @@ -374,11 +403,31 @@ func TestLabelsShouldOverwrittenLabelsOnlyDefinedInSettings(t *testing.T) {
t.Errorf("Unexpected error: %+v", err)
}

err = buildWapcClient(tc.namespaceLabels)
wapcRequest, err := json.Marshal(&kubernetes.GetResourceRequest{
APIVersion: "v1",
Kind: "Namespace",
Name: "default",
DisableCache: false,
})
if err != nil {
t.Errorf("Unexpected error: %+v", err)
t.Errorf("Cannot create wapcRequest payload: %+v", err)
}

wapcResponse, err := json.Marshal(&corev1.Namespace{
Metadata: &metav1.ObjectMeta{
Labels: tc.namespaceLabels,
},
})
if err != nil {
t.Errorf("Cannot create wapcResponse payload: %+v", err)
}

wapcClient := mocks.NewMockWapcClient(t)
wapcClient.On("HostCall", "kubewarden", "kubernetes", "get_resource",
wapcRequest).Return(wapcResponse, nil)

host.Client = wapcClient

responsePayload, err := validate(payload)
if err != nil {
t.Errorf("Unexpected error: %+v", err)
Expand Down
15 changes: 15 additions & 0 deletions vendor/github.com/davecgh/go-spew/LICENSE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 2c7b125

Please sign in to comment.