Skip to content

Commit

Permalink
Use VM apis from toolchain-common
Browse files Browse the repository at this point in the history
  • Loading branch information
rajivnathan committed Oct 12, 2023
1 parent 8c20bc4 commit 2d2d889
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 154 deletions.
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ require (
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/openshift/library-go v0.0.0-20230301092340-c13b89190a26 // indirect
github.com/pborman/uuid v1.2.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/prometheus/client_model v0.2.0 // indirect
github.com/prometheus/common v0.32.1 // indirect
Expand Down Expand Up @@ -109,4 +110,6 @@ require (
sigs.k8s.io/yaml v1.3.0 // indirect
)

replace github.com/codeready-toolchain/toolchain-common => github.com/rajivnathan/toolchain-common v0.0.0-20231012005117-897cfe93e4f3

go 1.19
7 changes: 5 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,6 @@ github.com/cockroachdb/errors v1.2.4/go.mod h1:rQD95gz6FARkaKkQXUksEje/d9a6wBJoC
github.com/cockroachdb/logtags v0.0.0-20190617123548-eb05cc24525f/go.mod h1:i/u985jwjWRlyHXQbwatDASoW0RMlZ/3i9yJHE2xLkI=
github.com/codeready-toolchain/api v0.0.0-20230918195153-739e8fb09a33 h1:hxXfcFq2JgFISVxrkISg8m9DZMzpcPWRjPspx3M3Sxo=
github.com/codeready-toolchain/api v0.0.0-20230918195153-739e8fb09a33/go.mod h1:nn3W6eKb9PFIVwSwZW7wDeLACMBOwAV+4kddGuN+ARM=
github.com/codeready-toolchain/toolchain-common v0.0.0-20230920120310-0f59f17bca92 h1:9gcDMkjSAjxM3RLUYCHv9prCDwdi7IgAWpTwTsDpGL8=
github.com/codeready-toolchain/toolchain-common v0.0.0-20230920120310-0f59f17bca92/go.mod h1:+rs3V8do2s0DzGPyCy2sgnvUs9GfSi5RVWx+AZC+cTM=
github.com/coreos/bbolt v1.3.2/go.mod h1:iRUV2dpdMOn7Bo10OQBFzIJO9kkE559Wcmn+qkEiiKk=
github.com/coreos/etcd v3.3.10+incompatible/go.mod h1:uF7uidLiAD3TWHmW31ZFd/JWoc32PjwdhPthX9715RE=
github.com/coreos/etcd v3.3.13+incompatible/go.mod h1:uF7uidLiAD3TWHmW31ZFd/JWoc32PjwdhPthX9715RE=
Expand Down Expand Up @@ -324,6 +322,7 @@ github.com/google/pprof v0.0.0-20210609004039-a478d1d731e9/go.mod h1:kpwsk12EmLe
github.com/google/pprof v0.0.0-20210720184732-4bb14d4b1be1/go.mod h1:kpwsk12EmLew5upagYY7GY0pfYCcupk39gWOCRROcvE=
github.com/google/renameio v0.1.0/go.mod h1:KWCgfxg9yswjAJkECMjeO8J8rahYeXnNhOm40UhjYkI=
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510/go.mod h1:pupxD2MaaD3pAXIBCelhxNneeOaAeabZDe5s4K6zSpQ=
github.com/google/uuid v1.0.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/google/uuid v1.1.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/google/uuid v1.1.2 h1:EVhdT+1Kseyi1/pUmXKaFxYsDNy9RQYkMWRH68J/W7Y=
github.com/google/uuid v1.1.2/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
Expand Down Expand Up @@ -488,6 +487,8 @@ github.com/openshift/library-go v0.0.0-20230301092340-c13b89190a26 h1:vXYT3dX03F
github.com/openshift/library-go v0.0.0-20230301092340-c13b89190a26/go.mod h1:KPBAXGaq7pPmA+1wUVtKr5Axg3R68IomWDkzaOxIhxM=
github.com/opentracing/opentracing-go v1.1.0/go.mod h1:UkNAQd3GIcIGf0SeVgPpRdFStlNbqXla1AfSYxPUl2o=
github.com/pascaldekloe/goe v0.0.0-20180627143212-57f6aae5913c/go.mod h1:lzWF7FIEvWOWxwDKqyGYQf6ZUaNfKdP144TG7ZOy1lc=
github.com/pborman/uuid v1.2.1 h1:+ZZIw58t/ozdjRaXh/3awHfmWRbzYxJoAdNJxe/3pvw=
github.com/pborman/uuid v1.2.1/go.mod h1:X/NO0urCmaxf9VXbdlT7C2Yzkj2IKimNn4k+gtPdI/k=
github.com/pelletier/go-toml v1.2.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic=
github.com/pelletier/go-toml v1.9.3/go.mod h1:u1nR/EPcESfeI/szUZKdtJ0xRNbUoANCkoOuaOx1Y+c=
github.com/peterbourgon/diskv v2.0.1+incompatible/go.mod h1:uqqh8zWWbv1HBMNONnaR/tNboyR3/BZd58JJSHlUSCU=
Expand Down Expand Up @@ -528,6 +529,8 @@ github.com/prometheus/procfs v0.6.0/go.mod h1:cz+aTbrPOrUb4q7XlbU9ygM+/jj0fzG6c1
github.com/prometheus/procfs v0.7.3 h1:4jVXhlkAyzOScmCkXBTOLRLTz8EeU+eyjrwB/EPq0VU=
github.com/prometheus/procfs v0.7.3/go.mod h1:cz+aTbrPOrUb4q7XlbU9ygM+/jj0fzG6c1xBZuNvfVA=
github.com/prometheus/tsdb v0.7.1/go.mod h1:qhTCs0VvXwvX/y3TZrWD7rabWM+ijKTux40TwIPHuXU=
github.com/rajivnathan/toolchain-common v0.0.0-20231012005117-897cfe93e4f3 h1:oS3bS8vaqob5PgJwf1A1oaqCfhwdXPmyKQwnOFMcJio=
github.com/rajivnathan/toolchain-common v0.0.0-20231012005117-897cfe93e4f3/go.mod h1:2Jez02Pv6IPzZnEB9cQfH4GTQzbOl+RY4fYC1w9/6/4=
github.com/redhat-cop/operator-utils v1.3.3-0.20220121120056-862ef22b8cdf h1:fsZiv9XuFo8G7IyzFWjG02vqzJG7kSqFvD1Wiq3V/o8=
github.com/redhat-cop/operator-utils v1.3.3-0.20220121120056-862ef22b8cdf/go.mod h1:FfTyeSCu+e2VLgeMh/1RFG8TSkVjKRPEyR6EmDt0RIw=
github.com/rogpeppe/fastuuid v0.0.0-20150106093220-6724a57986af/go.mod h1:XWv6SoW27p1b0cqNHllgS5HIMJraePCO15w5zCzIWYg=
Expand Down
127 changes: 0 additions & 127 deletions pkg/webhook/mutatingwebhook/types/virtualmachine.go

This file was deleted.

6 changes: 3 additions & 3 deletions pkg/webhook/mutatingwebhook/vm_mutate.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"encoding/json"
"net/http"

"github.com/codeready-toolchain/member-operator/pkg/webhook/mutatingwebhook/types"
vmapiv1 "github.com/codeready-toolchain/toolchain-common/pkg/virtualmachine/api/v1"

"github.com/pkg/errors"
admissionv1 "k8s.io/api/admission/v1"
Expand All @@ -21,7 +21,7 @@ func HandleMutateVirtualMachines(w http.ResponseWriter, r *http.Request) {
func vmMutator(admReview admissionv1.AdmissionReview) *admissionv1.AdmissionResponse {

// unmarshal the object to be sure that it's a VirtualMachine
vm := &types.VirtualMachine{}
vm := &vmapiv1.VirtualMachine{}
if err := json.Unmarshal(admReview.Request.Object.Raw, vm); err != nil {
vmLogger.Error(err, "unable unmarshal VirtualMachine json object", "AdmissionReview", admReview)
return responseWithError(errors.Wrapf(err, "unable unmarshal VirtualMachine json object - raw request object: %v", admReview.Request.Object.Raw))
Expand Down Expand Up @@ -58,7 +58,7 @@ func vmMutator(admReview admissionv1.AdmissionReview) *admissionv1.AdmissionResp
// The issue is that if the namespace has LimitRanges defined and the VirtualMachine resource does not have resource limits defined then it will use the LimitRanges which may be less than requested
// resources and the VirtualMachine will fail to start.
// This should be removed once https://issues.redhat.com/browse/CNV-32069 is complete.
func ensureLimits(vm *types.VirtualMachine, patchItems []map[string]interface{}) []map[string]interface{} {
func ensureLimits(vm *vmapiv1.VirtualMachine, patchItems []map[string]interface{}) []map[string]interface{} {
if vm.Spec.Template.Spec.Domain.Resources.Requests == nil {
return patchItems
}
Expand Down
31 changes: 9 additions & 22 deletions pkg/webhook/mutatingwebhook/vm_mutate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import (
"fmt"
"testing"

"github.com/codeready-toolchain/member-operator/pkg/webhook/mutatingwebhook/types"
vmapi "github.com/codeready-toolchain/toolchain-common/pkg/virtualmachine/api"
vmapiv1 "github.com/codeready-toolchain/toolchain-common/pkg/virtualmachine/api/v1"
"github.com/stretchr/testify/require"

admissionv1 "k8s.io/api/admission/v1"
Expand All @@ -30,7 +31,7 @@ func TestMutateVMSuccess(t *testing.T) {
t.Run("only memory request is set", func(t *testing.T) {
// given
req := ResourceList("1Gi", "")
vmAdmReview := vmAdmissionReview(t, withRequests(req))
vmAdmReview := vmAdmissionReview(t, vmapi.WithRequests(req))

// when
response := mutate(podLogger, vmAdmReview, vmMutator)
Expand All @@ -44,7 +45,7 @@ func TestMutateVMSuccess(t *testing.T) {
t.Run("memory and cpu requests are set", func(t *testing.T) {
// given
req := ResourceList("1Gi", "1")
vmAdmReview := vmAdmissionReview(t, withRequests(req))
vmAdmReview := vmAdmissionReview(t, vmapi.WithRequests(req))

// when
response := mutate(podLogger, vmAdmReview, vmMutator)
Expand All @@ -59,7 +60,7 @@ func TestMutateVMSuccess(t *testing.T) {
// given
req := ResourceList("1Gi", "1")
lim := ResourceList("2Gi", "2")
vmAdmReview := vmAdmissionReview(t, withRequests(req), withLimits(lim))
vmAdmReview := vmAdmissionReview(t, vmapi.WithRequests(req), vmapi.WithLimits(lim))

// when
response := mutate(podLogger, vmAdmReview, vmMutator)
Expand All @@ -73,7 +74,7 @@ func TestMutateVMSuccess(t *testing.T) {
// given
req := ResourceList("1Gi", "1")
lim := ResourceList("2Gi", "")
vmAdmReview := vmAdmissionReview(t, withRequests(req), withLimits(lim))
vmAdmReview := vmAdmissionReview(t, vmapi.WithRequests(req), vmapi.WithLimits(lim))

// when
response := mutate(podLogger, vmAdmReview, vmMutator)
Expand All @@ -88,7 +89,7 @@ func TestMutateVMSuccess(t *testing.T) {
// given
req := ResourceList("1Gi", "1")
lim := ResourceList("", "2")
vmAdmReview := vmAdmissionReview(t, withRequests(req), withLimits(lim))
vmAdmReview := vmAdmissionReview(t, vmapi.WithRequests(req), vmapi.WithLimits(lim))

// when
response := mutate(podLogger, vmAdmReview, vmMutator)
Expand Down Expand Up @@ -171,12 +172,12 @@ func vmSuccessResponse(options ...vmSuccessResponseOption) expectedSuccessRespon
return *resp
}

func vmAdmissionReview(t *testing.T, vmOptions ...vmOption) []byte {
func vmAdmissionReview(t *testing.T, vmOptions ...vmapi.VMOption) []byte {
admReview := &admissionv1.AdmissionReview{}
err := json.Unmarshal([]byte(vmRawAdmissionReviewJSONTemplate), admReview)
require.NoError(t, err)

vm := &types.VirtualMachine{}
vm := &vmapiv1.VirtualMachine{}
err = json.Unmarshal(admReview.Request.Object.Raw, vm)
require.NoError(t, err)

Expand All @@ -194,20 +195,6 @@ func vmAdmissionReview(t *testing.T, vmOptions ...vmOption) []byte {
return admReviewJSON
}

type vmOption func(*types.VirtualMachine)

func withRequests(requests corev1.ResourceList) vmOption {
return func(vm *types.VirtualMachine) {
vm.Spec.Template.Spec.Domain.Resources.Requests = requests
}
}

func withLimits(limits corev1.ResourceList) vmOption {
return func(vm *types.VirtualMachine) {
vm.Spec.Template.Spec.Domain.Resources.Limits = limits
}
}

var vmRawAdmissionReviewJSONTemplate = `{
"kind": "AdmissionReview",
"apiVersion": "admission.k8s.io/v1",
Expand Down

0 comments on commit 2d2d889

Please sign in to comment.