|
| 1 | +local com = import 'lib/commodore.libjsonnet'; |
| 2 | +local kap = import 'lib/kapitan.libjsonnet'; |
| 3 | +local kube = import 'lib/kube.libjsonnet'; |
| 4 | + |
| 5 | +local inv = kap.inventory(); |
| 6 | +local params = inv.parameters.openshift4_console; |
| 7 | + |
| 8 | +local namespace = { |
| 9 | + metadata+: { |
| 10 | + namespace: params.namespace, |
| 11 | + }, |
| 12 | +}; |
| 13 | + |
| 14 | +local makeConsoleNotification(name, args) = |
| 15 | + kube._Object('console.openshift.io/v1', 'ConsoleNotification', name) { |
| 16 | + metadata+: { |
| 17 | + labels+: { |
| 18 | + 'appuio.io/notification': 'true', |
| 19 | + }, |
| 20 | + }, |
| 21 | + spec: std.prune( |
| 22 | + { |
| 23 | + text: args.text, |
| 24 | + location: std.get(args, 'location', 'BannerTop'), |
| 25 | + color: std.get(args, 'color', '#fff'), |
| 26 | + backgroundColor: std.get(args, 'backgroundColor', '#2596be'), |
| 27 | + link: std.get(args, 'link'), |
| 28 | + }, |
| 29 | + ), |
| 30 | + }; |
| 31 | + |
| 32 | +local consoleNotifications = [ |
| 33 | + makeConsoleNotification(name, params.notifications[name]) |
| 34 | + for name in std.objectFields(params.notifications) |
| 35 | + if params.notifications[name] != null |
| 36 | +]; |
| 37 | + |
| 38 | +local nextChannelOverlay() = |
| 39 | + local overlays = inv.parameters.openshift_upgrade_controller.cluster_version.overlays; |
| 40 | + local channelOverlays = { |
| 41 | + [date]: overlays[date].spec.channel |
| 42 | + for date in std.objectFields(overlays) |
| 43 | + if std.objectHas(overlays[date].spec, 'channel') |
| 44 | + && std.split(overlays[date].spec.channel, '.')[1] > params.openshift_version.Minor |
| 45 | + }; |
| 46 | + local date = if std.length(channelOverlays) > 0 then |
| 47 | + std.sort(std.objectFields(channelOverlays))[0]; |
| 48 | + if date != null then |
| 49 | + { |
| 50 | + date: date, |
| 51 | + channel: channelOverlays[date], |
| 52 | + version: std.split(channelOverlays[date], '-')[1], |
| 53 | + }; |
| 54 | + |
| 55 | +local upgradeControllerNS = { |
| 56 | + metadata+: { |
| 57 | + namespace: inv.parameters.openshift_upgrade_controller.namespace, |
| 58 | + }, |
| 59 | +}; |
| 60 | + |
| 61 | +local notificationRBAC = |
| 62 | + local argocd_sa = kube.ServiceAccount('notification-manager') + namespace; |
| 63 | + local upgrade_sa = argocd_sa + upgradeControllerNS; |
| 64 | + local cluster_role = kube.ClusterRole('appuio:upgrade-notification-editor') { |
| 65 | + rules: [ |
| 66 | + { |
| 67 | + apiGroups: [ 'console.openshift.io' ], |
| 68 | + resources: [ 'consolenotifications' ], |
| 69 | + verbs: [ '*' ], |
| 70 | + }, |
| 71 | + { |
| 72 | + apiGroups: [ 'managedupgrade.appuio.io' ], |
| 73 | + resources: [ 'upgradeconfigs' ], |
| 74 | + verbs: [ 'get', 'list' ], |
| 75 | + }, |
| 76 | + // needed so that `oc version` can get the OCP server version |
| 77 | + { |
| 78 | + apiGroups: [ 'config.openshift.io' ], |
| 79 | + resources: [ 'clusterversions' ], |
| 80 | + verbs: [ 'get', 'list' ], |
| 81 | + }, |
| 82 | + { |
| 83 | + apiGroups: [ '' ], |
| 84 | + resources: [ 'configmaps' ], |
| 85 | + resourceNames: [ 'upgrade-notification-template' ], |
| 86 | + verbs: [ '*' ], |
| 87 | + }, |
| 88 | + ], |
| 89 | + }; |
| 90 | + local cluster_role_binding = |
| 91 | + kube.ClusterRoleBinding('appuio:upgrade-notification-manager') { |
| 92 | + subjects_: [ argocd_sa, upgrade_sa ], |
| 93 | + roleRef_: cluster_role, |
| 94 | + }; |
| 95 | + { |
| 96 | + argocd_sa: argocd_sa, |
| 97 | + upgrade_sa: upgrade_sa, |
| 98 | + cluster_role: cluster_role, |
| 99 | + cluster_role_binding: cluster_role_binding, |
| 100 | + }; |
| 101 | + |
| 102 | +local createUpgradeNotification(overlay) = |
| 103 | + [ |
| 104 | + kube.ConfigMap('upgrade-notification-template') + namespace { |
| 105 | + data: { |
| 106 | + 'upgrade.yaml': std.manifestYamlDoc( |
| 107 | + makeConsoleNotification('upgrade-%s' % overlay.version, params.upgrade_notification.notification) { |
| 108 | + metadata+: { |
| 109 | + labels+: { |
| 110 | + 'appuio.io/ocp-version': overlay.version, |
| 111 | + }, |
| 112 | + }, |
| 113 | + }, |
| 114 | + ), |
| 115 | + }, |
| 116 | + }, |
| 117 | + |
| 118 | + kube.ConfigMap('console-notification-script') { |
| 119 | + metadata+: { |
| 120 | + namespace: params.namespace, |
| 121 | + }, |
| 122 | + data: { |
| 123 | + 'create-console-notification.sh': (importstr 'scripts/create-console-notification.sh'), |
| 124 | + }, |
| 125 | + }, |
| 126 | + |
| 127 | + kube.Job('create-upgrade-notification') + namespace { |
| 128 | + metadata+: { |
| 129 | + annotations+: { |
| 130 | + 'argocd.argoproj.io/hook': 'PostSync', |
| 131 | + 'argocd.argoproj.io/hook-delete-policy': 'BeforeHookCreation', |
| 132 | + }, |
| 133 | + }, |
| 134 | + spec+: { |
| 135 | + template+: { |
| 136 | + spec+: { |
| 137 | + containers_+: { |
| 138 | + notification: kube.Container('notification') { |
| 139 | + image: '%(registry)s/%(repository)s:%(tag)s' % params.images.oc, |
| 140 | + name: 'create-console-notification', |
| 141 | + workingDir: '/export', |
| 142 | + command: [ '/scripts/create-console-notification.sh' ], |
| 143 | + env_+: { |
| 144 | + OVERLAY_DATE: overlay.date, |
| 145 | + OVERLAY_CHANNEL: overlay.channel, |
| 146 | + OVERLAY_VERSION: overlay.version, |
| 147 | + OVERLAY_VERSION_MINOR: std.split(overlay.version, '.')[1], |
| 148 | + }, |
| 149 | + volumeMounts_+: { |
| 150 | + 'upgrade-notification-template': { |
| 151 | + mountPath: 'export/template', |
| 152 | + readOnly: true, |
| 153 | + }, |
| 154 | + export: { |
| 155 | + mountPath: '/export', |
| 156 | + }, |
| 157 | + scripts: { |
| 158 | + mountPath: '/scripts', |
| 159 | + }, |
| 160 | + }, |
| 161 | + }, |
| 162 | + }, |
| 163 | + volumes_+: { |
| 164 | + 'upgrade-notification-template': { |
| 165 | + configMap: { |
| 166 | + name: 'upgrade-notification-template', |
| 167 | + defaultMode: std.parseOctal('0550'), |
| 168 | + }, |
| 169 | + }, |
| 170 | + export: { |
| 171 | + emptyDir: {}, |
| 172 | + }, |
| 173 | + scripts: { |
| 174 | + configMap: { |
| 175 | + name: 'console-notification-script', |
| 176 | + defaultMode: std.parseOctal('0550'), |
| 177 | + }, |
| 178 | + }, |
| 179 | + }, |
| 180 | + serviceAccountName: notificationRBAC.argocd_sa.metadata.name, |
| 181 | + }, |
| 182 | + }, |
| 183 | + }, |
| 184 | + }, |
| 185 | + ]; |
| 186 | + |
| 187 | + |
| 188 | +local hookScript = kube.ConfigMap('cleanup-upgrade-notification') + upgradeControllerNS { |
| 189 | + data: { |
| 190 | + 'cleanup-upgrade-notification.sh': (importstr 'scripts/cleanup-upgrade-notification.sh'), |
| 191 | + }, |
| 192 | +}; |
| 193 | + |
| 194 | +local ujh = kube._Object('managedupgrade.appuio.io/v1beta1', 'UpgradeJobHook', 'cleanup-upgrade-notification') + upgradeControllerNS { |
| 195 | + spec+: { |
| 196 | + selector: { |
| 197 | + matchLabels: { |
| 198 | + 'appuio-managed-upgrade': 'true', |
| 199 | + }, |
| 200 | + }, |
| 201 | + events: [ |
| 202 | + 'Finish', |
| 203 | + ], |
| 204 | + template+: { |
| 205 | + spec+: { |
| 206 | + template+: { |
| 207 | + spec+: { |
| 208 | + restartPolicy: 'Never', |
| 209 | + containers: [ |
| 210 | + kube.Container('cleanup') { |
| 211 | + image: '%(registry)s/%(repository)s:%(tag)s' % params.images.oc, |
| 212 | + command: [ '/usr/local/bin/cleanup' ], |
| 213 | + volumeMounts_+: { |
| 214 | + scripts: { |
| 215 | + mountPath: '/usr/local/bin/cleanup', |
| 216 | + readOnly: true, |
| 217 | + subPath: 'cleanup-upgrade-notification.sh', |
| 218 | + }, |
| 219 | + }, |
| 220 | + }, |
| 221 | + ], |
| 222 | + serviceAccountName: notificationRBAC.upgrade_sa.metadata.name, |
| 223 | + volumes: [ |
| 224 | + { |
| 225 | + name: 'scripts', |
| 226 | + configMap: { |
| 227 | + name: hookScript.metadata.name, |
| 228 | + defaultMode: std.parseOctal('0550'), |
| 229 | + }, |
| 230 | + }, |
| 231 | + ], |
| 232 | + }, |
| 233 | + }, |
| 234 | + }, |
| 235 | + }, |
| 236 | + }, |
| 237 | +}; |
| 238 | + |
| 239 | + |
| 240 | +local upgradeNotification = if params.upgrade_notification.enabled then |
| 241 | + local channelOverlay = nextChannelOverlay(); |
| 242 | + local notification = if channelOverlay != null then |
| 243 | + createUpgradeNotification(channelOverlay) |
| 244 | + else []; |
| 245 | + notification + [ |
| 246 | + hookScript, |
| 247 | + ujh, |
| 248 | + ] else []; |
| 249 | + |
| 250 | +{ |
| 251 | + rbac: if params.upgrade_notification.enabled then |
| 252 | + std.objectValues(notificationRBAC) else [], |
| 253 | + notifications: consoleNotifications, |
| 254 | + upgrade_notification: upgradeNotification, |
| 255 | +} |
0 commit comments