Skip to content

fix: avoid nameclashes by considering the filepath #969

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 37 additions & 2 deletions functions/go/gatekeeper/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,21 @@ import (
"fmt"
"io/ioutil"
"os"
"strings"

"github.com/GoogleContainerTools/kpt-functions-catalog/functions/go/gatekeeper/generated"
"github.com/spf13/cobra"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"sigs.k8s.io/kustomize/kyaml/fn/framework"
"sigs.k8s.io/kustomize/kyaml/kio"
"sigs.k8s.io/kustomize/kyaml/kio/kioutil"
k8syaml "sigs.k8s.io/yaml"
)

const (
stdin = "/dev/stdin"
stdout = "/dev/stdout"
stdin = "/dev/stdin"
stdout = "/dev/stdout"
nullByte = "\x00"
)

type GatekeeperProcessor struct {
Expand Down Expand Up @@ -59,6 +62,15 @@ func (gkp *GatekeeperProcessor) Process(resourceList *framework.ResourceList) er
return err
}

// add the filepath to the objects name in sanitized form. this is done
// to get unique identifier in case the same resource is defined in
// different files. Usually this happens when running the function
// across packages
if !isTemplate(un) && !isConstraint(un) {
un.SetName(fmt.Sprintf("%s%s%s", un.GetName(), nullByte,
strings.ReplaceAll(item.GetAnnotations()[kioutil.PathAnnotation], "/", nullByte)))
}

objects = append(objects, un)
}

Expand All @@ -74,6 +86,19 @@ func (gkp *GatekeeperProcessor) Process(resourceList *framework.ResourceList) er
},
}
}

// unwrap the null-byte filename hack again
if result != nil {
for i, item := range result.Items {
parts := strings.SplitN(item.ResourceRef.Name, nullByte, 2)
item.ResourceRef.Name = parts[0]
if len(parts) == 2 {
item.Field.Path = strings.ReplaceAll(parts[1], nullByte, "/")
}
result.Items[i] = item
}
}

resourceList.Result = result
if resultContainsError(result) {
return result
Expand Down Expand Up @@ -198,3 +223,13 @@ func resultContainsError(result *framework.Result) bool {
}
return false
}

func isTemplate(u *unstructured.Unstructured) bool {
gvk := u.GroupVersionKind()
return gvk.Kind == "ConstraintTemplate"
}

func isConstraint(u *unstructured.Unstructured) bool {
gvk := u.GroupVersionKind()
return gvk.Group == "constraints.gatekeeper.sh"
}