-
Notifications
You must be signed in to change notification settings - Fork 196
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New conditions pipeline stages (#1689)
- Loading branch information
Showing
12 changed files
with
306 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
/* | ||
* Copyright (c) Microsoft Corporation. | ||
* Licensed under the MIT license. | ||
*/ | ||
|
||
package astmodel | ||
|
||
import ( | ||
"go/token" | ||
|
||
"github.com/dave/dst" | ||
|
||
"github.com/Azure/azure-service-operator/hack/generator/pkg/astbuilder" | ||
) | ||
|
||
const ( | ||
ConditionsProperty = "Conditions" | ||
) | ||
|
||
// NewConditionerInterfaceImpl creates an InterfaceImplementation with GetConditions() and | ||
// SetConditions() methods, implementing the genruntime.Conditioner interface. | ||
func NewConditionerInterfaceImpl( | ||
idFactory IdentifierFactory, | ||
resource *ResourceType) (*InterfaceImplementation, error) { | ||
|
||
getConditions := &resourceFunction{ | ||
name: "Get" + ConditionsProperty, | ||
resource: resource, | ||
idFactory: idFactory, | ||
asFunc: getConditionsFunction, | ||
requiredPackages: NewPackageReferenceSet(GenRuntimeConditionsReference), | ||
} | ||
|
||
setConditions := &resourceFunction{ | ||
name: "Set" + ConditionsProperty, | ||
resource: resource, | ||
idFactory: idFactory, | ||
asFunc: setConditionsFunction, | ||
requiredPackages: NewPackageReferenceSet(GenRuntimeConditionsReference), | ||
} | ||
|
||
result := NewInterfaceImplementation( | ||
ConditionerTypeName, | ||
getConditions, | ||
setConditions) | ||
|
||
return result, nil | ||
} | ||
|
||
// getConditionsFunction returns a function declaration containing the implementation of the GetConditions() function. | ||
// | ||
// func (r *<receiver>)GetConditions() genruntime.Conditions { | ||
// return r.Status.Conditions | ||
// } | ||
func getConditionsFunction(k *resourceFunction, codeGenerationContext *CodeGenerationContext, receiver TypeName, methodName string) *dst.FuncDecl { | ||
receiverIdent := k.idFactory.CreateIdentifier(receiver.Name(), NotExported) | ||
receiverType := receiver.AsType(codeGenerationContext) | ||
|
||
status := astbuilder.Selector(dst.NewIdent(receiverIdent), "Status") | ||
|
||
fn := &astbuilder.FuncDetails{ | ||
Name: methodName, | ||
ReceiverIdent: receiverIdent, | ||
ReceiverType: &dst.StarExpr{ | ||
X: receiverType, | ||
}, | ||
Body: []dst.Stmt{ | ||
astbuilder.Returns(astbuilder.Selector(status, ConditionsProperty)), | ||
}, | ||
} | ||
|
||
fn.AddComments("returns the conditions of the resource") | ||
fn.AddReturn(ConditionsTypeName.AsType(codeGenerationContext)) | ||
|
||
return fn.DefineFunc() | ||
} | ||
|
||
// setConditionsFunction returns a function declaration containing the implementation of the SetConditions() function. | ||
// | ||
// func (r *<receiver>)SetConditions(conditions genruntime.Conditions) { | ||
// r.Status.Conditions = conditions | ||
// } | ||
func setConditionsFunction(k *resourceFunction, codeGenerationContext *CodeGenerationContext, receiver TypeName, methodName string) *dst.FuncDecl { | ||
conditionsParameterName := k.idFactory.CreateIdentifier(ConditionsProperty, NotExported) | ||
|
||
receiverIdent := k.idFactory.CreateIdentifier(receiver.Name(), NotExported) | ||
receiverType := receiver.AsType(codeGenerationContext) | ||
status := astbuilder.Selector(dst.NewIdent(receiverIdent), "Status") | ||
|
||
fn := &astbuilder.FuncDetails{ | ||
Name: methodName, | ||
ReceiverIdent: receiverIdent, | ||
ReceiverType: &dst.StarExpr{ | ||
X: receiverType, | ||
}, | ||
Body: []dst.Stmt{ | ||
astbuilder.QualifiedAssignment(status, "Conditions", token.ASSIGN, dst.NewIdent(conditionsParameterName)), | ||
}, | ||
} | ||
|
||
fn.AddParameter( | ||
conditionsParameterName, | ||
ConditionsTypeName.AsType(codeGenerationContext)) | ||
fn.AddComments("sets the conditions on the resource status") | ||
|
||
return fn.DefineFunc() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
hack/generator/pkg/codegen/pipeline/add_status_conditions.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/* | ||
* Copyright (c) Microsoft Corporation. | ||
* Licensed under the MIT license. | ||
*/ | ||
|
||
package pipeline | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/pkg/errors" | ||
|
||
"github.com/Azure/azure-service-operator/hack/generator/pkg/astmodel" | ||
) | ||
|
||
const AddStatusConditionsStageID = "addStatusConditions" | ||
|
||
func AddStatusConditions(idFactory astmodel.IdentifierFactory) Stage { | ||
return MakeStage( | ||
AddStatusConditionsStageID, | ||
"Adds the property 'Conditions' to all status types and implements genruntime.Conditioner on all resources", | ||
func(ctx context.Context, state *State) (*State, error) { | ||
defs := state.Types() | ||
result := make(astmodel.Types) | ||
|
||
propInjector := astmodel.NewPropertyInjector() | ||
statusDefs := astmodel.FindStatusTypes(defs) | ||
for _, def := range statusDefs { | ||
conditionsProp := astmodel.NewPropertyDefinition( | ||
astmodel.ConditionsProperty, | ||
"conditions", | ||
astmodel.NewArrayType(astmodel.ConditionTypeName)) | ||
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.FindResourceTypes(defs) | ||
for _, def := range resourceDefs { | ||
resourceType := def.Type().(*astmodel.ResourceType) | ||
|
||
conditionerImpl, err := astmodel.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) | ||
|
||
result.Add(def.WithType(resourceType)) | ||
} | ||
result = defs.OverlayWith(result) | ||
|
||
return state.WithTypes(result), nil | ||
}) | ||
} |
40 changes: 40 additions & 0 deletions
40
hack/generator/pkg/codegen/pipeline/add_status_conditions_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/* | ||
* Copyright (c) Microsoft Corporation. | ||
* Licensed under the MIT license. | ||
*/ | ||
|
||
package pipeline | ||
|
||
import ( | ||
"testing" | ||
|
||
. "github.com/onsi/gomega" | ||
|
||
"github.com/Azure/azure-service-operator/hack/generator/pkg/astmodel" | ||
"github.com/Azure/azure-service-operator/hack/generator/pkg/test" | ||
) | ||
|
||
// TestAddStatusConditions checks that the Add Status Conditions pipeline stage does what we expect | ||
func TestAddStatusConditions(t *testing.T) { | ||
g := NewGomegaWithT(t) | ||
|
||
idFactory := astmodel.NewIdentifierFactory() | ||
|
||
spec := test.CreateSpec(test.Pkg2020, "Person", test.FullNameProperty) | ||
status := test.CreateStatus(test.Pkg2020, "Person") | ||
resourceV1 := test.CreateResource(test.Pkg2020, "Person", spec, status) | ||
|
||
types := make(astmodel.Types) | ||
types.AddAll(resourceV1, spec, status) | ||
|
||
initialState := NewState().WithTypes(types) | ||
|
||
finalState, err := RunTestPipeline( | ||
initialState, | ||
AddStatusConditions(idFactory)) | ||
g.Expect(err).To(Succeed()) | ||
|
||
// When verifying the golden file, check to ensure that the Conditions property on the Status type looks correct, | ||
// and that the conditions.Conditioner interface is properly implemented on the resource. | ||
test.AssertPackagesGenerateExpectedCode(t, finalState.types) | ||
} |
52 changes: 52 additions & 0 deletions
52
...r/pkg/codegen/pipeline/testdata/TestAddStatusConditions/microsoft.person-v20200101.golden
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
// Code generated by azure-service-operator-codegen. DO NOT EDIT. | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT license. | ||
package v20200101 | ||
|
||
import ( | ||
"github.com/Azure/azure-service-operator/hack/generated/pkg/genruntime/conditions" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
) | ||
|
||
// +kubebuilder:object:root=true | ||
// +kubebuilder:subresource:status | ||
type Person struct { | ||
metav1.TypeMeta `json:",inline"` | ||
metav1.ObjectMeta `json:"metadata,omitempty"` | ||
Spec Person_Spec `json:"spec,omitempty"` | ||
Status Person_Status `json:"status,omitempty"` | ||
} | ||
|
||
var _ conditions.Conditioner = &Person{} | ||
|
||
// GetConditions returns the conditions of the resource | ||
func (person *Person) GetConditions() conditions.Conditions { | ||
return person.Status.Conditions | ||
} | ||
|
||
// SetConditions sets the conditions on the resource status | ||
func (person *Person) SetConditions(conditions conditions.Conditions) { | ||
person.Status.Conditions = conditions | ||
} | ||
|
||
// +kubebuilder:object:root=true | ||
type PersonList struct { | ||
metav1.TypeMeta `json:",inline"` | ||
metav1.ListMeta `json:"metadata,omitempty"` | ||
Items []Person `json:"items"` | ||
} | ||
|
||
type Person_Spec struct { | ||
//FullName: As would be used to address mail | ||
FullName string `json:"fullName"` | ||
} | ||
|
||
type Person_Status struct { | ||
//Conditions: The observed state of the resource | ||
Conditions []conditions.Condition `json:"conditions,omitempty"` | ||
Status string `json:"status"` | ||
} | ||
|
||
func init() { | ||
SchemeBuilder.Register(&Person{}, &PersonList{}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.