Skip to content

Commit

Permalink
Add fn export examples
Browse files Browse the repository at this point in the history
  • Loading branch information
ZhuGongpu committed Jul 24, 2020
1 parent 4e80de9 commit 0cf709c
Show file tree
Hide file tree
Showing 6 changed files with 156 additions and 0 deletions.
6 changes: 6 additions & 0 deletions package-examples/function-export-blueprint/Kptfile
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
9 changes: 9 additions & 0 deletions package-examples/function-export-blueprint/README.md
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.
18 changes: 18 additions & 0 deletions package-examples/function-export-blueprint/functions.yaml
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
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]
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)
}
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

0 comments on commit 0cf709c

Please sign in to comment.