-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(lint): Add no-missing-gateway-class rule
- Loading branch information
Showing
5 changed files
with
137 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
/// <reference types="jest-extended" /> | ||
import { Gateway } from "@kubernetes-models/gateway-api/gateway.networking.k8s.io/v1/Gateway"; | ||
import { GatewayClass } from "@kubernetes-models/gateway-api/gateway.networking.k8s.io/v1/GatewayClass"; | ||
import { createManifest, validateAll } from "../test-utils"; | ||
import rule from "./no-missing-gateway-class"; | ||
|
||
test("should pass when data is undefined", () => { | ||
const manifest = createManifest(undefined); | ||
expect(validateAll(rule, undefined, [manifest])).toBeEmpty(); | ||
}); | ||
|
||
test("should pass when data is an empty object", () => { | ||
const manifest = createManifest({}); | ||
expect(validateAll(rule, undefined, [manifest])).toBeEmpty(); | ||
}); | ||
|
||
test("should pass when gatewayClassName is empty", () => { | ||
const manifest = createManifest( | ||
new Gateway({ | ||
metadata: { name: "foo" }, | ||
spec: { | ||
gatewayClassName: "", | ||
listeners: [] | ||
} | ||
}) | ||
); | ||
expect(validateAll(rule, undefined, [manifest])).toBeEmpty(); | ||
}); | ||
|
||
test("should pass when gateway class exists", () => { | ||
const gatewayClass = createManifest( | ||
new GatewayClass({ | ||
metadata: { name: "bar" }, | ||
spec: { | ||
controllerName: "" | ||
} | ||
}) | ||
); | ||
const gateway = createManifest( | ||
new Gateway({ | ||
metadata: { name: "foo" }, | ||
|
||
spec: { | ||
gatewayClassName: "bar", | ||
listeners: [] | ||
} | ||
}) | ||
); | ||
expect(validateAll(rule, undefined, [gatewayClass, gateway])).toBeEmpty(); | ||
}); | ||
|
||
test("should pass when gateway class is allowed", () => { | ||
const gateway = createManifest( | ||
new Gateway({ | ||
metadata: { name: "foo" }, | ||
|
||
spec: { | ||
gatewayClassName: "bar", | ||
listeners: [] | ||
} | ||
}) | ||
); | ||
expect(validateAll(rule, { allow: ["bar"] }, [gateway])).toBeEmpty(); | ||
}); | ||
|
||
test("should report when gateway class does not exist", () => { | ||
const gateway = createManifest( | ||
new Gateway({ | ||
metadata: { name: "foo" }, | ||
|
||
spec: { | ||
gatewayClassName: "bar", | ||
listeners: [] | ||
} | ||
}) | ||
); | ||
expect(validateAll(rule, undefined, [gateway])).toEqual([ | ||
{ | ||
manifest: gateway, | ||
message: `GatewayClass "bar" does not exist.` | ||
} | ||
]); | ||
}); |
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,45 @@ | ||
import { array, object, optional, string } from "superstruct"; | ||
import { createRule } from "./types"; | ||
import { compilePattern, matchAny } from "../utils/pattern"; | ||
import { | ||
GATEWAY_GROUP, | ||
buildMissingResourceMessage, | ||
isGateway | ||
} from "../utils/manifest"; | ||
|
||
export default createRule({ | ||
config: object({ | ||
allow: optional(array(string())) | ||
}), | ||
factory(ctx) { | ||
const isAllowed = matchAny((ctx.config?.allow ?? []).map(compilePattern)); | ||
|
||
return { | ||
validateAll(manifests) { | ||
manifests.forEach((manifest) => { | ||
if (!isGateway(manifest)) return; | ||
|
||
const name = manifest.data.spec?.gatewayClassName; | ||
if (!name) return; | ||
|
||
if (isAllowed(name)) return; | ||
|
||
if ( | ||
manifests.find({ | ||
apiGroup: GATEWAY_GROUP, | ||
kind: "GatewayClass", | ||
name | ||
}) | ||
) { | ||
return; | ||
} | ||
|
||
ctx.report( | ||
manifest, | ||
buildMissingResourceMessage("GatewayClass", { name }) | ||
); | ||
}); | ||
} | ||
}; | ||
} | ||
}); |
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