Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion api/v1alpha1/operator_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,20 @@ type OperatorSpec struct {
}

const (
// TODO(user): add more Types
// TODO(user): add more Types, here and into GetTypes()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like this note.

TypeReady = "Ready"

// TODO(user): add more Reasons
ReasonNotImplemented = "NotImplemented"
)

func GetTypes() []string {
// TODO(user): add Types from above
return []string{
TypeReady,
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if there's a way to do this without exporting GetTypes(). This is more of an implementation detail than something that should be part of the API.

This is also only something we'd use in tests, I think?

Maybe something like:

internal/operatorutil/util.go:

// ConditionTypes is the full set of Operator condition types.
//
// NOTE: This is populated by the init function in 
//       api/v1alpha1/operator_types.go
var ConditionTypes []string

And then here:

func init() {
	operatorutil.ConditionTypes = append(operatorutil.ConditionTypes,
		// Add all Operator condition types to this list.
		TypeReady,
	)
)

WDYT? Is there another way I'm not thinking of that:

  • Keeps everything self-contained in the v1alpha1 packagae
  • Keeps the list of types internal
  • Makes the list of types available to tests, perhaps in both reconciler unit tests and e2es?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let me look.


// OperatorStatus defines the observed state of Operator
type OperatorStatus struct {
// +patchMergeKey=type
Expand Down
4 changes: 2 additions & 2 deletions controllers/operator_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,15 +71,15 @@ func (r *OperatorReconciler) Reconcile(ctx context.Context, req ctrl.Request) (c
compareOp := reconciledOp.DeepCopy()
existingOp.Status, compareOp.Status = operatorsv1alpha1.OperatorStatus{}, operatorsv1alpha1.OperatorStatus{}
existingOp.Finalizers, compareOp.Finalizers = []string{}, []string{}
specDiffers := !equality.Semantic.DeepEqual(existingOp, compareOp)
unexpectedFieldsChanged := !equality.Semantic.DeepEqual(existingOp, compareOp)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could we create a function that takes the reconciledOp and returns if an unexpected field was changed? it may make things easier and would allow us to update the block above to look like:

updateStatus := !equality.Semantic.DeepEqual(existingOp.Status, reconciledOp.Status)
updateFinalizers := !equality.Semantic.DeepEqual(existingOp.Finalizers, reconciledOp.Finalizers)
unexpectedFieldsChanged := checkForUnexpectedFieldChanges(reconciledOp) # Function name could be different.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You'd have to also pass existingOp in, but yeah, this seems like a good idea.


if updateStatus {
if updateErr := r.Status().Update(ctx, reconciledOp); updateErr != nil {
return res, utilerrors.NewAggregate([]error{reconcileErr, updateErr})
}
}

if specDiffers {
if unexpectedFieldsChanged {
panic("spec or metadata changed by reconciler")
}

Expand Down
21 changes: 19 additions & 2 deletions controllers/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ var _ = Describe("Reconcile Test", func() {
err = k8sClient.Delete(ctx, operator)
Expect(err).To(Not(HaveOccurred()))
})
It("has a Condition created", func() {
It("has all Conditions created", func() {
getOperator := &operatorsv1alpha1.Operator{}

err = k8sClient.Get(ctx, client.ObjectKey{
Expand All @@ -138,7 +138,24 @@ var _ = Describe("Reconcile Test", func() {
// There should always be a "Ready" condition, regardless of Status.
conds := getOperator.Status.Conditions
Expect(conds).To(Not(BeEmpty()))
Expect(conds).To(ContainElement(HaveField("Type", operatorsv1alpha1.TypeReady)))
types := operatorsv1alpha1.GetTypes()
Expect(conds).To(HaveLen(len(types)))
for _, t := range types {
Expect(conds).To(ContainElement(HaveField("Type", t)))
}
})
It("has matching gnerations in Conditions", func() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think there's a misspelling here, but I'm not sure if generations was the intended word to be spelt.

Suggested change
It("has matching gnerations in Conditions", func() {
It("has matching generations in Conditions", func() {

getOperator := &operatorsv1alpha1.Operator{}

err = k8sClient.Get(ctx, client.ObjectKey{
Name: opName,
}, getOperator)
Expect(err).To(Not(HaveOccurred()))

// The ObservedGeneration should match the resource generation
for _, c := range getOperator.Status.Conditions {
Expect(c).To(HaveField("ObservedGeneration", getOperator.GetGeneration()))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: HaveField doesn't give us compile-time checks on the field name, so its susceptible to typos, for example.

Suggested change
Expect(c).To(HaveField("ObservedGeneration", getOperator.GetGeneration()))
Expect(c.ObservedGeneration).To(Equal(getOperator.GetGeneration()))

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd agree with you 100% if this were not test code. Test code, on the other hand, I could see it either way.

}
})
})
})