{"id":470250,"date":"2025-08-11T21:01:49","date_gmt":"2025-08-11T21:01:49","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=470250"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=470250","title":{"rendered":"<span>\u041f\u043e\u043b\u0438\u0442\u0438\u043a\u0438 \u0431\u0435\u0437\u043e\u043f\u0430\u0441\u043d\u043e\u0441\u0442\u0438 k8s gatekeeper OPA. \u0418\u043d\u0442\u0435\u0433\u0440\u0430\u0446\u0438\u044f \u0441 GO<\/span>"},"content":{"rendered":"<div><!--[--><!--]--><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<p>\u041f\u043e\u0433\u043e\u0432\u043e\u0440\u0438\u043c \u043e \u043f\u043e\u043b\u0438\u0442\u0438\u043a\u0430\u0445 \u0431\u0435\u0437\u043e\u043f\u0430\u0441\u043d\u043e\u0441\u0442\u0438 <strong>OPA<\/strong> \u0432 \u043a\u0443\u0431\u0435\u0440\u0435. \u041e\u0431\u0441\u0443\u0434\u0438\u043c \u043d\u0430 \u043f\u0440\u0438\u043c\u0435\u0440\u0430\u0445 \u0437\u0430\u0447\u0435\u043c \u043e\u043d\u0438 \u043d\u0443\u0436\u043d\u044b, \u0432 \u043a\u0430\u043a\u0438\u0445 \u0441\u043b\u0443\u0447\u0430\u044f\u0445 \u043e\u043d\u0438 \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u043f\u043e\u043c\u043e\u0433\u0443\u0442 \u043e\u0431\u0435\u0437\u043e\u043f\u0430\u0441\u0438\u0442\u044c, \u043a\u043e\u0433\u0434\u0430 \u043f\u043e\u043b\u0438\u0442\u0438\u043a\u0438 \u043c\u043e\u0433\u0443\u0442 \u043f\u043e\u043b\u043e\u0436\u0438\u0442\u044c \u0432\u0441\u044e \u0441\u0438\u0441\u0442\u0435\u043c\u0443 \u0438 \u043a\u0430\u043a \u0438\u043c\u0438 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c\u0441\u044f \u0432 \u043a\u0443\u0431\u0435\u0440\u0435. \u041f\u043b\u044e\u0441\u043e\u043c \u0437\u0430\u0445\u0432\u0430\u0442\u0438\u043c \u043d\u0435\u043c\u043d\u043e\u0433\u043e \u043a\u043e\u0434\u0430 \u043d\u0430 <strong>go<\/strong> \u0434\u043b\u044f \u0440\u0430\u0431\u043e\u0442\u044b \u0441 \u043d\u0438\u043c\u0438.<\/p>\n<p><strong> \u0412\u0432\u0435\u0434\u0435\u043d\u0438\u0435<\/strong><\/p>\n<p>\u0412 \u043d\u0430\u0448\u0435 \u0432\u0440\u0435\u043c\u044f \u0441\u043f\u043b\u043e\u0448\u044c \u0438 \u0440\u044f\u0434\u043e\u043c \u043c\u044b \u0432\u0438\u0434\u0438\u043c \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u044e \u043e \u0442\u043e\u043c, \u0447\u0442\u043e \u043b\u0438\u0447\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435 \u043c\u0438\u043b\u043b\u0438\u043e\u043d\u043e\u0432 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u0435\u0439 \u0441\u043b\u0438\u0432\u0430\u0435\u0442\u0441\u044f. \u0412\u0438\u0434\u0438\u043c \u043d\u0430\u0440\u0443\u0448\u0435\u043d\u0438\u044f \u0440\u0430\u0431\u043e\u0442\u044b \u0440\u0430\u0437\u043d\u044b\u0445 \u043a\u0440\u0443\u043f\u043d\u044b\u0445 \u0441\u0438\u0441\u0442\u0435\u043c, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, 28 \u0438\u044e\u043b\u044f \u0431\u044b\u043b\u0438 \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u044b \u0443 \u0410\u044d\u0440\u043e\u0444\u043b\u043e\u0442\u0430 \u0438\u0437-\u0437\u0430 \u0447\u0435\u0433\u043e \u043e\u0442\u043c\u0435\u043d\u0438\u043b\u043e\u0441\u044c \u043c\u043d\u043e\u0433\u043e \u0440\u0435\u0439\u0441\u043e\u0432, \u043b\u044e\u0434\u0438 \u0441\u043f\u0430\u043b\u0438 \u0431\u0443\u043a\u0432\u0430\u043b\u044c\u043d\u043e \u0432 \u0430\u044d\u0440\u043e\u043f\u043e\u0440\u0442\u0435 \u0438 \u0443\u043f\u0430\u043b\u0430 \u043a\u0430\u043f\u0438\u0442\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043a\u043e\u043c\u043f\u0430\u043d\u0438\u0438. <\/p>\n<figure class=\"full-width\"><img decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/42c\/a95\/b2d\/42ca95b2d124cbe9e25138100956f464.png\" alt=\"\u0414\u0438\u043d\u0430\u043c\u0438\u043a\u0430 \u0430\u043a\u0446\u0438\u0439 \u043d\u0430 28 \u0438\u044e\u043b\u044f\" title=\"\u0414\u0438\u043d\u0430\u043c\u0438\u043a\u0430 \u0430\u043a\u0446\u0438\u0439 \u043d\u0430 28 \u0438\u044e\u043b\u044f\" width=\"851\" height=\"1280\" sizes=\"auto, (max-width: 780px) 100vw, 50vw\" srcset=\"https:\/\/habrastorage.org\/r\/w780\/getpro\/habr\/upload_files\/42c\/a95\/b2d\/42ca95b2d124cbe9e25138100956f464.png 780w,&#10;       https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/42c\/a95\/b2d\/42ca95b2d124cbe9e25138100956f464.png 781w\" loading=\"lazy\" decode=\"async\"\/><\/p>\n<div><figcaption>\u0414\u0438\u043d\u0430\u043c\u0438\u043a\u0430 \u0430\u043a\u0446\u0438\u0439 \u043d\u0430 28 \u0438\u044e\u043b\u044f<\/figcaption><\/div>\n<\/figure>\n<p>\u041f\u043e\u043b\u0438\u0442\u0438\u043a\u0438 \u0431\u0435\u0437\u043e\u043f\u0430\u0441\u043d\u043e\u0441\u0442\u0438 \u043d\u0443\u0436\u043d\u044b \u0434\u043b\u044f \u0441\u043e\u0431\u043b\u044e\u0434\u0435\u043d\u0438\u044f \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u043d\u044b\u0445 \u043f\u0440\u0430\u0432\u0438\u043b \u043a\u043e\u043c\u043f\u0430\u043d\u0438\u0438.\u00a0\u0421\u0435\u0433\u043e\u0434\u043d\u044f \u043c\u044b \u043e\u0431\u0441\u0443\u0434\u0438\u043c <strong>Open Policy Agent<\/strong>. \u0418\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442 \u0434\u043e\u0432\u043e\u043b\u044c\u043d\u043e \u043c\u043e\u0449\u043d\u044b\u0439 \u0438 \u0443\u043d\u0438\u0432\u0435\u0440\u0441\u0430\u043b\u044c\u043d\u044b\u0439, \u0435\u0441\u043b\u0438 \u0445\u043e\u0442\u0438\u0442\u0435 \u0434\u0435\u0442\u0430\u043b\u044c\u043d\u043e\u0433\u043e \u043f\u043e\u0433\u0440\u0443\u0436\u0435\u043d\u0438\u044f \u0440\u0435\u043a\u043e\u043c\u0435\u043d\u0434\u0443\u044e \u043f\u043e\u0433\u0440\u0443\u0437\u0438\u0442\u044c\u0441\u044f \u0432 \u043e\u0444\u0438\u0446\u0438\u0430\u043b\u044c\u043d\u0443\u044e <a href=\"https:\/\/www.openpolicyagent.org\/docs\" rel=\"noopener noreferrer nofollow\">\u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u044e<\/a>.\u00a0<\/p>\n<p>\u0411\u0430\u0437\u043e\u0432\u044b\u0435 \u0441\u043b\u0443\u0447\u0430\u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f \u043f\u043e\u043b\u0438\u0442\u0438\u043a:<\/p>\n<ul>\n<li>\n<p>\u041c\u043e\u0436\u0435\u0442 \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0442\u044c \u043c\u0430\u043d\u0438\u0444\u0435\u0441\u0442\u044b <code>Deployment<\/code>, <code>StatefulSet<\/code>, <code>DaemonSet<\/code> \u2014 \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u043e\u0431\u044f\u0437\u0430\u0442\u0435\u043b\u044c\u043d\u044b\u0435 <code>labels<\/code> \u0438 \u0430\u043d\u043d\u043e\u0442\u0430\u0446\u0438\u0438.\u00a0<\/p>\n<\/li>\n<li>\n<p>\u0414\u043b\u044f <code>Pod'\u043e\u0432<\/code> &#8212; \u0437\u0430\u043f\u0440\u0435\u0442 \u0437\u0430\u043f\u0443\u0441\u043a\u0430 \u043d\u0435 \u0438\u0437 \u0434\u043e\u0432\u0435\u0440\u0435\u043d\u043d\u044b\u0445 \u0440\u0435\u043f\u043e\u0437\u0438\u0442\u043e\u0440\u0438\u0435\u0432 \u0438\u043b\u0438 \u0437\u0430\u043f\u0440\u0435\u0442\u00a0\u0442\u0435\u0433\u0430 \u00a0<code>latest<\/code><\/p>\n<\/li>\n<li>\n<p>\u041f\u0440\u043e\u0432\u0435\u0440\u043a\u0430 \u0441\u043e\u0431\u044b\u0442\u0438\u0439 \u0432\u043d\u0435 <code>Pod'\u043e\u0432<\/code> \u0421\u043e\u0437\u0434\u0430\u043d\u0438\u0435\/\u0443\u0434\u0430\u043b\u0435\u043d\u0438\u0435 <code>Namespaces<\/code> \u0442\u043e\u043b\u044c\u043a\u043e \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0451\u043d\u043d\u044b\u043c\u0438 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f\u043c\u0438. \u0418 \u043c\u043d\u043e\u0433\u043e\u0435 \u0434\u0440\u0443\u0433\u043e\u0435.\u00a0<\/p>\n<\/li>\n<\/ul>\n<p><strong>\u041a\u0430\u043a OPA \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u0432 k8s<\/strong><\/p>\n<p>\u041e\u0431\u044b\u0447\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e\u0442 <strong>OPA Gatekeeper \u00a0<\/strong>\u0434\u043b\u044f \u0438\u043d\u0442\u0435\u0433\u0440\u0430\u0446\u0438\u0438 OPA \u0432 k8s:<\/p>\n<ul>\n<li>\n<p>\u041e\u043d \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0430\u0435\u0442\u0441\u044f \u043a\u0430\u043a <strong>ValidatingAdmissionWebhook<\/strong>(\u0440\u0443\u0436\u0435 MutatingAdmissionWebhook).<\/p>\n<\/li>\n<li>\n<p>\u041f\u0440\u0438 \u043a\u0430\u0436\u0434\u043e\u043c \u0437\u0430\u043f\u0440\u043e\u0441\u0435 \u043d\u0430 \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u0435\/\u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u0435 \u0440\u0435\u0441\u0443\u0440\u0441\u0430 OPA \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u0442 \u043e\u0431\u044a\u0435\u043a\u0442 \u0432 JSON.<\/p>\n<\/li>\n<li>\n<p>\u041f\u0440\u043e\u0433\u043e\u043d\u044f\u0435\u0442 \u0435\u0433\u043e \u0447\u0435\u0440\u0435\u0437 \u043f\u043e\u043b\u0438\u0442\u0438\u043a\u0438, \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u043d\u044b\u0435 \u043d\u0430 \u044f\u0437\u044b\u043a\u0435 <strong>Rego<\/strong>.<\/p>\n<\/li>\n<li>\n<p>\u0415\u0441\u043b\u0438 \u0447\u0442\u043e-\u0442\u043e \u043d\u0435 \u043f\u0440\u043e\u0445\u043e\u0434\u0438\u0442 \u2014 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 \u043e\u0448\u0438\u0431\u043a\u0443, \u0438 \u043e\u0431\u044a\u0435\u043a\u0442 \u043d\u0435 \u0441\u043e\u0437\u0434\u0430\u0451\u0442\u0441\u044f.<\/p>\n<\/li>\n<\/ul>\n<figure class=\"full-width\"><img decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/ed1\/813\/cad\/ed1813caddb62b26bb400b48326912aa.png\" alt=\"\u041f\u0440\u0438\u043d\u0446\u0438\u043f \u0440\u0430\u0431\u043e\u0442\u044b OPA \u0441 k8s\" title=\"\u041f\u0440\u0438\u043d\u0446\u0438\u043f \u0440\u0430\u0431\u043e\u0442\u044b OPA \u0441 k8s\" width=\"571\" height=\"346\" sizes=\"auto, (max-width: 780px) 100vw, 50vw\" srcset=\"https:\/\/habrastorage.org\/r\/w780\/getpro\/habr\/upload_files\/ed1\/813\/cad\/ed1813caddb62b26bb400b48326912aa.png 780w,&#10;       https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/ed1\/813\/cad\/ed1813caddb62b26bb400b48326912aa.png 781w\" loading=\"lazy\" decode=\"async\"\/><\/p>\n<div><figcaption>\u041f\u0440\u0438\u043d\u0446\u0438\u043f \u0440\u0430\u0431\u043e\u0442\u044b OPA \u0441 k8s<\/figcaption><\/div>\n<\/figure>\n<h2>\u041d\u0435\u043c\u043d\u043e\u0433\u043e \u043e \u0441\u0438\u043d\u0442\u0430\u043a\u0441\u0438\u0441\u0435 Rego<\/h2>\n<p>Rego \u0434\u0435\u043a\u043b\u0430\u0440\u0430\u0442\u0438\u0432\u043d\u044b\u0439 \u044f\u0437\u044b\u043a, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043e\u0447\u0435\u043d\u044c \u043f\u043e\u0445\u043e\u0436 \u043d\u0430 \u0441\u043e\u0432\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0435. <br \/>\u0411\u0430\u0437\u043e\u0432\u044b\u0439 \u0441\u0438\u043d\u0442\u0430\u043a\u0441\u0438\u0441 \u0431\u0443\u0434\u0435\u0442 \u043f\u043e\u043a\u0430\u0437\u0430\u043d \u043d\u0438\u0436\u0435.<\/p>\n<pre><code class=\"go\">package k8sallowedrepos  # \u0412\u0441\u043f\u043e\u043c\u043e\u0433\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0435 \u043f\u0440\u0430\u0432\u0438\u043b\u043e: true, \u0435\u0441\u043b\u0438 image \u043d\u0430\u0447\u0438\u043d\u0430\u0435\u0442\u0441\u044f \u0445\u043e\u0442\u044f \u0431\u044b \u0441 \u043e\u0434\u043d\u043e\u0433\u043e \u0438\u0437 \u0440\u0430\u0437\u0440\u0435\u0448\u0451\u043d\u043d\u044b\u0445 \u043f\u0440\u0435\u0444\u0438\u043a\u0441\u043e\u0432 allowed_image(image) {   allowed_repo := input.parameters.repos[_]   startswith(image, allowed_repo) }  # \u041f\u0440\u0430\u0432\u0438\u043b\u043e \u043d\u0430\u0440\u0443\u0448\u0435\u043d\u0438\u044f \u0434\u043b\u044f \u043e\u0431\u044b\u0447\u043d\u044b\u0445 \u043a\u043e\u043d\u0442\u0435\u0439\u043d\u0435\u0440\u043e\u0432 violation[{\"msg\": msg}] {   # \u041f\u0435\u0440\u0435\u0431\u0438\u0440\u0430\u0435\u043c \u0432\u0441\u0435 \u043a\u043e\u043d\u0442\u0435\u0439\u043d\u0435\u0440\u044b \u0432 Pod   container := input.review.object.spec.containers[_]    # \u0415\u0441\u043b\u0438 \u043d\u0438 \u043e\u0434\u0438\u043d \u0438\u0437 \u0440\u0430\u0437\u0440\u0435\u0448\u0451\u043d\u043d\u044b\u0445 \u043f\u0440\u0435\u0444\u0438\u043a\u0441\u043e\u0432 \u043d\u0435 \u043f\u043e\u0434\u0445\u043e\u0434\u0438\u0442 \u043f\u043e\u0434 \u043e\u0431\u0440\u0430\u0437 \u043a\u043e\u043d\u0442\u0435\u0439\u043d\u0435\u0440\u0430   not allowed_image(container.image)    # \u0424\u043e\u0440\u043c\u0438\u0440\u0443\u0435\u043c \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u0435 \u043e \u043d\u0430\u0440\u0443\u0448\u0435\u043d\u0438\u0438   msg := sprintf(\"container image '%v' is not from an allowed repository\", [container.image]) }<\/code><\/pre>\n<p>\u0421\u0430\u043c\u0430\u044f \u0432\u0430\u0436\u043d\u0430\u044f \u0447\u0430\u0441\u0442\u044c &#8212; <code>input<\/code>, \u043d\u0435\u043c\u043d\u043e\u0433\u043e \u0440\u0430\u0437\u0431\u0435\u0440\u0435\u043c \u0435\u0433\u043e \u0434\u0435\u0442\u0430\u043b\u0438<\/p>\n<ul>\n<li>\n<p><code><strong>input.request.kind<\/strong><\/code> \u2014 \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u0442 \u0442\u0438\u043f \u043e\u0431\u044a\u0435\u043a\u0442\u0430 k8s (\u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, Pod, Service \u0438 \u0442. \u0434.).<\/p>\n<\/li>\n<li>\n<p><code><strong>input.request.operation<\/strong><\/code> \u2014 \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u0442 \u0442\u0438\u043f \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438: CREATE, UPDATE, DELETE \u0438\u043b\u0438 CONNECT.<\/p>\n<\/li>\n<li>\n<p><code><strong>input.request.userInfo<\/strong><\/code> \u2014 \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u0442 \u0441\u0432\u0435\u0434\u0435\u043d\u0438\u044f \u043e\u0431 \u0438\u0434\u0435\u043d\u0442\u0438\u0447\u043d\u043e\u0441\u0442\u0438 \u0432\u044b\u0437\u044b\u0432\u0430\u044e\u0449\u0435\u0433\u043e \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f.<\/p>\n<\/li>\n<li>\n<p><code><strong>input.request.object<\/strong><\/code> \u2014 \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u0442 \u0432\u0435\u0441\u044c \u043e\u0431\u044a\u0435\u043a\u0442 Kubernetes.<\/p>\n<\/li>\n<li>\n<p><code><strong>input.request.oldObject<\/strong><\/code> \u2014 \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u0442 \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0443\u044e \u0432\u0435\u0440\u0441\u0438\u044e \u043e\u0431\u044a\u0435\u043a\u0442\u0430 Kubernetes \u043f\u0440\u0438 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u044f\u0445 UPDATE \u0438 DELETE.<\/p>\n<\/li>\n<\/ul>\n<h2>\u0423\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0430 OPA gatekeeper<\/h2>\n<p>\u0414\u043b\u044f \u0440\u0430\u0431\u043e\u0442\u044b \u0443 \u043d\u0430\u0441 \u0443\u0436\u0435 \u0434\u043e\u043b\u0436\u0435\u043d \u0431\u044b\u0442\u044c \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u043b\u0435\u043d \u043a\u0443\u0431\u0435\u0440(\u043f\u043e\u0434\u043e\u0439\u0434\u0435\u0442 <strong>minikube<\/strong> \u0438\u043b\u0438 <strong>k3s<\/strong>) \u0438 <strong>helm<\/strong>. \u041f\u043e\u0441\u043b\u0435 \u0443\u0441\u0442\u0430\u043d\u0430\u0432\u043b\u0438\u0432\u0430\u0435\u043c \u0432 helm gatekeeper.<\/p>\n<p><code>helm repo add gatekeeper <\/code><a href=\"https:\/\/open-policy-agent.github.io\/gatekeeper\/charts\" rel=\"noopener noreferrer nofollow\"><code>https:\/\/open-policy-agent.github.io\/gatekeeper\/charts<\/code><\/a><\/p>\n<p><code><br \/>helm repo update<\/code><\/p>\n<p><code><br \/>helm install gatekeeper\/gatekeeper --name-template=gatekeeper --namespace gatekeeper-system --create-namespace<\/code><br \/>\u0414\u043b\u044f \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0438 \u043a\u043e\u0440\u0440\u0435\u043a\u0442\u043d\u043e\u0439 \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0438 \u0432\u0432\u043e\u0434\u0438\u043c \u043a\u043e\u043c\u0430\u043d\u0434\u0443 <code>kubectl get pods -n gatekeeper-system<\/code> \u0432 \u043e\u0442\u0432\u0435\u0442\u0435 \u0434\u043e\u043b\u0436\u043d\u044b \u0431\u044b\u0442\u044c \u0441\u043e\u0437\u0434\u0430\u043d\u044b \u043f\u043e\u0434\u044b:<\/p>\n<figure class=\"full-width\"><img decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/b62\/de9\/5f7\/b62de95f7d7e9e64d236593122869c13.png\" alt=\"\u041f\u043e\u0434\u044b \u0441\u043e\u0437\u0434\u0430\u043d\u044b \u0438 \u0437\u0430\u043f\u0443\u0449\u0435\u043d\u044b\" title=\"\u041f\u043e\u0434\u044b \u0441\u043e\u0437\u0434\u0430\u043d\u044b \u0438 \u0437\u0430\u043f\u0443\u0449\u0435\u043d\u044b\" width=\"1412\" height=\"208\" sizes=\"auto, (max-width: 780px) 100vw, 50vw\" srcset=\"https:\/\/habrastorage.org\/r\/w780\/getpro\/habr\/upload_files\/b62\/de9\/5f7\/b62de95f7d7e9e64d236593122869c13.png 780w,&#10;       https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/b62\/de9\/5f7\/b62de95f7d7e9e64d236593122869c13.png 781w\" loading=\"lazy\" decode=\"async\"\/><\/p>\n<div><figcaption>\u041f\u043e\u0434\u044b \u0441\u043e\u0437\u0434\u0430\u043d\u044b \u0438 \u0437\u0430\u043f\u0443\u0449\u0435\u043d\u044b<\/figcaption><\/div>\n<\/figure>\n<h2>\u0421\u043e\u0437\u0434\u0430\u0435\u043c \u043f\u043e\u043b\u0438\u0442\u0438\u043a\u0438<\/h2>\n<p>\u0414\u043b\u044f \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f \u043f\u043e\u043b\u0438\u0442\u0438\u043a\u0438 \u043d\u0430\u043c \u043d\u0443\u0436\u043d\u043e \u0441\u043e\u0437\u0434\u0430\u0442\u044c <strong>constraint template<\/strong> \u0438 <strong>constraint<\/strong>. \u041e\u0431\u0430 \u043e\u0431\u044a\u0435\u043a\u0442\u0430 \u043c\u043e\u0436\u043d\u043e \u0441\u0440\u0430\u0432\u043d\u0438\u0442\u044c \u0441 \u0444\u0443\u043d\u043a\u0446\u0438\u0435\u0439(template) \u0438 \u043f\u0435\u0440\u0435\u0434\u0430\u0432\u0430\u0435\u043c\u044b\u0445 \u0432 \u043d\u0435\u0435 \u0434\u0430\u043d\u043d\u044b\u0445(constraint). <\/p>\n<p>\u041d\u0438\u0436\u0435 \u0431\u0443\u0434\u0435\u0442 \u0431\u0430\u0437\u043e\u0432\u044b\u0439 \u043f\u0440\u0438\u043c\u0435\u0440 Template, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0431\u0443\u0434\u0435\u0442 \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0438\u0432\u0430\u0442\u044c \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u043f\u043e\u0434\u043e\u0432 \u0438\u0437 \u043d\u0435\u0440\u0430\u0437\u0440\u0435\u0448\u0435\u043d\u043d\u044b\u0445 \u0440\u0435\u043f\u043e\u0437\u0438\u0442\u043e\u0440\u0438\u0435\u0432. \u0411\u0430\u0437\u043e\u0432\u044b\u0439 \u0444\u0430\u0439\u043b \u0434\u043b\u044f \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u0432 \u043a\u0443\u0431\u0435\u0440\u0430 + <strong>rego<\/strong> \u043a\u043e\u0434 \u0434\u043b\u044f \u0432\u0430\u043b\u0438\u0434\u0430\u0446\u0438\u0438.<\/p>\n<pre><code class=\"yaml\">apiVersion: templates.gatekeeper.sh\/v1beta1 kind: ConstraintTemplate metadata:   name: k8sallowedrepos  # \u0418\u043c\u044f \u0448\u0430\u0431\u043b\u043e\u043d\u0430 \u043f\u043e\u043b\u0438\u0442\u0438\u043a\u0438 spec:   crd:     spec:       names:         kind: K8sAllowedRepos  # \u0422\u0438\u043f Constraint, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0431\u0443\u0434\u0435\u0442 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u044d\u0442\u043e\u0442 \u0448\u0430\u0431\u043b\u043e\u043d       validation:         # \u041e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u0435\u043c \u0441\u0445\u0435\u043c\u0443 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u043e\u0432, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043c\u043e\u0436\u043d\u043e \u043f\u0435\u0440\u0435\u0434\u0430\u0442\u044c \u0432 Constraint         openAPIV3Schema:           properties:             repos:               type: array               items:                 type: string   targets:     - target: admission.k8s.gatekeeper.sh  # \u0423\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u043c, \u0447\u0442\u043e \u044d\u0442\u043e webhook \u0434\u043b\u044f admission \u043a\u043e\u043d\u0442\u0440\u043e\u043b\u044f       rego: |         package k8sallowedrepos         allowed_image(image) {           allowed_repo := input.parameters.repos[_]           startswith(image, allowed_repo)         }         violation[{\"msg\": msg}] {           container := input.review.object.spec.containers[_]           not allowed_image(container.image)           msg := sprintf(\"container image '%v' is not from an allowed repository\", [container.image])         } <\/code><\/pre>\n<p>\u0414\u0430\u043b\u0435\u0435 \u0441\u043e\u0437\u0434\u0430\u0434\u0438\u043c 2 constraint \u0442.\u0435. 2 \u043d\u0430\u0431\u043e\u0440\u0430 \u043f\u0440\u0430\u0432\u0438\u043b \u0434\u043b\u044f \u043f\u043e\u043b\u0438\u0442\u0438\u043a\u0438, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0431\u0443\u0434\u0443\u0442 \u0436\u0438\u0442\u044c \u0430\u0432\u0442\u043e\u043d\u043e\u043c\u043d\u043e.<\/p>\n<p>\u041f\u0435\u0440\u0432\u044b\u0439 <strong>constraint<\/strong> \u043d\u0443\u0436\u0435\u043d \u0434\u043b\u044f \u0442\u043e\u0433\u043e, \u0447\u0442\u043e\u0431\u044b \u0441\u0435\u0440\u0432\u0438\u0441\u044b \u0431\u0435\u043a\u0435\u043d\u0434\u0430(\u0441\u043c\u043e\u0442\u0440\u0438\u0442 \u043d\u0430 \u043b\u0435\u0439\u0431\u043b <strong>labelSelector<\/strong>) \u043c\u043e\u0433\u043b\u0438 \u0437\u0430\u043f\u0443\u0441\u043a\u0430\u0442\u044c \u043f\u043e\u0434\u044b \u0442\u043e\u043b\u044c\u043a\u043e \u0438\u0437 \u0440\u0430\u0437\u0440\u0435\u0448\u0435\u043d\u043d\u044b\u0445 \u0440\u0435\u043f\u043e\u0437\u0438\u0442\u043e\u0440\u0438\u0435\u0432(<strong>repos<\/strong>), \u0438\u043d\u0430\u0447\u0435 &#8212; \u043d\u0435 \u0437\u0430\u043f\u0443\u0441\u0442\u0438\u0442.<\/p>\n<pre><code class=\"yaml\">apiVersion: constraints.gatekeeper.sh\/v1beta1 kind: K8sAllowedRepos metadata:   name: allow-backend-repos spec:   match:     kinds:       - apiGroups: [\"\"]         kinds: [\"Pod\"]     # \u0424\u0438\u043b\u044c\u0442\u0440 \u043f\u043e \u043b\u0435\u0439\u0431\u043b\u0443 \"service=backend\"     labelSelector:       matchLabels:         service: backend   parameters:     repos:       - \"myregistry.com\/backend\/\"       - \"python\" <\/code><\/pre>\n<p>\u0412\u0442\u043e\u0440\u043e\u0439 <strong>constraint<\/strong> \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u0442\u0430\u043a \u0436\u0435, \u043a\u0430\u043a \u0438 \u043f\u0435\u0440\u0432\u044b\u0439, \u0442\u043e\u043b\u044c\u043a\u043e \u043e\u0442\u043d\u043e\u0441\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u0441\u0435\u0440\u0432\u0438\u0441\u043e\u0432 <strong>\u0444\u0440\u043e\u043d\u0442\u0435\u043d\u0434\u0430<\/strong>.<\/p>\n<pre><code class=\"yaml\">apiVersion: constraints.gatekeeper.sh\/v1beta1 kind: K8sAllowedRepos metadata:   name: allow-frontend-repos spec:   match:     kinds:       - apiGroups: [\"\"]         kinds: [\"Pod\"]  # \u041f\u0440\u0438\u043c\u0435\u043d\u044f\u0435\u0442\u0441\u044f \u043a Pod'\u0430\u043c     # \u0424\u0438\u043b\u044c\u0442\u0440 \u043f\u043e \u043b\u0435\u0439\u0431\u043b\u0443 \"service=frontend\"     labelSelector:       matchLabels:         service: frontend   parameters:     repos:       - \"myregistry.com\/frontend\/\"       - \"nginx\"<\/code><\/pre>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u043f\u0440\u0438\u043c\u0435\u043d\u0438\u043c \u043f\u043e\u043b\u0438\u0442\u0438\u043a\u0443 \u0432 k8s. \u0415\u0441\u043b\u0438 \u0432\u0441\u0435 \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0438\u0435 \u0448\u0430\u0433\u0438 \u043f\u0440\u043e\u0448\u043b\u0438 \u043a\u043e\u0440\u0440\u0435\u043a\u0442\u043d\u043e &#8212; \u0434\u0435\u043b\u0430\u0435\u043c apply \u0432\u0441\u0435\u0445 yaml \u0444\u0430\u0439\u043b\u043e\u0432. <strong>\u0412\u0430\u0436\u043d\u043e<\/strong> \u0447\u0442\u043e\u0431\u044b Template \u0431\u044b\u043b \u0441\u043e\u0437\u0434\u0430\u043d \u043f\u0435\u0440\u0432\u044b\u043c \u0442\u0430\u043a \u043a\u0430\u043a Constraint \u0446\u0435\u043f\u043b\u044f\u044e\u0442\u0441\u044f \u0438\u043c\u0435\u043d\u043d\u043e \u043a \u043d\u0435\u043c\u0443.<br \/>\u041f\u0440\u0438\u043c\u0435\u043d\u044f\u0435\u043c Template <code>kubectl apply -f ct.yaml<\/code> \u043f\u0440\u0438 \u0443\u0441\u043f\u0435\u0448\u043d\u043e\u043c \u043f\u0440\u0438\u043c\u0435\u043d\u0435\u043d\u0438\u0438 \u043f\u043e\u043b\u0443\u0447\u0438\u043c <code>constrainttemplate.templates.gatekeeper.sh\/k8sallowedrepos created<\/code>.<br \/>\u0422\u0435\u043f\u0435\u0440\u044c \u043f\u0440\u0438\u0441\u0442\u0443\u043f\u0438\u043c \u043a \u043f\u0440\u0438\u043c\u0435\u043d\u0435\u043d\u0438\u044e constraint <code>kubectl apply -f cons-front.yaml<\/code> \u0435\u0441\u043b\u0438 \u0432\u0441\u0435 \u0445\u043e\u0440\u043e\u0448\u043e \u043f\u043e\u043b\u0443\u0447\u0438\u043c <code>k8sallowedrepos.constraints.gatekeeper.sh\/allow-frontend-repos created<\/code>. \u0422\u043e\u0436\u0435 \u0441\u0430\u043c\u043e\u0435 \u043d\u0443\u0436\u043d\u043e \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0441 yaml \u0431\u0435\u043a\u0435\u043d\u0434\u0430.<\/p>\n<h2>\u041d\u0430\u0440\u0443\u0448\u0430\u0435\u043c \u043f\u043e\u043b\u0438\u0442\u0438\u043a\u0438<\/h2>\n<p>\u041f\u0440\u0438\u0441\u0442\u0443\u043f\u0438\u043c \u043a \u0441\u0430\u043c\u043e\u043c\u0443 \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u043e\u043c\u0443 &#8212; \u043d\u0430\u0440\u0443\u0448\u0438\u043c \u043f\u043e\u043b\u0438\u0442\u0438\u043a\u0438! \u0421\u043d\u0430\u0447\u0430\u043b\u0430 \u043f\u043e\u043f\u0440\u043e\u0431\u0443\u0435\u043c \u043f\u043e\u0434\u043d\u044f\u0442\u044c \u043d\u0430\u0448 \u043f\u043e\u0434 <code>kubectl apply -f backend-pod-bad.yaml<\/code><\/p>\n<pre><code class=\"yaml\">apiVersion: v1 kind: Pod metadata:   name: backend-bad   labels:     service: backend spec:   containers:   - name: app     image: nginx:latest  # \u0437\u0430\u043f\u0440\u0435\u0449\u0451\u043d\u043d\u044b\u0439 \u043e\u0431\u0440\u0430\u0437 \u0434\u043b\u044f backend<\/code><\/pre>\n<p>\u0418 OPA \u043d\u0435 \u0434\u0430\u0435\u0442 \u043d\u0430\u043c \u044d\u0442\u043e\u0433\u043e \u0441\u0434\u0435\u043b\u0430\u0442\u044c, \u0432\u0435\u0434\u044c \u043d\u0430\u0448 \u043e\u0431\u0440\u0430\u0437 \u043d\u0435 \u0438\u0437 \u0434\u043e\u0432\u0435\u0440\u0435\u043d\u043d\u043e\u0433\u043e \u0440\u0435\u043f\u043e\u0437\u0438\u0442\u043e\u0440\u0438\u044f! \u041f\u043e\u0441\u043b\u0435\u0434\u043d\u044f\u044f \u0447\u0430\u0441\u0442\u044c \u043e\u0442\u0432\u0435\u0442\u0430 \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0430 \u0438\u0437 \u043d\u0430\u0448\u0435\u0433\u043e Template.<br \/><strong>Error from server (Forbidden): error when creating &#171;backend-pod-bad.yaml&#187;: admission webhook &#171;<\/strong><a href=\"http:\/\/validation.gatekeeper.sh\" rel=\"noopener noreferrer nofollow\"><strong>validation.gatekeeper.sh<\/strong><\/a><strong>&#187; denied the request: [allow-backend-repos] container image &#8216;nginx:latest&#8217; is not from an allowed repository<\/strong><\/p>\n<p>\u041f\u043e\u0445\u043e\u0436\u0430\u044f \u0441\u0438\u0442\u0443\u0430\u0446\u0438\u044f \u0431\u0443\u0434\u0435\u0442, \u0435\u0441\u043b\u0438 \u043c\u044b \u043f\u043e\u043f\u0440\u043e\u0431\u0443\u0435\u043c \u0437\u0430\u043f\u0443\u0441\u0442\u0438\u0442\u044c &#171;\u043f\u043b\u043e\u0445\u043e\u0439&#187; \u043f\u043e\u0434 \u0434\u043b\u044f \u0441\u0435\u0440\u0432\u0438\u0441\u043e\u0432 \u0444\u0440\u043e\u043d\u0442\u0435\u043d\u0434\u0430 <code>kubectl apply -f frontend-pod-bad.yaml<\/code><\/p>\n<pre><code class=\"yaml\">apiVersion: v1 kind: Pod metadata:   name: frontend-bad   labels:     service: frontend spec:   containers:   - name: app     image: alpine:latest  # \u0437\u0430\u043f\u0440\u0435\u0449\u0451\u043d\u043d\u044b\u0439 \u043e\u0431\u0440\u0430\u0437 <\/code><\/pre>\n<p>\u041f\u043e\u043b\u0443\u0447\u0438\u043c \u043f\u0440\u0430\u043a\u0442\u0438\u0447\u0435\u0441\u043a\u0438 \u0438\u0434\u0435\u043d\u0442\u0438\u0447\u043d\u0443\u044e \u043e\u0448\u0438\u0431\u043a\u0443<br \/><strong>Error from server (Forbidden): error when creating &#171;frontend-pod-bad.yaml&#187;: admission webhook &#171;<\/strong><a href=\"http:\/\/validation.gatekeeper.sh\" rel=\"noopener noreferrer nofollow\"><strong>validation.gatekeeper.sh<\/strong><\/a><strong>&#187; denied the request: [allow-frontend-repos] container image &#8216;alpine:latest&#8217; is not from an allowed repository<\/strong><br \/>\u041e\u0431\u0440\u0430\u0442\u0438\u0442\u0435 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435, \u0447\u0442\u043e \u0431\u043b\u0430\u0433\u043e\u0434\u0430\u0440\u044f \u043b\u0435\u0439\u0431\u043b\u0430\u043c \u043f\u0440\u0438 \u043f\u043e\u043f\u044b\u0442\u043a\u0435 \u0441\u043e\u0437\u0434\u0430\u0442\u044c \u043f\u043e\u0434 \u0444\u0440\u043e\u043d\u0442\u0430 \u0438\u0434\u0435\u0442 \u043e\u0431\u0440\u0430\u0449\u0435\u043d\u0438\u0435 \u043a \u043a\u043e\u043d\u0441\u0442\u0440\u0435\u0439\u043d\u0442\u0443 \u0444\u0440\u043e\u043d\u0442\u0430 &#8212; <strong>allow-frontend-repos<\/strong>, \u0430 \u043f\u0440\u0438 \u0431\u0435\u043a\u0435 &#8212; \u043a \u043a\u043e\u043d\u0441\u0442\u0440\u0435\u0439\u043d\u0442\u0443 \u0431\u0435\u043a\u0430 <strong>allow-backend-repos<\/strong>.<br \/>\u041d\u043e \u0435\u0441\u043b\u0438 \u043c\u044b \u0437\u0430\u0445\u043e\u0442\u0438\u043c \u0441\u043e\u0437\u0434\u0430\u0442\u044c &#171;\u0445\u043e\u0440\u043e\u0448\u0438\u0439&#187; \u043f\u043e\u0434 &#8212; <code>kubectl apply -f backend-pod-good.yaml<\/code> \u043e\u043d \u0441\u043e\u0437\u0434\u0430\u0441\u0442\u0441\u044f \u0431\u0435\u0437 \u043d\u0430\u0440\u0443\u0448\u0435\u043d\u0438\u0439 <code>pod\/backend-good created<\/code> .<br \/>\u041f\u0440\u0438\u043c\u0435\u0440 &#171;\u0445\u043e\u0440\u043e\u0448\u0435\u0433\u043e&#187; \u043f\u043e\u0434\u0430:<\/p>\n<pre><code class=\"yaml\">apiVersion: v1 kind: Pod metadata:   name: backend-good   labels:     service: backend spec:   containers:   - name: app     image: python:3.12-slim  # \u0440\u0430\u0437\u0440\u0435\u0448\u0451\u043d\u043d\u044b\u0439, \u0442.\u043a. \u043d\u0430\u0447\u0438\u043d\u0430\u0435\u0442\u0441\u044f \u0441 \"python\"     command: [\"python\", \"-c\", \"print('Hello from Python Pod')\"]<\/code><\/pre>\n<h2>\u0418\u043d\u0442\u0435\u0433\u0440\u0430\u0446\u0438\u044f \u0441 GO<\/h2>\n<p>\u0421\u0434\u0435\u043b\u0430\u0435\u043c \u043f\u0440\u043e\u0441\u0442\u043e\u0439 \u0441\u043a\u0440\u0438\u043f\u0442, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0441\u043e\u0437\u0434\u0430\u0441\u0442 Template \u0438 Constraint \u043f\u043e\u043b\u0438\u0442\u0438\u043a\u0438. \u0421\u043a\u0440\u0438\u043f\u0442 \u0437\u0432\u0435\u0437\u0434 \u0441 \u043d\u0435\u0431\u0430 \u043d\u0435 \u0445\u0432\u0430\u0442\u0430\u0435\u0442, \u043d\u043e \u043e\u043d \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442. \u041f\u0440\u0438 \u0440\u0430\u0431\u043e\u0442\u0435 \u0441 \u043f\u043e\u043b\u0438\u0442\u0438\u043a\u0430\u043c\u0438 \u0435\u0441\u0442\u044c \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e \u043d\u044e\u0430\u043d\u0441\u043e\u0432, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440 \u0435\u0441\u043b\u0438 \u043f\u0440\u0438 \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u0438 <strong>Template<\/strong> \u0442\u0435\u0431\u0435 \u043d\u0430\u043f\u0438\u0441\u0430\u043b\u0438 &#171;<strong>created<\/strong>&#187; &#8212; \u043d\u0435 \u043e\u0431\u044f\u0437\u0430\u0442\u0435\u043b\u044c\u043d\u043e, \u0447\u0442\u043e \u043e\u043d \u043f\u043e\u043b\u043d\u043e\u0441\u0442\u044c\u044e \u0441\u043e\u0437\u0434\u0430\u043d \u0438 \u043d\u0443\u0436\u043d\u043e \u043f\u043e\u0434\u043e\u0436\u0434\u0430\u0442\u044c \u0442\u0430\u043a \u043a\u0430\u043a k8s \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 <strong>\u0430\u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u043e<\/strong>. \u041d\u043e \u043f\u043e\u0434\u043e\u0431\u043d\u044b\u0435 \u043c\u043e\u043c\u0435\u043d\u0442\u044b \u0431\u044b\u043b\u0438 \u043e\u043f\u0443\u0449\u0435\u043d\u044b \u0434\u043b\u044f \u0443\u043f\u0440\u043e\u0449\u0435\u043d\u0438\u044f \u043f\u043e\u043d\u0438\u043c\u0430\u043d\u0438\u044f.<\/p>\n<details class=\"spoiler\">\n<summary>\u0421\u043e\u0437\u0434\u0430\u0435\u043c Template \u0438 Constraint \u0431\u0435\u0437 \u0440\u0435\u0433\u0438\u0441\u0442\u0440\u0430\u0446\u0438\u0438 \u0438 \u0421\u041c\u0421<\/summary>\n<div class=\"spoiler__content\">\n<pre><code class=\"go\">package main  import ( \"context\" \"fmt\" \"time\"  \"k8s.io\/apimachinery\/pkg\/apis\/meta\/v1\/unstructured\"  \"sigs.k8s.io\/controller-runtime\/pkg\/client\" \"sigs.k8s.io\/controller-runtime\/pkg\/client\/config\" )  func main() { ctx := context.Background()  \/\/ \u041f\u043e\u0434\u043a\u043b\u044e\u0447\u0430\u0435\u043c\u0441\u044f \u043a Kubernetes \u0438\u0437 kubeconfig \u043f\u043e \u0443\u043c\u043e\u043b\u0447\u0430\u043d\u0438\u044e cfg, err := config.GetConfig() if err != nil { panic(err) }  cli, err := client.New(cfg, client.Options{}) if err != nil { panic(err) }  \/\/ \u0421\u043e\u0437\u0434\u0430\u0451\u043c ConstraintTemplate constraintTemplate := &amp;unstructured.Unstructured{ Object: map[string]interface{}{ \"apiVersion\": \"templates.gatekeeper.sh\/v1beta1\", \"kind\":       \"ConstraintTemplate\", \"metadata\": map[string]interface{}{ \"name\": \"k8sallowedrepos\", }, \"spec\": map[string]interface{}{ \"crd\": map[string]interface{}{ \"spec\": map[string]interface{}{ \"names\": map[string]interface{}{ \"kind\": \"K8sAllowedRepos\", }, \"validation\": map[string]interface{}{ \"openAPIV3Schema\": map[string]interface{}{ \"properties\": map[string]interface{}{ \"repos\": map[string]interface{}{ \"type\":  \"array\", \"items\": map[string]interface{}{\"type\": \"string\"}, }, }, }, }, }, }, \"targets\": []interface{}{ map[string]interface{}{ \"target\": \"admission.k8s.gatekeeper.sh\", \"rego\": ` package k8sallowedrepos  allowed_image(image) {   allowed_repo := input.parameters.repos[_]   startswith(image, allowed_repo) }  violation[{\"msg\": msg}] {   container := input.review.object.spec.containers[_]   not allowed_image(container.image)   msg := sprintf(\"container image '%v' is not from an allowed repository\", [container.image]) }  violation[{\"msg\": msg}] {   container := input.review.object.spec.initContainers[_]   not allowed_image(container.image)   msg := sprintf(\"initContainer image '%v' is not from an allowed repository\", [container.image]) } `, }, }, }, }, }  err = cli.Patch(ctx, constraintTemplate, client.Apply, client.ForceOwnership, client.FieldOwner(\"example\")) if err != nil { fmt.Println(\"Create or update ConstraintTemplate failed, try Create\") err = cli.Create(ctx, constraintTemplate) if err != nil { panic(err) } } fmt.Println(\"ConstraintTemplate created or updated\")  \/\/ \u0416\u0434\u0435\u043c \u043f\u0430\u0440\u0443 \u0441\u0435\u043a\u0443\u043d\u0434, \u0447\u0442\u043e\u0431\u044b CRD \u0438 \u0448\u0430\u0431\u043b\u043e\u043d \u0443\u0441\u043f\u0435\u043b\u0438 \u0437\u0430\u0440\u0435\u0433\u0438\u0441\u0442\u0440\u0438\u0440\u043e\u0432\u0430\u0442\u044c\u0441\u044f \u0432 API \/\/ \u041f\u043e-\u0445\u043e\u0440\u043e\u0448\u0435\u043c\u0443 \u043d\u0443\u0436\u043d\u0430 \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0430, \u0447\u0442\u043e CRD \u0438 \u0448\u0430\u0431\u043b\u043e\u043d \u0437\u0430\u0440\u0435\u0433\u0438\u0441\u0442\u0440\u0438\u0440\u043e\u0432\u0430\u043d\u044b \u0432 API time.Sleep(3 * time.Second)  \/\/ \u0421\u043e\u0437\u0434\u0430\u0435\u043c Constraint constraint := &amp;unstructured.Unstructured{ Object: map[string]interface{}{ \"apiVersion\": \"constraints.gatekeeper.sh\/v1beta1\", \"kind\":       \"K8sAllowedRepos\", \"metadata\": map[string]interface{}{ \"name\": \"allow-backend-repos\", }, \"spec\": map[string]interface{}{ \"match\": map[string]interface{}{ \"kinds\": []interface{}{ map[string]interface{}{ \"apiGroups\": []interface{}{\"\"}, \"kinds\":     []interface{}{\"Pod\"}, }, }, \"labelSelector\": map[string]interface{}{ \"matchLabels\": map[string]interface{}{ \"service\": \"backend\", }, }, }, \"parameters\": map[string]interface{}{ \"repos\": []interface{}{ \"myregistry.com\/backend\/\", \"python\", }, }, }, }, }  err = cli.Create(ctx, constraint) if err != nil { fmt.Printf(\"Failed to create Constraint: %v\\n\", err) } else { fmt.Println(\"Constraint created\") } } <\/code><\/pre>\n<p>\u041f\u0440\u0438\u043c\u0435\u0440 \u0432\u044b\u0432\u043e\u0434\u0430 \u0441 \u0442\u0435\u0440\u043c\u0438\u043d\u0430\u043b\u0430 \u0441 \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u043e\u0439 \u0441\u043e\u0437\u0434\u0430\u043d\u043d\u044b\u0445 \u043f\u043e\u043b\u0438\u0442\u0438\u043a<\/p>\n<pre><code class=\"yaml\">admin@MacBook-Pro-2 create_policy % go run main.go  ConstraintTemplate created or updated Constraint created admin@MacBook-Pro-2 create_policy % kubectl get constrainttemplates                   NAME              AGE k8sallowedrepos   8s admin@MacBook-Pro-2 create_policy % kubectl get constraint          NAME                  ENFORCEMENT-ACTION   TOTAL-VIOLATIONS allow-backend-repos   deny  <\/code><\/pre>\n<\/div>\n<\/details>\n<p>\u0422\u0430\u043a \u0436\u0435 \u0438\u043d\u0442\u0435\u0433\u0440\u0430\u0446\u0438\u0438 \u0441 <strong>k8s<\/strong> \u0438 <strong>gatekeeper<\/strong> \u0434\u043e\u0432\u043e\u043b\u044c\u043d\u043e \u043c\u043e\u0449\u043d\u044b\u0439 \u0438\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442, \u0441 \u043a\u043e\u0442\u043e\u0440\u044b\u043c \u043c\u043e\u0436\u043d\u043e \u043e\u0442\u0441\u043b\u0435\u0436\u0438\u0432\u0430\u0442\u044c \u043d\u0430\u0440\u0443\u0448\u0435\u043d\u0438\u044f \u043f\u043e\u043b\u0438\u0442\u0438\u043a, \u0432\u0441\u0435 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 \u043f\u043e \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044e \u043f\u043e\u043b\u0438\u0442\u0438\u043a \u0438 \u043c\u043d\u043e\u0433\u043e\u0435 \u0434\u0440\u0443\u0433\u043e\u0435. \u041d\u043e \u0437\u0434\u0435\u0441\u044c \u0441\u0442\u043e\u0438\u0442 \u0431\u044b\u0442\u044c \u043e\u0447\u0435\u043d\u044c \u0430\u043a\u043a\u0443\u0440\u0430\u0442\u043d\u044b\u043c \u0442\u0430\u043a \u043a\u0430\u043a \u043f\u0440\u0438 \u0440\u0430\u0431\u043e\u0442\u0435 \u043c\u043d\u043e\u0433\u043e \u043d\u044e\u0430\u043d\u0441\u043e\u0432.<\/p>\n<h2>\u0412\u043c\u0435\u0441\u0442\u043e \u0432\u044b\u0432\u043e\u0434\u0430(\u043b\u043e\u043c\u0430\u0435\u043c \u0432\u0441\u0435 \u043e\u0434\u043d\u043e\u0439 \u043f\u043e\u043b\u0438\u0442\u0438\u043a\u043e\u0439)<\/h2>\n<p>\u041f\u043e\u043b\u0438\u0442\u0438\u043a\u0438 \u0431\u0435\u0437\u043e\u043f\u0430\u0441\u043d\u043e\u0441\u0442\u0438 \u043e\u0447\u0435\u043d\u044c \u043c\u043e\u0449\u043d\u044b\u0439 \u0438 \u043f\u043e\u043b\u0435\u0437\u043d\u044b\u0439 \u0438\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442 \u0432 \u0445\u043e\u0440\u043e\u0448\u0438\u0445 \u0440\u0443\u043a\u0430\u0445. \u041d\u043e \u0432 \u043f\u043b\u043e\u0445\u0438\u0445 \u043c\u043e\u0436\u043d\u043e \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u043f\u043e\u0434\u043e\u0431\u043d\u044b\u0439 <strong>rego<\/strong> \u043a\u043e\u0434 \u0438 \u043b\u044e\u0431\u0430\u044f \u043f\u043e\u043f\u044b\u0442\u043a\u0430 \u0441\u043e\u0437\u0434\u0430\u0442\u044c \u0440\u0435\u0441\u0443\u0440\u0441 \u0432\u0435\u0440\u043d\u0451\u0442 \u043e\u0448\u0438\u0431\u043a\u0443 \u0441 \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u0435\u043c <code>\"Creating or updating resources is forbidden by denyall policy\"<\/code>. <strong>\u0411\u0443\u0434\u044c\u0442\u0435 \u0430\u043a\u043a\u0443\u0440\u0430\u0442\u043d\u044b!<\/strong><\/p>\n<pre><code class=\"go\">package denyall  violation[{\"msg\": msg}] {   msg := \"Creating or updating resources is forbidden by denyall policy\" }<\/code><\/pre>\n<p>P.S. \u0435\u0441\u043b\u0438 \u0441\u043e\u0437\u0434\u0430\u0434\u0438\u0442\u0435 \u0434\u043b\u044f \u0442\u0435\u0441\u0442\u043e\u0432(\u0442\u043e\u043b\u044c\u043a\u043e \u043d\u0435 \u043d\u0430 \u043f\u0440\u043e\u0434\u0435, \u043f\u0440\u043e\u0448\u0443) \u043f\u043e\u0434\u043e\u0431\u043d\u0443\u044e \u043f\u043e\u043b\u0438\u0442\u0438\u043a\u0443, \u0447\u0442\u043e\u0431\u044b \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0432\u0441\u0435 \u0432 \u0440\u0430\u0431\u043e\u0447\u0438\u0439 \u0432\u0438\u0434 &#8212; <strong>\u0443\u0434\u0430\u043b\u0438\u0442\u0435<\/strong> \u0432\u0430\u0448 Template.<\/p>\n<p>\u0415\u0441\u0442\u044c <a href=\"https:\/\/t.me\/gromyko01\" rel=\"noopener noreferrer nofollow\">\u0422\u0413<\/a>(\u043e\u0431\u0449\u0430\u0435\u043c\u0441\u044f, \u043f\u043e\u0441\u0442\u0438\u043c \u0432\u0441\u044f\u043a\u043e\u0435 IT\u0448\u043d\u043e\u0435) \u0438 \u043d\u0435\u043c\u043d\u043e\u0433\u043e <a href=\"https:\/\/www.youtube.com\/@Gromyko21\" rel=\"noopener noreferrer nofollow\">\u042e\u0442\u0443\u0431\u0430<\/a>.<\/p>\n<\/div>\n<\/div>\n<\/div>\n<p><!----><!----><\/div>\n<p><!----><!----><br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/articles\/935842\/\"> https:\/\/habr.com\/ru\/articles\/935842\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<div><!--[--><!--]--><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<p>\u041f\u043e\u0433\u043e\u0432\u043e\u0440\u0438\u043c \u043e \u043f\u043e\u043b\u0438\u0442\u0438\u043a\u0430\u0445 \u0431\u0435\u0437\u043e\u043f\u0430\u0441\u043d\u043e\u0441\u0442\u0438 <strong>OPA<\/strong> \u0432 \u043a\u0443\u0431\u0435\u0440\u0435. \u041e\u0431\u0441\u0443\u0434\u0438\u043c \u043d\u0430 \u043f\u0440\u0438\u043c\u0435\u0440\u0430\u0445 \u0437\u0430\u0447\u0435\u043c \u043e\u043d\u0438 \u043d\u0443\u0436\u043d\u044b, \u0432 \u043a\u0430\u043a\u0438\u0445 \u0441\u043b\u0443\u0447\u0430\u044f\u0445 \u043e\u043d\u0438 \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u043f\u043e\u043c\u043e\u0433\u0443\u0442 \u043e\u0431\u0435\u0437\u043e\u043f\u0430\u0441\u0438\u0442\u044c, \u043a\u043e\u0433\u0434\u0430 \u043f\u043e\u043b\u0438\u0442\u0438\u043a\u0438 \u043c\u043e\u0433\u0443\u0442 \u043f\u043e\u043b\u043e\u0436\u0438\u0442\u044c \u0432\u0441\u044e \u0441\u0438\u0441\u0442\u0435\u043c\u0443 \u0438 \u043a\u0430\u043a \u0438\u043c\u0438 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c\u0441\u044f \u0432 \u043a\u0443\u0431\u0435\u0440\u0435. \u041f\u043b\u044e\u0441\u043e\u043c \u0437\u0430\u0445\u0432\u0430\u0442\u0438\u043c \u043d\u0435\u043c\u043d\u043e\u0433\u043e \u043a\u043e\u0434\u0430 \u043d\u0430 <strong>go<\/strong> \u0434\u043b\u044f \u0440\u0430\u0431\u043e\u0442\u044b \u0441 \u043d\u0438\u043c\u0438.<\/p>\n<p><strong> \u0412\u0432\u0435\u0434\u0435\u043d\u0438\u0435<\/strong><\/p>\n<p>\u0412 \u043d\u0430\u0448\u0435 \u0432\u0440\u0435\u043c\u044f \u0441\u043f\u043b\u043e\u0448\u044c \u0438 \u0440\u044f\u0434\u043e\u043c \u043c\u044b \u0432\u0438\u0434\u0438\u043c \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u044e \u043e \u0442\u043e\u043c, \u0447\u0442\u043e \u043b\u0438\u0447\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435 \u043c\u0438\u043b\u043b\u0438\u043e\u043d\u043e\u0432 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u0435\u0439 \u0441\u043b\u0438\u0432\u0430\u0435\u0442\u0441\u044f. \u0412\u0438\u0434\u0438\u043c \u043d\u0430\u0440\u0443\u0448\u0435\u043d\u0438\u044f \u0440\u0430\u0431\u043e\u0442\u044b \u0440\u0430\u0437\u043d\u044b\u0445 \u043a\u0440\u0443\u043f\u043d\u044b\u0445 \u0441\u0438\u0441\u0442\u0435\u043c, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, 28 \u0438\u044e\u043b\u044f \u0431\u044b\u043b\u0438 \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u044b \u0443 \u0410\u044d\u0440\u043e\u0444\u043b\u043e\u0442\u0430 \u0438\u0437-\u0437\u0430 \u0447\u0435\u0433\u043e \u043e\u0442\u043c\u0435\u043d\u0438\u043b\u043e\u0441\u044c \u043c\u043d\u043e\u0433\u043e \u0440\u0435\u0439\u0441\u043e\u0432, \u043b\u044e\u0434\u0438 \u0441\u043f\u0430\u043b\u0438 \u0431\u0443\u043a\u0432\u0430\u043b\u044c\u043d\u043e \u0432 \u0430\u044d\u0440\u043e\u043f\u043e\u0440\u0442\u0435 \u0438 \u0443\u043f\u0430\u043b\u0430 \u043a\u0430\u043f\u0438\u0442\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043a\u043e\u043c\u043f\u0430\u043d\u0438\u0438. <\/p>\n<figure class=\"full-width\">\n<div><figcaption>\u0414\u0438\u043d\u0430\u043c\u0438\u043a\u0430 \u0430\u043a\u0446\u0438\u0439 \u043d\u0430 28 \u0438\u044e\u043b\u044f<\/figcaption><\/div>\n<\/figure>\n<p>\u041f\u043e\u043b\u0438\u0442\u0438\u043a\u0438 \u0431\u0435\u0437\u043e\u043f\u0430\u0441\u043d\u043e\u0441\u0442\u0438 \u043d\u0443\u0436\u043d\u044b \u0434\u043b\u044f \u0441\u043e\u0431\u043b\u044e\u0434\u0435\u043d\u0438\u044f \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u043d\u044b\u0445 \u043f\u0440\u0430\u0432\u0438\u043b \u043a\u043e\u043c\u043f\u0430\u043d\u0438\u0438.\u00a0\u0421\u0435\u0433\u043e\u0434\u043d\u044f \u043c\u044b \u043e\u0431\u0441\u0443\u0434\u0438\u043c <strong>Open Policy Agent<\/strong>. \u0418\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442 \u0434\u043e\u0432\u043e\u043b\u044c\u043d\u043e \u043c\u043e\u0449\u043d\u044b\u0439 \u0438 \u0443\u043d\u0438\u0432\u0435\u0440\u0441\u0430\u043b\u044c\u043d\u044b\u0439, \u0435\u0441\u043b\u0438 \u0445\u043e\u0442\u0438\u0442\u0435 \u0434\u0435\u0442\u0430\u043b\u044c\u043d\u043e\u0433\u043e \u043f\u043e\u0433\u0440\u0443\u0436\u0435\u043d\u0438\u044f \u0440\u0435\u043a\u043e\u043c\u0435\u043d\u0434\u0443\u044e \u043f\u043e\u0433\u0440\u0443\u0437\u0438\u0442\u044c\u0441\u044f \u0432 \u043e\u0444\u0438\u0446\u0438\u0430\u043b\u044c\u043d\u0443\u044e <a href=\"https:\/\/www.openpolicyagent.org\/docs\" rel=\"noopener noreferrer nofollow\">\u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u044e<\/a>.\u00a0<\/p>\n<p>\u0411\u0430\u0437\u043e\u0432\u044b\u0435 \u0441\u043b\u0443\u0447\u0430\u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f \u043f\u043e\u043b\u0438\u0442\u0438\u043a:<\/p>\n<ul>\n<li>\n<p>\u041c\u043e\u0436\u0435\u0442 \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0442\u044c \u043c\u0430\u043d\u0438\u0444\u0435\u0441\u0442\u044b <code>Deployment<\/code>, <code>StatefulSet<\/code>, <code>DaemonSet<\/code> \u2014 \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u043e\u0431\u044f\u0437\u0430\u0442\u0435\u043b\u044c\u043d\u044b\u0435 <code>labels<\/code> \u0438 \u0430\u043d\u043d\u043e\u0442\u0430\u0446\u0438\u0438.\u00a0<\/p>\n<\/li>\n<li>\n<p>\u0414\u043b\u044f <code>Pod'\u043e\u0432<\/code> &#8212; \u0437\u0430\u043f\u0440\u0435\u0442 \u0437\u0430\u043f\u0443\u0441\u043a\u0430 \u043d\u0435 \u0438\u0437 \u0434\u043e\u0432\u0435\u0440\u0435\u043d\u043d\u044b\u0445 \u0440\u0435\u043f\u043e\u0437\u0438\u0442\u043e\u0440\u0438\u0435\u0432 \u0438\u043b\u0438 \u0437\u0430\u043f\u0440\u0435\u0442\u00a0\u0442\u0435\u0433\u0430 \u00a0<code>latest<\/code><\/p>\n<\/li>\n<li>\n<p>\u041f\u0440\u043e\u0432\u0435\u0440\u043a\u0430 \u0441\u043e\u0431\u044b\u0442\u0438\u0439 \u0432\u043d\u0435 <code>Pod'\u043e\u0432<\/code> \u0421\u043e\u0437\u0434\u0430\u043d\u0438\u0435\/\u0443\u0434\u0430\u043b\u0435\u043d\u0438\u0435 <code>Namespaces<\/code> \u0442\u043e\u043b\u044c\u043a\u043e \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0451\u043d\u043d\u044b\u043c\u0438 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f\u043c\u0438. \u0418 \u043c\u043d\u043e\u0433\u043e\u0435 \u0434\u0440\u0443\u0433\u043e\u0435.\u00a0<\/p>\n<\/li>\n<\/ul>\n<p><strong>\u041a\u0430\u043a OPA \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u0432 k8s<\/strong><\/p>\n<p>\u041e\u0431\u044b\u0447\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e\u0442 <strong>OPA Gatekeeper \u00a0<\/strong>\u0434\u043b\u044f \u0438\u043d\u0442\u0435\u0433\u0440\u0430\u0446\u0438\u0438 OPA \u0432 k8s:<\/p>\n<ul>\n<li>\n<p>\u041e\u043d \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0430\u0435\u0442\u0441\u044f \u043a\u0430\u043a <strong>ValidatingAdmissionWebhook<\/strong>(\u0440\u0443\u0436\u0435 MutatingAdmissionWebhook).<\/p>\n<\/li>\n<li>\n<p>\u041f\u0440\u0438 \u043a\u0430\u0436\u0434\u043e\u043c \u0437\u0430\u043f\u0440\u043e\u0441\u0435 \u043d\u0430 \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u0435\/\u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u0435 \u0440\u0435\u0441\u0443\u0440\u0441\u0430 OPA \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u0442 \u043e\u0431\u044a\u0435\u043a\u0442 \u0432 JSON.<\/p>\n<\/li>\n<li>\n<p>\u041f\u0440\u043e\u0433\u043e\u043d\u044f\u0435\u0442 \u0435\u0433\u043e \u0447\u0435\u0440\u0435\u0437 \u043f\u043e\u043b\u0438\u0442\u0438\u043a\u0438, \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u043d\u044b\u0435 \u043d\u0430 \u044f\u0437\u044b\u043a\u0435 <strong>Rego<\/strong>.<\/p>\n<\/li>\n<li>\n<p>\u0415\u0441\u043b\u0438 \u0447\u0442\u043e-\u0442\u043e \u043d\u0435 \u043f\u0440\u043e\u0445\u043e\u0434\u0438\u0442 \u2014 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 \u043e\u0448\u0438\u0431\u043a\u0443, \u0438 \u043e\u0431\u044a\u0435\u043a\u0442 \u043d\u0435 \u0441\u043e\u0437\u0434\u0430\u0451\u0442\u0441\u044f.<\/p>\n<\/li>\n<\/ul>\n<figure class=\"full-width\">\n<div><figcaption>\u041f\u0440\u0438\u043d\u0446\u0438\u043f \u0440\u0430\u0431\u043e\u0442\u044b OPA \u0441 k8s<\/figcaption><\/div>\n<\/figure>\n<h2>\u041d\u0435\u043c\u043d\u043e\u0433\u043e \u043e \u0441\u0438\u043d\u0442\u0430\u043a\u0441\u0438\u0441\u0435 Rego<\/h2>\n<p>Rego \u0434\u0435\u043a\u043b\u0430\u0440\u0430\u0442\u0438\u0432\u043d\u044b\u0439 \u044f\u0437\u044b\u043a, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043e\u0447\u0435\u043d\u044c \u043f\u043e\u0445\u043e\u0436 \u043d\u0430 \u0441\u043e\u0432\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0435. <br \/>\u0411\u0430\u0437\u043e\u0432\u044b\u0439 \u0441\u0438\u043d\u0442\u0430\u043a\u0441\u0438\u0441 \u0431\u0443\u0434\u0435\u0442 \u043f\u043e\u043a\u0430\u0437\u0430\u043d \u043d\u0438\u0436\u0435.<\/p>\n<pre><code class=\"go\">package k8sallowedrepos  # \u0412\u0441\u043f\u043e\u043c\u043e\u0433\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0435 \u043f\u0440\u0430\u0432\u0438\u043b\u043e: true, \u0435\u0441\u043b\u0438 image \u043d\u0430\u0447\u0438\u043d\u0430\u0435\u0442\u0441\u044f \u0445\u043e\u0442\u044f \u0431\u044b \u0441 \u043e\u0434\u043d\u043e\u0433\u043e \u0438\u0437 \u0440\u0430\u0437\u0440\u0435\u0448\u0451\u043d\u043d\u044b\u0445 \u043f\u0440\u0435\u0444\u0438\u043a\u0441\u043e\u0432 allowed_image(image) {   allowed_repo := input.parameters.repos[_]   startswith(image, allowed_repo) }  # \u041f\u0440\u0430\u0432\u0438\u043b\u043e \u043d\u0430\u0440\u0443\u0448\u0435\u043d\u0438\u044f \u0434\u043b\u044f \u043e\u0431\u044b\u0447\u043d\u044b\u0445 \u043a\u043e\u043d\u0442\u0435\u0439\u043d\u0435\u0440\u043e\u0432 violation[{\"msg\": msg}] {   # \u041f\u0435\u0440\u0435\u0431\u0438\u0440\u0430\u0435\u043c \u0432\u0441\u0435 \u043a\u043e\u043d\u0442\u0435\u0439\u043d\u0435\u0440\u044b \u0432 Pod   container := input.review.object.spec.containers[_]    # \u0415\u0441\u043b\u0438 \u043d\u0438 \u043e\u0434\u0438\u043d \u0438\u0437 \u0440\u0430\u0437\u0440\u0435\u0448\u0451\u043d\u043d\u044b\u0445 \u043f\u0440\u0435\u0444\u0438\u043a\u0441\u043e\u0432 \u043d\u0435 \u043f\u043e\u0434\u0445\u043e\u0434\u0438\u0442 \u043f\u043e\u0434 \u043e\u0431\u0440\u0430\u0437 \u043a\u043e\u043d\u0442\u0435\u0439\u043d\u0435\u0440\u0430   not allowed_image(container.image)    # \u0424\u043e\u0440\u043c\u0438\u0440\u0443\u0435\u043c \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u0435 \u043e \u043d\u0430\u0440\u0443\u0448\u0435\u043d\u0438\u0438   msg := sprintf(\"container image '%v' is not from an allowed repository\", [container.image]) }<\/code><\/pre>\n<p>\u0421\u0430\u043c\u0430\u044f \u0432\u0430\u0436\u043d\u0430\u044f \u0447\u0430\u0441\u0442\u044c &#8212; <code>input<\/code>, \u043d\u0435\u043c\u043d\u043e\u0433\u043e \u0440\u0430\u0437\u0431\u0435\u0440\u0435\u043c \u0435\u0433\u043e \u0434\u0435\u0442\u0430\u043b\u0438<\/p>\n<ul>\n<li>\n<p><code><strong>input.request.kind<\/strong><\/code> \u2014 \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u0442 \u0442\u0438\u043f \u043e\u0431\u044a\u0435\u043a\u0442\u0430 k8s (\u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, Pod, Service \u0438 \u0442. \u0434.).<\/p>\n<\/li>\n<li>\n<p><code><strong>input.request.operation<\/strong><\/code> \u2014 \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u0442 \u0442\u0438\u043f \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438: CREATE, UPDATE, DELETE \u0438\u043b\u0438 CONNECT.<\/p>\n<\/li>\n<li>\n<p><code><strong>input.request.userInfo<\/strong><\/code> \u2014 \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u0442 \u0441\u0432\u0435\u0434\u0435\u043d\u0438\u044f \u043e\u0431 \u0438\u0434\u0435\u043d\u0442\u0438\u0447\u043d\u043e\u0441\u0442\u0438 \u0432\u044b\u0437\u044b\u0432\u0430\u044e\u0449\u0435\u0433\u043e \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f.<\/p>\n<\/li>\n<li>\n<p><code><strong>input.request.object<\/strong><\/code> \u2014 \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u0442 \u0432\u0435\u0441\u044c \u043e\u0431\u044a\u0435\u043a\u0442 Kubernetes.<\/p>\n<\/li>\n<li>\n<p><code><strong>input.request.oldObject<\/strong><\/code> \u2014 \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u0442 \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0443\u044e \u0432\u0435\u0440\u0441\u0438\u044e \u043e\u0431\u044a\u0435\u043a\u0442\u0430 Kubernetes \u043f\u0440\u0438 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u044f\u0445 UPDATE \u0438 DELETE.<\/p>\n<\/li>\n<\/ul>\n<h2>\u0423\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0430 OPA gatekeeper<\/h2>\n<p>\u0414\u043b\u044f \u0440\u0430\u0431\u043e\u0442\u044b \u0443 \u043d\u0430\u0441 \u0443\u0436\u0435 \u0434\u043e\u043b\u0436\u0435\u043d \u0431\u044b\u0442\u044c \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u043b\u0435\u043d \u043a\u0443\u0431\u0435\u0440(\u043f\u043e\u0434\u043e\u0439\u0434\u0435\u0442 <strong>minikube<\/strong> \u0438\u043b\u0438 <strong>k3s<\/strong>) \u0438 <strong>helm<\/strong>. \u041f\u043e\u0441\u043b\u0435 \u0443\u0441\u0442\u0430\u043d\u0430\u0432\u043b\u0438\u0432\u0430\u0435\u043c \u0432 helm gatekeeper.<\/p>\n<p><code>helm repo add gatekeeper <\/code><a href=\"https:\/\/open-policy-agent.github.io\/gatekeeper\/charts\" rel=\"noopener noreferrer nofollow\"><code>https:\/\/open-policy-agent.github.io\/gatekeeper\/charts<\/code><\/a><\/p>\n<p><code><br \/>helm repo update<\/code><\/p>\n<p><code><br \/>helm install gatekeeper\/gatekeeper --name-template=gatekeeper --namespace gatekeeper-system --create-namespace<\/code><br \/>\u0414\u043b\u044f \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0438 \u043a\u043e\u0440\u0440\u0435\u043a\u0442\u043d\u043e\u0439 \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0438 \u0432\u0432\u043e\u0434\u0438\u043c \u043a\u043e\u043c\u0430\u043d\u0434\u0443 <code>kubectl get pods -n gatekeeper-system<\/code> \u0432 \u043e\u0442\u0432\u0435\u0442\u0435 \u0434\u043e\u043b\u0436\u043d\u044b \u0431\u044b\u0442\u044c \u0441\u043e\u0437\u0434\u0430\u043d\u044b \u043f\u043e\u0434\u044b:<\/p>\n<figure class=\"full-width\">\n<div><figcaption>\u041f\u043e\u0434\u044b \u0441\u043e\u0437\u0434\u0430\u043d\u044b \u0438 \u0437\u0430\u043f\u0443\u0449\u0435\u043d\u044b<\/figcaption><\/div>\n<\/figure>\n<h2>\u0421\u043e\u0437\u0434\u0430\u0435\u043c \u043f\u043e\u043b\u0438\u0442\u0438\u043a\u0438<\/h2>\n<p>\u0414\u043b\u044f \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f \u043f\u043e\u043b\u0438\u0442\u0438\u043a\u0438 \u043d\u0430\u043c \u043d\u0443\u0436\u043d\u043e \u0441\u043e\u0437\u0434\u0430\u0442\u044c <strong>constraint template<\/strong> \u0438 <strong>constraint<\/strong>. \u041e\u0431\u0430 \u043e\u0431\u044a\u0435\u043a\u0442\u0430 \u043c\u043e\u0436\u043d\u043e \u0441\u0440\u0430\u0432\u043d\u0438\u0442\u044c \u0441 \u0444\u0443\u043d\u043a\u0446\u0438\u0435\u0439(template) \u0438 \u043f\u0435\u0440\u0435\u0434\u0430\u0432\u0430\u0435\u043c\u044b\u0445 \u0432 \u043d\u0435\u0435 \u0434\u0430\u043d\u043d\u044b\u0445(constraint). <\/p>\n<p>\u041d\u0438\u0436\u0435 \u0431\u0443\u0434\u0435\u0442 \u0431\u0430\u0437\u043e\u0432\u044b\u0439 \u043f\u0440\u0438\u043c\u0435\u0440 Template, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0431\u0443\u0434\u0435\u0442 \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0438\u0432\u0430\u0442\u044c \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u043f\u043e\u0434\u043e\u0432 \u0438\u0437 \u043d\u0435\u0440\u0430\u0437\u0440\u0435\u0448\u0435\u043d\u043d\u044b\u0445 \u0440\u0435\u043f\u043e\u0437\u0438\u0442\u043e\u0440\u0438\u0435\u0432. \u0411\u0430\u0437\u043e\u0432\u044b\u0439 \u0444\u0430\u0439\u043b \u0434\u043b\u044f \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u0432 \u043a\u0443\u0431\u0435\u0440\u0430 + <strong>rego<\/strong> \u043a\u043e\u0434 \u0434\u043b\u044f \u0432\u0430\u043b\u0438\u0434\u0430\u0446\u0438\u0438.<\/p>\n<pre><code class=\"yaml\">apiVersion: templates.gatekeeper.sh\/v1beta1 kind: ConstraintTemplate metadata:   name: k8sallowedrepos  # \u0418\u043c\u044f \u0448\u0430\u0431\u043b\u043e\u043d\u0430 \u043f\u043e\u043b\u0438\u0442\u0438\u043a\u0438 spec:   crd:     spec:       names:         kind: K8sAllowedRepos  # \u0422\u0438\u043f Constraint, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0431\u0443\u0434\u0435\u0442 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u044d\u0442\u043e\u0442 \u0448\u0430\u0431\u043b\u043e\u043d       validation:         # \u041e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u0435\u043c \u0441\u0445\u0435\u043c\u0443 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u043e\u0432, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043c\u043e\u0436\u043d\u043e \u043f\u0435\u0440\u0435\u0434\u0430\u0442\u044c \u0432 Constraint         openAPIV3Schema:           properties:             repos:               type: array               items:                 type: string   targets:     - target: admission.k8s.gatekeeper.sh  # \u0423\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u043c, \u0447\u0442\u043e \u044d\u0442\u043e webhook \u0434\u043b\u044f admission \u043a\u043e\u043d\u0442\u0440\u043e\u043b\u044f       rego: |         package k8sallowedrepos         allowed_image(image) {           allowed_repo := input.parameters.repos[_]           startswith(image, allowed_repo)         }         violation[{\"msg\": msg}] {           container := input.review.object.spec.containers[_]           not allowed_image(container.image)           msg := sprintf(\"container image '%v' is not from an allowed repository\", [container.image])         } <\/code><\/pre>\n<p>\u0414\u0430\u043b\u0435\u0435 \u0441\u043e\u0437\u0434\u0430\u0434\u0438\u043c 2 constraint \u0442.\u0435. 2 \u043d\u0430\u0431\u043e\u0440\u0430 \u043f\u0440\u0430\u0432\u0438\u043b \u0434\u043b\u044f \u043f\u043e\u043b\u0438\u0442\u0438\u043a\u0438, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0431\u0443\u0434\u0443\u0442 \u0436\u0438\u0442\u044c \u0430\u0432\u0442\u043e\u043d\u043e\u043c\u043d\u043e.<\/p>\n<p>\u041f\u0435\u0440\u0432\u044b\u0439 <strong>constraint<\/strong> \u043d\u0443\u0436\u0435\u043d \u0434\u043b\u044f \u0442\u043e\u0433\u043e, \u0447\u0442\u043e\u0431\u044b \u0441\u0435\u0440\u0432\u0438\u0441\u044b \u0431\u0435\u043a\u0435\u043d\u0434\u0430(\u0441\u043c\u043e\u0442\u0440\u0438\u0442 \u043d\u0430 \u043b\u0435\u0439\u0431\u043b <strong>labelSelector<\/strong>) \u043c\u043e\u0433\u043b\u0438 \u0437\u0430\u043f\u0443\u0441\u043a\u0430\u0442\u044c \u043f\u043e\u0434\u044b \u0442\u043e\u043b\u044c\u043a\u043e \u0438\u0437 \u0440\u0430\u0437\u0440\u0435\u0448\u0435\u043d\u043d\u044b\u0445 \u0440\u0435\u043f\u043e\u0437\u0438\u0442\u043e\u0440\u0438\u0435\u0432(<strong>repos<\/strong>), \u0438\u043d\u0430\u0447\u0435 &#8212; \u043d\u0435 \u0437\u0430\u043f\u0443\u0441\u0442\u0438\u0442.<\/p>\n<pre><code class=\"yaml\">apiVersion: constraints.gatekeeper.sh\/v1beta1 kind: K8sAllowedRepos metadata:   name: allow-backend-repos spec:   match:     kinds:       - apiGroups: [\"\"]         kinds: [\"Pod\"]     # \u0424\u0438\u043b\u044c\u0442\u0440 \u043f\u043e \u043b\u0435\u0439\u0431\u043b\u0443 \"service=backend\"     labelSelector:       matchLabels:         service: backend   parameters:     repos:       - \"myregistry.com\/backend\/\"       - \"python\" <\/code><\/pre>\n<p>\u0412\u0442\u043e\u0440\u043e\u0439 <strong>constraint<\/strong> \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u0442\u0430\u043a \u0436\u0435, \u043a\u0430\u043a \u0438 \u043f\u0435\u0440\u0432\u044b\u0439, \u0442\u043e\u043b\u044c\u043a\u043e \u043e\u0442\u043d\u043e\u0441\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u0441\u0435\u0440\u0432\u0438\u0441\u043e\u0432 <strong>\u0444\u0440\u043e\u043d\u0442\u0435\u043d\u0434\u0430<\/strong>.<\/p>\n<pre><code class=\"yaml\">apiVersion: constraints.gatekeeper.sh\/v1beta1 kind: K8sAllowedRepos metadata:   name: allow-frontend-repos spec:   match:     kinds:       - apiGroups: [\"\"]         kinds: [\"Pod\"]  # \u041f\u0440\u0438\u043c\u0435\u043d\u044f\u0435\u0442\u0441\u044f \u043a Pod'\u0430\u043c     # \u0424\u0438\u043b\u044c\u0442\u0440 \u043f\u043e \u043b\u0435\u0439\u0431\u043b\u0443 \"service=frontend\"     labelSelector:       matchLabels:         service: frontend   parameters:     repos:       - \"myregistry.com\/frontend\/\"       - \"nginx\"<\/code><\/pre>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u043f\u0440\u0438\u043c\u0435\u043d\u0438\u043c \u043f\u043e\u043b\u0438\u0442\u0438\u043a\u0443 \u0432 k8s. \u0415\u0441\u043b\u0438 \u0432\u0441\u0435 \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0438\u0435 \u0448\u0430\u0433\u0438 \u043f\u0440\u043e\u0448\u043b\u0438 \u043a\u043e\u0440\u0440\u0435\u043a\u0442\u043d\u043e &#8212; \u0434\u0435\u043b\u0430\u0435\u043c apply \u0432\u0441\u0435\u0445 yaml \u0444\u0430\u0439\u043b\u043e\u0432. <strong>\u0412\u0430\u0436\u043d\u043e<\/strong> \u0447\u0442\u043e\u0431\u044b Template \u0431\u044b\u043b \u0441\u043e\u0437\u0434\u0430\u043d \u043f\u0435\u0440\u0432\u044b\u043c \u0442\u0430\u043a \u043a\u0430\u043a Constraint \u0446\u0435\u043f\u043b\u044f\u044e\u0442\u0441\u044f \u0438\u043c\u0435\u043d\u043d\u043e \u043a \u043d\u0435\u043c\u0443.<br \/>\u041f\u0440\u0438\u043c\u0435\u043d\u044f\u0435\u043c Template <code>kubectl apply -f ct.yaml<\/code> \u043f\u0440\u0438 \u0443\u0441\u043f\u0435\u0448\u043d\u043e\u043c \u043f\u0440\u0438\u043c\u0435\u043d\u0435\u043d\u0438\u0438 \u043f\u043e\u043b\u0443\u0447\u0438\u043c <code>constrainttemplate.templates.gatekeeper.sh\/k8sallowedrepos created<\/code>.<br \/>\u0422\u0435\u043f\u0435\u0440\u044c \u043f\u0440\u0438\u0441\u0442\u0443\u043f\u0438\u043c \u043a \u043f\u0440\u0438\u043c\u0435\u043d\u0435\u043d\u0438\u044e constraint <code>kubectl apply -f cons-front.yaml<\/code> \u0435\u0441\u043b\u0438 \u0432\u0441\u0435 \u0445\u043e\u0440\u043e\u0448\u043e \u043f\u043e\u043b\u0443\u0447\u0438\u043c <code>k8sallowedrepos.constraints.gatekeeper.sh\/allow-frontend-repos created<\/code>. \u0422\u043e\u0436\u0435 \u0441\u0430\u043c\u043e\u0435 \u043d\u0443\u0436\u043d\u043e \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0441 yaml \u0431\u0435\u043a\u0435\u043d\u0434\u0430.<\/p>\n<h2>\u041d\u0430\u0440\u0443\u0448\u0430\u0435\u043c \u043f\u043e\u043b\u0438\u0442\u0438\u043a\u0438<\/h2>\n<p>\u041f\u0440\u0438\u0441\u0442\u0443\u043f\u0438\u043c \u043a \u0441\u0430\u043c\u043e\u043c\u0443 \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u043e\u043c\u0443 &#8212; \u043d\u0430\u0440\u0443\u0448\u0438\u043c \u043f\u043e\u043b\u0438\u0442\u0438\u043a\u0438! \u0421\u043d\u0430\u0447\u0430\u043b\u0430 \u043f\u043e\u043f\u0440\u043e\u0431\u0443\u0435\u043c \u043f\u043e\u0434\u043d\u044f\u0442\u044c \u043d\u0430\u0448 \u043f\u043e\u0434 <code>kubectl apply -f backend-pod-bad.yaml<\/code><\/p>\n<pre><code class=\"yaml\">apiVersion: v1 kind: Pod metadata:   name: backend-bad   labels:     service: backend spec:   containers:   - name: app     image: nginx:latest  # \u0437\u0430\u043f\u0440\u0435\u0449\u0451\u043d\u043d\u044b\u0439 \u043e\u0431\u0440\u0430\u0437 \u0434\u043b\u044f backend<\/code><\/pre>\n<p>\u0418 OPA \u043d\u0435 \u0434\u0430\u0435\u0442 \u043d\u0430\u043c \u044d\u0442\u043e\u0433\u043e \u0441\u0434\u0435\u043b\u0430\u0442\u044c, \u0432\u0435\u0434\u044c \u043d\u0430\u0448 \u043e\u0431\u0440\u0430\u0437 \u043d\u0435 \u0438\u0437 \u0434\u043e\u0432\u0435\u0440\u0435\u043d\u043d\u043e\u0433\u043e \u0440\u0435\u043f\u043e\u0437\u0438\u0442\u043e\u0440\u0438\u044f! \u041f\u043e\u0441\u043b\u0435\u0434\u043d\u044f\u044f \u0447\u0430\u0441\u0442\u044c \u043e\u0442\u0432\u0435\u0442\u0430 \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0430 \u0438\u0437 \u043d\u0430\u0448\u0435\u0433\u043e Template.<br \/><strong>Error from server (Forbidden): error when creating &#171;backend-pod-bad.yaml&#187;: admission webhook &#171;<\/strong><a href=\"http:\/\/validation.gatekeeper.sh\" rel=\"noopener noreferrer nofollow\"><strong>validation.gatekeeper.sh<\/strong><\/a><strong>&#187; denied the request: [allow-backend-repos] container image &#8216;nginx:latest&#8217; is not from an allowed repository<\/strong><\/p>\n<p>\u041f\u043e\u0445\u043e\u0436\u0430\u044f \u0441\u0438\u0442\u0443\u0430\u0446\u0438\u044f \u0431\u0443\u0434\u0435\u0442, \u0435\u0441\u043b\u0438 \u043c\u044b \u043f\u043e\u043f\u0440\u043e\u0431\u0443\u0435\u043c \u0437\u0430\u043f\u0443\u0441\u0442\u0438\u0442\u044c &#171;\u043f\u043b\u043e\u0445\u043e\u0439&#187; \u043f\u043e\u0434 \u0434\u043b\u044f \u0441\u0435\u0440\u0432\u0438\u0441\u043e\u0432 \u0444\u0440\u043e\u043d\u0442\u0435\u043d\u0434\u0430 <code>kubectl apply -f frontend-pod-bad.yaml<\/code><\/p>\n<pre><code class=\"yaml\">apiVersion: v1 kind: Pod metadata:   name: frontend-bad   labels:     service: frontend spec:   containers:   - name: app     image: alpine:latest  # \u0437\u0430\u043f\u0440\u0435\u0449\u0451\u043d\u043d\u044b\u0439 \u043e\u0431\u0440\u0430\u0437 <\/code><\/pre>\n<p>\u041f\u043e\u043b\u0443\u0447\u0438\u043c \u043f\u0440\u0430\u043a\u0442\u0438\u0447\u0435\u0441\u043a\u0438 \u0438\u0434\u0435\u043d\u0442\u0438\u0447\u043d\u0443\u044e \u043e\u0448\u0438\u0431\u043a\u0443<br \/><strong>Error from server (Forbidden): error when creating &#171;frontend-pod-bad.yaml&#187;: admission webhook &#171;<\/strong><a href=\"http:\/\/validation.gatekeeper.sh\" rel=\"noopener noreferrer nofollow\"><strong>validation.gatekeeper.sh<\/strong><\/a><strong>&#187; denied the request: [allow-frontend-repos] container image &#8216;alpine:latest&#8217; is not from an allowed repository<\/strong><br \/>\u041e\u0431\u0440\u0430\u0442\u0438\u0442\u0435 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435, \u0447\u0442\u043e \u0431\u043b\u0430\u0433\u043e\u0434\u0430\u0440\u044f \u043b\u0435\u0439\u0431\u043b\u0430\u043c \u043f\u0440\u0438 \u043f\u043e\u043f\u044b\u0442\u043a\u0435 \u0441\u043e\u0437\u0434\u0430\u0442\u044c \u043f\u043e\u0434 \u0444\u0440\u043e\u043d\u0442\u0430 \u0438\u0434\u0435\u0442 \u043e\u0431\u0440\u0430\u0449\u0435\u043d\u0438\u0435 \u043a \u043a\u043e\u043d\u0441\u0442\u0440\u0435\u0439\u043d\u0442\u0443 \u0444\u0440\u043e\u043d\u0442\u0430 &#8212; <strong>allow-frontend-repos<\/strong>, \u0430 \u043f\u0440\u0438 \u0431\u0435\u043a\u0435 &#8212; \u043a \u043a\u043e\u043d\u0441\u0442\u0440\u0435\u0439\u043d\u0442\u0443 \u0431\u0435\u043a\u0430 <strong>allow-backend-repos<\/strong>.<br \/>\u041d\u043e \u0435\u0441\u043b\u0438 \u043c\u044b \u0437\u0430\u0445\u043e\u0442\u0438\u043c \u0441\u043e\u0437\u0434\u0430\u0442\u044c &#171;\u0445\u043e\u0440\u043e\u0448\u0438\u0439&#187; \u043f\u043e\u0434 &#8212; <code>kubectl apply -f backend-pod-good.yaml<\/code> \u043e\u043d \u0441\u043e\u0437\u0434\u0430\u0441\u0442\u0441\u044f \u0431\u0435\u0437 \u043d\u0430\u0440\u0443\u0448\u0435\u043d\u0438\u0439 <code>pod\/backend-good created<\/code> .<br \/>\u041f\u0440\u0438\u043c\u0435\u0440 &#171;\u0445\u043e\u0440\u043e\u0448\u0435\u0433\u043e&#187; \u043f\u043e\u0434\u0430:<\/p>\n<pre><code class=\"yaml\">apiVersion: v1 kind: Pod metadata:   name: backend-good   labels:     service: backend spec:   containers:   - name: app     image: python:3.12-slim  # \u0440\u0430\u0437\u0440\u0435\u0448\u0451\u043d\u043d\u044b\u0439, \u0442.\u043a. \u043d\u0430\u0447\u0438\u043d\u0430\u0435\u0442\u0441\u044f \u0441 \"python\"     command: [\"python\", \"-c\", \"print('Hello from Python Pod')\"]<\/code><\/pre>\n<h2>\u0418\u043d\u0442\u0435\u0433\u0440\u0430\u0446\u0438\u044f \u0441 GO<\/h2>\n<p>\u0421\u0434\u0435\u043b\u0430\u0435\u043c \u043f\u0440\u043e\u0441\u0442\u043e\u0439 \u0441\u043a\u0440\u0438\u043f\u0442, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0441\u043e\u0437\u0434\u0430\u0441\u0442 Template \u0438 Constraint \u043f\u043e\u043b\u0438\u0442\u0438\u043a\u0438. \u0421\u043a\u0440\u0438\u043f\u0442 \u0437\u0432\u0435\u0437\u0434 \u0441 \u043d\u0435\u0431\u0430 \u043d\u0435 \u0445\u0432\u0430\u0442\u0430\u0435\u0442, \u043d\u043e \u043e\u043d \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442. \u041f\u0440\u0438 \u0440\u0430\u0431\u043e\u0442\u0435 \u0441 \u043f\u043e\u043b\u0438\u0442\u0438\u043a\u0430\u043c\u0438 \u0435\u0441\u0442\u044c \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e \u043d\u044e\u0430\u043d\u0441\u043e\u0432, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440 \u0435\u0441\u043b\u0438 \u043f\u0440\u0438 \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u0438 <strong>Template<\/strong> \u0442\u0435\u0431\u0435 \u043d\u0430\u043f\u0438\u0441\u0430\u043b\u0438 &#171;<strong>created<\/strong>&#187; &#8212; \u043d\u0435 \u043e\u0431\u044f\u0437\u0430\u0442\u0435\u043b\u044c\u043d\u043e, \u0447\u0442\u043e \u043e\u043d \u043f\u043e\u043b\u043d\u043e\u0441\u0442\u044c\u044e \u0441\u043e\u0437\u0434\u0430\u043d \u0438 \u043d\u0443\u0436\u043d\u043e \u043f\u043e\u0434\u043e\u0436\u0434\u0430\u0442\u044c \u0442\u0430\u043a \u043a\u0430\u043a k8s \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 <strong>\u0430\u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u043e<\/strong>. \u041d\u043e \u043f\u043e\u0434\u043e\u0431\u043d\u044b\u0435 \u043c\u043e\u043c\u0435\u043d\u0442\u044b \u0431\u044b\u043b\u0438 \u043e\u043f\u0443\u0449\u0435\u043d\u044b \u0434\u043b\u044f \u0443\u043f\u0440\u043e\u0449\u0435\u043d\u0438\u044f \u043f\u043e\u043d\u0438\u043c\u0430\u043d\u0438\u044f.<\/p>\n<details class=\"spoiler\">\n<summary>\u0421\u043e\u0437\u0434\u0430\u0435\u043c Template \u0438 Constraint \u0431\u0435\u0437 \u0440\u0435\u0433\u0438\u0441\u0442\u0440\u0430\u0446\u0438\u0438 \u0438 \u0421\u041c\u0421<\/summary>\n<div class=\"spoiler__content\">\n<pre><code class=\"go\">package main  import ( \"context\" \"fmt\" \"time\"  \"k8s.io\/apimachinery\/pkg\/apis\/meta\/v1\/unstructured\"  \"sigs.k8s.io\/controller-runtime\/pkg\/client\" \"sigs.k8s.io\/controller-runtime\/pkg\/client\/config\" )  func main() { ctx := context.Background()  \/\/ \u041f\u043e\u0434\u043a\u043b\u044e\u0447\u0430\u0435\u043c\u0441\u044f \u043a Kubernetes \u0438\u0437 kubeconfig \u043f\u043e \u0443\u043c\u043e\u043b\u0447\u0430\u043d\u0438\u044e cfg, err := config.GetConfig() if err != nil { panic(err) }  cli, err := client.New(cfg, client.Options{}) if err != nil { panic(err) }  \/\/ \u0421\u043e\u0437\u0434\u0430\u0451\u043c ConstraintTemplate constraintTemplate := &amp;unstructured.Unstructured{ Object: map[string]interface{}{ \"apiVersion\": \"templates.gatekeeper.sh\/v1beta1\", \"kind\":       \"ConstraintTemplate\", \"metadata\": map[string]interface{}{ \"name\": \"k8sallowedrepos\", }, \"spec\": map[string]interface{}{ \"crd\": map[string]interface{}{ \"spec\": map[string]interface{}{ \"names\": map[string]interface{}{ \"kind\": \"K8sAllowedRepos\", }, \"validation\": map[string]interface{}{ \"openAPIV3Schema\": map[string]interface{}{ \"properties\": map[string]interface{}{ \"repos\": map[string]interface{}{ \"type\":  \"array\", \"items\": map[string]interface{}{\"type\": \"string\"}, }, }, }, }, }, }, \"targets\": []interface{}{ map[string]interface{}{ \"target\": \"admission.k8s.gatekeeper.sh\", \"rego\": ` package k8sallowedrepos  allowed_image(image) {   allowed_repo := input.parameters.repos[_]   startswith(image, allowed_repo) }  violation[{\"msg\": msg}] {   container := input.review.object.spec.containers[_]   not<\/code><\/pre>\n<\/div>\n<\/details>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-470250","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/470250","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=470250"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/470250\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=470250"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=470250"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=470250"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}