From c2e71898c41280ffa457b562c25f8ae57423b1bb Mon Sep 17 00:00:00 2001 From: Bevan Arps Date: Fri, 25 Jun 2021 11:01:31 +1200 Subject: [PATCH 1/2] Select parameter name exactly once (#1589) Co-authored-by: Matthew Christopher --- .../property_assignment_function.go | 36 ++++++++----------- 1 file changed, 15 insertions(+), 21 deletions(-) diff --git a/hack/generator/pkg/conversions/property_assignment_function.go b/hack/generator/pkg/conversions/property_assignment_function.go index c8c8fddf86b..d3a550b565d 100644 --- a/hack/generator/pkg/conversions/property_assignment_function.go +++ b/hack/generator/pkg/conversions/property_assignment_function.go @@ -33,6 +33,10 @@ type PropertyAssignmentFunction struct { knownLocals *astmodel.KnownLocalsSet // conversionContext is additional information about the context in which this conversion was made conversionContext *PropertyConversionContext + // identifier to use for our receiver in generated code + receiverName string + // identifier to use for our parameter in generated code + parameterName string } // StoragePropertyConversion represents a function that generates the correct AST to convert a single property value @@ -58,11 +62,10 @@ func NewPropertyAssignmentFromFunction( direction: ConvertFrom, conversions: make(map[string]StoragePropertyConversion), knownLocals: astmodel.NewKnownLocalsSet(idFactory), + receiverName: idFactory.CreateIdentifier(receiver.Name().Name(), astmodel.NotExported), + parameterName: "source", } - // TODO: Bevan will improve how this is done (avoid hard-coding "source") - result.knownLocals.Add("source") - result.conversionContext = conversionContext.WithFunctionName(result.Name()). WithKnownLocals(result.knownLocals). WithDirection(ConvertFrom) @@ -88,11 +91,10 @@ func NewPropertyAssignmentToFunction( direction: ConvertTo, conversions: make(map[string]StoragePropertyConversion), knownLocals: astmodel.NewKnownLocalsSet(idFactory), + receiverName: idFactory.CreateIdentifier(receiver.Name().Name(), astmodel.NotExported), + parameterName: "destination", } - // TODO: Bevan will improve how this is done (avoid hard-coding "destination") - result.knownLocals.Add("destination") - result.conversionContext = conversionContext.WithFunctionName(result.Name()). WithKnownLocals(result.knownLocals). WithDirection(ConvertTo) @@ -153,41 +155,32 @@ func (fn *PropertyAssignmentFunction) Equals(f astmodel.Function) bool { // AsFunc renders this function as an AST for serialization to a Go source file func (fn *PropertyAssignmentFunction) AsFunc(generationContext *astmodel.CodeGenerationContext, receiver astmodel.TypeName) *dst.FuncDecl { - var parameterName string var description string switch fn.direction { case ConvertFrom: - parameterName = "source" description = fmt.Sprintf("populates our %s from the provided source %s", receiver.Name(), fn.otherDefinition.Name().Name()) case ConvertTo: - parameterName = "destination" description = fmt.Sprintf("populates the provided destination %s from our %s", fn.otherDefinition.Name().Name(), receiver.Name()) default: panic(fmt.Sprintf("unexpected conversion direction %q", fn.direction)) } - // Create a sensible name for our receiver - receiverName := fn.idFactory.CreateIdentifier(receiver.Name(), astmodel.NotExported) - // We always use a pointer receiver so we can modify it receiverType := astmodel.NewOptionalType(receiver).AsType(generationContext) funcDetails := &astbuilder.FuncDetails{ - ReceiverIdent: receiverName, + ReceiverIdent: fn.receiverName, ReceiverType: receiverType, Name: fn.Name(), - Body: fn.generateBody(receiverName, parameterName, generationContext), + Body: fn.generateBody(fn.receiverName, fn.parameterName, generationContext), } parameterPackage := generationContext.MustGetImportedPackageName(fn.otherDefinition.Name().PackageReference) funcDetails.AddParameter( - parameterName, + fn.parameterName, &dst.StarExpr{ - X: &dst.SelectorExpr{ - X: dst.NewIdent(parameterPackage), - Sel: dst.NewIdent(fn.otherDefinition.Name().Name()), - }, + X: astbuilder.Selector(dst.NewIdent(parameterPackage), fn.otherDefinition.Name().Name()), }) funcDetails.AddReturns("error") @@ -291,8 +284,9 @@ func (fn *PropertyAssignmentFunction) createConversions(receiver astmodel.TypeDe panic(fmt.Sprintf("unexpected conversion direction %q", fn.direction)) } - // Flag receiver name as used - fn.knownLocals.Add(receiver.Name().Name()) + // Flag receiver and parameter names as used + fn.knownLocals.Add(fn.receiverName) + fn.knownLocals.Add(fn.parameterName) for destinationName, destinationEndpoint := range destinationEndpoints { sourceEndpoint, ok := sourceEndpoints[destinationName] From f40b9a7046cb6d440faca59de988af73a9c84639 Mon Sep 17 00:00:00 2001 From: Bevan Arps Date: Sat, 26 Jun 2021 09:49:08 +1200 Subject: [PATCH 2/2] Move pipeline stages into subpackage (#1593) * Move PipelineTarget into new package * Rename to Target for clarity * Move PipelineStage into subpackage * Rename PipelineStage to Stage for clarity in new subpackage Also renames constructor function * Move reportTypesAndVersions into subpackage * Move rogueCheck into subpackage * Move deleteGenerated into subpackage * Move filterTypes in to subpackage * Move applyArmConversionInterface into subpackage * Move AddCrossResourceReferences into subpackage * Move applyKubernetesResourceInterface into subpackage * Move propertyRewrites into subpackage * Move assertTypesStructureValid into subpackage * Move augmentStatus into subpackage * Move allof-anyof-objects into subpackage * Move ensureArmTypeExistsForEveryType into subpackage * Move exportControllerResourceRegistrations into subpackage * Move exportPackages into subpackage * Move ReplaceAnyTypeWithJSON into subpackage * Move stripUnreferenced into subpackage * Move flatten-resources into subpackage * Move crossplane into subpackage * Move createArmTypes into sub package * Move createStorage into subpackage * Move pluralizeNames into subpackage * Move determineResourceOwnership into subpackage * Move jsonTestCases into subpackage * Move loadSchema into subpackage * Move markStorageVersion into subpackage * Move nameTypes into subpackage * Move markStorageVersion into subpackage * Move removeEmbeddedResources into subpackage * Move simplifyDefinitions into subpackage * Move removeAliases into subpackage * Update golden files * Apply suggestions from code review Co-authored-by: Matthew Christopher Co-authored-by: Christian Muirhead * Update pipeline golden files * Move collapseCrossGroupReferences into subfolder Co-authored-by: Matthew Christopher Co-authored-by: Christian Muirhead --- hack/generator/pkg/codegen/code_generator.go | 143 ++++++++---------- .../pkg/codegen/code_generator_test.go | 59 ++++---- .../pkg/codegen/golden_files_test.go | 58 ++++--- .../add_arm_conversion_interface.go} | 10 +- .../add_cross_resource_references.go} | 20 +-- .../apply_export_filters.go} | 13 +- .../apply_kubernetes_resource_interface.go} | 13 +- .../apply_property_rewrites.go} | 10 +- .../assert_types_collection_valid.go} | 10 +- .../augment_status.go} | 15 +- .../augment_status_test.go} | 5 +- .../check_for_anytype.go} | 14 +- .../check_for_anytype_test.go} | 30 ++-- .../collapse_cross_group_refs.go} | 11 +- .../convert_allof_and_oneof_to_objects.go} | 11 +- ...onvert_allof_and_oneof_to_objects_test.go} | 2 +- .../create_arm_types.go} | 10 +- .../create_storage_types.go} | 14 +- .../crossplane_add_at_provider.go} | 10 +- .../crossplane_add_embedded_resource_spec.go} | 10 +- ...rossplane_add_embedded_resource_status.go} | 10 +- .../crossplane_add_for_provider.go} | 10 +- .../crossplane_add_owner_properties.go} | 10 +- .../delete_generated_code.go} | 8 +- .../determine_resource_ownership.go} | 6 +- .../ensure_type_has_arm_type.go} | 10 +- .../export_controller_type_registrations.go} | 11 +- .../export_generated_code.go} | 17 ++- .../flatten_resources.go} | 13 +- .../improve_resource_pluralization.go} | 8 +- .../json_serialization_test_cases.go} | 6 +- .../load_schema.go} | 17 ++- .../mark_storage_version.go} | 10 +- .../name_types_for_crd.go} | 11 +- .../remove_embedded_resources.go} | 8 +- .../remove_type_aliases.go} | 11 +- .../replace_anytype_with_json.go} | 8 +- .../replace_anytype_with_json_test.go} | 11 +- .../report_type_versions.go} | 13 +- .../resource_registration_file.go | 2 +- .../simplify_definitions.go} | 11 +- .../simplify_definitions_test.go} | 2 +- .../{pipeline_stage.go => pipeline/stage.go} | 67 ++++++-- .../strip_unused_types.go} | 6 +- .../strip_unused_types_test.go} | 2 +- hack/generator/pkg/codegen/pipeline/target.go | 51 +++++++ .../pkg/codegen/{ => pipeline}/version.go | 4 +- .../pkg/codegen/pipeline_factory_test.go | 7 +- hack/generator/pkg/codegen/pipeline_target.go | 36 ----- .../testdata/ARMCodeGeneratorPipeline.golden | 36 ++--- .../testdata/TestCodeGeneratorPipeline.golden | 18 +-- hack/generator/pkg/test/package_reference.go | 17 +++ 52 files changed, 501 insertions(+), 424 deletions(-) rename hack/generator/pkg/codegen/{pipeline_add_arm_conversion_interface.go => pipeline/add_arm_conversion_interface.go} (96%) rename hack/generator/pkg/codegen/{pipeline_add_cross_resource_references.go => pipeline/add_cross_resource_references.go} (91%) rename hack/generator/pkg/codegen/{pipeline_apply_export_filters.go => pipeline/apply_export_filters.go} (84%) rename hack/generator/pkg/codegen/{pipeline_apply_kubernetes_resource_interface.go => pipeline/apply_kubernetes_resource_interface.go} (84%) rename hack/generator/pkg/codegen/{pipeline_apply_property_rewrites.go => pipeline/apply_property_rewrites.go} (83%) rename hack/generator/pkg/codegen/{pipeline_assert_types_collection_valid.go => pipeline/assert_types_collection_valid.go} (74%) rename hack/generator/pkg/codegen/{pipeline_augment_status.go => pipeline/augment_status.go} (97%) rename hack/generator/pkg/codegen/{pipeline_augment_status_test.go => pipeline/augment_status_test.go} (98%) rename hack/generator/pkg/codegen/{pipeline_check_for_anytype.go => pipeline/check_for_anytype.go} (90%) rename hack/generator/pkg/codegen/{pipeline_check_for_anytype_test.go => pipeline/check_for_anytype_test.go} (77%) rename hack/generator/pkg/codegen/{pipeline_collapse_cross_group_refs.go => pipeline/collapse_cross_group_refs.go} (92%) rename hack/generator/pkg/codegen/{pipeline_convert_allof_and_oneof_to_objects.go => pipeline/convert_allof_and_oneof_to_objects.go} (98%) rename hack/generator/pkg/codegen/{pipeline_convert_allof_and_oneof_to_objects_test.go => pipeline/convert_allof_and_oneof_to_objects_test.go} (99%) rename hack/generator/pkg/codegen/{pipeline_create_arm_types.go => pipeline/create_arm_types.go} (97%) rename hack/generator/pkg/codegen/{pipeline_create_storage_types.go => pipeline/create_storage_types.go} (91%) rename hack/generator/pkg/codegen/{crossplane_pipeline_add_at_provider.go => pipeline/crossplane_add_at_provider.go} (90%) rename hack/generator/pkg/codegen/{crossplane_pipeline_add_embedded_resource_spec.go => pipeline/crossplane_add_embedded_resource_spec.go} (85%) rename hack/generator/pkg/codegen/{crossplane_pipeline_add_embedded_resource_status.go => pipeline/crossplane_add_embedded_resource_status.go} (86%) rename hack/generator/pkg/codegen/{crossplane_pipeline_add_for_provider.go => pipeline/crossplane_add_for_provider.go} (93%) rename hack/generator/pkg/codegen/{crossplane_pipeline_add_owner_properties.go => pipeline/crossplane_add_owner_properties.go} (93%) rename hack/generator/pkg/codegen/{pipeline_delete_generated_code.go => pipeline/delete_generated_code.go} (95%) rename hack/generator/pkg/codegen/{pipeline_determine_resource_ownership.go => pipeline/determine_resource_ownership.go} (98%) rename hack/generator/pkg/codegen/{pipeline_ensure_type_has_arm_type.go => pipeline/ensure_type_has_arm_type.go} (88%) rename hack/generator/pkg/codegen/{pipeline_export_controller_type_registrations.go => pipeline/export_controller_type_registrations.go} (80%) rename hack/generator/pkg/codegen/{pipeline_export_generated_code.go => pipeline/export_generated_code.go} (93%) rename hack/generator/pkg/codegen/{pipeline_flatten_resources.go => pipeline/flatten_resources.go} (92%) rename hack/generator/pkg/codegen/{pipeline_improve_resource_pluralization.go => pipeline/improve_resource_pluralization.go} (93%) rename hack/generator/pkg/codegen/{pipeline_json_serialization_test_cases.go => pipeline/json_serialization_test_cases.go} (93%) rename hack/generator/pkg/codegen/{pipeline_load_schema.go => pipeline/load_schema.go} (96%) rename hack/generator/pkg/codegen/{pipeline_mark_storage_version.go => pipeline/mark_storage_version.go} (93%) rename hack/generator/pkg/codegen/{pipeline_name_types_for_crd.go => pipeline/name_types_for_crd.go} (97%) rename hack/generator/pkg/codegen/{pipeline_remove_embedded_resources.go => pipeline/remove_embedded_resources.go} (69%) rename hack/generator/pkg/codegen/{pipeline_remove_type_aliases.go => pipeline/remove_type_aliases.go} (94%) rename hack/generator/pkg/codegen/{pipeline_replace_anytype_with_json.go => pipeline/replace_anytype_with_json.go} (92%) rename hack/generator/pkg/codegen/{pipeline_replace_anytype_with_json_test.go => pipeline/replace_anytype_with_json_test.go} (87%) rename hack/generator/pkg/codegen/{pipeline_report_type_versions.go => pipeline/report_type_versions.go} (93%) rename hack/generator/pkg/codegen/{ => pipeline}/resource_registration_file.go (99%) rename hack/generator/pkg/codegen/{pipeline_simplify_definitions.go => pipeline/simplify_definitions.go} (90%) rename hack/generator/pkg/codegen/{pipeline_simplify_definitions_test.go => pipeline/simplify_definitions_test.go} (98%) rename hack/generator/pkg/codegen/{pipeline_stage.go => pipeline/stage.go} (57%) rename hack/generator/pkg/codegen/{pipeline_strip_unused_types.go => pipeline/strip_unused_types.go} (92%) rename hack/generator/pkg/codegen/{pipeline_strip_unused_types_test.go => pipeline/strip_unused_types_test.go} (98%) create mode 100644 hack/generator/pkg/codegen/pipeline/target.go rename hack/generator/pkg/codegen/{ => pipeline}/version.go (87%) delete mode 100644 hack/generator/pkg/codegen/pipeline_target.go create mode 100644 hack/generator/pkg/test/package_reference.go diff --git a/hack/generator/pkg/codegen/code_generator.go b/hack/generator/pkg/codegen/code_generator.go index 3f8c809b780..71f8c3f24f2 100644 --- a/hack/generator/pkg/codegen/code_generator.go +++ b/hack/generator/pkg/codegen/code_generator.go @@ -14,24 +14,14 @@ import ( "k8s.io/klog/v2" "github.com/Azure/azure-service-operator/hack/generator/pkg/astmodel" + "github.com/Azure/azure-service-operator/hack/generator/pkg/codegen/pipeline" "github.com/Azure/azure-service-operator/hack/generator/pkg/config" ) // CodeGenerator is a generator of code type CodeGenerator struct { configuration *config.Configuration - pipeline []PipelineStage -} - -func translatePipelineToTarget(pipeline config.GenerationPipeline) (PipelineTarget, error) { - switch pipeline { - case config.GenerationPipelineAzure: - return ARMTarget, nil - case config.GenerationPipelineCrossplane: - return CrossplaneTarget, nil - default: - return PipelineTarget{}, errors.Errorf("unknown pipeline target kind %s", pipeline) - } + pipeline []pipeline.Stage } // NewCodeGeneratorFromConfigFile produces a new Generator with the given configuration file @@ -41,7 +31,7 @@ func NewCodeGeneratorFromConfigFile(configurationFile string) (*CodeGenerator, e return nil, err } - target, err := translatePipelineToTarget(configuration.Pipeline) + target, err := pipeline.TranslatePipelineToTarget(configuration.Pipeline) if err != nil { return nil, err } @@ -50,9 +40,9 @@ func NewCodeGeneratorFromConfigFile(configurationFile string) (*CodeGenerator, e } // NewTargetedCodeGeneratorFromConfig produces a new code generator with the given configuration and -// only the stages appropriate for the specfied target. +// only the stages appropriate for the specified target. func NewTargetedCodeGeneratorFromConfig( - configuration *config.Configuration, idFactory astmodel.IdentifierFactory, target PipelineTarget) (*CodeGenerator, error) { + configuration *config.Configuration, idFactory astmodel.IdentifierFactory, target pipeline.Target) (*CodeGenerator, error) { result, err := NewCodeGeneratorFromConfig(configuration, idFactory) if err != nil { @@ -60,7 +50,7 @@ func NewTargetedCodeGeneratorFromConfig( } // Filter stages to use only those appropriate for our target - var stages []PipelineStage + var stages []pipeline.Stage for _, s := range result.pipeline { if s.IsUsedFor(target) { stages = append(stages, s) @@ -87,104 +77,104 @@ func NewCodeGeneratorFromConfig(configuration *config.Configuration, idFactory a return result, nil } -func createAllPipelineStages(idFactory astmodel.IdentifierFactory, configuration *config.Configuration) []PipelineStage { - return []PipelineStage{ +func createAllPipelineStages(idFactory astmodel.IdentifierFactory, configuration *config.Configuration) []pipeline.Stage { + return []pipeline.Stage{ - loadSchemaIntoTypes(idFactory, configuration, defaultSchemaLoader), + pipeline.LoadSchemaIntoTypes(idFactory, configuration, pipeline.DefaultSchemaLoader), // Import status info from Swagger: - augmentResourcesWithStatus(idFactory, configuration), + pipeline.AugmentResourcesWithStatus(idFactory, configuration), // Reduces oneOf/allOf types from schemas to object types: - convertAllOfAndOneOfToObjects(idFactory), + pipeline.ConvertAllOfAndOneOfToObjects(idFactory), // Flatten out any nested resources created by allOf, etc. we want to do this before naming types or things // get named with names like Resource_Spec_Spec_Spec: - flattenResources(), + pipeline.FlattenResources(), - stripUnreferencedTypeDefinitions(), + pipeline.StripUnreferencedTypeDefinitions(), // Name all anonymous object, enum, and validated types (required by controller-gen): - nameTypesForCRD(idFactory), + pipeline.NameTypesForCRD(idFactory), // Apply property type rewrites from the config file - // Must come after nameTypesForCRD ('nameTypes)' and convertAllOfAndOneOfToObjects ('allof-anyof-objects') so + // Must come after NameTypesForCRD ('nameTypes)' and ConvertAllOfAndOneOfToObjects ('allof-anyof-objects') so // that objects are all expanded - applyPropertyRewrites(configuration). + pipeline.ApplyPropertyRewrites(configuration). RequiresPrerequisiteStages("nameTypes", "allof-anyof-objects"), // Figure out resource owners: - determineResourceOwnership(configuration), + pipeline.DetermineResourceOwnership(configuration), // Strip out redundant type aliases: - removeTypeAliases(), + pipeline.RemoveTypeAliases(), // Collapse cross group references - collapseCrossGroupReferences(), + pipeline.CollapseCrossGroupReferences(), // De-pluralize resource types // (Must come after type aliases are resolved) - improveResourcePluralization(). + pipeline.ImproveResourcePluralization(). RequiresPrerequisiteStages("removeAliases"), - stripUnreferencedTypeDefinitions(), + pipeline.StripUnreferencedTypeDefinitions(), - assertTypesCollectionValid(), + pipeline.AssertTypesCollectionValid(), - removeEmbeddedResources().UsedFor(ARMTarget), // TODO: For now only used for ARM, + pipeline.RemoveEmbeddedResources().UsedFor(pipeline.ARMTarget), // TODO: For now only used for ARM, // Apply export filters before generating // ARM types for resources etc: - applyExportFilters(configuration), + pipeline.ApplyExportFilters(configuration), - stripUnreferencedTypeDefinitions(), + pipeline.StripUnreferencedTypeDefinitions(), - replaceAnyTypeWithJSON(), + pipeline.ReplaceAnyTypeWithJSON(), - addCrossResourceReferences(configuration, idFactory).UsedFor(ARMTarget), + pipeline.AddCrossResourceReferences(configuration, idFactory).UsedFor(pipeline.ARMTarget), - reportOnTypesAndVersions(configuration).UsedFor(ARMTarget), // TODO: For now only used for ARM + pipeline.ReportOnTypesAndVersions(configuration).UsedFor(pipeline.ARMTarget), // TODO: For now only used for ARM - createARMTypes(idFactory).UsedFor(ARMTarget), - applyARMConversionInterface(idFactory).UsedFor(ARMTarget), - applyKubernetesResourceInterface(idFactory).UsedFor(ARMTarget), + pipeline.CreateARMTypes(idFactory).UsedFor(pipeline.ARMTarget), + pipeline.ApplyARMConversionInterface(idFactory).UsedFor(pipeline.ARMTarget), + pipeline.ApplyKubernetesResourceInterface(idFactory).UsedFor(pipeline.ARMTarget), - addCrossplaneOwnerProperties(idFactory).UsedFor(CrossplaneTarget), - addCrossplaneForProvider(idFactory).UsedFor(CrossplaneTarget), - addCrossplaneAtProvider(idFactory).UsedFor(CrossplaneTarget), - addCrossplaneEmbeddedResourceSpec(idFactory).UsedFor(CrossplaneTarget), - addCrossplaneEmbeddedResourceStatus(idFactory).UsedFor(CrossplaneTarget), + pipeline.AddCrossplaneOwnerProperties(idFactory).UsedFor(pipeline.CrossplaneTarget), + pipeline.AddCrossplaneForProvider(idFactory).UsedFor(pipeline.CrossplaneTarget), + pipeline.AddCrossplaneAtProvider(idFactory).UsedFor(pipeline.CrossplaneTarget), + pipeline.AddCrossplaneEmbeddedResourceSpec(idFactory).UsedFor(pipeline.CrossplaneTarget), + pipeline.AddCrossplaneEmbeddedResourceStatus(idFactory).UsedFor(pipeline.CrossplaneTarget), - createStorageTypes(idFactory).UsedFor(ARMTarget), // TODO: For now only used for ARM - simplifyDefinitions(), - injectJsonSerializationTests(idFactory).UsedFor(ARMTarget), + pipeline.CreateStorageTypes(idFactory).UsedFor(pipeline.ARMTarget), // TODO: For now only used for ARM + pipeline.SimplifyDefinitions(), + pipeline.InjectJsonSerializationTests(idFactory).UsedFor(pipeline.ARMTarget), - markStorageVersion(), + pipeline.MarkStorageVersion(), // Safety checks at the end: - ensureDefinitionsDoNotUseAnyTypes(), - ensureARMTypeExistsForEveryResource().UsedFor(ARMTarget), + pipeline.EnsureDefinitionsDoNotUseAnyTypes(), + pipeline.EnsureARMTypeExistsForEveryResource().UsedFor(pipeline.ARMTarget), - deleteGeneratedCode(configuration.FullTypesOutputPath()), + pipeline.DeleteGeneratedCode(configuration.FullTypesOutputPath()), - exportPackages(configuration.FullTypesOutputPath()). + pipeline.ExportPackages(configuration.FullTypesOutputPath()). RequiresPrerequisiteStages("deleteGenerated"), - exportControllerResourceRegistrations(configuration.FullTypesRegistrationOutputFilePath()).UsedFor(ARMTarget), + pipeline.ExportControllerResourceRegistrations(configuration.FullTypesRegistrationOutputFilePath()).UsedFor(pipeline.ARMTarget), } } // Generate produces the Go code corresponding to the configured JSON schema in the given output folder func (generator *CodeGenerator) Generate(ctx context.Context) error { - klog.V(1).Infof("Generator version: %v", combinedVersion()) + klog.V(1).Infof("Generator version: %v", pipeline.CombinedVersion()) defs := make(astmodel.Types) for i, stage := range generator.pipeline { - klog.V(0).Infof("%d/%d: %s", i+1, len(generator.pipeline), stage.description) + klog.V(0).Infof("%d/%d: %s", i+1, len(generator.pipeline), stage.Description()) // Defensive copy (in case the pipeline modifies its inputs) so that we can compare types in vs out - defsOut, err := stage.action(ctx, defs.Copy()) + defsOut, err := stage.Run(ctx, defs.Copy()) if err != nil { - return errors.Wrapf(err, "failed during pipeline stage %d/%d: %s", i+1, len(generator.pipeline), stage.description) + return errors.Wrapf(err, "failed during pipeline stage %d/%d: %s", i+1, len(generator.pipeline), stage.Description()) } defsAdded := defsOut.Except(defs) @@ -216,18 +206,17 @@ func (generator *CodeGenerator) verifyPipeline() error { stagesExpected := make(map[string][]string) for _, stage := range generator.pipeline { - for _, prereq := range stage.prerequisites { - if _, ok := stagesSeen[prereq]; !ok { - errs = append(errs, errors.Errorf("prerequisite %q of stage %q not satisfied.", prereq, stage.id)) - } + err := stage.CheckPrerequisites(stagesSeen) + if err != nil { + errs = append(errs, err) } - for _, postreq := range stage.postrequisites { - stagesExpected[postreq] = append(stagesExpected[postreq], stage.id) + for _, postreq := range stage.Postrequisites() { + stagesExpected[postreq] = append(stagesExpected[postreq], stage.Id()) } - stagesSeen[stage.id] = struct{}{} - delete(stagesExpected, stage.id) + stagesSeen[stage.Id()] = struct{}{} + delete(stagesExpected, stage.Id()) } for required, requiredBy := range stagesExpected { @@ -248,30 +237,30 @@ func (generator *CodeGenerator) RemoveStages(stageIds ...string) { stagesToRemove[s] = false } - var pipeline []PipelineStage + var stages []pipeline.Stage for _, stage := range generator.pipeline { - if _, ok := stagesToRemove[stage.id]; ok { - stagesToRemove[stage.id] = true + if _, ok := stagesToRemove[stage.Id()]; ok { + stagesToRemove[stage.Id()] = true continue } - pipeline = append(pipeline, stage) + stages = append(stages, stage) } for stage, removed := range stagesToRemove { if !removed { - panic(fmt.Sprintf("Expected to remove stage %s from pipeline, but it wasn't found.", stage)) + panic(fmt.Sprintf("Expected to remove stage %s from stages, but it wasn't found.", stage)) } } - generator.pipeline = pipeline + generator.pipeline = stages } // ReplaceStage replaces all uses of an existing stage with another one. // Only available for test builds. // Will panic if the existing stage is not found. -func (generator *CodeGenerator) ReplaceStage(existingStage string, stage PipelineStage) { +func (generator *CodeGenerator) ReplaceStage(existingStage string, stage pipeline.Stage) { replaced := false for i, s := range generator.pipeline { if s.HasId(existingStage) { @@ -288,12 +277,12 @@ func (generator *CodeGenerator) ReplaceStage(existingStage string, stage Pipelin // InjectStageAfter injects a new stage immediately after the first occurrence of an existing stage // Only available for test builds. // Will panic if the existing stage is not found. -func (generator *CodeGenerator) InjectStageAfter(existingStage string, stage PipelineStage) { +func (generator *CodeGenerator) InjectStageAfter(existingStage string, stage pipeline.Stage) { injected := false for i, s := range generator.pipeline { if s.HasId(existingStage) { - var p []PipelineStage + var p []pipeline.Stage p = append(p, generator.pipeline[:i+1]...) p = append(p, stage) p = append(p, generator.pipeline[i+1:]...) @@ -304,7 +293,7 @@ func (generator *CodeGenerator) InjectStageAfter(existingStage string, stage Pip } if !injected { - panic(fmt.Sprintf("Expected to inject stage %s but %s wasn't found", stage.id, existingStage)) + panic(fmt.Sprintf("Expected to inject stage %s but %s wasn't found", stage.Id(), existingStage)) } } diff --git a/hack/generator/pkg/codegen/code_generator_test.go b/hack/generator/pkg/codegen/code_generator_test.go index 919acc1d0e0..c291f3f7919 100644 --- a/hack/generator/pkg/codegen/code_generator_test.go +++ b/hack/generator/pkg/codegen/code_generator_test.go @@ -10,6 +10,7 @@ import ( "testing" "github.com/Azure/azure-service-operator/hack/generator/pkg/astmodel" + "github.com/Azure/azure-service-operator/hack/generator/pkg/codegen/pipeline" . "github.com/onsi/gomega" ) @@ -33,7 +34,7 @@ func TestRemoveStages_RemovesSpecifiedStages(t *testing.T) { g := NewGomegaWithT(t) gen := &CodeGenerator{ - pipeline: []PipelineStage{ + pipeline: []pipeline.Stage{ fooStage, barStage, bazStage, @@ -49,7 +50,7 @@ func TestRemoveStages_PanicsForUnknownStage(t *testing.T) { g := NewGomegaWithT(t) gen := &CodeGenerator{ - pipeline: []PipelineStage{ + pipeline: []pipeline.Stage{ fooStage, barStage, bazStage, @@ -64,8 +65,8 @@ func TestRemoveStages_PanicsForUnknownStage(t *testing.T) { gen.RemoveStages("foo", "baz") } -func MakeFakePipelineStage(id string) PipelineStage { - return MakePipelineStage( +func MakeFakePipelineStage(id string) pipeline.Stage { + return pipeline.MakeStage( id, "Stage "+id, func(ctx context.Context, types astmodel.Types) (astmodel.Types, error) { return types, nil }) @@ -79,7 +80,7 @@ func TestReplaceStage_ReplacesSpecifiedStage(t *testing.T) { g := NewGomegaWithT(t) gen := &CodeGenerator{ - pipeline: []PipelineStage{ + pipeline: []pipeline.Stage{ fooStage, barStage, bazStage, @@ -96,7 +97,7 @@ func TestReplaceStage_PanicsForUnknownStage(t *testing.T) { g := NewGomegaWithT(t) gen := &CodeGenerator{ - pipeline: []PipelineStage{ + pipeline: []pipeline.Stage{ fooStage, barStage, bazStage, @@ -117,7 +118,7 @@ func TestInjectStageAfter_InjectsSpecifiedStage(t *testing.T) { g := NewGomegaWithT(t) gen := &CodeGenerator{ - pipeline: []PipelineStage{ + pipeline: []pipeline.Stage{ fooStage, barStage, bazStage, @@ -134,7 +135,7 @@ func TestInjectStageAfter_PanicsForUnknownStage(t *testing.T) { g := NewGomegaWithT(t) gen := &CodeGenerator{ - pipeline: []PipelineStage{ + pipeline: []pipeline.Stage{ fooStage, barStage, bazStage, @@ -155,7 +156,7 @@ func TestVerifyPipeline_GivenNoPrerequisites_ReturnsNoError(t *testing.T) { g := NewGomegaWithT(t) gen := &CodeGenerator{ - pipeline: []PipelineStage{ + pipeline: []pipeline.Stage{ fooStage, barStage, bazStage, @@ -168,10 +169,10 @@ func TestVerifyPipeline_GivenNoPrerequisites_ReturnsNoError(t *testing.T) { func TestVerifyPipeline_GivenSatisfiedPrerequisites_ReturnsNoError(t *testing.T) { g := NewGomegaWithT(t) - stage := MakeFakePipelineStage("stage").RequiresPrerequisiteStages(barStage.id) + stage := MakeFakePipelineStage("stage").RequiresPrerequisiteStages(barStage.Id()) gen := &CodeGenerator{ - pipeline: []PipelineStage{ + pipeline: []pipeline.Stage{ fooStage, barStage, stage, @@ -185,10 +186,10 @@ func TestVerifyPipeline_GivenSatisfiedPrerequisites_ReturnsNoError(t *testing.T) func TestVerifyPipeline_GivenUnsatisfiedPrerequisites_ReturnsError(t *testing.T) { g := NewGomegaWithT(t) - stage := MakeFakePipelineStage("stage").RequiresPrerequisiteStages(barStage.id) + stage := MakeFakePipelineStage("stage").RequiresPrerequisiteStages(barStage.Id()) gen := &CodeGenerator{ - pipeline: []PipelineStage{ + pipeline: []pipeline.Stage{ fooStage, stage, bazStage, @@ -197,17 +198,17 @@ func TestVerifyPipeline_GivenUnsatisfiedPrerequisites_ReturnsError(t *testing.T) err := gen.verifyPipeline() g.Expect(err).NotTo(BeNil()) - g.Expect(err.Error()).To(ContainSubstring(stage.id)) - g.Expect(err.Error()).To(ContainSubstring(barStage.id)) + g.Expect(err.Error()).To(ContainSubstring(stage.Id())) + g.Expect(err.Error()).To(ContainSubstring(barStage.Id())) } func TestVerifyPipeline_GivenOutOfOrderPrerequisites_ReturnsError(t *testing.T) { g := NewGomegaWithT(t) - stage := MakeFakePipelineStage("stage").RequiresPrerequisiteStages(barStage.id) + stage := MakeFakePipelineStage("stage").RequiresPrerequisiteStages(barStage.Id()) gen := &CodeGenerator{ - pipeline: []PipelineStage{ + pipeline: []pipeline.Stage{ fooStage, stage, barStage, @@ -217,17 +218,17 @@ func TestVerifyPipeline_GivenOutOfOrderPrerequisites_ReturnsError(t *testing.T) err := gen.verifyPipeline() g.Expect(err).NotTo(BeNil()) - g.Expect(err.Error()).To(ContainSubstring(stage.id)) - g.Expect(err.Error()).To(ContainSubstring(barStage.id)) + g.Expect(err.Error()).To(ContainSubstring(stage.Id())) + g.Expect(err.Error()).To(ContainSubstring(barStage.Id())) } func TestVerifyPipeline_GivenSatisfiedPostrequisites_ReturnsNoError(t *testing.T) { g := NewGomegaWithT(t) - stage := MakeFakePipelineStage("stage").RequiresPostrequisiteStages(barStage.id) + stage := MakeFakePipelineStage("stage").RequiresPostrequisiteStages(barStage.Id()) gen := &CodeGenerator{ - pipeline: []PipelineStage{ + pipeline: []pipeline.Stage{ fooStage, stage, barStage, @@ -242,10 +243,10 @@ func TestVerifyPipeline_GivenSatisfiedPostrequisites_ReturnsNoError(t *testing.T func TestVerifyPipeline_GivenUnsatisfiedPostrequisites_ReturnsError(t *testing.T) { g := NewGomegaWithT(t) - stage := MakeFakePipelineStage("stage").RequiresPrerequisiteStages(barStage.id) + stage := MakeFakePipelineStage("stage").RequiresPrerequisiteStages(barStage.Id()) gen := &CodeGenerator{ - pipeline: []PipelineStage{ + pipeline: []pipeline.Stage{ fooStage, stage, bazStage, @@ -254,17 +255,17 @@ func TestVerifyPipeline_GivenUnsatisfiedPostrequisites_ReturnsError(t *testing.T err := gen.verifyPipeline() g.Expect(err).NotTo(BeNil()) - g.Expect(err.Error()).To(ContainSubstring(stage.id)) - g.Expect(err.Error()).To(ContainSubstring(barStage.id)) + g.Expect(err.Error()).To(ContainSubstring(stage.Id())) + g.Expect(err.Error()).To(ContainSubstring(barStage.Id())) } func TestVerifyPipeline_GivenOutOfOrderPostrequisites_ReturnsError(t *testing.T) { g := NewGomegaWithT(t) - stage := MakeFakePipelineStage("stage").RequiresPostrequisiteStages(barStage.id) + stage := MakeFakePipelineStage("stage").RequiresPostrequisiteStages(barStage.Id()) gen := &CodeGenerator{ - pipeline: []PipelineStage{ + pipeline: []pipeline.Stage{ fooStage, barStage, stage, @@ -274,6 +275,6 @@ func TestVerifyPipeline_GivenOutOfOrderPostrequisites_ReturnsError(t *testing.T) err := gen.verifyPipeline() g.Expect(err).NotTo(BeNil()) - g.Expect(err.Error()).To(ContainSubstring(stage.id)) - g.Expect(err.Error()).To(ContainSubstring(barStage.id)) + g.Expect(err.Error()).To(ContainSubstring(stage.Id())) + g.Expect(err.Error()).To(ContainSubstring(barStage.Id())) } diff --git a/hack/generator/pkg/codegen/golden_files_test.go b/hack/generator/pkg/codegen/golden_files_test.go index 223b0146ebf..d44fc46d58e 100644 --- a/hack/generator/pkg/codegen/golden_files_test.go +++ b/hack/generator/pkg/codegen/golden_files_test.go @@ -22,8 +22,10 @@ import ( "k8s.io/klog/v2" "github.com/Azure/azure-service-operator/hack/generator/pkg/astmodel" + "github.com/Azure/azure-service-operator/hack/generator/pkg/codegen/pipeline" "github.com/Azure/azure-service-operator/hack/generator/pkg/config" "github.com/Azure/azure-service-operator/hack/generator/pkg/jsonast" + "github.com/Azure/azure-service-operator/hack/generator/pkg/test" ) type GoldenTestConfig struct { @@ -61,22 +63,16 @@ func loadTestConfig(path string) (GoldenTestConfig, error) { return result, nil } -var goModulePrefix = "github.com/Azure/azure-service-operator/testing" - -func makeTestLocalPackageReference(group string, version string) astmodel.LocalPackageReference { - return astmodel.MakeLocalPackageReference(goModulePrefix, group, version) -} - func makeEmbeddedTestTypeDefinition() astmodel.TypeDefinition { - name := astmodel.MakeTypeName(makeTestLocalPackageReference("test", "v1alpha1api20200101"), "EmbeddedTestType") + name := astmodel.MakeTypeName(test.MakeLocalPackageReference("test", "v1alpha1api20200101"), "EmbeddedTestType") t := astmodel.NewObjectType() t = t.WithProperty(astmodel.NewPropertyDefinition("FancyProp", "fancyProp", astmodel.IntType)) return astmodel.MakeTypeDefinition(name, t) } -func injectEmbeddedStructType() PipelineStage { - return MakePipelineStage( +func injectEmbeddedStructType() pipeline.Stage { + return pipeline.MakeStage( "injectEmbeddedStructType", "Injects an embedded struct into each object", func(ctx context.Context, defs astmodel.Types) (astmodel.Types, error) { @@ -108,16 +104,16 @@ func injectEmbeddedStructType() PipelineStage { } func runGoldenTest(t *testing.T, path string, testConfig GoldenTestConfig) { - for _, pipeline := range testConfig.Pipelines { + for _, p := range testConfig.Pipelines { testName := strings.TrimPrefix(t.Name(), "TestGolden/") // Append pipeline name at the end of file name if there is more than one pipeline under test if len(testConfig.Pipelines) > 1 { - testName = filepath.Join(filepath.Dir(testName), fmt.Sprintf("%s_%s", filepath.Base(testName), string(pipeline))) + testName = filepath.Join(filepath.Dir(testName), fmt.Sprintf("%s_%s", filepath.Base(testName), string(p))) } - t.Run(string(pipeline), func(t *testing.T) { - codegen, err := NewTestCodeGenerator(testName, path, t, testConfig, pipeline) + t.Run(string(p), func(t *testing.T) { + codegen, err := NewTestCodeGenerator(testName, path, t, testConfig, p) if err != nil { t.Fatalf("failed to create code generator: %v", err) } @@ -130,12 +126,12 @@ func runGoldenTest(t *testing.T, path string, testConfig GoldenTestConfig) { } } -func NewTestCodeGenerator(testName string, path string, t *testing.T, testConfig GoldenTestConfig, pipeline config.GenerationPipeline) (*CodeGenerator, error) { +func NewTestCodeGenerator(testName string, path string, t *testing.T, testConfig GoldenTestConfig, genPipeline config.GenerationPipeline) (*CodeGenerator, error) { idFactory := astmodel.NewIdentifierFactory() cfg := config.NewConfiguration() - cfg.GoModulePath = goModulePrefix + cfg.GoModulePath = test.GoModulePrefix - pipelineTarget, err := translatePipelineToTarget(pipeline) + pipelineTarget, err := pipeline.TranslatePipelineToTarget(genPipeline) if err != nil { return nil, err } @@ -146,7 +142,7 @@ func NewTestCodeGenerator(testName string, path string, t *testing.T, testConfig } // TODO: This isn't as clean as would be liked -- should we remove panic from RemoveStages? - switch pipeline { + switch genPipeline { case config.GenerationPipelineAzure: codegen.RemoveStages("deleteGenerated", "rogueCheck", "createStorage", "reportTypesAndVersions") if !testConfig.HasARMResources { @@ -167,7 +163,7 @@ func NewTestCodeGenerator(testName string, path string, t *testing.T, testConfig } default: - return nil, errors.Errorf("unknown pipeline kind %q", string(pipeline)) + return nil, errors.Errorf("unknown pipeline kind %q", string(genPipeline)) } codegen.ReplaceStage("loadSchema", loadTestSchemaIntoTypes(idFactory, cfg, path)) @@ -185,10 +181,10 @@ func NewTestCodeGenerator(testName string, path string, t *testing.T, testConfig func loadTestSchemaIntoTypes( idFactory astmodel.IdentifierFactory, configuration *config.Configuration, - path string) PipelineStage { + path string) pipeline.Stage { source := configuration.SchemaURL - return MakePipelineStage( + return pipeline.MakeStage( "loadTestSchema", "Load and walk schema (test)", func(ctx context.Context, types astmodel.Types) (astmodel.Types, error) { @@ -219,10 +215,10 @@ func loadTestSchemaIntoTypes( }) } -func exportPackagesTestPipelineStage(t *testing.T, testName string) PipelineStage { +func exportPackagesTestPipelineStage(t *testing.T, testName string) pipeline.Stage { g := goldie.New(t) - return MakePipelineStage( + return pipeline.MakeStage( "exportTestPackages", "Export packages for test", func(ctx context.Context, defs astmodel.Types) (astmodel.Types, error) { @@ -266,18 +262,18 @@ func exportPackagesTestPipelineStage(t *testing.T, testName string) PipelineStag }) } -func stripUnusedTypesPipelineStage() PipelineStage { - return MakePipelineStage( +func stripUnusedTypesPipelineStage() pipeline.Stage { + return pipeline.MakeStage( "stripUnused", "Strip unused types for test", func(ctx context.Context, defs astmodel.Types) (astmodel.Types, error) { // The golden files always generate a top-level Test type - mark // that as the root. roots := astmodel.NewTypeNameSet(astmodel.MakeTypeName( - makeTestLocalPackageReference("test", "v1alpha1api20200101"), + test.MakeLocalPackageReference("test", "v1alpha1api20200101"), "Test", )) - defs, err := StripUnusedDefinitions(roots, defs) + defs, err := pipeline.StripUnusedDefinitions(roots, defs) if err != nil { return nil, errors.Wrapf(err, "could not strip unused types") } @@ -287,18 +283,18 @@ func stripUnusedTypesPipelineStage() PipelineStage { } // TODO: Ideally we wouldn't need a test specific function here, but currently -// TODO: we're hardcoding references, and even if we were sourcing them from Swagger +// TODO: we're hard-coding references, and even if we were sourcing them from Swagger // TODO: we have no way to give Swagger to the golden files tests currently. -func addCrossResourceReferencesForTest(idFactory astmodel.IdentifierFactory) PipelineStage { - return MakePipelineStage( +func addCrossResourceReferencesForTest(idFactory astmodel.IdentifierFactory) pipeline.Stage { + return pipeline.MakeStage( "addCrossResourceReferences", "Add cross resource references for test", func(ctx context.Context, defs astmodel.Types) (astmodel.Types, error) { result := make(astmodel.Types) isCrossResourceReference := func(_ astmodel.TypeName, prop *astmodel.PropertyDefinition) bool { - return doesPropertyLookLikeARMReference(prop) + return pipeline.DoesPropertyLookLikeARMReference(prop) } - visitor := makeCrossResourceReferenceTypeVisitor(idFactory, isCrossResourceReference) + visitor := pipeline.MakeCrossResourceReferenceTypeVisitor(idFactory, isCrossResourceReference) for _, def := range defs { // Skip Status types diff --git a/hack/generator/pkg/codegen/pipeline_add_arm_conversion_interface.go b/hack/generator/pkg/codegen/pipeline/add_arm_conversion_interface.go similarity index 96% rename from hack/generator/pkg/codegen/pipeline_add_arm_conversion_interface.go rename to hack/generator/pkg/codegen/pipeline/add_arm_conversion_interface.go index a32024a7d58..0623599ca84 100644 --- a/hack/generator/pkg/codegen/pipeline_add_arm_conversion_interface.go +++ b/hack/generator/pkg/codegen/pipeline/add_arm_conversion_interface.go @@ -3,7 +3,7 @@ * Licensed under the MIT license. */ -package codegen +package pipeline import ( "context" @@ -15,13 +15,13 @@ import ( "github.com/Azure/azure-service-operator/hack/generator/pkg/astmodel/armconversion" ) -// applyARMConversionInterface adds the genruntime.ARMTransformer interface and the Owner property +// ApplyARMConversionInterface adds the genruntime.ARMTransformer interface and the Owner property // to all Kubernetes types. // The genruntime.ARMTransformer interface is used to convert from the Kubernetes type to the corresponding ARM type and back. -func applyARMConversionInterface(idFactory astmodel.IdentifierFactory) PipelineStage { - return MakePipelineStage( +func ApplyARMConversionInterface(idFactory astmodel.IdentifierFactory) Stage { + return MakeStage( "applyArmConversionInterface", - "Apply the ARM conversion interface to Kubernetes types", + "Add ARM conversion interfaces to Kubernetes types", func(ctx context.Context, definitions astmodel.Types) (astmodel.Types, error) { converter := &armConversionApplier{ definitions: definitions, diff --git a/hack/generator/pkg/codegen/pipeline_add_cross_resource_references.go b/hack/generator/pkg/codegen/pipeline/add_cross_resource_references.go similarity index 91% rename from hack/generator/pkg/codegen/pipeline_add_cross_resource_references.go rename to hack/generator/pkg/codegen/pipeline/add_cross_resource_references.go index 89526fa4549..98c41533608 100644 --- a/hack/generator/pkg/codegen/pipeline_add_cross_resource_references.go +++ b/hack/generator/pkg/codegen/pipeline/add_cross_resource_references.go @@ -3,7 +3,7 @@ * Licensed under the MIT license. */ -package codegen +package pipeline import ( "context" @@ -21,11 +21,11 @@ var armIDDescriptionRegex = regexp.MustCompile("(?i).*/subscriptions/.*?/resourc // TODO: For now not supporting array or map of references. Unsure if it actually ever happens in practice. -// addCrossResourceReferences replaces cross resource references with genruntime.ResourceReference. -func addCrossResourceReferences(configuration *config.Configuration, idFactory astmodel.IdentifierFactory) PipelineStage { - return MakePipelineStage( +// AddCrossResourceReferences replaces cross resource references with genruntime.ResourceReference. +func AddCrossResourceReferences(configuration *config.Configuration, idFactory astmodel.IdentifierFactory) Stage { + return MakeStage( "addCrossResourceReferences", - "Replaces cross resource references with genruntime.ResourceReference", + "Replace cross-resource references with genruntime.ResourceReference", func(ctx context.Context, definitions astmodel.Types) (astmodel.Types, error) { result := make(astmodel.Types) @@ -37,14 +37,14 @@ func addCrossResourceReferences(configuration *config.Configuration, idFactory a } _, isReference := knownReferences[ref] - if doesPropertyLookLikeARMReference(prop) && !isReference { + if DoesPropertyLookLikeARMReference(prop) && !isReference { klog.V(0).Infof("\"%s.%s\" looks like a resource reference but was not labelled as one", typeName, prop.PropertyName()) } return isReference } - visitor := makeCrossResourceReferenceTypeVisitor(idFactory, isCrossResourceReference) + visitor := MakeCrossResourceReferenceTypeVisitor(idFactory, isCrossResourceReference) for _, def := range definitions { // Skip Status types @@ -82,7 +82,7 @@ type crossResourceReferenceTypeVisitor struct { isPropertyAnARMReference crossResourceReferenceChecker } -func makeCrossResourceReferenceTypeVisitor(idFactory astmodel.IdentifierFactory, referenceChecker crossResourceReferenceChecker) crossResourceReferenceTypeVisitor { +func MakeCrossResourceReferenceTypeVisitor(idFactory astmodel.IdentifierFactory, referenceChecker crossResourceReferenceChecker) crossResourceReferenceTypeVisitor { visitor := crossResourceReferenceTypeVisitor{ isPropertyAnARMReference: referenceChecker, } @@ -117,9 +117,9 @@ func makeCrossResourceReferenceTypeVisitor(idFactory astmodel.IdentifierFactory, return visitor } -// doesPropertyLookLikeARMReference uses a simple heuristic to determine if a property looks like it might be an ARM reference. +// DoesPropertyLookLikeARMReference uses a simple heuristic to determine if a property looks like it might be an ARM reference. // This can be used for logging/reporting purposes to discover references which we missed. -func doesPropertyLookLikeARMReference(prop *astmodel.PropertyDefinition) bool { +func DoesPropertyLookLikeARMReference(prop *astmodel.PropertyDefinition) bool { // The property must be a string or optional string isString := prop.PropertyType().Equals(astmodel.StringType) isOptionalString := prop.PropertyType().Equals(astmodel.NewOptionalType(astmodel.StringType)) diff --git a/hack/generator/pkg/codegen/pipeline_apply_export_filters.go b/hack/generator/pkg/codegen/pipeline/apply_export_filters.go similarity index 84% rename from hack/generator/pkg/codegen/pipeline_apply_export_filters.go rename to hack/generator/pkg/codegen/pipeline/apply_export_filters.go index a949b88d91a..ce0024694cf 100644 --- a/hack/generator/pkg/codegen/pipeline_apply_export_filters.go +++ b/hack/generator/pkg/codegen/pipeline/apply_export_filters.go @@ -3,22 +3,23 @@ * Licensed under the MIT license. */ -package codegen +package pipeline import ( "context" "fmt" + "k8s.io/klog/v2" + "github.com/Azure/azure-service-operator/hack/generator/pkg/astmodel" "github.com/Azure/azure-service-operator/hack/generator/pkg/config" - "k8s.io/klog/v2" ) -// applyExportFilters creates a PipelineStage to reduce our set of types for export -func applyExportFilters(configuration *config.Configuration) PipelineStage { - return MakePipelineStage( +// ApplyExportFilters creates a Stage to reduce our set of types for export +func ApplyExportFilters(configuration *config.Configuration) Stage { + return MakeStage( "filterTypes", - "Filter generated types", + "Apply export filters to reduce the number of generated types", func(ctx context.Context, types astmodel.Types) (astmodel.Types, error) { return filterTypes(configuration, types) }) diff --git a/hack/generator/pkg/codegen/pipeline_apply_kubernetes_resource_interface.go b/hack/generator/pkg/codegen/pipeline/apply_kubernetes_resource_interface.go similarity index 84% rename from hack/generator/pkg/codegen/pipeline_apply_kubernetes_resource_interface.go rename to hack/generator/pkg/codegen/pipeline/apply_kubernetes_resource_interface.go index 3779bb5b191..f5369f20938 100644 --- a/hack/generator/pkg/codegen/pipeline_apply_kubernetes_resource_interface.go +++ b/hack/generator/pkg/codegen/pipeline/apply_kubernetes_resource_interface.go @@ -3,21 +3,22 @@ * Licensed under the MIT license. */ -package codegen +package pipeline import ( "context" - "github.com/Azure/azure-service-operator/hack/generator/pkg/astmodel" "github.com/pkg/errors" + + "github.com/Azure/azure-service-operator/hack/generator/pkg/astmodel" ) -// applyKubernetesResourceInterface ensures that every Resource implements the KubernetesResource interface -func applyKubernetesResourceInterface(idFactory astmodel.IdentifierFactory) PipelineStage { +// ApplyKubernetesResourceInterface ensures that every Resource implements the KubernetesResource interface +func ApplyKubernetesResourceInterface(idFactory astmodel.IdentifierFactory) Stage { - return MakePipelineStage( + return MakeStage( "applyKubernetesResourceInterface", - "Ensures that every resource implements the KubernetesResource interface", + "Add the KubernetesResource interface to every resource", func(ctx context.Context, types astmodel.Types) (astmodel.Types, error) { skip := make(map[astmodel.TypeName]struct{}) diff --git a/hack/generator/pkg/codegen/pipeline_apply_property_rewrites.go b/hack/generator/pkg/codegen/pipeline/apply_property_rewrites.go similarity index 83% rename from hack/generator/pkg/codegen/pipeline_apply_property_rewrites.go rename to hack/generator/pkg/codegen/pipeline/apply_property_rewrites.go index 6c2197cc690..d51f95e50d4 100644 --- a/hack/generator/pkg/codegen/pipeline_apply_property_rewrites.go +++ b/hack/generator/pkg/codegen/pipeline/apply_property_rewrites.go @@ -3,7 +3,7 @@ * Licensed under the MIT license. */ -package codegen +package pipeline import ( "context" @@ -14,14 +14,14 @@ import ( "github.com/Azure/azure-service-operator/hack/generator/pkg/config" ) -// applyPropertyRewrites applies any typeTransformers for properties. +// ApplyPropertyRewrites applies any typeTransformers for properties. // It is its own pipeline stage so that we can apply it after the allOf/oneOf types have // been "lowered" to objects. -func applyPropertyRewrites(config *config.Configuration) PipelineStage { +func ApplyPropertyRewrites(config *config.Configuration) Stage { - return MakePipelineStage( + return MakeStage( "propertyRewrites", - "Applying type transformers to properties", + "Modify property types using configured transforms", func(ctx context.Context, types astmodel.Types) (astmodel.Types, error) { newTypes := make(astmodel.Types, len(types)) diff --git a/hack/generator/pkg/codegen/pipeline_assert_types_collection_valid.go b/hack/generator/pkg/codegen/pipeline/assert_types_collection_valid.go similarity index 74% rename from hack/generator/pkg/codegen/pipeline_assert_types_collection_valid.go rename to hack/generator/pkg/codegen/pipeline/assert_types_collection_valid.go index 7015617ee6f..7e7649ae2f5 100644 --- a/hack/generator/pkg/codegen/pipeline_assert_types_collection_valid.go +++ b/hack/generator/pkg/codegen/pipeline/assert_types_collection_valid.go @@ -3,7 +3,7 @@ * Licensed under the MIT license. */ -package codegen +package pipeline import ( "context" @@ -11,13 +11,13 @@ import ( "github.com/Azure/azure-service-operator/hack/generator/pkg/astmodel" ) -// assertTypesCollectionValid creates a PipelineStage that ensures that each reachable type in the types collection +// AssertTypesCollectionValid creates a Stage that ensures that each reachable type in the types collection // has TypeName's that are all reachable as well. This check fails if there is any TypeName that refers to a type that doesn't // exist. -func assertTypesCollectionValid() PipelineStage { - return MakePipelineStage( +func AssertTypesCollectionValid() Stage { + return MakeStage( "assertTypesStructureValid", - "Asserts that the types collection is valid", + "Verify that all local TypeNames refer to a type", func(ctx context.Context, types astmodel.Types) (astmodel.Types, error) { visitor := astmodel.TypeVisitorBuilder{}.Build() typeWalker := astmodel.NewTypeWalker(types, visitor) diff --git a/hack/generator/pkg/codegen/pipeline_augment_status.go b/hack/generator/pkg/codegen/pipeline/augment_status.go similarity index 97% rename from hack/generator/pkg/codegen/pipeline_augment_status.go rename to hack/generator/pkg/codegen/pipeline/augment_status.go index 034f8fea7d8..ff843e58193 100644 --- a/hack/generator/pkg/codegen/pipeline_augment_status.go +++ b/hack/generator/pkg/codegen/pipeline/augment_status.go @@ -3,7 +3,7 @@ * Licensed under the MIT license. */ -package codegen +package pipeline import ( "context" @@ -18,16 +18,17 @@ import ( kerrors "k8s.io/apimachinery/pkg/util/errors" - "github.com/Azure/azure-service-operator/hack/generator/pkg/astmodel" - "github.com/Azure/azure-service-operator/hack/generator/pkg/config" - "github.com/Azure/azure-service-operator/hack/generator/pkg/jsonast" "github.com/go-openapi/spec" "github.com/pkg/errors" "golang.org/x/sync/errgroup" "k8s.io/klog/v2" + + "github.com/Azure/azure-service-operator/hack/generator/pkg/astmodel" + "github.com/Azure/azure-service-operator/hack/generator/pkg/config" + "github.com/Azure/azure-service-operator/hack/generator/pkg/jsonast" ) -/* augmentResourcesWithStatus creates a PipelineStage to add status information into the generated resources. +/* AugmentResourcesWithStatus creates a Stage to add status information into the generated resources. This information is derived from the Azure Swagger specifications. We parse the Swagger specs and look for any actions that appear to be ARM resources (have PUT methods with types we can use and appropriate names in the @@ -40,8 +41,8 @@ added to the Status field of the Resource type, after we have renamed all the st avoid any conflicts with existing Spec types that have already been defined. */ -func augmentResourcesWithStatus(idFactory astmodel.IdentifierFactory, config *config.Configuration) PipelineStage { - return MakePipelineStage( +func AugmentResourcesWithStatus(idFactory astmodel.IdentifierFactory, config *config.Configuration) Stage { + return MakeStage( "augmentStatus", "Add information from Swagger specs for 'status' fields", func(ctx context.Context, types astmodel.Types) (astmodel.Types, error) { diff --git a/hack/generator/pkg/codegen/pipeline_augment_status_test.go b/hack/generator/pkg/codegen/pipeline/augment_status_test.go similarity index 98% rename from hack/generator/pkg/codegen/pipeline_augment_status_test.go rename to hack/generator/pkg/codegen/pipeline/augment_status_test.go index ad8a866300e..26294004010 100644 --- a/hack/generator/pkg/codegen/pipeline_augment_status_test.go +++ b/hack/generator/pkg/codegen/pipeline/augment_status_test.go @@ -3,12 +3,13 @@ * Licensed under the MIT license. */ -package codegen +package pipeline import ( - . "github.com/onsi/gomega" "runtime" "testing" + + . "github.com/onsi/gomega" ) func Test_ShouldSkipDir_GivenPath_HasExpectedResult(t *testing.T) { diff --git a/hack/generator/pkg/codegen/pipeline_check_for_anytype.go b/hack/generator/pkg/codegen/pipeline/check_for_anytype.go similarity index 90% rename from hack/generator/pkg/codegen/pipeline_check_for_anytype.go rename to hack/generator/pkg/codegen/pipeline/check_for_anytype.go index 38144f82ebe..644bfc4542f 100644 --- a/hack/generator/pkg/codegen/pipeline_check_for_anytype.go +++ b/hack/generator/pkg/codegen/pipeline/check_for_anytype.go @@ -3,7 +3,7 @@ * Licensed under the MIT license. */ -package codegen +package pipeline import ( "context" @@ -16,31 +16,31 @@ import ( "github.com/Azure/azure-service-operator/hack/generator/pkg/astmodel" ) -// filterOutDefinitionsUsingAnyType returns a stage that will check for any definitions +// FilterOutDefinitionsUsingAnyType returns a stage that will check for any definitions // containing AnyTypes. It accepts a set of packages that we expect to contain types // with AnyTypes. Those packages will be quietly filtered out of the output of the // stage, but if there are more AnyTypes in other packages they'll be reported as an // error. The stage will also return an error if there are packages that we expect // to have AnyTypes but turn out not to, ensuring that we clean up our configuration // as the schemas are fixed and our handling improves. -func filterOutDefinitionsUsingAnyType(packages []string) PipelineStage { +func FilterOutDefinitionsUsingAnyType(packages []string) Stage { return checkForAnyType("Filter out rogue definitions using AnyTypes", packages) } // ensureDefinitionsDoNotUseAnyTypes returns a stage that will check for any // definitions containing AnyTypes. The stage will return errors for each type // found that uses an AnyType. -func ensureDefinitionsDoNotUseAnyTypes() PipelineStage { - return checkForAnyType("Catch rogue definitions using AnyTypes", []string{}) +func EnsureDefinitionsDoNotUseAnyTypes() Stage { + return checkForAnyType("Check for rogue definitions using AnyTypes", []string{}) } -func checkForAnyType(description string, packages []string) PipelineStage { +func checkForAnyType(description string, packages []string) Stage { expectedPackages := make(map[string]struct{}, len(packages)) for _, p := range packages { expectedPackages[p] = struct{}{} } - return MakePipelineStage( + return MakeStage( "rogueCheck", description, func(ctx context.Context, defs astmodel.Types) (astmodel.Types, error) { diff --git a/hack/generator/pkg/codegen/pipeline_check_for_anytype_test.go b/hack/generator/pkg/codegen/pipeline/check_for_anytype_test.go similarity index 77% rename from hack/generator/pkg/codegen/pipeline_check_for_anytype_test.go rename to hack/generator/pkg/codegen/pipeline/check_for_anytype_test.go index 6dd14e259ab..21e84c6977d 100644 --- a/hack/generator/pkg/codegen/pipeline_check_for_anytype_test.go +++ b/hack/generator/pkg/codegen/pipeline/check_for_anytype_test.go @@ -3,23 +3,25 @@ * Licensed under the MIT license. */ -package codegen +package pipeline import ( "context" "testing" "github.com/Azure/azure-service-operator/hack/generator/pkg/astmodel" + "github.com/Azure/azure-service-operator/hack/generator/pkg/test" + "github.com/pkg/errors" . "github.com/onsi/gomega" ) -func TestFindsAnytypes(t *testing.T) { +func TestFindsAnyTypes(t *testing.T) { g := NewGomegaWithT(t) - p1 := makeTestLocalPackageReference("horo.logy", "v20200730") - p2 := makeTestLocalPackageReference("road.train", "v20200730") - p3 := makeTestLocalPackageReference("wah.wah", "v20200730") + p1 := test.MakeLocalPackageReference("horo.logy", "v20200730") + p2 := test.MakeLocalPackageReference("road.train", "v20200730") + p3 := test.MakeLocalPackageReference("wah.wah", "v20200730") defs := make(astmodel.Types) add := func(p astmodel.PackageReference, n string, t astmodel.Type) { @@ -36,7 +38,7 @@ func TestFindsAnytypes(t *testing.T) { // One that's fine. add(p3, "C", astmodel.NewArrayType(astmodel.IntType)) - results, err := filterOutDefinitionsUsingAnyType(nil).action(context.Background(), defs) + results, err := FilterOutDefinitionsUsingAnyType(nil).action(context.Background(), defs) g.Expect(results).To(HaveLen(0)) g.Expect(err).To(MatchError("AnyTypes found - add exclusions for: horo.logy/v20200730, road.train/v20200730")) @@ -44,9 +46,9 @@ func TestFindsAnytypes(t *testing.T) { func TestIgnoresExpectedAnyTypePackages(t *testing.T) { g := NewGomegaWithT(t) - p1 := makeTestLocalPackageReference("horo.logy", "v20200730") - p2 := makeTestLocalPackageReference("road.train", "v20200730") - p3 := makeTestLocalPackageReference("wah.wah", "v20200730") + p1 := test.MakeLocalPackageReference("horo.logy", "v20200730") + p2 := test.MakeLocalPackageReference("road.train", "v20200730") + p3 := test.MakeLocalPackageReference("wah.wah", "v20200730") defs := make(astmodel.Types) add := func(p astmodel.PackageReference, n string, t astmodel.Type) { @@ -64,7 +66,7 @@ func TestIgnoresExpectedAnyTypePackages(t *testing.T) { add(p3, "C", astmodel.NewArrayType(astmodel.IntType)) exclusions := []string{"horo.logy/v20200730", "road.train/v20200730"} - results, err := filterOutDefinitionsUsingAnyType(exclusions).action(context.Background(), defs) + results, err := FilterOutDefinitionsUsingAnyType(exclusions).action(context.Background(), defs) g.Expect(err).To(BeNil()) expected := make(astmodel.Types) @@ -76,9 +78,9 @@ func TestIgnoresExpectedAnyTypePackages(t *testing.T) { func TestComplainsAboutUnneededExclusions(t *testing.T) { g := NewGomegaWithT(t) - p1 := makeTestLocalPackageReference("horo.logy", "v20200730") - p2 := makeTestLocalPackageReference("road.train", "v20200730") - p3 := makeTestLocalPackageReference("wah.wah", "v20200730") + p1 := test.MakeLocalPackageReference("horo.logy", "v20200730") + p2 := test.MakeLocalPackageReference("road.train", "v20200730") + p3 := test.MakeLocalPackageReference("wah.wah", "v20200730") defs := make(astmodel.Types) add := func(p astmodel.PackageReference, n string, t astmodel.Type) { @@ -101,7 +103,7 @@ func TestComplainsAboutUnneededExclusions(t *testing.T) { "gamma.knife/v20200821", "road.train/v20200730", } - results, err := filterOutDefinitionsUsingAnyType(exclusions).action(context.Background(), defs) + results, err := FilterOutDefinitionsUsingAnyType(exclusions).action(context.Background(), defs) g.Expect(results).To(HaveLen(0)) g.Expect(errors.Cause(err)).To(MatchError("no AnyTypes found in: gamma.knife/v20200821, people.vultures/20200821")) } diff --git a/hack/generator/pkg/codegen/pipeline_collapse_cross_group_refs.go b/hack/generator/pkg/codegen/pipeline/collapse_cross_group_refs.go similarity index 92% rename from hack/generator/pkg/codegen/pipeline_collapse_cross_group_refs.go rename to hack/generator/pkg/codegen/pipeline/collapse_cross_group_refs.go index 9db587777b4..30189e1e29c 100644 --- a/hack/generator/pkg/codegen/pipeline_collapse_cross_group_refs.go +++ b/hack/generator/pkg/codegen/pipeline/collapse_cross_group_refs.go @@ -3,19 +3,20 @@ * Licensed under the MIT license. */ -package codegen +package pipeline import ( "context" - "github.com/Azure/azure-service-operator/hack/generator/pkg/astmodel" "github.com/pkg/errors" + + "github.com/Azure/azure-service-operator/hack/generator/pkg/astmodel" ) -// collapseCrossGroupReferences finds and removes references between API groups. This isn't particularly common +// CollapseCrossGroupReferences finds and removes references between API groups. This isn't particularly common // but does occur in a few instances, for example from Microsoft.Compute -> Microsoft.Compute.Extensions. -func collapseCrossGroupReferences() PipelineStage { - return MakePipelineStage( +func CollapseCrossGroupReferences() Stage { + return MakeStage( "collapseCrossGroupReferences", "Finds and removes cross group references", func(ctx context.Context, types astmodel.Types) (astmodel.Types, error) { diff --git a/hack/generator/pkg/codegen/pipeline_convert_allof_and_oneof_to_objects.go b/hack/generator/pkg/codegen/pipeline/convert_allof_and_oneof_to_objects.go similarity index 98% rename from hack/generator/pkg/codegen/pipeline_convert_allof_and_oneof_to_objects.go rename to hack/generator/pkg/codegen/pipeline/convert_allof_and_oneof_to_objects.go index d0d1d8ecb3c..c8a082493d6 100644 --- a/hack/generator/pkg/codegen/pipeline_convert_allof_and_oneof_to_objects.go +++ b/hack/generator/pkg/codegen/pipeline/convert_allof_and_oneof_to_objects.go @@ -3,16 +3,17 @@ * Licensed under the MIT license. */ -package codegen +package pipeline import ( "context" "fmt" "strings" - "github.com/Azure/azure-service-operator/hack/generator/pkg/astmodel" "github.com/pkg/errors" "k8s.io/klog/v2" + + "github.com/Azure/azure-service-operator/hack/generator/pkg/astmodel" ) // This is needed when we are processing a Resource that happens to be nested inside @@ -27,9 +28,9 @@ var ( chooseStatus resourceFieldSelector = "Status" ) -// convertAllOfAndOneOfToObjects reduces the AllOfType and OneOfType to ObjectType -func convertAllOfAndOneOfToObjects(idFactory astmodel.IdentifierFactory) PipelineStage { - return MakePipelineStage( +// ConvertAllOfAndOneOfToObjects reduces the AllOfType and OneOfType to ObjectType +func ConvertAllOfAndOneOfToObjects(idFactory astmodel.IdentifierFactory) Stage { + return MakeStage( "allof-anyof-objects", "Convert allOf and oneOf to object types", func(ctx context.Context, defs astmodel.Types) (astmodel.Types, error) { diff --git a/hack/generator/pkg/codegen/pipeline_convert_allof_and_oneof_to_objects_test.go b/hack/generator/pkg/codegen/pipeline/convert_allof_and_oneof_to_objects_test.go similarity index 99% rename from hack/generator/pkg/codegen/pipeline_convert_allof_and_oneof_to_objects_test.go rename to hack/generator/pkg/codegen/pipeline/convert_allof_and_oneof_to_objects_test.go index c09cc67f870..1e938cb9f18 100644 --- a/hack/generator/pkg/codegen/pipeline_convert_allof_and_oneof_to_objects_test.go +++ b/hack/generator/pkg/codegen/pipeline/convert_allof_and_oneof_to_objects_test.go @@ -3,7 +3,7 @@ * Licensed under the MIT license. */ -package codegen +package pipeline import ( "testing" diff --git a/hack/generator/pkg/codegen/pipeline_create_arm_types.go b/hack/generator/pkg/codegen/pipeline/create_arm_types.go similarity index 97% rename from hack/generator/pkg/codegen/pipeline_create_arm_types.go rename to hack/generator/pkg/codegen/pipeline/create_arm_types.go index 3b529df94f0..43d0ef5a828 100644 --- a/hack/generator/pkg/codegen/pipeline_create_arm_types.go +++ b/hack/generator/pkg/codegen/pipeline/create_arm_types.go @@ -3,7 +3,7 @@ * Licensed under the MIT license. */ -package codegen +package pipeline import ( "context" @@ -15,12 +15,12 @@ import ( "github.com/Azure/azure-service-operator/hack/generator/pkg/astmodel" ) -// createARMTypes walks the type graph and builds new types for communicating +// CreateARMTypes walks the type graph and builds new types for communicating // with ARM -func createARMTypes(idFactory astmodel.IdentifierFactory) PipelineStage { - return MakePipelineStage( +func CreateARMTypes(idFactory astmodel.IdentifierFactory) Stage { + return MakeStage( "createArmTypes", - "Creates ARM types", + "Create types for interaction with ARM", func(ctx context.Context, definitions astmodel.Types) (astmodel.Types, error) { armTypeCreator := &armTypeCreator{definitions: definitions, idFactory: idFactory} diff --git a/hack/generator/pkg/codegen/pipeline_create_storage_types.go b/hack/generator/pkg/codegen/pipeline/create_storage_types.go similarity index 91% rename from hack/generator/pkg/codegen/pipeline_create_storage_types.go rename to hack/generator/pkg/codegen/pipeline/create_storage_types.go index 2ebb33b24fc..96ba2a05db0 100644 --- a/hack/generator/pkg/codegen/pipeline_create_storage_types.go +++ b/hack/generator/pkg/codegen/pipeline/create_storage_types.go @@ -3,21 +3,23 @@ * Licensed under the MIT license. */ -package codegen +package pipeline import ( "context" - "github.com/Azure/azure-service-operator/hack/generator/pkg/astmodel" - "github.com/Azure/azure-service-operator/hack/generator/pkg/codegen/storage" + kerrors "k8s.io/apimachinery/pkg/util/errors" "k8s.io/klog/v2" + + "github.com/Azure/azure-service-operator/hack/generator/pkg/astmodel" + "github.com/Azure/azure-service-operator/hack/generator/pkg/codegen/storage" ) -// createStorageTypes returns a pipeline stage that creates dedicated storage types for each resource and nested object. +// CreateStorageTypes returns a pipeline stage that creates dedicated storage types for each resource and nested object. // Storage versions are created for *all* API versions to allow users of older versions of the operator to easily // upgrade. This is of course a bit odd for the first release, but defining the approach from day one is useful. -func createStorageTypes(idFactory astmodel.IdentifierFactory) PipelineStage { - return MakePipelineStage( +func CreateStorageTypes(idFactory astmodel.IdentifierFactory) Stage { + return MakeStage( "createStorage", "Create storage versions of CRD types", func(ctx context.Context, types astmodel.Types) (astmodel.Types, error) { diff --git a/hack/generator/pkg/codegen/crossplane_pipeline_add_at_provider.go b/hack/generator/pkg/codegen/pipeline/crossplane_add_at_provider.go similarity index 90% rename from hack/generator/pkg/codegen/crossplane_pipeline_add_at_provider.go rename to hack/generator/pkg/codegen/pipeline/crossplane_add_at_provider.go index 16be6360bb8..5ffc63f25ea 100644 --- a/hack/generator/pkg/codegen/crossplane_pipeline_add_at_provider.go +++ b/hack/generator/pkg/codegen/pipeline/crossplane_add_at_provider.go @@ -3,7 +3,7 @@ * Licensed under the MIT license. */ -package codegen +package pipeline import ( "context" @@ -14,12 +14,12 @@ import ( "github.com/Azure/azure-service-operator/hack/generator/pkg/astmodel" ) -// addCrossplaneAtProvider adds an "AtProvider" property as the sole property in every resource status -func addCrossplaneAtProvider(idFactory astmodel.IdentifierFactory) PipelineStage { +// AddCrossplaneAtProvider adds an "AtProvider" property as the sole property in every resource status +func AddCrossplaneAtProvider(idFactory astmodel.IdentifierFactory) Stage { - return MakePipelineStage( + return MakeStage( "addCrossplaneAtProviderProperty", - "Adds an 'AtProvider' property on every status", + "Add an 'AtProvider' property on every status", func(ctx context.Context, types astmodel.Types) (astmodel.Types, error) { result := make(astmodel.Types) diff --git a/hack/generator/pkg/codegen/crossplane_pipeline_add_embedded_resource_spec.go b/hack/generator/pkg/codegen/pipeline/crossplane_add_embedded_resource_spec.go similarity index 85% rename from hack/generator/pkg/codegen/crossplane_pipeline_add_embedded_resource_spec.go rename to hack/generator/pkg/codegen/pipeline/crossplane_add_embedded_resource_spec.go index 17409b5119d..2eab407b958 100644 --- a/hack/generator/pkg/codegen/crossplane_pipeline_add_embedded_resource_spec.go +++ b/hack/generator/pkg/codegen/pipeline/crossplane_add_embedded_resource_spec.go @@ -3,7 +3,7 @@ * Licensed under the MIT license. */ -package codegen +package pipeline import ( "context" @@ -15,12 +15,12 @@ import ( var CrossplaneRuntimeV1Alpha1Package = astmodel.MakeExternalPackageReference("github.com/crossplane/crossplane-runtime/apis/core/v1alpha1") -// addCrossplaneEmbeddedResourceSpec puts an embedded runtimev1alpha1.ResourceSpec on every spec type -func addCrossplaneEmbeddedResourceSpec(idFactory astmodel.IdentifierFactory) PipelineStage { +// AddCrossplaneEmbeddedResourceSpec puts an embedded runtimev1alpha1.ResourceSpec on every spec type +func AddCrossplaneEmbeddedResourceSpec(idFactory astmodel.IdentifierFactory) Stage { - return MakePipelineStage( + return MakeStage( "addCrossplaneEmbeddedResourceSpec", - "Adds an embedded runtimev1alpha1.ResourceSpec to every spec type", + "Add an embedded runtimev1alpha1.ResourceSpec to every spec type", func(ctx context.Context, types astmodel.Types) (astmodel.Types, error) { specTypeName := astmodel.MakeTypeName( CrossplaneRuntimeV1Alpha1Package, diff --git a/hack/generator/pkg/codegen/crossplane_pipeline_add_embedded_resource_status.go b/hack/generator/pkg/codegen/pipeline/crossplane_add_embedded_resource_status.go similarity index 86% rename from hack/generator/pkg/codegen/crossplane_pipeline_add_embedded_resource_status.go rename to hack/generator/pkg/codegen/pipeline/crossplane_add_embedded_resource_status.go index ba028ba4e3e..11b3b321b86 100644 --- a/hack/generator/pkg/codegen/crossplane_pipeline_add_embedded_resource_status.go +++ b/hack/generator/pkg/codegen/pipeline/crossplane_add_embedded_resource_status.go @@ -3,7 +3,7 @@ * Licensed under the MIT license. */ -package codegen +package pipeline import ( "context" @@ -13,12 +13,12 @@ import ( "github.com/Azure/azure-service-operator/hack/generator/pkg/astmodel" ) -// addCrossplaneEmbeddedResourceStatus puts an embedded runtimev1alpha1.ResourceStatus on every spec type -func addCrossplaneEmbeddedResourceStatus(idFactory astmodel.IdentifierFactory) PipelineStage { +// AddCrossplaneEmbeddedResourceStatus puts an embedded runtimev1alpha1.ResourceStatus on every spec type +func AddCrossplaneEmbeddedResourceStatus(idFactory astmodel.IdentifierFactory) Stage { - return MakePipelineStage( + return MakeStage( "addCrossplaneEmbeddedResourceStatus", - "Adds an embedded runtimev1alpha1.ResourceStatus to every status type", + "Add an embedded runtimev1alpha1.ResourceStatus to every status type", func(ctx context.Context, types astmodel.Types) (astmodel.Types, error) { statusTypeName := astmodel.MakeTypeName( CrossplaneRuntimeV1Alpha1Package, diff --git a/hack/generator/pkg/codegen/crossplane_pipeline_add_for_provider.go b/hack/generator/pkg/codegen/pipeline/crossplane_add_for_provider.go similarity index 93% rename from hack/generator/pkg/codegen/crossplane_pipeline_add_for_provider.go rename to hack/generator/pkg/codegen/pipeline/crossplane_add_for_provider.go index 03fb8e7627f..fc14dc501fb 100644 --- a/hack/generator/pkg/codegen/crossplane_pipeline_add_for_provider.go +++ b/hack/generator/pkg/codegen/pipeline/crossplane_add_for_provider.go @@ -3,7 +3,7 @@ * Licensed under the MIT license. */ -package codegen +package pipeline import ( "context" @@ -14,13 +14,13 @@ import ( "github.com/Azure/azure-service-operator/hack/generator/pkg/astmodel" ) -// addCrossplaneForProvider adds a "ForProvider" property as the sole property in every resource spec +// AddCrossplaneForProvider adds a "ForProvider" property as the sole property in every resource spec // and moves everything that was at the spec level down a level into the ForProvider type -func addCrossplaneForProvider(idFactory astmodel.IdentifierFactory) PipelineStage { +func AddCrossplaneForProvider(idFactory astmodel.IdentifierFactory) Stage { - return MakePipelineStage( + return MakeStage( "addCrossplaneForProviderProperty", - "Adds a 'ForProvider' property on every spec", + "Add a 'ForProvider' property on every spec", func(ctx context.Context, types astmodel.Types) (astmodel.Types, error) { result := make(astmodel.Types) diff --git a/hack/generator/pkg/codegen/crossplane_pipeline_add_owner_properties.go b/hack/generator/pkg/codegen/pipeline/crossplane_add_owner_properties.go similarity index 93% rename from hack/generator/pkg/codegen/crossplane_pipeline_add_owner_properties.go rename to hack/generator/pkg/codegen/pipeline/crossplane_add_owner_properties.go index 60d17a7145a..020a21311ad 100644 --- a/hack/generator/pkg/codegen/crossplane_pipeline_add_owner_properties.go +++ b/hack/generator/pkg/codegen/pipeline/crossplane_add_owner_properties.go @@ -3,7 +3,7 @@ * Licensed under the MIT license. */ -package codegen +package pipeline import ( "context" @@ -14,12 +14,12 @@ import ( "github.com/Azure/azure-service-operator/hack/generator/pkg/astmodel" ) -// addCrossplaneOwnerProperties adds the 3-tuple of (xName, xNameRef, xNameSelector) for each owning resource -func addCrossplaneOwnerProperties(idFactory astmodel.IdentifierFactory) PipelineStage { +// AddCrossplaneOwnerProperties adds the 3-tuple of (xName, xNameRef, xNameSelector) for each owning resource +func AddCrossplaneOwnerProperties(idFactory astmodel.IdentifierFactory) Stage { - return MakePipelineStage( + return MakeStage( "addCrossplaneOwnerProperties", - "Adds the 3-tuple of (xName, xNameRef, xNameSelector) for each owning resource", + "Add the 3-tuple of (xName, xNameRef, xNameSelector) for each owning resource", func(ctx context.Context, types astmodel.Types) (astmodel.Types, error) { referenceTypeName := astmodel.MakeTypeName( CrossplaneRuntimeV1Alpha1Package, diff --git a/hack/generator/pkg/codegen/pipeline_delete_generated_code.go b/hack/generator/pkg/codegen/pipeline/delete_generated_code.go similarity index 95% rename from hack/generator/pkg/codegen/pipeline_delete_generated_code.go rename to hack/generator/pkg/codegen/pipeline/delete_generated_code.go index 8032c7ff014..5b205d6d980 100644 --- a/hack/generator/pkg/codegen/pipeline_delete_generated_code.go +++ b/hack/generator/pkg/codegen/pipeline/delete_generated_code.go @@ -3,7 +3,7 @@ * Licensed under the MIT license. */ -package codegen +package pipeline import ( "bufio" @@ -23,9 +23,9 @@ import ( "github.com/Azure/azure-service-operator/hack/generator/pkg/astmodel" ) -// deleteGeneratedCode creates a pipeline stage for cleanup of our output folder prior to generating files -func deleteGeneratedCode(outputFolder string) PipelineStage { - return MakePipelineStage( +// DeleteGeneratedCode creates a pipeline stage for cleanup of our output folder prior to generating files +func DeleteGeneratedCode(outputFolder string) Stage { + return MakeStage( "deleteGenerated", "Delete generated code from "+outputFolder, func(ctx context.Context, types astmodel.Types) (astmodel.Types, error) { diff --git a/hack/generator/pkg/codegen/pipeline_determine_resource_ownership.go b/hack/generator/pkg/codegen/pipeline/determine_resource_ownership.go similarity index 98% rename from hack/generator/pkg/codegen/pipeline_determine_resource_ownership.go rename to hack/generator/pkg/codegen/pipeline/determine_resource_ownership.go index 3a16ac12754..5568d7bc09e 100644 --- a/hack/generator/pkg/codegen/pipeline_determine_resource_ownership.go +++ b/hack/generator/pkg/codegen/pipeline/determine_resource_ownership.go @@ -3,7 +3,7 @@ * Licensed under the MIT license. */ -package codegen +package pipeline import ( "context" @@ -17,8 +17,8 @@ import ( const resourcesPropertyName = astmodel.PropertyName("Resources") -func determineResourceOwnership(configuration *config.Configuration) PipelineStage { - return MakePipelineStage( +func DetermineResourceOwnership(configuration *config.Configuration) Stage { + return MakeStage( "determineResourceOwnership", "Determine ARM resource relationships", func(ctx context.Context, types astmodel.Types) (astmodel.Types, error) { diff --git a/hack/generator/pkg/codegen/pipeline_ensure_type_has_arm_type.go b/hack/generator/pkg/codegen/pipeline/ensure_type_has_arm_type.go similarity index 88% rename from hack/generator/pkg/codegen/pipeline_ensure_type_has_arm_type.go rename to hack/generator/pkg/codegen/pipeline/ensure_type_has_arm_type.go index 47b911a566e..9fd74da6ca0 100644 --- a/hack/generator/pkg/codegen/pipeline_ensure_type_has_arm_type.go +++ b/hack/generator/pkg/codegen/pipeline/ensure_type_has_arm_type.go @@ -3,7 +3,7 @@ * Licensed under the MIT license. */ -package codegen +package pipeline import ( "context" @@ -15,11 +15,11 @@ import ( ) // TODO: Wondering if we should have an even stronger version of this that asserts it for all types rather than just the top level? -// ensureARMTypeExistsForEveryResource performs a check ensuring that every Kubernetes resource spec/status has a corresponding ARM type -func ensureARMTypeExistsForEveryResource() PipelineStage { - return MakePipelineStage( +// EnsureARMTypeExistsForEveryResource performs a check ensuring that every Kubernetes resource spec/status has a corresponding ARM type +func EnsureARMTypeExistsForEveryResource() Stage { + return MakeStage( "ensureArmTypeExistsForEveryType", - "Ensure that an ARM type for every top level resource spec/status exists", + "Check that an ARM type exists for both Spec and Status of each resource", func(ctx context.Context, types astmodel.Types) (astmodel.Types, error) { return types, validateExpectedTypesHaveARMType(types) }) diff --git a/hack/generator/pkg/codegen/pipeline_export_controller_type_registrations.go b/hack/generator/pkg/codegen/pipeline/export_controller_type_registrations.go similarity index 80% rename from hack/generator/pkg/codegen/pipeline_export_controller_type_registrations.go rename to hack/generator/pkg/codegen/pipeline/export_controller_type_registrations.go index 48cef4e73b0..aed0b563081 100644 --- a/hack/generator/pkg/codegen/pipeline_export_controller_type_registrations.go +++ b/hack/generator/pkg/codegen/pipeline/export_controller_type_registrations.go @@ -3,7 +3,7 @@ * Licensed under the MIT license. */ -package codegen +package pipeline import ( "context" @@ -14,13 +14,12 @@ import ( "github.com/Azure/azure-service-operator/hack/generator/pkg/astmodel" ) -// exportControllerResourceRegistrations creates a PipelineStage to generate type registrations +// ExportControllerResourceRegistrations creates a Stage to generate type registrations // for resources. -func exportControllerResourceRegistrations(outputPath string) PipelineStage { - description := fmt.Sprintf("Export resource registrations to %q", outputPath) - return MakePipelineStage( +func ExportControllerResourceRegistrations(outputPath string) Stage { + return MakeStage( "exportControllerResourceRegistrations", - description, + fmt.Sprintf("Export resource registrations to %q", outputPath), func(ctx context.Context, types astmodel.Types) (astmodel.Types, error) { // If the configuration doesn't specify an output destination for us, just do nothing diff --git a/hack/generator/pkg/codegen/pipeline_export_generated_code.go b/hack/generator/pkg/codegen/pipeline/export_generated_code.go similarity index 93% rename from hack/generator/pkg/codegen/pipeline_export_generated_code.go rename to hack/generator/pkg/codegen/pipeline/export_generated_code.go index dd48d124d3c..6e770f948e6 100644 --- a/hack/generator/pkg/codegen/pipeline_export_generated_code.go +++ b/hack/generator/pkg/codegen/pipeline/export_generated_code.go @@ -3,7 +3,7 @@ * Licensed under the MIT license. */ -package codegen +package pipeline import ( "context" @@ -14,16 +14,17 @@ import ( "sync" "time" - "github.com/Azure/azure-service-operator/hack/generator/pkg/astmodel" "github.com/pkg/errors" kerrors "k8s.io/apimachinery/pkg/util/errors" "k8s.io/klog/v2" + + "github.com/Azure/azure-service-operator/hack/generator/pkg/astmodel" ) -// exportPackages creates a PipelineStage to export our generated code as a set of packages -func exportPackages(outputPath string) PipelineStage { +// ExportPackages creates a Stage to export our generated code as a set of packages +func ExportPackages(outputPath string) Stage { description := fmt.Sprintf("Export packages to %q", outputPath) - return MakePipelineStage( + return MakeStage( "exportPackages", description, func(ctx context.Context, types astmodel.Types) (astmodel.Types, error) { @@ -44,7 +45,7 @@ func exportPackages(outputPath string) PipelineStage { // CreatePackagesForDefinitions groups type definitions into packages func CreatePackagesForDefinitions(definitions astmodel.Types) (map[astmodel.PackageReference]*astmodel.PackageDefinition, error) { - genVersion := combinedVersion() + genVersion := CombinedVersion() packages := make(map[astmodel.PackageReference]*astmodel.PackageDefinition) for _, def := range definitions { defName := def.Name() @@ -178,7 +179,7 @@ type progressMeter struct { mutex sync.Mutex } -// Log() writes a log message for our progress to this point +// Log writes a log message for our progress to this point func (export *progressMeter) Log() { started := export.resetAt export.resetAt = time.Now() @@ -197,7 +198,7 @@ func (export *progressMeter) Log() { export.resetAt = time.Now() } -// LogProgress() accumulates totals until a new label is supplied, when it will write a log message +// LogProgress accumulates totals until a new label is supplied, when it will write a log message func (export *progressMeter) LogProgress(label string, definitions int, files int) { export.mutex.Lock() defer export.mutex.Unlock() diff --git a/hack/generator/pkg/codegen/pipeline_flatten_resources.go b/hack/generator/pkg/codegen/pipeline/flatten_resources.go similarity index 92% rename from hack/generator/pkg/codegen/pipeline_flatten_resources.go rename to hack/generator/pkg/codegen/pipeline/flatten_resources.go index b2ed39c12f2..a60fad88c4f 100644 --- a/hack/generator/pkg/codegen/pipeline_flatten_resources.go +++ b/hack/generator/pkg/codegen/pipeline/flatten_resources.go @@ -3,19 +3,20 @@ * Licensed under the MIT license. */ -package codegen +package pipeline import ( "context" - "github.com/Azure/azure-service-operator/hack/generator/pkg/astmodel" "github.com/pkg/errors" + + "github.com/Azure/azure-service-operator/hack/generator/pkg/astmodel" ) -// flattenResources flattens any resources directly inside other resources -func flattenResources() PipelineStage { - return MakePipelineStage( - "flatten-resources", +// FlattenResources flattens any resources directly inside other resources +func FlattenResources() Stage { + return MakeStage( + "flattenResources", "Flatten nested resource types", func(ctx context.Context, defs astmodel.Types) (astmodel.Types, error) { diff --git a/hack/generator/pkg/codegen/pipeline_improve_resource_pluralization.go b/hack/generator/pkg/codegen/pipeline/improve_resource_pluralization.go similarity index 93% rename from hack/generator/pkg/codegen/pipeline_improve_resource_pluralization.go rename to hack/generator/pkg/codegen/pipeline/improve_resource_pluralization.go index 48898cca5af..8d5e5ca1cd1 100644 --- a/hack/generator/pkg/codegen/pipeline_improve_resource_pluralization.go +++ b/hack/generator/pkg/codegen/pipeline/improve_resource_pluralization.go @@ -3,7 +3,7 @@ * Licensed under the MIT license. */ -package codegen +package pipeline import ( "context" @@ -11,10 +11,10 @@ import ( "github.com/Azure/azure-service-operator/hack/generator/pkg/astmodel" ) -// improveResourcePluralization improves pluralization for resources -func improveResourcePluralization() PipelineStage { +// ImproveResourcePluralization improves pluralization for resources +func ImproveResourcePluralization() Stage { - return MakePipelineStage( + return MakeStage( "pluralizeNames", "Improve resource pluralization", func(ctx context.Context, types astmodel.Types) (astmodel.Types, error) { diff --git a/hack/generator/pkg/codegen/pipeline_json_serialization_test_cases.go b/hack/generator/pkg/codegen/pipeline/json_serialization_test_cases.go similarity index 93% rename from hack/generator/pkg/codegen/pipeline_json_serialization_test_cases.go rename to hack/generator/pkg/codegen/pipeline/json_serialization_test_cases.go index 8f32501a361..4bd59316045 100644 --- a/hack/generator/pkg/codegen/pipeline_json_serialization_test_cases.go +++ b/hack/generator/pkg/codegen/pipeline/json_serialization_test_cases.go @@ -3,7 +3,7 @@ * Licensed under the MIT license. */ -package codegen +package pipeline import ( "context" @@ -14,9 +14,9 @@ import ( kerrors "k8s.io/apimachinery/pkg/util/errors" ) -func injectJsonSerializationTests(idFactory astmodel.IdentifierFactory) PipelineStage { +func InjectJsonSerializationTests(idFactory astmodel.IdentifierFactory) Stage { - return MakePipelineStage( + return MakeStage( "jsonTestCases", "Add test cases to verify JSON serialization", func(ctx context.Context, types astmodel.Types) (astmodel.Types, error) { diff --git a/hack/generator/pkg/codegen/pipeline_load_schema.go b/hack/generator/pkg/codegen/pipeline/load_schema.go similarity index 96% rename from hack/generator/pkg/codegen/pipeline_load_schema.go rename to hack/generator/pkg/codegen/pipeline/load_schema.go index 89872a1bb53..bf5ef43289f 100644 --- a/hack/generator/pkg/codegen/pipeline_load_schema.go +++ b/hack/generator/pkg/codegen/pipeline/load_schema.go @@ -3,7 +3,7 @@ * Licensed under the MIT license. */ -package codegen +package pipeline import ( "context" @@ -11,13 +11,14 @@ import ( "os" "strings" - "github.com/Azure/azure-service-operator/hack/generator/pkg/astmodel" - "github.com/Azure/azure-service-operator/hack/generator/pkg/config" - "github.com/Azure/azure-service-operator/hack/generator/pkg/jsonast" "github.com/pkg/errors" "github.com/xeipuuv/gojsonreference" "github.com/xeipuuv/gojsonschema" + "github.com/Azure/azure-service-operator/hack/generator/pkg/astmodel" + "github.com/Azure/azure-service-operator/hack/generator/pkg/config" + "github.com/Azure/azure-service-operator/hack/generator/pkg/jsonast" + "k8s.io/klog/v2" ) @@ -113,7 +114,7 @@ func (loader *cancellableJSONLoader) LoaderFactory() gojsonschema.JSONLoaderFact type schemaLoader func(ctx context.Context, rewrite *config.RewriteRule, source string) (*gojsonschema.Schema, error) -func defaultSchemaLoader(ctx context.Context, rewrite *config.RewriteRule, source string) (*gojsonschema.Schema, error) { +func DefaultSchemaLoader(ctx context.Context, rewrite *config.RewriteRule, source string) (*gojsonschema.Schema, error) { sl := gojsonschema.NewSchemaLoader() var loader gojsonschema.JSONLoader = &cancellableJSONLoader{ctx, gojsonschema.NewReferenceLoaderFileSystem(source, &cancellableFileSystem{ctx})} @@ -133,13 +134,13 @@ func defaultSchemaLoader(ctx context.Context, rewrite *config.RewriteRule, sourc return schema, nil } -func loadSchemaIntoTypes( +func LoadSchemaIntoTypes( idFactory astmodel.IdentifierFactory, configuration *config.Configuration, - schemaLoader schemaLoader) PipelineStage { + schemaLoader schemaLoader) Stage { source := configuration.SchemaURL - return MakePipelineStage( + return MakeStage( "loadSchema", "Load and walk schema", func(ctx context.Context, types astmodel.Types) (astmodel.Types, error) { diff --git a/hack/generator/pkg/codegen/pipeline_mark_storage_version.go b/hack/generator/pkg/codegen/pipeline/mark_storage_version.go similarity index 93% rename from hack/generator/pkg/codegen/pipeline_mark_storage_version.go rename to hack/generator/pkg/codegen/pipeline/mark_storage_version.go index 119bea869f6..e24b94954d7 100644 --- a/hack/generator/pkg/codegen/pipeline_mark_storage_version.go +++ b/hack/generator/pkg/codegen/pipeline/mark_storage_version.go @@ -3,7 +3,7 @@ * Licensed under the MIT license. */ -package codegen +package pipeline import ( "context" @@ -14,11 +14,11 @@ import ( "github.com/Azure/azure-service-operator/hack/generator/pkg/astmodel" ) -// markStorageVersion creates a PipelineStage to mark a particular version as a storage version -func markStorageVersion() PipelineStage { - return MakePipelineStage( +// MarkStorageVersion creates a Stage to mark a particular version as a storage version +func MarkStorageVersion() Stage { + return MakeStage( "markStorageVersion", - "Marking the latest version of each resource as the storage version", + "Mark the latest version of each resource as the storage version", func(ctx context.Context, types astmodel.Types) (astmodel.Types, error) { updatedDefs, err := MarkLatestResourceVersionsForStorage(types) if err != nil { diff --git a/hack/generator/pkg/codegen/pipeline_name_types_for_crd.go b/hack/generator/pkg/codegen/pipeline/name_types_for_crd.go similarity index 97% rename from hack/generator/pkg/codegen/pipeline_name_types_for_crd.go rename to hack/generator/pkg/codegen/pipeline/name_types_for_crd.go index 1db0933ca8b..61693421a4c 100644 --- a/hack/generator/pkg/codegen/pipeline_name_types_for_crd.go +++ b/hack/generator/pkg/codegen/pipeline/name_types_for_crd.go @@ -3,20 +3,21 @@ * Licensed under the MIT license. */ -package codegen +package pipeline import ( "context" - "github.com/Azure/azure-service-operator/hack/generator/pkg/astmodel" "github.com/pkg/errors" kerrors "k8s.io/apimachinery/pkg/util/errors" + + "github.com/Azure/azure-service-operator/hack/generator/pkg/astmodel" ) -// nameTypesForCRD - for CRDs all inner enums and objects and validated types must be named, so we do it here -func nameTypesForCRD(idFactory astmodel.IdentifierFactory) PipelineStage { +// NameTypesForCRD - for CRDs all inner enums and objects and validated types must be named, so we do it here +func NameTypesForCRD(idFactory astmodel.IdentifierFactory) Stage { - return MakePipelineStage( + return MakeStage( "nameTypes", "Name inner types for CRD", func(ctx context.Context, types astmodel.Types) (astmodel.Types, error) { diff --git a/hack/generator/pkg/codegen/pipeline_remove_embedded_resources.go b/hack/generator/pkg/codegen/pipeline/remove_embedded_resources.go similarity index 69% rename from hack/generator/pkg/codegen/pipeline_remove_embedded_resources.go rename to hack/generator/pkg/codegen/pipeline/remove_embedded_resources.go index c5b2e7d3441..dcb9df543d8 100644 --- a/hack/generator/pkg/codegen/pipeline_remove_embedded_resources.go +++ b/hack/generator/pkg/codegen/pipeline/remove_embedded_resources.go @@ -3,7 +3,7 @@ * Licensed under the MIT license. */ -package codegen +package pipeline import ( "context" @@ -12,10 +12,10 @@ import ( "github.com/Azure/azure-service-operator/hack/generator/pkg/codegen/embeddedresources" ) -func removeEmbeddedResources() PipelineStage { - return MakePipelineStage( +func RemoveEmbeddedResources() Stage { + return MakeStage( "removeEmbeddedResources", - "Removes properties that point to embedded resources. Only removes structural aspects of embedded resources, Id/ARMId references are retained.", + "Remove properties that point to embedded resources. Only removes structural aspects of embedded resources, Id/ARMId references are retained.", func(ctx context.Context, types astmodel.Types) (astmodel.Types, error) { remover, err := embeddedresources.MakeEmbeddedResourceRemover(types) diff --git a/hack/generator/pkg/codegen/pipeline_remove_type_aliases.go b/hack/generator/pkg/codegen/pipeline/remove_type_aliases.go similarity index 94% rename from hack/generator/pkg/codegen/pipeline_remove_type_aliases.go rename to hack/generator/pkg/codegen/pipeline/remove_type_aliases.go index 3c9297e4ad9..97f1b1c47ea 100644 --- a/hack/generator/pkg/codegen/pipeline_remove_type_aliases.go +++ b/hack/generator/pkg/codegen/pipeline/remove_type_aliases.go @@ -3,7 +3,7 @@ * Licensed under the MIT license. */ -package codegen +package pipeline import ( "context" @@ -11,14 +11,15 @@ import ( kerrors "k8s.io/apimachinery/pkg/util/errors" - "github.com/Azure/azure-service-operator/hack/generator/pkg/astmodel" "github.com/pkg/errors" "k8s.io/klog/v2" + + "github.com/Azure/azure-service-operator/hack/generator/pkg/astmodel" ) -// removeTypeAliases creates a pipeline stage removing type aliases -func removeTypeAliases() PipelineStage { - return MakePipelineStage( +// RemoveTypeAliases creates a pipeline stage removing type aliases +func RemoveTypeAliases() Stage { + return MakeStage( "removeAliases", "Remove type aliases", func(ctx context.Context, types astmodel.Types) (astmodel.Types, error) { diff --git a/hack/generator/pkg/codegen/pipeline_replace_anytype_with_json.go b/hack/generator/pkg/codegen/pipeline/replace_anytype_with_json.go similarity index 92% rename from hack/generator/pkg/codegen/pipeline_replace_anytype_with_json.go rename to hack/generator/pkg/codegen/pipeline/replace_anytype_with_json.go index a8aef761384..e5aadce2970 100644 --- a/hack/generator/pkg/codegen/pipeline_replace_anytype_with_json.go +++ b/hack/generator/pkg/codegen/pipeline/replace_anytype_with_json.go @@ -3,7 +3,7 @@ * Licensed under the MIT license. */ -package codegen +package pipeline import ( "context" @@ -30,10 +30,10 @@ var ( mapOfJSON = astmodel.NewMapType(astmodel.StringType, astmodel.JSONTypeName) ) -func replaceAnyTypeWithJSON() PipelineStage { - return MakePipelineStage( +func ReplaceAnyTypeWithJSON() Stage { + return MakeStage( "replaceAnyTypeWithJSON", - "Replacing interface{}s with arbitrary JSON", + "Replace properties using interface{} with arbitrary JSON", func(ctx context.Context, types astmodel.Types) (astmodel.Types, error) { replaceAnyWithJson := func(it *astmodel.PrimitiveType) astmodel.Type { diff --git a/hack/generator/pkg/codegen/pipeline_replace_anytype_with_json_test.go b/hack/generator/pkg/codegen/pipeline/replace_anytype_with_json_test.go similarity index 87% rename from hack/generator/pkg/codegen/pipeline_replace_anytype_with_json_test.go rename to hack/generator/pkg/codegen/pipeline/replace_anytype_with_json_test.go index 8dda5b02567..612065fdb7c 100644 --- a/hack/generator/pkg/codegen/pipeline_replace_anytype_with_json_test.go +++ b/hack/generator/pkg/codegen/pipeline/replace_anytype_with_json_test.go @@ -3,20 +3,21 @@ * Licensed under the MIT license. */ -package codegen +package pipeline import ( "context" "testing" "github.com/Azure/azure-service-operator/hack/generator/pkg/astmodel" + "github.com/Azure/azure-service-operator/hack/generator/pkg/test" . "github.com/onsi/gomega" ) func TestReplacingAnyTypes(t *testing.T) { g := NewGomegaWithT(t) - p1 := makeTestLocalPackageReference("horo.logy", "v20200730") + p1 := test.MakeLocalPackageReference("horo.logy", "v20200730") aName := astmodel.MakeTypeName(p1, "A") bName := astmodel.MakeTypeName(p1, "B") @@ -30,7 +31,7 @@ func TestReplacingAnyTypes(t *testing.T) { ), )) - results, err := replaceAnyTypeWithJSON().action(context.Background(), defs) + results, err := ReplaceAnyTypeWithJSON().action(context.Background(), defs) g.Expect(err).To(BeNil()) @@ -52,7 +53,7 @@ func TestReplacingMapMapInterface(t *testing.T) { // map[string]JSON, rather than the right one, since // controller-gen can't handle it at the moment. g := NewGomegaWithT(t) - p1 := makeTestLocalPackageReference("horo.logy", "v20200730") + p1 := test.MakeLocalPackageReference("horo.logy", "v20200730") aName := astmodel.MakeTypeName(p1, "A") defs := make(astmodel.Types) @@ -70,7 +71,7 @@ func TestReplacingMapMapInterface(t *testing.T) { ), )) - results, err := replaceAnyTypeWithJSON().action(context.Background(), defs) + results, err := ReplaceAnyTypeWithJSON().action(context.Background(), defs) g.Expect(err).To(BeNil()) diff --git a/hack/generator/pkg/codegen/pipeline_report_type_versions.go b/hack/generator/pkg/codegen/pipeline/report_type_versions.go similarity index 93% rename from hack/generator/pkg/codegen/pipeline_report_type_versions.go rename to hack/generator/pkg/codegen/pipeline/report_type_versions.go index 242f7547e31..f7013946fd9 100644 --- a/hack/generator/pkg/codegen/pipeline_report_type_versions.go +++ b/hack/generator/pkg/codegen/pipeline/report_type_versions.go @@ -3,7 +3,7 @@ * Licensed under the MIT license. */ -package codegen +package pipeline import ( "context" @@ -12,17 +12,18 @@ import ( "path" "strings" + "github.com/pkg/errors" + kerrors "k8s.io/apimachinery/pkg/util/errors" + "github.com/Azure/azure-service-operator/hack/generator/pkg/astmodel" "github.com/Azure/azure-service-operator/hack/generator/pkg/config" "github.com/Azure/azure-service-operator/hack/generator/pkg/reporting" - "github.com/pkg/errors" - kerrors "k8s.io/apimachinery/pkg/util/errors" ) -// reportOnTypesAndVersions creates a pipeline stage that removes any wrapper types prior to actual code generation -func reportOnTypesAndVersions(configuration *config.Configuration) PipelineStage { +// ReportOnTypesAndVersions creates a pipeline stage that removes any wrapper types prior to actual code generation +func ReportOnTypesAndVersions(configuration *config.Configuration) Stage { - return MakePipelineStage( + return MakeStage( "reportTypesAndVersions", "Generate reports on types and versions in each package", func(ctx context.Context, types astmodel.Types) (astmodel.Types, error) { diff --git a/hack/generator/pkg/codegen/resource_registration_file.go b/hack/generator/pkg/codegen/pipeline/resource_registration_file.go similarity index 99% rename from hack/generator/pkg/codegen/resource_registration_file.go rename to hack/generator/pkg/codegen/pipeline/resource_registration_file.go index 1ccfbb83778..5ce1114a2d9 100644 --- a/hack/generator/pkg/codegen/resource_registration_file.go +++ b/hack/generator/pkg/codegen/pipeline/resource_registration_file.go @@ -3,7 +3,7 @@ * Licensed under the MIT license. */ -package codegen +package pipeline import ( "go/token" diff --git a/hack/generator/pkg/codegen/pipeline_simplify_definitions.go b/hack/generator/pkg/codegen/pipeline/simplify_definitions.go similarity index 90% rename from hack/generator/pkg/codegen/pipeline_simplify_definitions.go rename to hack/generator/pkg/codegen/pipeline/simplify_definitions.go index 843438ad20e..68a3d39653b 100644 --- a/hack/generator/pkg/codegen/pipeline_simplify_definitions.go +++ b/hack/generator/pkg/codegen/pipeline/simplify_definitions.go @@ -3,19 +3,20 @@ * Licensed under the MIT license. */ -package codegen +package pipeline import ( "context" - "github.com/Azure/azure-service-operator/hack/generator/pkg/astmodel" kerrors "k8s.io/apimachinery/pkg/util/errors" "k8s.io/klog/v2" + + "github.com/Azure/azure-service-operator/hack/generator/pkg/astmodel" ) -// simplifyDefinitions creates a pipeline stage that removes any wrapper types prior to actual code generation -func simplifyDefinitions() PipelineStage { - return MakePipelineStage( +// SimplifyDefinitions creates a pipeline stage that removes any wrapper types prior to actual code generation +func SimplifyDefinitions() Stage { + return MakeStage( "simplifyDefinitions", "Flatten definitions by removing wrapper types", func(ctx context.Context, defs astmodel.Types) (astmodel.Types, error) { diff --git a/hack/generator/pkg/codegen/pipeline_simplify_definitions_test.go b/hack/generator/pkg/codegen/pipeline/simplify_definitions_test.go similarity index 98% rename from hack/generator/pkg/codegen/pipeline_simplify_definitions_test.go rename to hack/generator/pkg/codegen/pipeline/simplify_definitions_test.go index 27213558951..14882106664 100644 --- a/hack/generator/pkg/codegen/pipeline_simplify_definitions_test.go +++ b/hack/generator/pkg/codegen/pipeline/simplify_definitions_test.go @@ -3,7 +3,7 @@ * Licensed under the MIT license. */ -package codegen +package pipeline import ( . "github.com/onsi/gomega" diff --git a/hack/generator/pkg/codegen/pipeline_stage.go b/hack/generator/pkg/codegen/pipeline/stage.go similarity index 57% rename from hack/generator/pkg/codegen/pipeline_stage.go rename to hack/generator/pkg/codegen/pipeline/stage.go index 54ea79f5bc6..4b444d45b31 100644 --- a/hack/generator/pkg/codegen/pipeline_stage.go +++ b/hack/generator/pkg/codegen/pipeline/stage.go @@ -3,19 +3,22 @@ * Licensed under the MIT license. */ -package codegen +package pipeline import ( "context" "fmt" "strings" + "github.com/pkg/errors" + kerrors "k8s.io/apimachinery/pkg/util/errors" + "github.com/Azure/azure-service-operator/hack/generator/pkg/astmodel" ) -// PipelineStage represents a composable stage of processing that can transform or process the set +// Stage represents a composable stage of processing that can transform or process the set // of generated types -type PipelineStage struct { +type Stage struct { // Unique identifier used to manipulate the pipeline from code id string // Description of the stage to use when logging @@ -23,19 +26,19 @@ type PipelineStage struct { // Stage implementation action func(context.Context, astmodel.Types) (astmodel.Types, error) // Tag used for filtering - targets []PipelineTarget + targets []Target // Identifiers for other stages that must be completed before this one prerequisites []string // Identifiers for other stages that must be completed after this one postrequisites []string } -// MakePipelineStage creates a new pipeline stage that's ready for execution -func MakePipelineStage( +// MakeStage creates a new pipeline stage that's ready for execution +func MakeStage( id string, description string, - action func(context.Context, astmodel.Types) (astmodel.Types, error)) PipelineStage { - return PipelineStage{ + action func(context.Context, astmodel.Types) (astmodel.Types, error)) Stage { + return Stage{ id: id, description: description, action: action, @@ -43,12 +46,12 @@ func MakePipelineStage( } // HasId returns true if this stage has the specified id, false otherwise -func (stage *PipelineStage) HasId(id string) bool { +func (stage *Stage) HasId(id string) bool { return stage.id == id } // RequiresPrerequisiteStages declares which stages must have completed before this one is executed -func (stage PipelineStage) RequiresPrerequisiteStages(prerequisites ...string) PipelineStage { +func (stage Stage) RequiresPrerequisiteStages(prerequisites ...string) Stage { if len(stage.prerequisites) > 0 { panic(fmt.Sprintf( "Prerequisites of stage '%s' already set to '%s'; cannot modify to '%s'.", @@ -66,7 +69,7 @@ func (stage PipelineStage) RequiresPrerequisiteStages(prerequisites ...string) P // This is not completely isomorphic with RequiresPrerequisiteStages as there may be supporting stages that are // sometimes omitted from execution when targeting different outcomes. Having both pre- and post-requisites allows the // dependencies to drop out cleanly when different stages are present. -func (stage PipelineStage) RequiresPostrequisiteStages(postrequisites ...string) PipelineStage { +func (stage Stage) RequiresPostrequisiteStages(postrequisites ...string) Stage { if len(stage.postrequisites) > 0 { panic(fmt.Sprintf( "Postrequisites of stage '%s' already set to '%s'; cannot modify to '%s'.", @@ -81,13 +84,13 @@ func (stage PipelineStage) RequiresPostrequisiteStages(postrequisites ...string) } // UsedFor specifies that this stage should be used for only the specified targets -func (stage PipelineStage) UsedFor(targets ...PipelineTarget) PipelineStage { +func (stage Stage) UsedFor(targets ...Target) Stage { stage.targets = targets return stage } // IsUsedFor returns true if this stage should be used for the specified target -func (stage *PipelineStage) IsUsedFor(target PipelineTarget) bool { +func (stage *Stage) IsUsedFor(target Target) bool { if len(stage.targets) == 0 { // Stages without specific targeting are always used @@ -103,3 +106,41 @@ func (stage *PipelineStage) IsUsedFor(target PipelineTarget) bool { return false } + +// Id returns the unique identifier for this stage +func (stage *Stage) Id() string { + return stage.id +} + +// Description returns a human readable description of this stage +func (stage *Stage) Description() string { + return stage.description +} + +// Run is used to execute the action associated with this stage +func (stage *Stage) Run(ctx context.Context, types astmodel.Types) (astmodel.Types, error) { + return stage.action(ctx, types) +} + +// CheckPrerequisites returns an error if the prerequisites of this stage have not been met +func (stage *Stage) CheckPrerequisites(priorStages map[string]struct{}) error { + var errs []error + for _, prereq := range stage.prerequisites { + if _, ok := priorStages[prereq]; !ok { + errs = append(errs, errors.Errorf("prerequisite %q of stage %q not satisfied.", prereq, stage.id)) + } + } + + return kerrors.NewAggregate(errs) +} + +// Postrequisites returns the unique ids of stages that must run after this stage +func (stage *Stage) Postrequisites() []string { + return stage.postrequisites +} + +// Targets returns the targets this stage should be used for +// If no targets are returned, this stage should always be used +func (stage *Stage) Targets() []Target { + return stage.targets +} diff --git a/hack/generator/pkg/codegen/pipeline_strip_unused_types.go b/hack/generator/pkg/codegen/pipeline/strip_unused_types.go similarity index 92% rename from hack/generator/pkg/codegen/pipeline_strip_unused_types.go rename to hack/generator/pkg/codegen/pipeline/strip_unused_types.go index b7dff204919..edbdbfbbd03 100644 --- a/hack/generator/pkg/codegen/pipeline_strip_unused_types.go +++ b/hack/generator/pkg/codegen/pipeline/strip_unused_types.go @@ -3,7 +3,7 @@ * Licensed under the MIT license. */ -package codegen +package pipeline import ( "context" @@ -11,8 +11,8 @@ import ( "github.com/Azure/azure-service-operator/hack/generator/pkg/astmodel" ) -func stripUnreferencedTypeDefinitions() PipelineStage { - return MakePipelineStage( +func StripUnreferencedTypeDefinitions() Stage { + return MakeStage( "stripUnreferenced", "Strip unreferenced types", func(ctx context.Context, defs astmodel.Types) (astmodel.Types, error) { diff --git a/hack/generator/pkg/codegen/pipeline_strip_unused_types_test.go b/hack/generator/pkg/codegen/pipeline/strip_unused_types_test.go similarity index 98% rename from hack/generator/pkg/codegen/pipeline_strip_unused_types_test.go rename to hack/generator/pkg/codegen/pipeline/strip_unused_types_test.go index afce031331c..6180de27623 100644 --- a/hack/generator/pkg/codegen/pipeline_strip_unused_types_test.go +++ b/hack/generator/pkg/codegen/pipeline/strip_unused_types_test.go @@ -3,7 +3,7 @@ * Licensed under the MIT license. */ -package codegen +package pipeline import ( "testing" diff --git a/hack/generator/pkg/codegen/pipeline/target.go b/hack/generator/pkg/codegen/pipeline/target.go new file mode 100644 index 00000000000..59c97a13b33 --- /dev/null +++ b/hack/generator/pkg/codegen/pipeline/target.go @@ -0,0 +1,51 @@ +/* + * Copyright (c) Microsoft Corporation. + * Licensed under the MIT license. + */ + +package pipeline + +import ( + "fmt" + + "github.com/pkg/errors" + + "github.com/Azure/azure-service-operator/hack/generator/pkg/config" +) + +// Target is used to classify what kind of pipeline we have +// Deliberately wraps a string because we *do* *not* want type compatibility with literal strings +type Target struct { + name string +} + +var _ fmt.Stringer = Target{} + +var ( + // ARMTarget is used to tag stages that are required when generating types for working directly with Azure + ARMTarget Target = MakePipelineTarget("azure") + + // CrossplaneTarget is used to tag stages that are required when generating types for working with Crossplane + CrossplaneTarget Target = MakePipelineTarget("crossplane") +) + +func MakePipelineTarget(tag string) Target { + return Target{ + name: tag, + } +} + +func (t Target) String() string { + return t.name +} + +func TranslatePipelineToTarget(pipeline config.GenerationPipeline) (Target, error) { + switch pipeline { + case config.GenerationPipelineAzure: + return ARMTarget, nil + case config.GenerationPipelineCrossplane: + return CrossplaneTarget, nil + default: + return Target{}, errors.Errorf("unknown pipeline target kind %s", pipeline) + } +} diff --git a/hack/generator/pkg/codegen/version.go b/hack/generator/pkg/codegen/pipeline/version.go similarity index 87% rename from hack/generator/pkg/codegen/version.go rename to hack/generator/pkg/codegen/pipeline/version.go index 673118f176b..aa89f561534 100644 --- a/hack/generator/pkg/codegen/version.go +++ b/hack/generator/pkg/codegen/pipeline/version.go @@ -3,7 +3,7 @@ * Licensed under the MIT license. */ -package codegen +package pipeline import ( "fmt" @@ -15,7 +15,7 @@ var ( GitTreeState string ) -func combinedVersion() string { +func CombinedVersion() string { result := GitCommit if GitTreeState != "clean" { result += fmt.Sprintf(" (tree is %s)", GitTreeState) diff --git a/hack/generator/pkg/codegen/pipeline_factory_test.go b/hack/generator/pkg/codegen/pipeline_factory_test.go index 58bd9e355d1..deab9739f70 100644 --- a/hack/generator/pkg/codegen/pipeline_factory_test.go +++ b/hack/generator/pkg/codegen/pipeline_factory_test.go @@ -11,9 +11,10 @@ import ( "strings" "testing" + "github.com/sebdah/goldie/v2" + "github.com/Azure/azure-service-operator/hack/generator/pkg/astmodel" "github.com/Azure/azure-service-operator/hack/generator/pkg/config" - "github.com/sebdah/goldie/v2" . "github.com/onsi/gomega" ) @@ -55,7 +56,7 @@ func writePipeline(title string, codegen *CodeGenerator) []byte { for _, s := range codegen.pipeline { targets := "" - for _, t := range s.targets { + for _, t := range s.Targets() { if len(targets) > 0 { targets = targets + "; " } @@ -63,7 +64,7 @@ func writePipeline(title string, codegen *CodeGenerator) []byte { targets = targets + t.String() } - fmt.Fprintf(&b, "%-35s %-10s %s\n", s.id, targets, s.description) + fmt.Fprintf(&b, "%-35s %-10s %s\n", s.Id(), targets, s.Description()) } return b.Bytes() diff --git a/hack/generator/pkg/codegen/pipeline_target.go b/hack/generator/pkg/codegen/pipeline_target.go deleted file mode 100644 index 3ad9e12e8ab..00000000000 --- a/hack/generator/pkg/codegen/pipeline_target.go +++ /dev/null @@ -1,36 +0,0 @@ -/* - * Copyright (c) Microsoft Corporation. - * Licensed under the MIT license. - */ - -package codegen - -import ( - "fmt" -) - -// PipelineTarget is used to classify what kind of pipeline we have -// Deliberately wraps a string because we *do* *not* want type compatibility with literal strings -type PipelineTarget struct { - name string -} - -var _ fmt.Stringer = PipelineTarget{} - -var ( - // ARMTarget is used to tag stages that are required when generating types for working directly with Azure - ARMTarget PipelineTarget = MakePipelineTarget("azure") - - // CrossplaneTarget is used to tag stages that are required when generating types for working with Crossplane - CrossplaneTarget PipelineTarget = MakePipelineTarget("crossplane") -) - -func MakePipelineTarget(tag string) PipelineTarget { - return PipelineTarget{ - name: tag, - } -} - -func (t PipelineTarget) String() string { - return t.name -} diff --git a/hack/generator/pkg/codegen/testdata/ARMCodeGeneratorPipeline.golden b/hack/generator/pkg/codegen/testdata/ARMCodeGeneratorPipeline.golden index 0f3f79529b1..1fcdb185879 100644 --- a/hack/generator/pkg/codegen/testdata/ARMCodeGeneratorPipeline.golden +++ b/hack/generator/pkg/codegen/testdata/ARMCodeGeneratorPipeline.golden @@ -3,36 +3,36 @@ Expected Pipeline Stages for ARM Code Generation loadSchema Load and walk schema augmentStatus Add information from Swagger specs for 'status' fields allof-anyof-objects Convert allOf and oneOf to object types -flatten-resources Flatten nested resource types +flattenResources Flatten nested resource types stripUnreferenced Strip unreferenced types nameTypes Name inner types for CRD -propertyRewrites Applying type transformers to properties +propertyRewrites Modify property types using configured transforms determineResourceOwnership Determine ARM resource relationships removeAliases Remove type aliases collapseCrossGroupReferences Finds and removes cross group references pluralizeNames Improve resource pluralization stripUnreferenced Strip unreferenced types -assertTypesStructureValid Asserts that the types collection is valid -removeEmbeddedResources azure Removes properties that point to embedded resources. Only removes structural aspects of embedded resources, Id/ARMId references are retained. -filterTypes Filter generated types +assertTypesStructureValid Verify that all local TypeNames refer to a type +removeEmbeddedResources azure Remove properties that point to embedded resources. Only removes structural aspects of embedded resources, Id/ARMId references are retained. +filterTypes Apply export filters to reduce the number of generated types stripUnreferenced Strip unreferenced types -replaceAnyTypeWithJSON Replacing interface{}s with arbitrary JSON -addCrossResourceReferences azure Replaces cross resource references with genruntime.ResourceReference +replaceAnyTypeWithJSON Replace properties using interface{} with arbitrary JSON +addCrossResourceReferences azure Replace cross-resource references with genruntime.ResourceReference reportTypesAndVersions azure Generate reports on types and versions in each package -createArmTypes azure Creates ARM types -applyArmConversionInterface azure Apply the ARM conversion interface to Kubernetes types -applyKubernetesResourceInterface azure Ensures that every resource implements the KubernetesResource interface -addCrossplaneOwnerProperties crossplane Adds the 3-tuple of (xName, xNameRef, xNameSelector) for each owning resource -addCrossplaneForProviderProperty crossplane Adds a 'ForProvider' property on every spec -addCrossplaneAtProviderProperty crossplane Adds an 'AtProvider' property on every status -addCrossplaneEmbeddedResourceSpec crossplane Adds an embedded runtimev1alpha1.ResourceSpec to every spec type -addCrossplaneEmbeddedResourceStatus crossplane Adds an embedded runtimev1alpha1.ResourceStatus to every status type +createArmTypes azure Create types for interaction with ARM +applyArmConversionInterface azure Add ARM conversion interfaces to Kubernetes types +applyKubernetesResourceInterface azure Add the KubernetesResource interface to every resource +addCrossplaneOwnerProperties crossplane Add the 3-tuple of (xName, xNameRef, xNameSelector) for each owning resource +addCrossplaneForProviderProperty crossplane Add a 'ForProvider' property on every spec +addCrossplaneAtProviderProperty crossplane Add an 'AtProvider' property on every status +addCrossplaneEmbeddedResourceSpec crossplane Add an embedded runtimev1alpha1.ResourceSpec to every spec type +addCrossplaneEmbeddedResourceStatus crossplane Add an embedded runtimev1alpha1.ResourceStatus to every status type createStorage azure Create storage versions of CRD types simplifyDefinitions Flatten definitions by removing wrapper types jsonTestCases azure Add test cases to verify JSON serialization -markStorageVersion Marking the latest version of each resource as the storage version -rogueCheck Catch rogue definitions using AnyTypes -ensureArmTypeExistsForEveryType azure Ensure that an ARM type for every top level resource spec/status exists +markStorageVersion Mark the latest version of each resource as the storage version +rogueCheck Check for rogue definitions using AnyTypes +ensureArmTypeExistsForEveryType azure Check that an ARM type exists for both Spec and Status of each resource deleteGenerated Delete generated code from . exportPackages Export packages to "." exportControllerResourceRegistrations azure Export resource registrations to "" diff --git a/hack/generator/pkg/codegen/testdata/TestCodeGeneratorPipeline.golden b/hack/generator/pkg/codegen/testdata/TestCodeGeneratorPipeline.golden index 21037a9bdbe..3f6abaf36a6 100644 --- a/hack/generator/pkg/codegen/testdata/TestCodeGeneratorPipeline.golden +++ b/hack/generator/pkg/codegen/testdata/TestCodeGeneratorPipeline.golden @@ -3,23 +3,23 @@ Expected Pipeline Stages for Test Code Generation loadTestSchema Load and walk schema (test) augmentStatus Add information from Swagger specs for 'status' fields allof-anyof-objects Convert allOf and oneOf to object types -flatten-resources Flatten nested resource types +flattenResources Flatten nested resource types stripUnused Strip unused types for test nameTypes Name inner types for CRD -propertyRewrites Applying type transformers to properties +propertyRewrites Modify property types using configured transforms determineResourceOwnership Determine ARM resource relationships removeAliases Remove type aliases pluralizeNames Improve resource pluralization stripUnused Strip unused types for test -assertTypesStructureValid Asserts that the types collection is valid -filterTypes Filter generated types +assertTypesStructureValid Verify that all local TypeNames refer to a type +filterTypes Apply export filters to reduce the number of generated types stripUnused Strip unused types for test -replaceAnyTypeWithJSON Replacing interface{}s with arbitrary JSON -addCrossResourceReferences azure Replaces cross resource references with genruntime.ResourceReference -applyKubernetesResourceInterface azure Ensures that every resource implements the KubernetesResource interface +replaceAnyTypeWithJSON Replace properties using interface{} with arbitrary JSON +addCrossResourceReferences azure Replace cross-resource references with genruntime.ResourceReference +applyKubernetesResourceInterface azure Add the KubernetesResource interface to every resource simplifyDefinitions Flatten definitions by removing wrapper types jsonTestCases azure Add test cases to verify JSON serialization -markStorageVersion Marking the latest version of each resource as the storage version -ensureArmTypeExistsForEveryType azure Ensure that an ARM type for every top level resource spec/status exists +markStorageVersion Mark the latest version of each resource as the storage version +ensureArmTypeExistsForEveryType azure Check that an ARM type exists for both Spec and Status of each resource exportTestPackages Export packages for test exportControllerResourceRegistrations azure Export resource registrations to "" diff --git a/hack/generator/pkg/test/package_reference.go b/hack/generator/pkg/test/package_reference.go new file mode 100644 index 00000000000..781783864b9 --- /dev/null +++ b/hack/generator/pkg/test/package_reference.go @@ -0,0 +1,17 @@ +/* + * Copyright (c) Microsoft Corporation. + * Licensed under the MIT license. + */ + +package test + +import ( + "github.com/Azure/azure-service-operator/hack/generator/pkg/astmodel" +) + +var GoModulePrefix = "github.com/Azure/azure-service-operator/testing" + +// MakeLocalPackageReference makes a local package reference for testing purposes +func MakeLocalPackageReference(group string, version string) astmodel.LocalPackageReference { + return astmodel.MakeLocalPackageReference(GoModulePrefix, group, version) +}