-
Notifications
You must be signed in to change notification settings - Fork 228
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
156 additions
and
0 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,6 @@ | ||
apiVersion: kpt.dev/v1alpha1 | ||
kind: Kptfile | ||
metadata: | ||
name: function-export-blueprint | ||
packageMetadata: | ||
shortDescription: example blueprint for kpt fn export |
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,9 @@ | ||
# Function Export Example | ||
|
||
The blueprint is part of Kpt Function Export Guide: | ||
|
||
- `resources/resources.yaml`: declares a `Deployment` and a `Namespace`. | ||
- `resources/constraints/`: declares constraints used by the `gatekeeper-validate` function. | ||
- `functions.yaml`: runs two functions from [Kpt Functions Catalog](../../catalog) declaratively: | ||
- `label-namespace` adds a label to all Namespaces. | ||
- `gatekeeper-validate` enforces constraints over all resources. |
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,18 @@ | ||
apiVersion: v1 | ||
kind: ConfigMap | ||
metadata: | ||
annotations: | ||
config.k8s.io/function: | | ||
container: | ||
image: gcr.io/kpt-functions/label-namespace | ||
data: | ||
label_name: color | ||
label_value: blue | ||
--- | ||
apiVersion: v1 | ||
kind: ConfigMap | ||
metadata: | ||
annotations: | ||
config.k8s.io/function: | | ||
container: | ||
image: gcr.io/kpt-functions/gatekeeper-validate |
28 changes: 28 additions & 0 deletions
28
...-examples/function-export-blueprint/resources/constraints/deployment-must-have-owner.yaml
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,28 @@ | ||
# Copyright 2020 Google LLC | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
# [START constraint] | ||
apiVersion: constraints.gatekeeper.sh/v1beta1 | ||
kind: K8sRequiredLabels | ||
metadata: | ||
name: deployment-must-have-owner | ||
spec: | ||
match: | ||
kinds: | ||
- apiGroups: ["apps"] | ||
kinds: ["Deployment"] | ||
parameters: | ||
labels: | ||
- key: "owner" | ||
message: "Deployment objects should have an 'owner' label indicating who created them." | ||
# [END constraint] |
66 changes: 66 additions & 0 deletions
66
package-examples/function-export-blueprint/resources/constraints/requiredlabels.yaml
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,66 @@ | ||
# Copyright 2020 Google LLC | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
apiVersion: templates.gatekeeper.sh/v1beta1 | ||
kind: ConstraintTemplate | ||
metadata: | ||
name: k8srequiredlabels | ||
spec: | ||
crd: | ||
spec: | ||
names: | ||
kind: K8sRequiredLabels | ||
validation: | ||
# Schema for the `parameters` field | ||
openAPIV3Schema: | ||
properties: | ||
message: | ||
type: string | ||
labels: | ||
type: array | ||
items: | ||
type: object | ||
properties: | ||
key: | ||
type: string | ||
allowedRegex: | ||
type: string | ||
targets: | ||
- target: admission.k8s.gatekeeper.sh | ||
rego: | | ||
package k8srequiredlabels | ||
get_message(parameters, _default) = msg { | ||
not parameters.message | ||
msg := _default | ||
} | ||
get_message(parameters, _default) = msg { | ||
msg := parameters.message | ||
} | ||
violation[{"msg": msg, "details": {"missing_labels": missing}}] { | ||
provided := {label | input.review.object.metadata.labels[label]} | ||
required := {label | label := input.parameters.labels[_].key} | ||
missing := required - provided | ||
count(missing) > 0 | ||
def_msg := sprintf("you must provide labels: %v", [missing]) | ||
msg := get_message(input.parameters, def_msg) | ||
} | ||
violation[{"msg": msg}] { | ||
value := input.review.object.metadata.labels[key] | ||
expected := input.parameters.labels[_] | ||
expected.key == key | ||
# do not match if allowedRegex is not defined, or is an empty string | ||
expected.allowedRegex != "" | ||
not re_match(expected.allowedRegex, value) | ||
def_msg := sprintf("Label <%v: %v> does not satisfy allowed regex: %v", [key, value, expected.allowedRegex]) | ||
msg := get_message(input.parameters, def_msg) | ||
} |
29 changes: 29 additions & 0 deletions
29
package-examples/function-export-blueprint/resources/resources.yaml
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,29 @@ | ||
apiVersion: v1 | ||
kind: Namespace | ||
metadata: | ||
name: development | ||
--- | ||
apiVersion: apps/v1 | ||
kind: Deployment | ||
metadata: | ||
name: nginx-deployment | ||
namespace: development | ||
labels: | ||
app: nginx | ||
# You can remove this label and see what will happen to your workflow result. | ||
owner: alice | ||
spec: | ||
replicas: 3 | ||
selector: | ||
matchLabels: | ||
app: nginx | ||
template: | ||
metadata: | ||
labels: | ||
app: nginx | ||
spec: | ||
containers: | ||
- name: nginx | ||
image: nginx:1.14.2 | ||
ports: | ||
- containerPort: 80 |