diff --git a/README.md b/README.md index b1bfe64..cfae113 100644 --- a/README.md +++ b/README.md @@ -56,12 +56,15 @@ steps: from_secret: k8s_ca_crt k8s_skip_tls: false namespace: default + init_templates: + - testdata/namespace.yaml config_files: - default:test-config:testdata/config.yaml - default:test-config:testdata/config.yaml:a.yaml templates: - testdata/deployment.yaml - testdata/service.yaml + - testdata/*.yaml app_name: ${DRONE_REPO_NAME} ``` OR diff --git a/go.mod b/go.mod index d020bba..6c539d6 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,10 @@ module github.com/zc2638/drone-k8s-plugin go 1.19 require ( + github.com/99nil/gopkg v0.0.0-20221020090523-251ae5920751 github.com/Masterminds/sprig v2.22.0+incompatible + github.com/a8m/envsubst v1.3.0 + github.com/bmatcuk/doublestar/v4 v4.4.0 github.com/mitchellh/go-homedir v1.1.0 github.com/mitchellh/mapstructure v1.5.0 github.com/sirupsen/logrus v1.9.0 @@ -20,7 +23,6 @@ require ( github.com/Masterminds/semver v1.5.0 // indirect github.com/PuerkitoBio/purell v1.1.1 // indirect github.com/PuerkitoBio/urlesc v0.0.0-20170810143723-de5bf2ad4578 // indirect - github.com/a8m/envsubst v1.3.0 // indirect github.com/davecgh/go-spew v1.1.1 // indirect github.com/emicklei/go-restful/v3 v3.8.0 // indirect github.com/fsnotify/fsnotify v1.6.0 // indirect diff --git a/go.sum b/go.sum index f543b52..095981e 100644 --- a/go.sum +++ b/go.sum @@ -36,6 +36,8 @@ cloud.google.com/go/storage v1.8.0/go.mod h1:Wv1Oy7z6Yz3DshWRJFhqM/UCfaWIRTdp0RX cloud.google.com/go/storage v1.10.0/go.mod h1:FLPqc6j+Ki4BU591ie1oL6qBQGu2Bl/tZ9ullr3+Kg0= cloud.google.com/go/storage v1.14.0/go.mod h1:GrKmX003DSIwi9o29oFT7YDnHYwZoctc3fOKtUw0Xmo= dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU= +github.com/99nil/gopkg v0.0.0-20221020090523-251ae5920751 h1:Iebp4akbVu6Sf2WlFqb9/aBl7mxH1jTgkG0zHJFrzw8= +github.com/99nil/gopkg v0.0.0-20221020090523-251ae5920751/go.mod h1:qbZsXE0O2/hY9mWK0tOOCbg8FrFarc3Ym/39i7V9akQ= github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo= github.com/Masterminds/goutils v1.1.1 h1:5nUrii3FMTL5diU80unEVvNevw1nH4+ZV4DSLVJLSYI= @@ -50,6 +52,8 @@ github.com/PuerkitoBio/urlesc v0.0.0-20170810143723-de5bf2ad4578 h1:d+Bc7a5rLufV github.com/PuerkitoBio/urlesc v0.0.0-20170810143723-de5bf2ad4578/go.mod h1:uGdkoq3SwY9Y+13GIhn11/XLaGBb4BfwItxLd5jeuXE= github.com/a8m/envsubst v1.3.0 h1:GmXKmVssap0YtlU3E230W98RWtWCyIZzjtf1apWWyAg= github.com/a8m/envsubst v1.3.0/go.mod h1:MVUTQNGQ3tsjOOtKCNd+fl8RzhsXcDvvAEzkhGtlsbY= +github.com/bmatcuk/doublestar/v4 v4.4.0 h1:LmAwNwhjEbYtyVLzjcP/XeVw4nhuScHGkF/XWXnvIic= +github.com/bmatcuk/doublestar/v4 v4.4.0/go.mod h1:xBQ8jztBU6kakFMg+8WGxn0c6z1fTSPVIjEY1Wr7jzc= github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU= github.com/chzyer/logex v1.1.10/go.mod h1:+Ywpsq7O8HXn0nuIou7OrIPyXbp3wmkHB+jjWRnGsAI= github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e/go.mod h1:nSuG5e5PlCu98SY8svDHJxuZscDgtXS6KTTbou5AhLI= diff --git a/pkg/plugin/plugin.go b/pkg/plugin/plugin.go index 23258fe..da51868 100644 --- a/pkg/plugin/plugin.go +++ b/pkg/plugin/plugin.go @@ -22,6 +22,10 @@ import ( "regexp" "strconv" + "github.com/99nil/gopkg/sets" + + "github.com/bmatcuk/doublestar/v4" + pkgruntime "k8s.io/apimachinery/pkg/runtime" v1 "k8s.io/api/core/v1" @@ -79,16 +83,26 @@ func run( } func parseObjectSet(templates []string, envMap map[string]string) ([][]unstructured.Unstructured, error) { - if len(templates) == 0 { + fileSet := sets.New[string]() + for _, v := range templates { + matches, err := doublestar.FilepathGlob(v) + if err != nil { + return nil, err + } + fileSet.Add(matches...) + } + + files := fileSet.List() + if len(files) == 0 { return nil, nil } - objSet := make([][]unstructured.Unstructured, 0, len(templates)) - for _, v := range templates { + objSet := make([][]unstructured.Unstructured, 0, len(files)) + for _, v := range files { ext := filepath.Ext(v) isYamlFile := ext == ".yaml" || ext == ".yml" if !isYamlFile { - logrus.Warnf("Ignore file (%s), not a yaml or yml file", v) + logrus.Warnf("Ignore dir or file (%s), not a yaml or yml file", v) continue }