Skip to content

Commit

Permalink
Rework the way eventing-rabbitmq does Rabbit codegen
Browse files Browse the repository at this point in the history
Today we rerun codegen for the Rabbit clients to get injection, but because they use kubebuilder their layout is a bit strange.  There's a hack to temporarily copy the api types into the right structure, and then undo it, which is a bit fragile (if codegen references that package, then it won't build!).  We also try to contort so that we can use their existing client/informer/lister codegen, which isn't really necessary.

With this change, we instead permanently copy the types into a suitable structure under `third_party/pkg/apis` and generate all of the clients we need under `third_party/pkg/client`.  This is a slight superset of what Istio and Contour are already doing (we don't need to copy those types), and feels a bit simpler (and less fragile) than what we are currently doing.

/kind cleanup
  • Loading branch information
mattmoor committed Aug 17, 2021
1 parent 9bfb7b2 commit 19f27f9
Show file tree
Hide file tree
Showing 111 changed files with 8,933 additions and 44 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ require (
knative.dev/hack v0.0.0-20210622141627-e28525d8d260
knative.dev/pkg v0.0.0-20210803160015-21eb4c167cc5
knative.dev/reconciler-test v0.0.0-20210803183715-b61cc77c06f6
sigs.k8s.io/controller-runtime v0.8.3
)

replace (
Expand Down
39 changes: 19 additions & 20 deletions hack/update-codegen.sh
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,15 @@ export GOFLAGS=-mod=

echo "=== Update Codegen for $MODULE_NAME"

# RabbitMQ uses Kubebuilder
# Kubebuilder project layout has API under 'api/v1beta1', ie. 'github.com/rabbitmq/messaging-topology-operator/api/v1beta1'
# client-go codegen expects group name (rabbitmq.com) in the path, ie. 'github.com/rabbitmq/messaging-topology-operator/api/rabbitmq.com/v1beta1'
# Because there's no way how to modify any of these settings, to enable client codegen,
# we need to reorganize things a little bit (copy to 'third_party/api/rabbitmq.com/v1beta1')
rm -rf ${REPO_ROOT_DIR}/third_party/pkg/apis/rabbitmq.com
mkdir -p ${REPO_ROOT_DIR}/third_party/pkg/apis/rabbitmq.com
cp -R "${REPO_ROOT_DIR}/vendor/github.com/rabbitmq/messaging-topology-operator/api/v1beta1" ${REPO_ROOT_DIR}/third_party/pkg/apis/rabbitmq.com

group "Kubernetes Codegen"

# generate the code with:
Expand All @@ -42,6 +51,12 @@ ${CODEGEN_PKG}/generate-groups.sh "deepcopy" \
"duck:v1beta1" \
--go-header-file ${REPO_ROOT_DIR}/hack/boilerplate/boilerplate.go.txt

# Generate our own client (otherwise injection won't work)
${CODEGEN_PKG}/generate-groups.sh "client,informer,lister" \
knative.dev/eventing-rabbitmq/third_party/pkg/client knative.dev/eventing-rabbitmq/third_party/pkg/apis \
"rabbitmq.com:v1beta1" \
--go-header-file ${REPO_ROOT_DIR}/hack/boilerplate/boilerplate.go.txt

group "Knative Codegen"

# Knative Injection
Expand All @@ -52,26 +67,10 @@ ${KNATIVE_CODEGEN_PKG}/hack/generate-knative.sh "injection" \

group "RabbitMQ Codegen"

# RabbitMQ uses Kubebuilder
# Kubebuilder project layout has API under 'api/v1beta1', ie. 'github.com/rabbitmq/messaging-topology-operator/api/v1beta1'
# client-go codegen expects group name (rabbitmq.com) in the path, ie. 'github.com/rabbitmq/messaging-topology-operator/api/rabbitmq.com/v1beta1'
# Because there's no way how to modify any of these settings, to enable client codegen,
# we need to hack things a little bit (in vendor move temporarily api directory in 'api/rabbitmq.com/v1beta1')
rm -rf ${REPO_ROOT_DIR}/vendor/github.com/rabbitmq/messaging-topology-operator/api/rabbitmq.com
mkdir ${REPO_ROOT_DIR}/vendor/github.com/rabbitmq/messaging-topology-operator/api/rabbitmq.com
cp -R "${REPO_ROOT_DIR}/vendor/github.com/rabbitmq/messaging-topology-operator/api/v1beta1/" ${REPO_ROOT_DIR}/vendor/github.com/rabbitmq/messaging-topology-operator/api/rabbitmq.com/v1beta1

OUTPUT_PKG="knative.dev/eventing-rabbitmq/pkg/client/injection/rabbitmq.com" \
VERSIONED_CLIENTSET_PKG="github.com/rabbitmq/messaging-topology-operator/pkg/generated/clientset/versioned" \
EXTERNAL_INFORMER_PKG="github.com/rabbitmq/messaging-topology-operator/pkg/generated/informers/externalversions" \
${KNATIVE_CODEGEN_PKG}/hack/generate-knative.sh "injection" \
github.com/rabbitmq/messaging-topology-operator/rabbitmq.com \
github.com/rabbitmq/messaging-topology-operator/api \
"rabbitmq.com:v1beta1" \
--go-header-file ${REPO_ROOT_DIR}/hack/boilerplate/boilerplate.go.txt \

mv ${REPO_ROOT_DIR}/vendor/github.com/rabbitmq/messaging-topology-operator/api/rabbitmq.com/v1beta1 ${REPO_ROOT_DIR}/vendor/github.com/rabbitmq/messaging-topology-operator/api/v1beta1
rm -rf ${REPO_ROOT_DIR}/vendor/github.com/rabbitmq/messaging-topology-operator/api/rabbitmq.com
${KNATIVE_CODEGEN_PKG}/hack/generate-knative.sh "injection" \
knative.dev/eventing-rabbitmq/third_party/pkg/client knative.dev/eventing-rabbitmq/third_party/pkg/apis \
"rabbitmq.com:v1beta1" \
--go-header-file ${REPO_ROOT_DIR}/hack/boilerplate/boilerplate.go.txt

group "Update deps post-codegen"

Expand Down
4 changes: 2 additions & 2 deletions hack/update-deps.sh
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ source $(dirname "$0")/../vendor/knative.dev/hack/library.sh

go_update_deps "$@"

rm -rf $(find third_party/ -name '*.mod')
rm -rf $(find third_party/ -name '*.go')
rm -rf $(find third_party/VENDOR-LICENSE/ -name '*.mod')
rm -rf $(find third_party/VENDOR-LICENSE/ -name '*.go')

# Remove the _webhook.go files that cause (unnecessarily) controller-runtime
# to get pulled in, which in turn causes issues with double-defining 'kubeconfig'
Expand Down
2 changes: 1 addition & 1 deletion pkg/reconciler/broker/broker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ import (
clientgotesting "k8s.io/client-go/testing"
rabbitmqduck "knative.dev/eventing-rabbitmq/pkg/apis/duck/v1beta1"
rabbitduck "knative.dev/eventing-rabbitmq/pkg/client/injection/ducks/duck/v1beta1/rabbit"
fakerabbitclient "knative.dev/eventing-rabbitmq/pkg/client/injection/rabbitmq.com/client/fake"
fakerabbitclient "knative.dev/eventing-rabbitmq/third_party/pkg/client/injection/client/fake"
"knative.dev/eventing-rabbitmq/pkg/reconciler/broker/resources"
eventingduckv1 "knative.dev/eventing/pkg/apis/duck/v1"
"knative.dev/eventing/pkg/apis/eventing"
Expand Down
8 changes: 4 additions & 4 deletions pkg/reconciler/broker/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (
"log"

"knative.dev/eventing-rabbitmq/pkg/client/injection/ducks/duck/v1beta1/rabbit"
rabbitmqclient "knative.dev/eventing-rabbitmq/pkg/client/injection/rabbitmq.com/client"
rabbitmqclient "knative.dev/eventing-rabbitmq/third_party/pkg/client/injection/client"
eventingclient "knative.dev/eventing/pkg/client/injection/client"

"github.com/kelseyhightower/envconfig"
Expand All @@ -31,9 +31,9 @@ import (
"knative.dev/eventing/pkg/apis/eventing"
eventingv1 "knative.dev/eventing/pkg/apis/eventing/v1"

bindinginformer "knative.dev/eventing-rabbitmq/pkg/client/injection/rabbitmq.com/informers/rabbitmq.com/v1beta1/binding"
exchangeinformer "knative.dev/eventing-rabbitmq/pkg/client/injection/rabbitmq.com/informers/rabbitmq.com/v1beta1/exchange"
queueinformer "knative.dev/eventing-rabbitmq/pkg/client/injection/rabbitmq.com/informers/rabbitmq.com/v1beta1/queue"
bindinginformer "knative.dev/eventing-rabbitmq/third_party/pkg/client/injection/informers/rabbitmq.com/v1beta1/binding"
exchangeinformer "knative.dev/eventing-rabbitmq/third_party/pkg/client/injection/informers/rabbitmq.com/v1beta1/exchange"
queueinformer "knative.dev/eventing-rabbitmq/third_party/pkg/client/injection/informers/rabbitmq.com/v1beta1/queue"
brokerinformer "knative.dev/eventing/pkg/client/injection/informers/eventing/v1/broker"
brokerreconciler "knative.dev/eventing/pkg/client/injection/reconciler/eventing/v1/broker"
"knative.dev/eventing/pkg/duck"
Expand Down
8 changes: 4 additions & 4 deletions pkg/reconciler/broker/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ import (

// Fake injection informers
_ "knative.dev/eventing-rabbitmq/pkg/client/injection/ducks/duck/v1beta1/rabbit/fake"
_ "knative.dev/eventing-rabbitmq/pkg/client/injection/rabbitmq.com/client/fake"
_ "knative.dev/eventing-rabbitmq/pkg/client/injection/rabbitmq.com/informers/rabbitmq.com/v1beta1/binding/fake"
_ "knative.dev/eventing-rabbitmq/pkg/client/injection/rabbitmq.com/informers/rabbitmq.com/v1beta1/exchange/fake"
_ "knative.dev/eventing-rabbitmq/pkg/client/injection/rabbitmq.com/informers/rabbitmq.com/v1beta1/queue/fake"
_ "knative.dev/eventing-rabbitmq/third_party/pkg/client/injection/client/fake"
_ "knative.dev/eventing-rabbitmq/third_party/pkg/client/injection/informers/rabbitmq.com/v1beta1/binding/fake"
_ "knative.dev/eventing-rabbitmq/third_party/pkg/client/injection/informers/rabbitmq.com/v1beta1/exchange/fake"
_ "knative.dev/eventing-rabbitmq/third_party/pkg/client/injection/informers/rabbitmq.com/v1beta1/queue/fake"
_ "knative.dev/eventing/pkg/client/injection/informers/eventing/v1/broker/fake"
_ "knative.dev/pkg/client/injection/ducks/duck/v1/addressable/fake"
_ "knative.dev/pkg/client/injection/ducks/duck/v1/conditions/fake"
Expand Down
2 changes: 1 addition & 1 deletion pkg/reconciler/testing/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ import (
"knative.dev/pkg/controller"
"knative.dev/pkg/reconciler"

fakerabbitclient "knative.dev/eventing-rabbitmq/pkg/client/injection/rabbitmq.com/client/fake"
fakerabbitclient "knative.dev/eventing-rabbitmq/third_party/pkg/client/injection/client/fake"
fakeeventingclient "knative.dev/eventing/pkg/client/injection/client/fake"
fakekubeclient "knative.dev/pkg/client/injection/kube/client/fake"
fakedynamicclient "knative.dev/pkg/injection/clients/dynamicclient/fake"
Expand Down
8 changes: 4 additions & 4 deletions pkg/reconciler/trigger/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (
"context"
"log"

rabbitmqclient "knative.dev/eventing-rabbitmq/pkg/client/injection/rabbitmq.com/client"
rabbitmqclient "knative.dev/eventing-rabbitmq/third_party/pkg/client/injection/client"
eventingclient "knative.dev/eventing/pkg/client/injection/client"
kubeclient "knative.dev/pkg/client/injection/kube/client"
"knative.dev/pkg/injection/clients/dynamicclient"
Expand All @@ -32,8 +32,8 @@ import (

v1 "knative.dev/eventing/pkg/apis/eventing/v1"

bindinginformer "knative.dev/eventing-rabbitmq/pkg/client/injection/rabbitmq.com/informers/rabbitmq.com/v1beta1/binding"
exchangeinformer "knative.dev/eventing-rabbitmq/pkg/client/injection/rabbitmq.com/informers/rabbitmq.com/v1beta1/exchange"
bindinginformer "knative.dev/eventing-rabbitmq/third_party/pkg/client/injection/informers/rabbitmq.com/v1beta1/binding"
exchangeinformer "knative.dev/eventing-rabbitmq/third_party/pkg/client/injection/informers/rabbitmq.com/v1beta1/exchange"
brokerinformer "knative.dev/eventing/pkg/client/injection/informers/eventing/v1/broker"
triggerinformer "knative.dev/eventing/pkg/client/injection/informers/eventing/v1/trigger"
brokerreconciler "knative.dev/eventing/pkg/client/injection/reconciler/eventing/v1/broker"
Expand All @@ -43,7 +43,7 @@ import (
"knative.dev/pkg/client/injection/ducks/duck/v1/source"
deploymentinformer "knative.dev/pkg/client/injection/kube/informers/apps/v1/deployment"

queueinformer "knative.dev/eventing-rabbitmq/pkg/client/injection/rabbitmq.com/informers/rabbitmq.com/v1beta1/queue"
queueinformer "knative.dev/eventing-rabbitmq/third_party/pkg/client/injection/informers/rabbitmq.com/v1beta1/queue"

"knative.dev/pkg/configmap"
"knative.dev/pkg/controller"
Expand Down
8 changes: 4 additions & 4 deletions pkg/reconciler/trigger/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ import (

// Fake injection informers
_ "knative.dev/eventing-rabbitmq/pkg/client/injection/ducks/duck/v1beta1/rabbit/fake"
_ "knative.dev/eventing-rabbitmq/pkg/client/injection/rabbitmq.com/client/fake"
_ "knative.dev/eventing-rabbitmq/pkg/client/injection/rabbitmq.com/informers/rabbitmq.com/v1beta1/binding/fake"
_ "knative.dev/eventing-rabbitmq/pkg/client/injection/rabbitmq.com/informers/rabbitmq.com/v1beta1/exchange/fake"
_ "knative.dev/eventing-rabbitmq/pkg/client/injection/rabbitmq.com/informers/rabbitmq.com/v1beta1/queue/fake"
_ "knative.dev/eventing-rabbitmq/third_party/pkg/client/injection/client/fake"
_ "knative.dev/eventing-rabbitmq/third_party/pkg/client/injection/informers/rabbitmq.com/v1beta1/binding/fake"
_ "knative.dev/eventing-rabbitmq/third_party/pkg/client/injection/informers/rabbitmq.com/v1beta1/exchange/fake"
_ "knative.dev/eventing-rabbitmq/third_party/pkg/client/injection/informers/rabbitmq.com/v1beta1/queue/fake"
_ "knative.dev/eventing/pkg/client/injection/informers/eventing/v1/broker/fake"
_ "knative.dev/eventing/pkg/client/injection/informers/eventing/v1/trigger/fake"
_ "knative.dev/pkg/client/injection/ducks/duck/v1/addressable/fake"
Expand Down
2 changes: 1 addition & 1 deletion pkg/reconciler/trigger/trigger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ import (
rabbitv1beta1 "github.com/rabbitmq/messaging-topology-operator/api/v1beta1"
clientgotesting "k8s.io/client-go/testing"
rabbitduck "knative.dev/eventing-rabbitmq/pkg/client/injection/ducks/duck/v1beta1/rabbit"
fakerabbitclient "knative.dev/eventing-rabbitmq/pkg/client/injection/rabbitmq.com/client/fake"
fakerabbitclient "knative.dev/eventing-rabbitmq/third_party/pkg/client/injection/client/fake"
naming "knative.dev/eventing-rabbitmq/pkg/rabbitmqnaming"
"knative.dev/eventing-rabbitmq/pkg/reconciler/broker"
"knative.dev/eventing-rabbitmq/pkg/reconciler/trigger/resources"
Expand Down
6 changes: 3 additions & 3 deletions pkg/reconciler/triggerstandalone/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ import (

// Fake injection informers
_ "knative.dev/eventing-rabbitmq/pkg/client/injection/ducks/duck/v1beta1/rabbit/fake"
_ "knative.dev/eventing-rabbitmq/pkg/client/injection/rabbitmq.com/client/fake"
_ "knative.dev/eventing-rabbitmq/pkg/client/injection/rabbitmq.com/informers/rabbitmq.com/v1beta1/binding/fake"
_ "knative.dev/eventing-rabbitmq/pkg/client/injection/rabbitmq.com/informers/rabbitmq.com/v1beta1/queue/fake"
_ "knative.dev/eventing-rabbitmq/third_party/pkg/client/injection/client/fake"
_ "knative.dev/eventing-rabbitmq/third_party/pkg/client/injection/informers/rabbitmq.com/v1beta1/binding/fake"
_ "knative.dev/eventing-rabbitmq/third_party/pkg/client/injection/informers/rabbitmq.com/v1beta1/queue/fake"
_ "knative.dev/eventing/pkg/client/injection/informers/eventing/v1/broker/fake"
_ "knative.dev/eventing/pkg/client/injection/informers/eventing/v1/trigger/fake"
_ "knative.dev/pkg/client/injection/ducks/duck/v1/addressable/fake"
Expand Down
81 changes: 81 additions & 0 deletions third_party/pkg/apis/rabbitmq.com/v1beta1/binding_types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/*
RabbitMQ Messaging Topology Kubernetes Operator
Copyright 2021 VMware, Inc.
This product is licensed to you under the Mozilla Public License 2.0 license (the "License"). You may not use this product except in compliance with the Mozilla 2.0 License.
This product may include a number of subcomponents with separate copyright notices and license terms. Your use of these subcomponents is subject to the terms and conditions of the subcomponent's license, as noted in the LICENSE file.
*/

package v1beta1

import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
)

// BindingSpec defines the desired state of Binding
type BindingSpec struct {
// Default to vhost '/'
// +kubebuilder:default:=/
Vhost string `json:"vhost,omitempty"`
// +kubebuilder:validation:Optional
Source string `json:"source,omitempty"`
// +kubebuilder:validation:Optional
Destination string `json:"destination,omitempty"`
// +kubebuilder:validation:Optional
// +kubebuilder:validation:Enum=exchange;queue
DestinationType string `json:"destinationType,omitempty"`
// +kubebuilder:validation:Optional
RoutingKey string `json:"routingKey,omitempty"`
// +kubebuilder:validation:Type=object
// +kubebuilder:pruning:PreserveUnknownFields
Arguments *runtime.RawExtension `json:"arguments,omitempty"`
// Reference to the RabbitmqCluster that the binding will be created in.
// Required property.
// +kubebuilder:validation:Required
RabbitmqClusterReference RabbitmqClusterReference `json:"rabbitmqClusterReference"`
}

// BindingStatus defines the observed state of Binding
type BindingStatus struct {
// observedGeneration is the most recent successful generation observed for this Binding. It corresponds to the
// Binding's generation, which is updated on mutation by the API Server.
ObservedGeneration int64 `json:"observedGeneration,omitempty"`
Conditions []Condition `json:"conditions,omitempty"`
}

// +genclient
// +kubebuilder:object:root=true
// +kubebuilder:resource:categories=all
// +kubebuilder:subresource:status

// Binding is the Schema for the bindings API
type Binding struct {
metav1.TypeMeta `json:",inline"`
metav1.ObjectMeta `json:"metadata,omitempty"`

Spec BindingSpec `json:"spec,omitempty"`
Status BindingStatus `json:"status,omitempty"`
}

// +kubebuilder:object:root=true

// BindingList contains a list of Binding
type BindingList struct {
metav1.TypeMeta `json:",inline"`
metav1.ListMeta `json:"metadata,omitempty"`
Items []Binding `json:"items"`
}

func (b *Binding) GroupResource() schema.GroupResource {
return schema.GroupResource{
Group: b.GroupVersionKind().Group,
Resource: b.GroupVersionKind().Kind,
}
}

func init() {
SchemeBuilder.Register(&Binding{}, &BindingList{})
}
44 changes: 44 additions & 0 deletions third_party/pkg/apis/rabbitmq.com/v1beta1/conditions.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package v1beta1

import (
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

const ready ConditionType = "Ready"

type ConditionType string

type Condition struct {
// Type indicates the scope of RabbitmqCluster status addressed by the condition.
Type ConditionType `json:"type"`
// True, False, or Unknown
Status corev1.ConditionStatus `json:"status"`
// The last time this Condition type changed.
LastTransitionTime metav1.Time `json:"lastTransitionTime,omitempty"`
// One word, camel-case reason for current status of the condition.
Reason string `json:"reason,omitempty"`
// Full text reason for current status of the condition.
Message string `json:"message,omitempty"`
}

// Ready indicates that the last Create/Update operator on the CR was successful.
func Ready() Condition {
return Condition{
Type: ready,
Status: corev1.ConditionTrue,
LastTransitionTime: metav1.Now(),
Reason: "SuccessfulCreateOrUpdate",
}
}

// NotReady indicates that the last Create/Update operator on the CR failed.
func NotReady(msg string) Condition {
return Condition{
Type: ready,
Status: corev1.ConditionFalse,
LastTransitionTime: metav1.Now(),
Reason: "FailedCreateOrUpdate",
Message: msg,
}
}
Loading

0 comments on commit 19f27f9

Please sign in to comment.