|
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | +import os.path |
| 4 | + |
| 5 | + |
| 6 | +def generate_lib_resources(directory, types, clients, modifiers, health_checks): |
| 7 | + generate_resourceread(directory=os.path.join(directory, 'resourceread'), types=types) |
| 8 | + generate_resourcebuilder(directory=os.path.join(directory, 'resourcebuilder'), types=types, clients=clients, modifiers=modifiers, health_checks=health_checks) |
| 9 | + |
| 10 | + |
| 11 | +def generate_resourceread(directory, types): |
| 12 | + package_name = os.path.basename(directory) |
| 13 | + path = os.path.join(directory, package_name + '.go') |
| 14 | + lines = [ |
| 15 | + '// auto-generated with generate-lib-resources.py', |
| 16 | + '', |
| 17 | + '// Package {} reads supported objects from bytes.'.format(package_name), |
| 18 | + 'package {}'.format(package_name), |
| 19 | + '', |
| 20 | + 'import (', |
| 21 | + ] |
| 22 | + |
| 23 | + imports = {} |
| 24 | + for import_name in [ |
| 25 | + 'k8s.io/apimachinery/pkg/runtime', |
| 26 | + 'k8s.io/apimachinery/pkg/runtime/serializer', |
| 27 | + ]: |
| 28 | + imports[import_name] = '\t"{}"'.format(import_name) |
| 29 | + |
| 30 | + for package in sorted(types.keys()): |
| 31 | + base, version = os.path.split(package) # FIXME: should be using network path operations on package names |
| 32 | + short_name = os.path.basename(base) |
| 33 | + imports[package] = '\t{}{} "{}"'.format(short_name, version, package) |
| 34 | + |
| 35 | + lines.extend([import_line for _, import_line in sorted(imports.items(), key=lambda package_line: package_line[0])]) |
| 36 | + lines.extend([ |
| 37 | + ')', |
| 38 | + '', |
| 39 | + 'var (', |
| 40 | + '\tscheme = runtime.NewScheme()', |
| 41 | + '\tcodecs = serializer.NewCodecFactory(scheme)', |
| 42 | + '\tdecoder runtime.Decoder', |
| 43 | + ')', |
| 44 | + '', |
| 45 | + 'func init() {', |
| 46 | + ]) |
| 47 | + |
| 48 | + sgvs = scheme_group_versions(types=types) |
| 49 | + for _, data in sorted(sgvs.items()): |
| 50 | + lines.extend([ |
| 51 | + '\tif err := {0}{1}.AddToScheme(scheme); err != nil {{'.format(data['short_name'], data['version']), |
| 52 | + '\t\tpanic(err)', |
| 53 | + '\t}', |
| 54 | + ]) |
| 55 | + |
| 56 | + lines.append('\tdecoder = codecs.UniversalDecoder(') |
| 57 | + lines.extend(['\t\t{},'.format(sgv) for sgv in sorted(sgvs.keys())]) |
| 58 | + lines.extend([ |
| 59 | + '\t)', |
| 60 | + '}', |
| 61 | + '', |
| 62 | + '// Read reads an object from bytes.', |
| 63 | + 'func Read(objBytes []byte) (runtime.Object, error) {', |
| 64 | + '\treturn runtime.Decode(decoder, objBytes)', |
| 65 | + '}', |
| 66 | + '', |
| 67 | + '// ReadOrDie reads an object from bytes. Panics on error.', |
| 68 | + 'func ReadOrDie(objBytes []byte) runtime.Object {', |
| 69 | + '\trequiredObj, err := runtime.Decode(decoder, objBytes)', |
| 70 | + '\tif err != nil {', |
| 71 | + '\t\tpanic(err)', |
| 72 | + '\t}', |
| 73 | + '\treturn requiredObj', |
| 74 | + '}', |
| 75 | + '', # trailing newline |
| 76 | + ]) |
| 77 | + with open(path, 'w') as f: |
| 78 | + f.write('\n'.join(lines)) |
| 79 | + |
| 80 | + |
| 81 | +def generate_resourcebuilder(directory, types, clients, modifiers, health_checks): |
| 82 | + package_name = os.path.basename(directory) |
| 83 | + path = os.path.join(directory, package_name + '.go') |
| 84 | + lines = [ |
| 85 | + '// auto-generated with generate-lib-resources.py', |
| 86 | + '', |
| 87 | + '// Package {} reads supported objects from bytes.'.format(package_name), |
| 88 | + 'package {}'.format(package_name), |
| 89 | + '', |
| 90 | + 'import (', |
| 91 | + '\t"context"', |
| 92 | + '\t"fmt"', |
| 93 | + '', |
| 94 | + ] |
| 95 | + |
| 96 | + imports = {} |
| 97 | + for import_name in [ |
| 98 | + 'github.com/openshift/cluster-version-operator/lib', |
| 99 | + 'github.com/openshift/cluster-version-operator/lib/resourceapply', |
| 100 | + 'github.com/openshift/cluster-version-operator/lib/resourceread', |
| 101 | + 'k8s.io/client-go/rest', |
| 102 | + ]: |
| 103 | + imports[import_name] = '\t"{}"'.format(import_name) |
| 104 | + |
| 105 | + ignored_packages = set() |
| 106 | + |
| 107 | + for package in types.keys(): |
| 108 | + if not clients.get(package): |
| 109 | + ignored_packages.add(package) |
| 110 | + continue |
| 111 | + base, version = os.path.split(package) |
| 112 | + short_name = os.path.basename(base) |
| 113 | + imports[package] = '\t{}{} "{}"'.format(short_name, version, package) |
| 114 | + |
| 115 | + client_properties = {} |
| 116 | + for package, client in clients.items(): |
| 117 | + if package in ignored_packages: |
| 118 | + continue |
| 119 | + base, version = os.path.split(client['package']) |
| 120 | + short_name = os.path.basename(base) |
| 121 | + client_short_name = '{}client{}'.format(short_name, version) |
| 122 | + imports[client['package']] = '\t{} "{}"'.format(client_short_name, client['package']) |
| 123 | + client_properties['{}Client{}'.format(short_name, version)] = { |
| 124 | + 'package': package, |
| 125 | + 'client_short_name': client_short_name, |
| 126 | + 'type': '*{}.{}'.format(client_short_name, client['type']), |
| 127 | + 'protobuf': client['package'].startswith('k8s.io/') and 'kube-aggregator' not in client['package'], |
| 128 | + } |
| 129 | + |
| 130 | + lines.extend([import_line for _, import_line in sorted(imports.items(), key=lambda package_line: package_line[0])]) |
| 131 | + |
| 132 | + longest_property = max(len(prop_name) for prop_name in client_properties.keys()) |
| 133 | + |
| 134 | + lines.extend([ |
| 135 | + ')', |
| 136 | + '', |
| 137 | + '// builder manages single-manifest cluster reconciliation and monitoring.', |
| 138 | + 'type builder struct {', |
| 139 | + '\traw []byte', |
| 140 | + '\tmode Mode', |
| 141 | + '\tmodifier MetaV1ObjectModifierFunc', |
| 142 | + '', |
| 143 | + ]) |
| 144 | + lines.extend([ |
| 145 | + '\t{:{width}} {}'.format(prop_name, data['type'], width=longest_property) |
| 146 | + for prop_name, data in sorted(client_properties.items()) |
| 147 | + ]) |
| 148 | + |
| 149 | + lines.extend([ |
| 150 | + '}', |
| 151 | + '', |
| 152 | + 'func newBuilder(config *rest.Config, m lib.Manifest) Interface {', |
| 153 | + '\treturn &builder{', |
| 154 | + '\t\traw: m.Raw,', |
| 155 | + '', |
| 156 | + ]) |
| 157 | + for prop_name, data in sorted(client_properties.items()): |
| 158 | + new_client_arg = 'config' |
| 159 | + if data.get('protobuf'): |
| 160 | + new_client_arg = 'withProtobuf({})'.format(new_client_arg) |
| 161 | + lines.append('\t\t{:{width}} {}.NewForConfigOrDie({}),'.format(prop_name + ':', data['client_short_name'], new_client_arg, width=longest_property+1)) |
| 162 | + |
| 163 | + lines.extend([ |
| 164 | + '\t}', |
| 165 | + '}', |
| 166 | + '', |
| 167 | + 'func (b *builder) WithMode(m Mode) Interface {', |
| 168 | + '\tb.mode = m', |
| 169 | + '\treturn b', |
| 170 | + '}', |
| 171 | + '', |
| 172 | + 'func (b *builder) WithModifier(f MetaV1ObjectModifierFunc) Interface {', |
| 173 | + '\tb.modifier = f', |
| 174 | + '\treturn b', |
| 175 | + '}', |
| 176 | + '', |
| 177 | + 'func (b *builder) Do(ctx context.Context) error {', |
| 178 | + '\tobj := resourceread.ReadOrDie(b.raw)', |
| 179 | + '', |
| 180 | + '\tswitch typedObject := obj.(type) {' |
| 181 | + ]) |
| 182 | + |
| 183 | + for package, type_names in sorted(types.items()): |
| 184 | + if package in ignored_packages: |
| 185 | + continue |
| 186 | + base, version = os.path.split(package) |
| 187 | + short_name = os.path.basename(base) |
| 188 | + try: |
| 189 | + client_prop_name = [key for key, data in client_properties.items() if data['package'] == package][0] |
| 190 | + except IndexError as error: |
| 191 | + raise ValueError('no client property found for {}'.format(package)) |
| 192 | + for type_name in sorted(type_names): |
| 193 | + lines.extend([ |
| 194 | + '\tcase *{}{}.{}:'.format(short_name, version, type_name), |
| 195 | + '\t\tif b.modifier != nil {', |
| 196 | + '\t\t\tb.modifier(typedObject)', |
| 197 | + '\t\t}', |
| 198 | + ]) |
| 199 | + type_key = (package, type_name) |
| 200 | + modifier = modifiers.get(type_key) |
| 201 | + if modifier: |
| 202 | + lines.extend([ |
| 203 | + '\t\tif err := {}(ctx, typedObject); err != nil {{'.format(modifier), |
| 204 | + '\t\t\treturn err', |
| 205 | + '\t\t}', |
| 206 | + ]) |
| 207 | + lines.extend([ |
| 208 | + '\t\tif _, _, err := resourceapply.Apply{}{}(ctx, b.{}, typedObject); err != nil {{'.format(type_name, version, client_prop_name), |
| 209 | + '\t\t\treturn err', |
| 210 | + '\t\t}', |
| 211 | + ]) |
| 212 | + health_check = health_checks.get(type_key) |
| 213 | + if health_check: |
| 214 | + lines.append('\t\treturn {}(ctx, typedObject)'.format(health_check)) |
| 215 | + |
| 216 | + lines.extend([ |
| 217 | + '\tdefault:', |
| 218 | + '\t\treturn fmt.Errorf("unrecognized manifest type: %T", obj)', |
| 219 | + '\t}', |
| 220 | + '', |
| 221 | + '\treturn nil', |
| 222 | + '}', |
| 223 | + '', |
| 224 | + 'func init() {', |
| 225 | + '\trm := NewResourceMapper()', |
| 226 | + ]) |
| 227 | + |
| 228 | + for sgv, data in sorted(scheme_group_versions(types=types).items()): |
| 229 | + if data['package'] in ignored_packages: |
| 230 | + continue |
| 231 | + for type_name in sorted(data['types']): |
| 232 | + lines.append('\trm.RegisterGVK({}.WithKind("{}"), newBuilder)'.format(sgv, type_name)) |
| 233 | + |
| 234 | + lines.extend([ |
| 235 | + '\trm.AddToMap(Mapper)', |
| 236 | + '}', |
| 237 | + '', # trailing newline |
| 238 | + ]) |
| 239 | + |
| 240 | + with open(path, 'w') as f: |
| 241 | + f.write('\n'.join(lines)) |
| 242 | + |
| 243 | + |
| 244 | +def scheme_group_versions(types): |
| 245 | + sgvs = {} |
| 246 | + for package, type_names in types.items(): |
| 247 | + base, version = os.path.split(package) |
| 248 | + short_name = os.path.basename(base) |
| 249 | + sgv = '{}{}.SchemeGroupVersion'.format(short_name, version) |
| 250 | + sgvs[sgv] = { |
| 251 | + 'package': package, |
| 252 | + 'short_name': short_name, |
| 253 | + 'types': type_names, |
| 254 | + 'version': version, |
| 255 | + } |
| 256 | + return sgvs |
| 257 | + |
| 258 | + |
| 259 | +if __name__ == '__main__': |
| 260 | + types = { |
| 261 | + 'github.com/openshift/api/image/v1': {'ImageStream'}, # for payload loading |
| 262 | + 'github.com/openshift/api/security/v1': {'SecurityContextConstraints'}, |
| 263 | + 'k8s.io/api/apps/v1': {'DaemonSet', 'Deployment'}, |
| 264 | + 'k8s.io/api/batch/v1': {'Job'}, |
| 265 | + 'k8s.io/api/core/v1': {'ConfigMap', 'Namespace', 'Service', 'ServiceAccount'}, |
| 266 | + 'k8s.io/api/rbac/v1': {'ClusterRole', 'ClusterRoleBinding', 'Role', 'RoleBinding'}, |
| 267 | + 'k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1': {'CustomResourceDefinition'}, |
| 268 | + 'k8s.io/kube-aggregator/pkg/apis/apiregistration/v1': {'APIService'}, |
| 269 | + } |
| 270 | + |
| 271 | + types['k8s.io/api/rbac/v1beta1'] = types['k8s.io/api/rbac/v1'] |
| 272 | + types['k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1beta1'] = types['k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1'] |
| 273 | + types['k8s.io/kube-aggregator/pkg/apis/apiregistration/v1beta1'] = types['k8s.io/kube-aggregator/pkg/apis/apiregistration/v1'] |
| 274 | + |
| 275 | + clients = { |
| 276 | + 'github.com/openshift/api/security/v1': {'package': 'github.com/openshift/client-go/security/clientset/versioned/typed/security/v1', 'type': 'SecurityV1Client'}, |
| 277 | + 'github.com/openshift/api/config/v1': {'package': 'github.com/openshift/client-go/config/clientset/versioned/typed/config/v1', 'type': 'ConfigV1Client'}, |
| 278 | + 'k8s.io/api/apps/v1': {'package': 'k8s.io/client-go/kubernetes/typed/apps/v1', 'type': 'AppsV1Client'}, |
| 279 | + 'k8s.io/api/batch/v1': {'package': 'k8s.io/client-go/kubernetes/typed/batch/v1', 'type': 'BatchV1Client'}, |
| 280 | + 'k8s.io/api/core/v1': {'package': 'k8s.io/client-go/kubernetes/typed/core/v1', 'type': 'CoreV1Client'}, |
| 281 | + 'k8s.io/api/rbac/v1': {'package': 'k8s.io/client-go/kubernetes/typed/rbac/v1', 'type': 'RbacV1Client'}, |
| 282 | + 'k8s.io/api/rbac/v1beta1': {'package': 'k8s.io/client-go/kubernetes/typed/rbac/v1beta1', 'type': 'RbacV1beta1Client'}, |
| 283 | + 'k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1': {'package': 'k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset/typed/apiextensions/v1', 'type': 'ApiextensionsV1Client'}, |
| 284 | + 'k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1beta1': {'package': 'k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset/typed/apiextensions/v1beta1', 'type': 'ApiextensionsV1beta1Client'}, |
| 285 | + 'k8s.io/kube-aggregator/pkg/apis/apiregistration/v1': {'package': 'k8s.io/kube-aggregator/pkg/client/clientset_generated/clientset/typed/apiregistration/v1', 'type': 'ApiregistrationV1Client'}, |
| 286 | + 'k8s.io/kube-aggregator/pkg/apis/apiregistration/v1beta1': {'package': 'k8s.io/kube-aggregator/pkg/client/clientset_generated/clientset/typed/apiregistration/v1beta1', 'type': 'ApiregistrationV1beta1Client'}, |
| 287 | + } |
| 288 | + |
| 289 | + modifiers = { |
| 290 | + ('k8s.io/api/apps/v1', 'Deployment'): 'b.modifyDeployment', |
| 291 | + ('k8s.io/api/apps/v1', 'DaemonSet'): 'b.modifyDaemonSet', |
| 292 | + } |
| 293 | + |
| 294 | + health_checks = { |
| 295 | + ('k8s.io/api/apps/v1', 'Deployment'): 'b.checkDeploymentHealth', |
| 296 | + ('k8s.io/api/apps/v1', 'DaemonSet'): 'b.checkDaemonSetHealth', |
| 297 | + ('k8s.io/api/batch/v1', 'Job'): 'b.checkJobHealth', |
| 298 | + } |
| 299 | + |
| 300 | + generate_lib_resources(directory='lib', types=types, clients=clients, modifiers=modifiers, health_checks=health_checks) |
0 commit comments