-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add defaults plugin * update deps * remove var * simplify * cleanup tests * use pointer * refactor cli, add config pkg * add test * update tests/readme * check err * write custom manifest encoder * move things around * simplify some code * remove struct tags * add json tags * update .licensed.yaml * update licensed-go image * use struct instead of pointer * update licensed * user jsonpatch instead * updates tests * merge function * update deps * update images * make tests more readable * add desc * use yaml encoder for encoding resources * fix tests * update test * remove separator * use nil config to skip merge when no defaults are specified
- Loading branch information
1 parent
adba3c0
commit 5cffae7
Showing
11 changed files
with
298 additions
and
208 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
FROM golang:1.16 as build | ||
FROM golang:1.20 as build | ||
ARG VERSION="0.0.0" | ||
ARG GIT_COMMIT | ||
WORKDIR /go/src/app | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package defaults | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
|
||
"github.com/drone/drone-go/drone" | ||
"github.com/drone/drone-go/plugin/converter" | ||
jsonpatch "github.com/evanphx/json-patch/v5" | ||
"github.com/kanopy-platform/drone-extension-router/pkg/manifest" | ||
) | ||
|
||
type Config struct { | ||
Pipeline *manifest.Pipeline `json:"pipeline,omitempty"` | ||
} | ||
|
||
type Defaults struct { | ||
config Config | ||
} | ||
|
||
func New(c Config) *Defaults { | ||
return &Defaults{config: c} | ||
} | ||
|
||
func (d *Defaults) Convert(ctx context.Context, req *converter.Request) (*drone.Config, error) { | ||
// decode pipeline resources | ||
resources, err := manifest.Decode(req.Config.Data) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
// merge defaults into user-defined resources | ||
for _, r := range resources { | ||
switch r.(type) { | ||
case *manifest.Pipeline: | ||
if d.config.Pipeline != nil { | ||
if err := merge(d.config.Pipeline, r); err != nil { | ||
return nil, err | ||
} | ||
} | ||
} | ||
} | ||
|
||
// encode pipeline resources | ||
data, err := manifest.Encode(resources) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &drone.Config{ | ||
Data: string(data), | ||
}, nil | ||
} | ||
|
||
func merge(defaults, user manifest.Resource) error { | ||
defaultBytes, err := json.Marshal(defaults) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
userBytes, err := json.Marshal(user) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
userBytes, err = jsonpatch.MergePatch(defaultBytes, userBytes) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return json.Unmarshal(userBytes, user) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,151 @@ | ||
package defaults_test | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/drone/drone-go/drone" | ||
"github.com/drone/drone-go/plugin/converter" | ||
"github.com/kanopy-platform/drone-extension-router/internal/plugin/convert/defaults" | ||
"github.com/kanopy-platform/drone-extension-router/pkg/manifest" | ||
"github.com/stretchr/testify/assert" | ||
"gopkg.in/yaml.v3" | ||
) | ||
|
||
func TestDefaults(t *testing.T) { | ||
tests := []struct { | ||
desc string | ||
config string | ||
request string | ||
want string | ||
}{ | ||
{ | ||
desc: "test with empty defaults and request", | ||
}, | ||
{ | ||
desc: "test without defaults", | ||
request: "kind: pipeline", | ||
want: "kind: pipeline\n", | ||
}, | ||
{ | ||
desc: "test with defaults", | ||
config: ` | ||
pipeline: | ||
node_selector: | ||
d: d | ||
test: d | ||
map: | ||
d: d | ||
test: d | ||
list: | ||
- test: d`, | ||
request: ` | ||
kind: pipeline | ||
node_selector: | ||
r: r | ||
test: r | ||
map: | ||
r: r | ||
test: r | ||
list: | ||
- test: r`, | ||
want: `kind: pipeline | ||
node_selector: | ||
d: d | ||
r: r | ||
test: r | ||
list: | ||
- test: r | ||
map: | ||
d: d | ||
r: r | ||
test: r | ||
`, | ||
}, | ||
{ | ||
desc: "test default node_selector and tolerations", | ||
config: ` | ||
pipeline: | ||
kind: pipeline | ||
node_selector: | ||
instancegroup: drone | ||
tolerations: | ||
- key: dedicated | ||
operator: Equal | ||
value: drone | ||
effect: NoSchedule`, | ||
request: ` | ||
kind: pipeline | ||
node_selector: | ||
instancegroup: batch | ||
tolerations: | ||
- key: dedicated | ||
operator: Equal | ||
value: batch | ||
effect: NoSchedule`, | ||
want: `kind: pipeline | ||
node_selector: | ||
instancegroup: batch | ||
tolerations: | ||
- key: dedicated | ||
operator: Equal | ||
value: batch | ||
effect: NoSchedule | ||
`, | ||
}, | ||
{ | ||
desc: "test with multiple objects", | ||
config: ` | ||
pipeline: | ||
type: test`, | ||
request: ` | ||
kind: pipeline | ||
name: user | ||
--- | ||
kind: fake | ||
fake: field | ||
--- | ||
kind: secret | ||
name: user`, | ||
want: `kind: pipeline | ||
type: test | ||
name: user | ||
--- | ||
kind: fake | ||
fake: field | ||
--- | ||
kind: secret | ||
name: user | ||
`, | ||
}, | ||
} | ||
|
||
for _, test := range tests { | ||
c := defaults.Config{} | ||
assert.NoError(t, yaml.Unmarshal([]byte(test.config), &c), test.desc) | ||
|
||
req := &converter.Request{Config: drone.Config{Data: test.request}} | ||
|
||
config, err := defaults.New(c).Convert(context.TODO(), req) | ||
assert.NoError(t, err, test.desc) | ||
|
||
assert.Equal(t, test.want, config.Data, test.desc) | ||
} | ||
} | ||
|
||
func BenchmarkConvertNil(b *testing.B) { | ||
benchmarkConvert(b, defaults.Config{Pipeline: nil}) | ||
} | ||
|
||
func BenchmarkConvert(b *testing.B) { | ||
benchmarkConvert(b, defaults.Config{Pipeline: &manifest.Pipeline{}}) | ||
} | ||
|
||
func benchmarkConvert(b *testing.B, conf defaults.Config) { | ||
plugin := defaults.New(conf) | ||
req := &converter.Request{Config: drone.Config{Data: "kind: pipeline"}} | ||
|
||
for n := 0; n < b.N; n++ { | ||
_, _ = plugin.Convert(context.TODO(), req) | ||
} | ||
} |