Skip to content

Commit

Permalink
Merge pull request kubevirt#4984 from ashleyschuett/patches-generic-m…
Browse files Browse the repository at this point in the history
…atch

Patches generic match
  • Loading branch information
kubevirt-bot authored Feb 11, 2021
2 parents 483bef0 + 0974e67 commit 951e329
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 5 deletions.
6 changes: 6 additions & 0 deletions api/openapi-spec/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -9242,6 +9242,12 @@
},
"v1.CustomizeComponentsPatch": {
"type": "object",
"required": [
"resourceName",
"resourceType",
"patch",
"type"
],
"properties": {
"patch": {
"type": "string"
Expand Down
5 changes: 5 additions & 0 deletions manifests/generated/kv-resource.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,11 @@ spec:
type: string
type:
type: string
required:
- patch
- resourceName
- resourceType
- type
type: object
type: array
x-kubernetes-list-type: atomic
Expand Down
10 changes: 9 additions & 1 deletion pkg/virt-operator/resource/apply/patches.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,14 +181,22 @@ func (c *Customizer) GetPatchesForResource(resourceType, name string) []v1.Custo
patches := make([]v1.CustomizeComponentsPatch, 0)

for _, p := range allPatches {
if strings.EqualFold(p.ResourceType, resourceType) && strings.EqualFold(p.ResourceName, name) {
if valueMatchesKey(p.ResourceType, resourceType) && valueMatchesKey(p.ResourceName, name) {
patches = append(patches, p)
}
}

return patches
}

func valueMatchesKey(value, key string) bool {
if value == "*" {
return true
}

return strings.EqualFold(key, value)
}

func getHash(customizations v1.CustomizeComponents) (string, error) {
// #nosec CWE: 326 - Use of weak cryptographic primitive (http://cwe.mitre.org/data/definitions/326.html)
// reason: sha1 is not used for encryption but for creating a hash value
Expand Down
20 changes: 20 additions & 0 deletions pkg/virt-operator/resource/apply/patches_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ package apply

import (
. "github.com/onsi/ginkgo"
"github.com/onsi/ginkgo/extensions/table"
. "github.com/onsi/gomega"

"kubevirt.io/client-go/log"
Expand Down Expand Up @@ -57,6 +58,12 @@ var _ = Describe("Patches", func() {
Patch: `{"metadata":{"labels":{"new-key":"added-this-label"}}}`,
Type: v1.StrategicMergePatchType,
},
{
ResourceName: "*",
ResourceType: "Deployment",
Patch: `{"spec":{"template":{"spec":{"imagePullSecrets":[{"name":"image-pull"}]}}}}`,
Type: v1.StrategicMergePatchType,
},
},
})

Expand All @@ -75,6 +82,7 @@ var _ = Describe("Patches", func() {
err := config.GenericApplyPatches(deployments)
Expect(err).ToNot(HaveOccurred())
Expect(deployment.ObjectMeta.Labels["new-key"]).To(Equal("added-this-label"))
Expect(deployment.Spec.Template.Spec.ImagePullSecrets[0].Name).To(Equal("image-pull"))

err = config.GenericApplyPatches([]string{"string"})
Expect(err).To(HaveOccurred())
Expand Down Expand Up @@ -146,4 +154,16 @@ var _ = Describe("Patches", func() {
Expect(h1).To(Equal(h2))
})
})

table.DescribeTable("valueMatchesKey", func(value, key string, expected bool) {

matches := valueMatchesKey(value, key)
Expect(matches).To(Equal(expected))

},
table.Entry("should match wildcard", "*", "Deployment", true),
table.Entry("should match with different cases", "deployment", "Deployment", true),
table.Entry("should not match", "Service", "Deployment", false),
)

})
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,11 @@ var CRDsValidation map[string]string = map[string]string{
type: string
type:
type: string
required:
- patch
- resourceName
- resourceType
- type
type: object
type: array
x-kubernetes-list-type: atomic
Expand Down

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

8 changes: 4 additions & 4 deletions staging/src/kubevirt.io/client-go/api/v1/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -1352,10 +1352,10 @@ type CustomizeComponents struct {

// +k8s:openapi-gen=true
type CustomizeComponentsPatch struct {
ResourceName string `json:"resourceName,omitempty"`
ResourceType string `json:"resourceType,omitempty"`
Patch string `json:"patch,omitempty"`
Type PatchType `json:"type,omitempty"`
ResourceName string `json:"resourceName"`
ResourceType string `json:"resourceType"`
Patch string `json:"patch"`
Type PatchType `json:"type"`
}

type PatchType string
Expand Down

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

0 comments on commit 951e329

Please sign in to comment.