Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create OCP project template and add organization label to projects #16

Merged
merged 3 commits into from
Sep 10, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions class/appuio-cloud.yml
Original file line number Diff line number Diff line change
@@ -9,5 +9,6 @@ parameters:
- appuio-cloud/component/main.jsonnet
- appuio-cloud/component/namespace-policies.jsonnet
- appuio-cloud/component/generated-rolebindings.jsonnet
- appuio-cloud/component/project-template.jsonnet
input_type: jsonnet
output_path: appuio-cloud/
100 changes: 65 additions & 35 deletions component/namespace-policies.jsonnet
Original file line number Diff line number Diff line change
@@ -43,6 +43,59 @@ local appuioNsProvisionersRoleBinding = kube.ClusterRoleBinding('appuio-ns-provi
],
};

local matchAllNamespaces = {
all: {
resources: {
kinds: [
'Namespace',
],
},
},
};

local matchProjectRequestProjects = {
all: [ {
resources: {
annotations: {
'openshift.io/requester': '?*',
},
kinds: [
'Project',
],
},
} ],
};

local setDefaultOrgPolicy(name, match, exclude, username) = {
name: name,
match: match,
exclude: exclude,
context: [
{
name: 'ocpuser',
apiCall: {
urlPath: '/apis/user.openshift.io/v1/users/%s' % username,
// We want the full output of the API call. Despite the docs not
// saying anything, if we omit jmesPath here, we don't get the
// variable ocpuser in the resulting context at all. Instead, we
// provide '@' for jmesPath which responds to the current
// element, giving us the full response as ocpuser.
jmesPath: '@',
},
},
],
mutate: {
patchStrategicMerge: {
metadata: {
labels: {
'+(appuio.io/organization)':
'{{ocpuser.metadata.annotations."appuio.io/default-organization"}}',
},
},
},
},
};

/**
* Organization Namespaces
* This policy will:
@@ -58,41 +111,18 @@ local organizationNamespaces = kyverno.ClusterPolicy('organization-namespaces')
validationFailureAction: 'enforce',
background: false,
rules: [
{
name: 'set-default-organization',
match: {
resources: {
kinds: [
'Namespace',
],
},
},
context: [
{
name: 'ocpuser',
apiCall: {
urlPath: '/apis/user.openshift.io/v1/users/{{request.userInfo.username}}',
// We want the full output of the API call. Despite the docs not
// saying anything, if we omit jmesPath here, we don't get the
// variable ocpuser in the resulting context at all. Instead, we
// provide '@' for jmesPath which responds to the current
// element, giving us the full response as ocpuser.
jmesPath: '@',
},
},
],
exclude: common.BypassNamespaceRestrictionsSubjects(),
mutate: {
patchStrategicMerge: {
metadata: {
labels: {
'+(appuio.io/organization)':
'{{ocpuser.metadata.annotations."appuio.io/default-organization"}}',
},
},
},
},
},
setDefaultOrgPolicy(
'set-default-organization-ns',
matchAllNamespaces,
common.BypassNamespaceRestrictionsSubjects(),
'{{request.userInfo.username}}'
),
setDefaultOrgPolicy(
'set-default-organization-project',
matchProjectRequestProjects,
{},
'{{request.object.metadata.annotations."openshift.io/requester"}}',
),
{
name: 'has-organization',
match: {
44 changes: 44 additions & 0 deletions component/project-template.jsonnet
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
local kube = import 'lib/kube.libjsonnet';
local projectTemplate =
kube._Object('template.openshift.io/v1', 'Template', 'project-request') {
metadata+: {
namespace: 'openshift-config',
},
objects: [
{
apiVersion: 'project.openshift.io/v1',
kind: 'Project',
metadata: {
annotations: {
'openshift.io/description': '${PROJECT_DESCRIPTION}',
'openshift.io/display-name': '${PROJECT_DISPLAYNAME}',
'openshift.io/requester': '${PROJECT_REQUESTING_USER}',
},
name: '${PROJECT_NAME}',
},
},
],
parameters: [
{ name: 'PROJECT_NAME' },
{ name: 'PROJECT_DISPLAYNAME' },
{ name: 'PROJECT_DESCRIPTION' },
{ name: 'PROJECT_ADMIN_USER' },
{ name: 'PROJECT_REQUESTING_USER' },
],
};

local ocpProjectConfig =
kube._Object('config.openshift.io/v1', 'Project', 'cluster') {
spec: {
projectRequestTemplate: {
name: projectTemplate.metadata.name,
},
},
};

{
'20_project_template': [
ocpProjectConfig,
projectTemplate,
],
}
Original file line number Diff line number Diff line change
@@ -40,15 +40,35 @@ spec:
name: argocd-application-controller
namespace: argocd
match:
resources:
kinds:
- Namespace
all:
resources:
kinds:
- Namespace
mutate:
patchStrategicMerge:
metadata:
labels:
+(appuio.io/organization): '{{ocpuser.metadata.annotations."appuio.io/default-organization"}}'
name: set-default-organization-ns
- context:
- apiCall:
jmesPath: '@'
urlPath: /apis/user.openshift.io/v1/users/{{request.object.metadata.annotations."openshift.io/requester"}}
name: ocpuser
exclude: {}
match:
all:
- resources:
annotations:
openshift.io/requester: ?*
kinds:
- Project
mutate:
patchStrategicMerge:
metadata:
labels:
+(appuio.io/organization): '{{ocpuser.metadata.annotations."appuio.io/default-organization"}}'
name: set-default-organization
name: set-default-organization-project
- exclude:
clusterRoles:
- cluster-admin
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
apiVersion: config.openshift.io/v1
kind: Project
metadata:
annotations: {}
labels:
name: cluster
name: cluster
spec:
projectRequestTemplate:
name: project-request
---
apiVersion: template.openshift.io/v1
kind: Template
metadata:
annotations: {}
labels:
name: project-request
name: project-request
namespace: openshift-config
objects:
- apiVersion: project.openshift.io/v1
kind: Project
metadata:
annotations:
openshift.io/description: ${PROJECT_DESCRIPTION}
openshift.io/display-name: ${PROJECT_DISPLAYNAME}
openshift.io/requester: ${PROJECT_REQUESTING_USER}
name: ${PROJECT_NAME}
parameters:
- name: PROJECT_NAME
- name: PROJECT_DISPLAYNAME
- name: PROJECT_DESCRIPTION
- name: PROJECT_ADMIN_USER
- name: PROJECT_REQUESTING_USER