Skip to content

Commit

Permalink
chore: move the cluster and component controllers to separate packages (
Browse files Browse the repository at this point in the history
  • Loading branch information
leon-inf authored Jan 10, 2025
1 parent 47fdd7a commit ac3c0c8
Show file tree
Hide file tree
Showing 83 changed files with 1,946 additions and 1,055 deletions.
6 changes: 4 additions & 2 deletions cmd/manager/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ import (
workloadsv1 "github.com/apecloud/kubeblocks/apis/workloads/v1"
workloadsv1alpha1 "github.com/apecloud/kubeblocks/apis/workloads/v1alpha1"
appscontrollers "github.com/apecloud/kubeblocks/controllers/apps"
"github.com/apecloud/kubeblocks/controllers/apps/cluster"
"github.com/apecloud/kubeblocks/controllers/apps/component"
"github.com/apecloud/kubeblocks/controllers/apps/configuration"
experimentalcontrollers "github.com/apecloud/kubeblocks/controllers/experimental"
extensionscontrollers "github.com/apecloud/kubeblocks/controllers/extensions"
Expand Down Expand Up @@ -443,7 +445,7 @@ func main() {
os.Exit(1)
}

if err = (&appscontrollers.ClusterReconciler{
if err = (&cluster.ClusterReconciler{
Client: client,
Scheme: mgr.GetScheme(),
Recorder: mgr.GetEventRecorderFor("cluster-controller"),
Expand All @@ -453,7 +455,7 @@ func main() {
os.Exit(1)
}

if err = (&appscontrollers.ComponentReconciler{
if err = (&component.ComponentReconciler{
Client: client,
Scheme: mgr.GetScheme(),
Recorder: mgr.GetEventRecorderFor("component-controller"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

package apps
package cluster

import (
"context"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

package apps
package cluster

import (
"encoding/json"
Expand Down Expand Up @@ -422,7 +422,7 @@ var _ = Describe("Cluster Controller", func() {
clusterKey = client.ObjectKeyFromObject(clusterObj)

By("waiting for the cluster controller to create resources completely")
waitForCreatingResourceCompletely(clusterKey, compName, otherCompName)
waitForCreatingResourceCompletely(clusterKey)

By("scale in the target component")
Expect(testapps.GetAndChangeObj(&testCtx, clusterKey, func(cluster *appsv1.Cluster) {
Expand Down Expand Up @@ -512,19 +512,6 @@ var _ = Describe("Cluster Controller", func() {
g.Expect(svc.Spec.Selector).Should(HaveKeyWithValue(constant.RoleLabelKey, constant.Follower))
g.Expect(svc.Spec.ExternalTrafficPolicy).Should(BeEquivalentTo(corev1.ServiceExternalTrafficPolicyTypeLocal))
})).Should(Succeed())

By("check default component service created")
compSvcKey := types.NamespacedName{
Namespace: clusterKey.Namespace,
Name: constant.GenerateComponentServiceName(clusterObj.Name, compName, ""),
}
Eventually(testapps.CheckObj(&testCtx, compSvcKey, func(g Gomega, svc *corev1.Service) {
g.Expect(svc.Spec.Selector).Should(HaveKeyWithValue(constant.AppManagedByLabelKey, constant.AppName))
g.Expect(svc.Spec.Selector).Should(HaveKeyWithValue(constant.AppInstanceLabelKey, clusterObj.Name))
g.Expect(svc.Spec.Selector).Should(HaveKeyWithValue(constant.KBAppComponentLabelKey, compName))
g.Expect(svc.Spec.Selector).Should(HaveKey(constant.RoleLabelKey))
g.Expect(svc.Spec.Selector).Should(HaveKeyWithValue(constant.RoleLabelKey, constant.Leader))
})).Should(Succeed())
}

type expectService struct {
Expand Down Expand Up @@ -1199,7 +1186,8 @@ var _ = Describe("Cluster Controller", func() {
})).Should(Succeed())
})

Context("cluster component annotations and labels", func() {
// TODO: refactor the case and should not depend on objects created by the component controller
PContext("cluster component annotations and labels", func() {
BeforeEach(func() {
cleanEnv()
createAllDefinitionObjects()
Expand All @@ -1216,21 +1204,22 @@ var _ = Describe("Cluster Controller", func() {
(*metaMap)[key] = value
}

checkRelatedObject := func(compName string, checkFunc func(g Gomega, obj client.Object)) {
// check related services of the component
defaultSvcName := constant.GenerateComponentServiceName(clusterObj.Name, compName, "")
Eventually(testapps.CheckObj(&testCtx, client.ObjectKey{Name: defaultSvcName,
Namespace: testCtx.DefaultNamespace}, func(g Gomega, svc *corev1.Service) {
checkFunc(g, svc)
})).Should(Succeed())

// check related account secret of the component
rootAccountSecretName := constant.GenerateAccountSecretName(clusterObj.Name, compName, "root")
Eventually(testapps.CheckObj(&testCtx, client.ObjectKey{Name: rootAccountSecretName,
Namespace: testCtx.DefaultNamespace}, func(g Gomega, secret *corev1.Secret) {
checkFunc(g, secret)
})).Should(Succeed())
}
// TODO: remove it
// checkRelatedObject := func(compName string, checkFunc func(g Gomega, obj client.Object)) {
// // check related services of the component
// defaultSvcName := constant.GenerateComponentServiceName(clusterObj.Name, compName, "")
// Eventually(testapps.CheckObj(&testCtx, client.ObjectKey{Name: defaultSvcName,
// Namespace: testCtx.DefaultNamespace}, func(g Gomega, svc *corev1.Service) {
// checkFunc(g, svc)
// })).Should(Succeed())
//
// // check related account secret of the component
// rootAccountSecretName := constant.GenerateAccountSecretName(clusterObj.Name, compName, "root")
// Eventually(testapps.CheckObj(&testCtx, client.ObjectKey{Name: rootAccountSecretName,
// Namespace: testCtx.DefaultNamespace}, func(g Gomega, secret *corev1.Secret) {
// checkFunc(g, secret)
// })).Should(Succeed())
// }

testUpdateAnnoAndLabels := func(compName string,
changeCluster func(cluster *appsv1.Cluster),
Expand All @@ -1247,17 +1236,19 @@ var _ = Describe("Cluster Controller", func() {
checkWorkloadFunc(g, compObj.Spec.Labels, compObj.Spec.Annotations, false)
})).Should(Succeed())

By("check related objects annotations and labels")
checkRelatedObject(defaultCompName, func(g Gomega, obj client.Object) {
checkRelatedObjFunc(g, obj)
})

By("InstanceSet.spec.template.annotations/labels need to be consistent with component")
// The labels and annotations of the Pod will be kept consistent with those of the InstanceSet
Eventually(testapps.CheckObj(&testCtx, client.ObjectKey{Name: workloadName, Namespace: testCtx.DefaultNamespace},
func(g Gomega, instanceSet *workloadsv1.InstanceSet) {
checkWorkloadFunc(g, instanceSet.Spec.Template.GetLabels(), instanceSet.Spec.Template.GetAnnotations(), true)
})).Should(Succeed())
// TODO: remove it
// By("check related objects annotations and labels")
// checkRelatedObject(defaultCompName, func(g Gomega, obj client.Object) {
// checkRelatedObjFunc(g, obj)
// })

// TODO: remove it
// By("InstanceSet.spec.template.annotations/labels need to be consistent with component")
//// The labels and annotations of the Pod will be kept consistent with those of the InstanceSet
// Eventually(testapps.CheckObj(&testCtx, client.ObjectKey{Name: workloadName, Namespace: testCtx.DefaultNamespace},
// func(g Gomega, instanceSet *workloadsv1.InstanceSet) {
// checkWorkloadFunc(g, instanceSet.Spec.Template.GetLabels(), instanceSet.Spec.Template.GetAnnotations(), true)
// })).Should(Succeed())
}

It("test add/override annotations and labels", func() {
Expand Down Expand Up @@ -1342,7 +1333,6 @@ var _ = Describe("Cluster Controller", func() {
g.Expect(obj.GetLabels()[key2]).Should(Equal(value2))
g.Expect(obj.GetAnnotations()[key2]).Should(Equal(value2))
})

})
})
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

package apps
package cluster

import (
"context"
Expand Down Expand Up @@ -194,11 +194,6 @@ func (c *clusterPlanBuilder) AddTransformer(transformer ...graph.Transformer) gr
return c
}

func (c *clusterPlanBuilder) AddParallelTransformer(transformer ...graph.Transformer) graph.PlanBuilder {
c.transformers = append(c.transformers, &ParallelTransformers{transformers: transformer})
return c
}

// Build runs all transformers to generate a plan
func (c *clusterPlanBuilder) Build() (graph.Plan, error) {
var err error
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

package apps
package cluster

import (
. "github.com/onsi/ginkgo/v2"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

package apps
package cluster

import (
"fmt"
Expand Down
Loading

0 comments on commit ac3c0c8

Please sign in to comment.