/
add_status_conditions.go
92 lines (76 loc) · 3.55 KB
/
add_status_conditions.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/*
* Copyright (c) Microsoft Corporation.
* Licensed under the MIT license.
*/
package pipeline
import (
"context"
"github.com/pkg/errors"
"github.com/Azure/azure-service-operator/v2/tools/generator/internal/astmodel"
"github.com/Azure/azure-service-operator/v2/tools/generator/internal/functions"
)
const AddStatusConditionsStageID = "addStatusConditions"
func AddStatusConditions(idFactory astmodel.IdentifierFactory) *Stage {
return NewStage(
AddStatusConditionsStageID,
"Add the property 'Conditions' to all status types and implements genruntime.Conditioner on all resources",
func(ctx context.Context, state *State) (*State, error) {
defs := state.Definitions()
result := make(astmodel.TypeDefinitionSet)
propInjector := astmodel.NewPropertyInjector()
statusDefs := astmodel.FindStatusDefinitions(defs)
for _, def := range statusDefs {
conditionsProp := astmodel.NewPropertyDefinition(
astmodel.ConditionsProperty,
"conditions",
astmodel.NewArrayType(astmodel.ConditionType))
conditionsProp = conditionsProp.WithDescription("The observed state of the resource").MakeOptional()
updatedDef, err := propInjector.Inject(def, conditionsProp)
if err != nil {
return nil, errors.Wrapf(err, "couldn't add Conditions condition to status %q", def.Name())
}
result.Add(updatedDef)
}
resourceDefs := astmodel.FindResourceDefinitions(defs)
for _, def := range resourceDefs {
resourceType := def.Type().(*astmodel.ResourceType)
conditionerImpl, err := NewConditionerInterfaceImpl(idFactory, resourceType)
if err != nil {
return nil, errors.Wrapf(err, "couldn't create genruntime.Conditioner implementation for %q", def.Name())
}
resourceType = resourceType.WithInterface(conditionerImpl)
// Resources with the genruntime.Conditioner interface should also have kubebuilder:printcolumn set
// so that the conditions are displayed
resourceType = resourceType.WithAnnotation("// +kubebuilder:printcolumn:name=\"Ready\",type=\"string\",JSONPath=\".status.conditions[?(@.type=='Ready')].status\"")
resourceType = resourceType.WithAnnotation("// +kubebuilder:printcolumn:name=\"Severity\",type=\"string\",JSONPath=\".status.conditions[?(@.type=='Ready')].severity\"")
resourceType = resourceType.WithAnnotation("// +kubebuilder:printcolumn:name=\"Reason\",type=\"string\",JSONPath=\".status.conditions[?(@.type=='Ready')].reason\"")
resourceType = resourceType.WithAnnotation("// +kubebuilder:printcolumn:name=\"Message\",type=\"string\",JSONPath=\".status.conditions[?(@.type=='Ready')].message\"")
result.Add(def.WithType(resourceType))
}
return state.WithOverlaidDefinitions(result), nil
})
}
// NewConditionerInterfaceImpl creates an InterfaceImplementation with GetConditions() and
// SetConditions() methods, implementing the genruntime.Conditioner interface.
func NewConditionerInterfaceImpl(
idFactory astmodel.IdentifierFactory,
resource *astmodel.ResourceType,
) (*astmodel.InterfaceImplementation, error) {
getConditions := functions.NewResourceFunction(
"Get"+astmodel.ConditionsProperty,
resource,
idFactory,
functions.GetConditionsFunction,
astmodel.NewPackageReferenceSet(astmodel.GenRuntimeConditionsReference))
setConditions := functions.NewResourceFunction(
"Set"+astmodel.ConditionsProperty,
resource,
idFactory,
functions.SetConditionsFunction,
astmodel.NewPackageReferenceSet(astmodel.GenRuntimeConditionsReference))
result := astmodel.NewInterfaceImplementation(
astmodel.ConditionerType,
getConditions,
setConditions)
return result, nil
}