/
ensure_type_has_arm_type.go
72 lines (57 loc) · 2.05 KB
/
ensure_type_has_arm_type.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/*
* Copyright (c) Microsoft Corporation.
* Licensed under the MIT license.
*/
package pipeline
import (
"context"
"github.com/pkg/errors"
kerrors "k8s.io/apimachinery/pkg/util/errors"
"github.com/Azure/azure-service-operator/v2/tools/generator/internal/astmodel"
)
// 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() *Stage {
return NewLegacyStage(
"ensureArmTypeExistsForEveryType",
"Check that an ARM type exists for both Spec and Status of each resource",
func(ctx context.Context, definitions astmodel.TypeDefinitionSet) (astmodel.TypeDefinitionSet, error) {
return definitions, validateExpectedTypesHaveARMType(definitions)
})
}
// validateExpectedTypesHaveARMType returns an error containing details about all
// definitions which do not have a matching ARM type.
func validateExpectedTypesHaveARMType(definitions astmodel.TypeDefinitionSet) error {
findARMType := func(t astmodel.Type) error {
name, ok := astmodel.AsInternalTypeName(t)
if !ok {
return errors.Errorf("type was not of type TypeName, instead %T", t)
}
armName := astmodel.CreateARMTypeName(name)
if _, ok = definitions[armName]; !ok {
return errors.Errorf("couldn't find ARM type %q", armName)
}
return nil
}
var errs []error
for name, def := range definitions {
if astmodel.IsStoragePackageReference(name.PackageReference()) {
// Don't need ARM types within Storage Packages
continue
}
if resourceType, ok := definitions.ResolveResourceType(def.Type()); ok {
err := findARMType(resourceType.SpecType())
if err != nil {
errs = append(errs, err)
}
statusType := astmodel.IgnoringErrors(resourceType.StatusType())
if statusType != nil {
err := findARMType(statusType)
if err != nil {
errs = append(errs, err)
}
}
}
}
return kerrors.NewAggregate(errs)
}