Skip to content

Commit

Permalink
Improve syncronizeFactory API
Browse files Browse the repository at this point in the history
  • Loading branch information
holgerkoser committed Dec 9, 2024
1 parent f66b68c commit 3aa8c2d
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 33 deletions.
19 changes: 10 additions & 9 deletions backend/lib/io/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const cache = require('../cache')
const logger = require('../logger')
const authorization = require('../services/authorization')
const { authenticate } = require('../security')
const { trimObjectMetadata } = require('../utils')
const { simplifyObjectMetadata } = require('../utils')

const { isHttpError } = createError

Expand Down Expand Up @@ -143,15 +143,16 @@ function uidNotFoundFactory (group, kind) {
}

const constants = Object.freeze({
OBJECT_FORBIDDEN: 0,
OBJECT_DEFAULT: 1,
OBJECT_UNMODIFIED: 2,
OBJECT_NONE: 0,
OBJECT_SIMPLE: 1,
OBJECT_ORIGINAL: 2,
})

function synchronizeFactory (kind, options = {}) {
const {
group = 'core.gardener.cloud',
predicate = () => constants.OBJECT_DEFAULT,
accessResolver = () => constants.OBJECT_SIMPLE,
simplifyObject = simplifyObjectMetadata,
} = options
const uidNotFound = uidNotFoundFactory(group, kind)

Expand All @@ -162,13 +163,13 @@ function synchronizeFactory (kind, options = {}) {
// the project has been removed from the cache
return uidNotFound(uid)
}
switch (predicate(socket, object)) {
case constants.OBJECT_DEFAULT: {
switch (accessResolver(socket, object)) {
case constants.OBJECT_SIMPLE: {
const clonedObject = cloneDeep(object)
trimObjectMetadata(clonedObject)
simplifyObject(clonedObject)
return clonedObject
}
case constants.OBJECT_UNMODIFIED: {
case constants.OBJECT_ORIGINAL: {
return object
}
default: {
Expand Down
12 changes: 8 additions & 4 deletions backend/lib/io/projects.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@
'use strict'

const { get } = require('lodash')
const { isMemberOf } = require('../utils')
const {
isMemberOf,
simplifyProject,
} = require('../utils')
const {
constants,
getUserFromSocket,
Expand All @@ -17,11 +20,12 @@ const {
module.exports = {
synchronize: synchronizeFactory('Project', {
group: 'core.gardener.cloud',
predicate (socket, object) {
accessResolver (socket, object) {
const user = getUserFromSocket(socket)
return get(user, ['profiles', 'canListProjects'], false) || isMemberOf(object, user)
? constants.OBJECT_DEFAULT
: constants.OBJECT_FORBIDDEN
? constants.OBJECT_SIMPLE
: constants.OBJECT_NONE
},
simplifyObject: simplifyProject,
}),
}
9 changes: 4 additions & 5 deletions backend/lib/io/shoots.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

'use strict'

const _ = require('lodash')
const createError = require('http-errors')
const cache = require('../cache')
const logger = require('../logger')
Expand Down Expand Up @@ -80,7 +79,7 @@ function unsubscribe (socket) {
}

const synchronize = synchronizeFactory('Shoot', {
predicate (socket, object) {
accessResolver (socket, object) {
const rooms = Array.from(socket.rooms).filter(room => room !== socket.id)
const [
isAdmin,
Expand All @@ -91,11 +90,11 @@ const synchronize = synchronizeFactory('Shoot', {
const { namespace, name } = object.metadata
const qualifiedName = [namespace, name].join('/')
if (qualifiedNames.includes(qualifiedName)) {
return constants.OBJECT_UNMODIFIED
return constants.OBJECT_ORIGINAL
} else if (isAdmin || namespaces.includes(namespace)) {
return constants.OBJECT_DEFAULT
return constants.OBJECT_SIMPLE
}
return constants.OBJECT_FORBIDDEN
return constants.OBJECT_NONE
},
})

Expand Down
4 changes: 2 additions & 2 deletions backend/lib/routes/shoots.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
const express = require('express')
const { shoots } = require('../services')
const { metricsRoute } = require('../middleware')
const { trimObjectMetadata } = require('../utils')
const { simplifyObjectMetadata } = require('../utils')

const router = module.exports = express.Router({
mergeParams: true,
Expand All @@ -26,7 +26,7 @@ router.route('/')
const labelSelector = req.query.labelSelector
const shootList = await shoots.list({ user, namespace, labelSelector })
for (const object of shootList.items) {
trimObjectMetadata(object)
simplifyObjectMetadata(object)
}
res.send(shootList)
} catch (err) {
Expand Down
4 changes: 2 additions & 2 deletions backend/lib/services/projects.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const { dashboardClient } = require('@gardener-dashboard/kube-client')
const { PreconditionFailed, InternalServerError } = require('http-errors')
const shoots = require('./shoots')
const authorization = require('./authorization')
const { projectFilter, trimProject } = require('../utils')
const { projectFilter, simplifyProject } = require('../utils')
const cache = require('../cache')
const PROJECT_INITIALIZATION_TIMEOUT = 30 * 1000

Expand All @@ -32,7 +32,7 @@ exports.list = async function ({ user }) {
.chain(cache.getProjects())
.filter(projectFilter(user, canListProjects))
.map(_.cloneDeep)
.map(trimProject)
.map(simplifyProject)
.value()
}

Expand Down
10 changes: 5 additions & 5 deletions backend/lib/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,16 +99,16 @@ function parseRooms (rooms) {
]
}

function trimObjectMetadata (object) {
function simplifyObjectMetadata (object) {
object.metadata.managedFields = undefined
if (object.metadata.annotations) {
object.metadata.annotations['kubectl.kubernetes.io/last-applied-configuration'] = undefined
}
return object
}

function trimProject (project) {
project = trimObjectMetadata(project)
function simplifyProject (project) {
project = simplifyObjectMetadata(project)
_.set(project, ['spec', 'members'], undefined)
return project
}
Expand Down Expand Up @@ -247,8 +247,8 @@ module.exports = {
isMemberOf,
projectFilter,
parseRooms,
trimObjectMetadata,
trimProject,
simplifyObjectMetadata,
simplifyProject,
parseSelectors,
filterBySelectors,
getConfigValue,
Expand Down
12 changes: 6 additions & 6 deletions backend/test/utils.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ const {
parseSelectors,
filterBySelectors,
constants,
trimObjectMetadata,
trimProject,
simplifyObjectMetadata,
simplifyProject,
parseRooms,
} = require('../lib/utils')

Expand Down Expand Up @@ -79,14 +79,14 @@ describe('utils', function () {
foo: 'bar',
},
}
expect(trimObjectMetadata({ metadata })).toEqual({ metadata })
expect(simplifyObjectMetadata({ metadata })).toEqual({ metadata })
const extendedMetadata = merge(metadata, {
managedFields,
annotations: {
'kubectl.kubernetes.io/last-applied-configuration': lastAppliedConfiguration,
},
})
expect(trimObjectMetadata({ metadata: extendedMetadata })).toEqual({ metadata })
expect(simplifyObjectMetadata({ metadata: extendedMetadata })).toEqual({ metadata })
})

it('should trim project metadata and remove spec.members', () => {
Expand All @@ -105,7 +105,7 @@ describe('utils', function () {
const project = { metadata, spec }

// Test case where metadata does not have managedFields or last-applied-configuration
expect(trimProject(cloneDeep(project))).toEqual({
expect(simplifyProject(cloneDeep(project))).toEqual({
metadata,
spec: {},
})
Expand All @@ -119,7 +119,7 @@ describe('utils', function () {
})
const extendedProject = { metadata: extendedMetadata, spec }

expect(trimProject(cloneDeep(extendedProject))).toEqual({
expect(simplifyProject(cloneDeep(extendedProject))).toEqual({
metadata,
spec: {},
})
Expand Down

0 comments on commit 3aa8c2d

Please sign in to comment.