Skip to content

Commit dad39be

Browse files
Update set-namespace function (#123)
update set-namespace function
1 parent 982f0a0 commit dad39be

14 files changed

+862
-123
lines changed

functions/go/Makefile

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
GOPATH := $(shell go env GOPATH)
2+
3+
# Edit this list to contain all go functions
4+
FUNCTIONS := \
5+
set-namespace
6+
7+
CURRENT_FUNCTION := unknown
8+
9+
.PHONY: fix vet fmt test lint all
10+
.PHONY: $(FUNCTIONS)
11+
12+
all: $(FUNCTIONS)
13+
14+
$(FUNCTIONS):
15+
$(MAKE) CURRENT_FUNCTION=$@ function
16+
17+
function: fix vet fmt test lint
18+
19+
fix:
20+
cd $(CURRENT_FUNCTION) && go fix ./...
21+
22+
fmt:
23+
cd $(CURRENT_FUNCTION) && go fmt ./...
24+
25+
lint:
26+
(which $(GOPATH)/bin/golangci-lint || \
27+
curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(GOPATH)/bin v1.32.2)
28+
cd $(CURRENT_FUNCTION) && $(GOPATH)/bin/golangci-lint run ./...
29+
30+
test:
31+
cd $(CURRENT_FUNCTION) && go test -cover ./...
32+
33+
vet:
34+
cd $(CURRENT_FUNCTION) && go vet ./...

functions/go/publish-functions.sh

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,15 @@ image_tag=${TAG:-latest}
2525
prefix="release-go-functions-"
2626
[[ "${image_tag}" = "${prefix}"* ]] && image_tag="${image_tag#$prefix}"
2727

28+
# TODO: Put build and publish commands in Makefile
29+
make
30+
2831
# iterate over each subdir, build and push Docker images.
2932
for dir in */
3033
do
3134
image_name=gcr.io/kpt-functions/"${dir%/}"
3235
image="${image_name}":"${image_tag}"
3336
set -x
34-
( cd "${dir}" && make )
3537
docker build -t "${image}" -t "${image_name}" -f "${dir}"/Dockerfile "${dir}"
3638
if [ -z "${BUILDONLY}" ]; then
3739
docker push "${image_name}"

functions/go/set-namespace/Dockerfile

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
FROM golang:1.14
1+
FROM golang:1.15
22
ENV CGO_ENABLED=0
33
WORKDIR /go/src/
44
COPY . .
5-
RUN go build -v -o /usr/local/bin/set-namespace ./
6-
5+
RUN go build -v -o /usr/local/bin/function ./
76
FROM alpine:latest
8-
COPY --from=0 /usr/local/bin/set-namespace /usr/local/bin/set-namespace
9-
CMD ["set-namespace"]
7+
COPY --from=0 /usr/local/bin/function /usr/local/bin/function
8+
CMD ["function"]

functions/go/set-namespace/Makefile

Lines changed: 0 additions & 21 deletions
This file was deleted.
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// Copyright 2019 The Kubernetes Authors.
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
//go:generate pluginator
5+
package main
6+
7+
import (
8+
"sigs.k8s.io/kustomize/api/filters/namespace"
9+
"sigs.k8s.io/kustomize/api/resmap"
10+
"sigs.k8s.io/kustomize/api/types"
11+
"sigs.k8s.io/kustomize/kyaml/filtersutil"
12+
"sigs.k8s.io/yaml"
13+
)
14+
15+
// Change or set the namespace of non-cluster level resources.
16+
type plugin struct {
17+
Namespace string `json:"namespace,omitempty" yaml:"namespace,omitempty"`
18+
FieldSpecs []types.FieldSpec `json:"fieldSpecs,omitempty" yaml:"fieldSpecs,omitempty"`
19+
}
20+
21+
//noinspection GoUnusedGlobalVariable
22+
var KustomizePlugin plugin
23+
24+
func (p *plugin) Config(
25+
_ *resmap.PluginHelpers, c []byte) (err error) {
26+
p.Namespace = ""
27+
p.FieldSpecs = nil
28+
return yaml.Unmarshal(c, p)
29+
}
30+
31+
func (p *plugin) Transform(m resmap.ResMap) error {
32+
if len(p.Namespace) == 0 {
33+
return nil
34+
}
35+
for _, r := range m.Resources() {
36+
if r.IsEmpty() {
37+
// Don't mutate empty objects?
38+
continue
39+
}
40+
err := filtersutil.ApplyToJSON(namespace.Filter{
41+
Namespace: p.Namespace,
42+
FsSlice: p.FieldSpecs,
43+
}, r)
44+
if err != nil {
45+
return err
46+
}
47+
}
48+
return nil
49+
}

0 commit comments

Comments
 (0)