Giter Site home page Giter Site logo

Comments (2)

maxsmythe avatar maxsmythe commented on May 22, 2024 1

Thanks for the info!

We do something with our e2e tests using kind.

I think this bug was actually addressed by this file:

func TestConstraintEnforcement(t *testing.T) {
tcs := []struct {
name string
obj *unstructured.Unstructured
ns *corev1.Namespace
constraint *unstructured.Unstructured
allowed bool
}{
{
name: "match deny all",
obj: makeResource("some", "Thing"),
ns: makeNamespace("my-ns"),
constraint: makeConstraint(),
allowed: false,
},
{
name: "match namespace",
obj: makeResource("some", "Thing"),
ns: makeNamespace("my-ns"),
constraint: makeConstraint(setNamespaceName("my-ns")),
allowed: false,
},
{
name: "no match namespace",
obj: makeResource("some", "Thing"),
ns: makeNamespace("my-ns"),
constraint: makeConstraint(setNamespaceName("not-my-ns")),
allowed: true,
},
{
name: "match excludedNamespaces",
obj: makeResource("some", "Thing"),
ns: makeNamespace("my-ns"),
constraint: makeConstraint(setExcludedNamespaceName("my-ns")),
allowed: true,
},
{
name: "no match excludedNamespaces",
obj: makeResource("some", "Thing"),
ns: makeNamespace("my-ns"),
constraint: makeConstraint(setExcludedNamespaceName("not-my-ns")),
allowed: false,
},
{
name: "match labelselector",
obj: makeResource("some", "Thing", map[string]string{"a": "label"}),
ns: makeNamespace("my-ns"),
constraint: makeConstraint(setLabelSelector("a", "label")),
allowed: false,
},
{
name: "no match labelselector",
obj: makeResource("some", "Thing", map[string]string{"a": "label"}),
ns: makeNamespace("my-ns"),
constraint: makeConstraint(setLabelSelector("different", "label")),
allowed: true,
},
{
name: "match nsselector",
obj: makeResource("some", "Thing"),
ns: makeNamespace("my-ns", map[string]string{"a": "label"}),
constraint: makeConstraint(setNamespaceSelector("a", "label")),
allowed: false,
},
{
name: "no match nsselector",
obj: makeResource("some", "Thing"),
ns: makeNamespace("my-ns", map[string]string{"a": "label"}),
constraint: makeConstraint(setNamespaceSelector("different", "label")),
allowed: true,
},
{
name: "match kinds",
obj: makeResource("some", "Thing"),
ns: makeNamespace("my-ns"),
constraint: makeConstraint(setKinds([]string{"some"}, []string{"Thing"})),
allowed: false,
},
{
name: "no match kinds",
obj: makeResource("some", "Thing"),
ns: makeNamespace("my-ns"),
constraint: makeConstraint(setKinds([]string{"different"}, []string{"Thing"})),
allowed: true,
},
{
name: "match everything",
obj: makeResource("some", "Thing", map[string]string{"obj": "label"}),
ns: makeNamespace("my-ns", map[string]string{"ns": "label"}),
constraint: makeConstraint(
setKinds([]string{"some"}, []string{"Thing"}),
setNamespaceName("my-ns"),
setLabelSelector("obj", "label"),
setNamespaceSelector("ns", "label"),
),
allowed: false,
},
{
name: "match everything but kind",
obj: makeResource("some", "Thing", map[string]string{"obj": "label"}),
ns: makeNamespace("my-ns", map[string]string{"ns": "label"}),
constraint: makeConstraint(
setKinds([]string{"different"}, []string{"Thing"}),
setNamespaceName("my-ns"),
setLabelSelector("obj", "label"),
setNamespaceSelector("ns", "label"),
),
allowed: true,
},
{
name: "match everything but namespace",
obj: makeResource("some", "Thing", map[string]string{"obj": "label"}),
ns: makeNamespace("my-ns", map[string]string{"ns": "label"}),
constraint: makeConstraint(
setKinds([]string{"some"}, []string{"Thing"}),
setNamespaceName("different-ns"),
setLabelSelector("obj", "label"),
setNamespaceSelector("ns", "label"),
),
allowed: true,
},
{
name: "match everything but labelselector",
obj: makeResource("some", "Thing", map[string]string{"obj": "label"}),
ns: makeNamespace("my-ns", map[string]string{"ns": "label"}),
constraint: makeConstraint(
setKinds([]string{"some"}, []string{"Thing"}),
setNamespaceName("my-ns"),
setLabelSelector("obj", "different-label"),
setNamespaceSelector("ns", "label"),
),
allowed: true,
},
{
name: "match everything but nsselector",
obj: makeResource("some", "Thing", map[string]string{"obj": "label"}),
ns: makeNamespace("my-ns", map[string]string{"ns": "label"}),
constraint: makeConstraint(
setKinds([]string{"some"}, []string{"Thing"}),
setNamespaceName("my-ns"),
setLabelSelector("obj", "label"),
setNamespaceSelector("ns", "different-label"),
),
allowed: true,
},
}

Which allows us to run through match semantics as a unit test.

from gatekeeper.

nilekhc avatar nilekhc commented on May 22, 2024

@maxsmythe in our company, I implemented a pipeline where I spin up k3s cluster, install gatekeeper, install template and constraints (from policy library like directory) and then test them by running actual create resource with the help of client-go. Let me know if this is the right approach and if so how can I implement here.
@ritazh

from gatekeeper.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.