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

ControllerRegistration support / List Networking Types #881

Merged
merged 18 commits into from
Dec 16, 2020
Merged
Show file tree
Hide file tree
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
69 changes: 69 additions & 0 deletions backend/__fixtures__/controllerregistrations.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
//
// SPDX-FileCopyrightText: 2020 SAP SE or an SAP affiliate company and Gardener contributors
//
// SPDX-License-Identifier: Apache-2.0
//

'use strict'

const { cloneDeep, find } = require('lodash')

function getControllerRegistration ({ uid, name, version, resources }) {
const metadata = {
name,
uid
}
const spec = {}
if (resources) {
spec.resources = resources
}
if (version) {
spec.deployment = {
providerConfig: {
values: {
image: {
tag: version
}
}
}
}
}
return { metadata, spec }
}

const controllerRegistrationList = [
getControllerRegistration({
uid: 1,
name: 'foo',
version: 'v1.0.0',
resources: [{
kind: 'OperatingSystemConfig',
type: 'gardenlinux'
}]
}),
getControllerRegistration({
uid: 2,
name: 'bar',
resources: [{
kind: 'Network',
type: 'gardium'
}]
})
]

const controllerregistrations = {
create (options) {
return getControllerRegistration(options)
},
get (name) {
const items = controllerregistrations.list()
return find(items, ['metadata.name', name])
},
list () {
return cloneDeep(controllerRegistrationList)
}
}

module.exports = {
...controllerregistrations
}
2 changes: 2 additions & 0 deletions backend/__fixtures__/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const seeds = require('./seeds')
const secrets = require('./secrets')
const secretbindings = require('./secretbindings')
const quotas = require('./quotas')
const controllerregistrations = require('./controllerregistrations')
const projects = require('./projects')
const serviceaccounts = require('./serviceaccounts')
const cloudprofiles = require('./cloudprofiles')
Expand Down Expand Up @@ -44,6 +45,7 @@ const fixtures = {
serviceaccounts,
cloudprofiles,
quotas,
controllerregistrations,
nodes,
terminals,
github,
Expand Down
1 change: 1 addition & 0 deletions backend/jest.setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ jest.mock('./lib/cache', () => {
cache.seeds.replace(fixtures.seeds.list())
cache.quotas.replace(fixtures.quotas.list())
cache.projects.replace(fixtures.projects.list())
cache.controllerregistrations.replace(fixtures.controllerregistrations.list())
cache.ticketCache = createTicketCache()
cache.resetTicketCache = () => (cache.ticketCache = createTicketCache())
return originalCache
Expand Down
10 changes: 9 additions & 1 deletion backend/lib/cache/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ class Cache {
this.seeds = new Store()
this.quotas = new Store()
this.projects = new Store()
this.controllerregistrations = new Store()
this.ticketCache = createTicketCache()
}

Expand All @@ -43,7 +44,7 @@ class Cache {
*/
synchronize (client) {
if (!this.synchronizationPromise) {
const keys = ['cloudprofiles', 'quotas', 'seeds', 'projects']
const keys = ['cloudprofiles', 'quotas', 'seeds', 'projects', 'controllerregistrations']
const iteratee = key => initializeStoreSynchronization(this[key], client['core.gardener.cloud'][key])
this.synchronizationPromise = Promise.all(keys.map(iteratee))
}
Expand Down Expand Up @@ -73,6 +74,10 @@ class Cache {
return this.list('projects')
}

getControllerRegistrations () {
return this.list('controllerregistrations')
}

getTicketCache () {
return this.ticketCache
}
Expand Down Expand Up @@ -113,6 +118,9 @@ module.exports = {
getProjects () {
return cache.getProjects()
},
getControllerRegistrations () {
return cache.getControllerRegistrations()
},
findProjectByNamespace (namespace) {
const project = cache.projects.find(['spec.namespace', namespace])
if (!project) {
Expand Down
22 changes: 22 additions & 0 deletions backend/lib/routes/gardenerExtensions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//
// SPDX-FileCopyrightText: 2020 SAP SE or an SAP affiliate company and Gardener contributors
//
// SPDX-License-Identifier: Apache-2.0
//

'use strict'

const express = require('express')
const { controllerregistrations } = require('../services')

const router = module.exports = express.Router()

router.route('/')
.get(async (req, res, next) => {
try {
const user = req.user
res.send(await controllerregistrations.listExtensions({ user }))
} catch (err) {
next(err)
}
})
2 changes: 2 additions & 0 deletions backend/lib/routes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ module.exports = {
'/cloudprofiles': require('./cloudprofiles'),
'/seeds': require('./seeds'),
'/shoots': require('./shoots'),
'/gardener-extensions': require('./gardenerExtensions'),
'/networking-types': require('./networkingTypes'),
'/namespaces': require('./namespaces'),
'/namespaces/:namespace/shoots': require('./shoots'),
'/namespaces/:namespace/infrastructure-secrets': require('./infrastructureSecrets'),
Expand Down
5 changes: 0 additions & 5 deletions backend/lib/routes/info.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ const express = require('express')
const { extend } = require('@gardener-dashboard/request')
const logger = require('../logger')
const { decodeBase64 } = require('../utils')
const { isHttpError } = require('@gardener-dashboard/request')
const { dashboardClient } = require('@gardener-dashboard/kube-client')
const { version } = require('../../package')

Expand Down Expand Up @@ -50,9 +49,5 @@ async function fetchGardenerVersion () {
return version
} catch (err) {
logger.warn(`Could not fetch gardener version. Error: ${err.message}`)
if (isHttpError(err, 404)) {
return undefined
}
throw err
}
}
21 changes: 21 additions & 0 deletions backend/lib/routes/networkingTypes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//
// SPDX-FileCopyrightText: 2020 SAP SE or an SAP affiliate company and Gardener contributors
//
// SPDX-License-Identifier: Apache-2.0
//

'use strict'

const express = require('express')
const { controllerregistrations } = require('../services')

const router = module.exports = express.Router()

router.route('/')
.get(async (req, res, next) => {
try {
res.send(await controllerregistrations.listNetworkingTypes())
} catch (err) {
next(err)
}
})
15 changes: 12 additions & 3 deletions backend/lib/services/authorization.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +72,12 @@ exports.canListSeeds = function (user) {
})
}

exports.canListCloudProfiles = function (user, name) {
exports.canListCloudProfiles = function (user) {
return hasAuthorization(user, {
resourceAttributes: {
verb: 'list',
group: 'core.gardener.cloud',
resource: 'cloudprofiles',
name
resource: 'cloudprofiles'
}
})
}
Expand All @@ -94,6 +93,16 @@ exports.canGetCloudProfiles = function (user, name) {
})
}

exports.canListControllerRegistrations = function (user) {
return hasAuthorization(user, {
resourceAttributes: {
verb: 'list',
group: 'core.gardener.cloud',
resource: 'controllerregistrations'
}
})
}

/*
SelfSubjectRulesReview should only be used to hide/show actions or views on the UI and not for authorization checks.
*/
Expand Down
37 changes: 37 additions & 0 deletions backend/lib/services/controllerregistrations.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
//
// SPDX-FileCopyrightText: 2020 SAP SE or an SAP affiliate company and Gardener contributors
//
// SPDX-License-Identifier: Apache-2.0
//

'use strict'

const { Forbidden } = require('http-errors')
const { getControllerRegistrations } = require('../cache')
const authorization = require('./authorization')
const _ = require('lodash')

exports.listExtensions = async function ({ user }) {
const allowed = await authorization.canListControllerRegistrations(user)
if (!allowed) {
throw new Forbidden('You are not allowed to list controllerregistrations')
}

const controllerregistrations = getControllerRegistrations()
return _.map(controllerregistrations, ({ metadata, spec }) => {
return {
name: metadata.name,
version: _.get(spec, 'deployment.providerConfig.values.image.tag')
}
})
}

exports.listNetworkingTypes = async function () {
const controllerregistrations = getControllerRegistrations()
petersutter marked this conversation as resolved.
Show resolved Hide resolved
grolu marked this conversation as resolved.
Show resolved Hide resolved
return _
.chain(controllerregistrations)
.flatMap('spec.resources')
.filter(['kind', 'Network'])
.map('type')
.value()
}
3 changes: 2 additions & 1 deletion backend/lib/services/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,6 @@ module.exports = {
authorization: require('./authorization'),
authentication: require('./authentication'),
tickets: require('./tickets'),
terminals: require('./terminals')
terminals: require('./terminals'),
controllerregistrations: require('./controllerregistrations')
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ Array [
"nonResourceAttributes": undefined,
"resourceAttributes": Object {
"group": "core.gardener.cloud",
"name": undefined,
"resource": "cloudprofiles",
"verb": "list",
},
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`api controllerregistrations should return all gardener extensions 1`] = `
Array [
Array [
Object {
":authority": "kubernetes:6443",
":method": "post",
":path": "/apis/authorization.k8s.io/v1/selfsubjectaccessreviews",
":scheme": "https",
"authorization": "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6ImpvaG4uZG9lQGV4YW1wbGUub3JnIiwiaWF0IjoxNTc3ODM2ODAwLCJhdWQiOlsiZ2FyZGVuZXIiXSwiZXhwIjozMTU1NzE2ODAwLCJqdGkiOiJqdGkifQ.LkQ9PEN893UNTsZZn2Ux_CAYNOoQ2ISboWuHiAc5HHU",
},
Object {
"apiVersion": "authorization.k8s.io/v1",
"kind": "SelfSubjectAccessReview",
"spec": Object {
"nonResourceAttributes": undefined,
"resourceAttributes": Object {
"group": "core.gardener.cloud",
"resource": "controllerregistrations",
"verb": "list",
},
},
},
],
]
`;

exports[`api controllerregistrations should return all gardener extensions 2`] = `
Array [
Object {
"name": "foo",
"version": "v1.0.0",
},
Object {
"name": "bar",
},
]
`;

exports[`api controllerregistrations should return all networking types 1`] = `
Array [
"gardium",
]
`;
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,6 @@ Array [
"nonResourceAttributes": undefined,
"resourceAttributes": Object {
"group": "core.gardener.cloud",
"name": undefined,
"resource": "cloudprofiles",
"verb": "list",
},
Expand Down Expand Up @@ -412,7 +411,6 @@ Array [
"nonResourceAttributes": undefined,
"resourceAttributes": Object {
"group": "core.gardener.cloud",
"name": undefined,
"resource": "cloudprofiles",
"verb": "list",
},
Expand Down
Loading