Skip to content

Commit

Permalink
return object patch
Browse files Browse the repository at this point in the history
Signed-off-by: Pavel Okhlopkov <[email protected]>
  • Loading branch information
Pavel Okhlopkov committed Nov 14, 2024
1 parent 631dc0e commit 9068de4
Show file tree
Hide file tree
Showing 20 changed files with 2,274 additions and 17 deletions.
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ require (
github.com/kennygrant/sanitize v1.2.4
github.com/onsi/ginkgo v1.16.5
github.com/onsi/gomega v1.35.1
github.com/pkg/errors v0.9.1 // indirect
github.com/pkg/errors v0.9.1
github.com/prometheus/client_golang v1.20.5
github.com/stretchr/testify v1.9.0
golang.org/x/time v0.7.0
Expand All @@ -34,7 +34,7 @@ require (
replace github.com/go-openapi/validate => github.com/flant/go-openapi-validate v0.19.12-flant.0

require (
github.com/deckhouse/module-sdk v0.0.0-20241113125902-56440b53be96
github.com/deckhouse/module-sdk v0.0.0-20241114091625-9e129814d38d
github.com/gojuno/minimock/v3 v3.4.1
)

Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/deckhouse/deckhouse/pkg/log v0.0.0-20241106140903-258b93b3334e h1:QUQy+5Bv7/UzhfrytiG3c5gfLGhPppepVbRpbMisVIw=
github.com/deckhouse/deckhouse/pkg/log v0.0.0-20241106140903-258b93b3334e/go.mod h1:Mk5HRzkc5pIcDIZ2JJ6DPuuqnwhXVkb3you8M8Mg+4w=
github.com/deckhouse/module-sdk v0.0.0-20241113125902-56440b53be96 h1:bbA7KCDPWN8gfFl2tRWU2Ik03eS5p+VFz0LC8uqqXCo=
github.com/deckhouse/module-sdk v0.0.0-20241113125902-56440b53be96/go.mod h1:q9jIxjVSVhPRJLWC001c7ujYZIzoPKjiF4LV0Zm+XCE=
github.com/deckhouse/module-sdk v0.0.0-20241114091625-9e129814d38d h1:aHOxJAQcs2GgB7H5Lu6AfFiqrxBO+dyu7GxOn1Rg+OY=
github.com/deckhouse/module-sdk v0.0.0-20241114091625-9e129814d38d/go.mod h1:q9jIxjVSVhPRJLWC001c7ujYZIzoPKjiF4LV0Zm+XCE=
github.com/docker/go-units v0.4.0/go.mod h1:fgPhTUdO+D/Jk86RDLlptpiXQzgHJF7gydDDbaIK4Dk=
github.com/emicklei/go-restful/v3 v3.11.0 h1:rAQeMHw1c7zTmncogyy8VvRZwtkmkZ4FxERmMY4rD+g=
github.com/emicklei/go-restful/v3 v3.11.0/go.mod h1:6n3XBCmQQb25CM2LCACGz8ukIrRry+4bhvbpWn3mrbc=
Expand Down
144 changes: 144 additions & 0 deletions pkg/kube/object-patch/helpers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
package objectpatch

import (
"bytes"
"encoding/json"
"fmt"
"io"

"github.com/deckhouse/module-sdk/pkg/app"
"github.com/deckhouse/module-sdk/pkg/jq"
"gopkg.in/yaml.v3"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
k8yaml "sigs.k8s.io/yaml"

"github.com/flant/kube-client/manifest"
)

func unmarshalFromJSONOrYAML(specs []byte) ([]OperationSpec, error) {
fromJsonSpecs, err := unmarshalFromJson(specs)
if err != nil {
return unmarshalFromYaml(specs)
}

return fromJsonSpecs, nil
}

func unmarshalFromJson(jsonSpecs []byte) ([]OperationSpec, error) {
var specSlice []OperationSpec

dec := json.NewDecoder(bytes.NewReader(jsonSpecs))
for {
var doc OperationSpec
err := dec.Decode(&doc)
if err == io.EOF {
break
}
if err != nil {
return nil, err
}

specSlice = append(specSlice, doc)
}

return specSlice, nil
}

func unmarshalFromYaml(yamlSpecs []byte) ([]OperationSpec, error) {
var specSlice []OperationSpec

dec := yaml.NewDecoder(bytes.NewReader(yamlSpecs))
for {
var doc OperationSpec
err := dec.Decode(&doc)
if err == io.EOF {
break
}
if err != nil {
return nil, err
}

specSlice = append(specSlice, doc)
}

return specSlice, nil
}

func applyJQPatch(jqFilter string, obj *unstructured.Unstructured) (*unstructured.Unstructured, error) {
objBytes, err := obj.MarshalJSON()
if err != nil {
return nil, err
}

filterResult, err := jq.ApplyJqFilter(jqFilter, objBytes, app.JqLibraryPath)
if err != nil {
return nil, fmt.Errorf("failed to apply jqFilter:\n%sto Object:\n%s\n"+
"error: %s", jqFilter, obj, err)
}

retObj := &unstructured.Unstructured{}
_, _, err = unstructured.UnstructuredJSONScheme.Decode([]byte(filterResult), nil, retObj)
if err != nil {
return nil, fmt.Errorf("failed to convert filterResult:\n%s\nto Unstructured Object\nerror: %s", filterResult, err)
}

return retObj, nil
}

func generateSubresources(subresource string) (ret []string) {
if subresource != "" {
ret = append(ret, subresource)
}

return
}

func toUnstructured(obj interface{}) (*unstructured.Unstructured, error) {
switch v := obj.(type) {
case []byte:
mft, err := manifest.NewFromYAML(string(v))
if err != nil {
return nil, err
}
return mft.Unstructured(), nil
case string:
mft, err := manifest.NewFromYAML(v)
if err != nil {
return nil, err
}
return mft.Unstructured(), nil
case map[string]interface{}:
return &unstructured.Unstructured{Object: v}, nil
default:
objectContent, err := runtime.DefaultUnstructuredConverter.ToUnstructured(obj)
if err != nil {
return nil, fmt.Errorf("convert to unstructured: %v", err)
}
return &unstructured.Unstructured{Object: objectContent}, nil
}
}

func convertPatchToBytes(patch interface{}) ([]byte, error) {
var err error
var intermediate interface{}
switch v := patch.(type) {
case []byte:
err = k8yaml.Unmarshal(v, &intermediate)
case string:
err = k8yaml.Unmarshal([]byte(v), &intermediate)
default:
intermediate = v
}
if err != nil {
return nil, err
}

// Try to encode to JSON.
var patchBytes []byte
patchBytes, err = json.Marshal(intermediate)
if err != nil {
return nil, err
}
return patchBytes, nil
}
Loading

0 comments on commit 9068de4

Please sign in to comment.