Skip to content

Commit

Permalink
chore: remove service field in instanceset (#8378)
Browse files Browse the repository at this point in the history
  • Loading branch information
cjc7373 authored Nov 6, 2024
1 parent 67a0f6f commit 19fb216
Show file tree
Hide file tree
Showing 18 changed files with 2 additions and 1,343 deletions.
10 changes: 0 additions & 10 deletions apis/workloads/v1/instanceset_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,16 +98,6 @@ type InstanceSetSpec struct {
// More info: https://kubernetes.io/docs/concepts/overview/working-with-objects/labels/#label-selectors
Selector *metav1.LabelSelector `json:"selector"`

// Defines the behavior of a service spec.
// Provides read-write service.
// https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#spec-and-status
//
// Note: This field will be removed in future version.
//
// +kubebuilder:pruning:PreserveUnknownFields
// +optional
Service *corev1.Service `json:"service,omitempty"`

Template corev1.PodTemplateSpec `json:"template"`

// Overrides values in default Template.
Expand Down
5 changes: 0 additions & 5 deletions apis/workloads/v1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

543 changes: 0 additions & 543 deletions config/crd/bases/workloads.kubeblocks.io_instancesets.yaml

Large diffs are not rendered by default.

22 changes: 0 additions & 22 deletions controllers/apps/transformer_component_workload.go
Original file line number Diff line number Diff line change
Expand Up @@ -280,27 +280,6 @@ func copyAndMergeITS(oldITS, newITS *workloads.InstanceSet, synthesizeComp *comp
}
}

// be compatible with existed cluster
updateService := func(itsObj, itsProto *workloads.InstanceSet) *corev1.Service {
if itsProto.Spec.Service != nil {
return itsProto.Spec.Service
}
if itsObj.Spec.Service == nil {
return nil
}
defaultServiceName := itsObj.Name
for _, svc := range synthesizeComp.ComponentServices {
if svc.PodService != nil && *svc.PodService || svc.DisableAutoProvision != nil && *svc.DisableAutoProvision {
continue
}
serviceName := constant.GenerateComponentServiceName(synthesizeComp.ClusterName, synthesizeComp.Name, svc.ServiceName)
if defaultServiceName == serviceName {
return itsObj.Spec.Service
}
}
return nil
}

itsObjCopy := oldITS.DeepCopy()
itsProto := newITS

Expand All @@ -321,7 +300,6 @@ func copyAndMergeITS(oldITS, newITS *workloads.InstanceSet, synthesizeComp *comp
mergeMetadataMap(itsObjCopy.Spec.Template.Annotations, &itsProto.Spec.Template.Annotations)
itsObjCopy.Spec.Template = *itsProto.Spec.Template.DeepCopy()
itsObjCopy.Spec.Replicas = itsProto.Spec.Replicas
itsObjCopy.Spec.Service = updateService(itsObjCopy, itsProto)
itsObjCopy.Spec.Roles = itsProto.Spec.Roles
itsObjCopy.Spec.RoleProbe = itsProto.Spec.RoleProbe
itsObjCopy.Spec.MembershipReconfiguration = itsProto.Spec.MembershipReconfiguration
Expand Down
12 changes: 0 additions & 12 deletions controllers/workloads/instanceset_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,6 @@ var _ = Describe("InstanceSet Controller", func() {
It("should reconcile well", func() {
name := "test-instance-set"
port := int32(12345)
service := &corev1.Service{
Spec: corev1.ServiceSpec{
Ports: []corev1.ServicePort{
{
Name: "foo",
Protocol: corev1.ProtocolTCP,
Port: port,
},
},
},
}
commonLabels := map[string]string{
constant.AppManagedByLabelKey: constant.AppName,
constant.AppNameLabelKey: "ClusterDefName",
Expand Down Expand Up @@ -79,7 +68,6 @@ var _ = Describe("InstanceSet Controller", func() {
}
its := builder.NewInstanceSetBuilder(testCtx.DefaultNamespace, name).
AddMatchLabelsInMap(commonLabels).
SetService(service).
SetTemplate(template).
AddCustomHandler(action).
GetObject()
Expand Down
543 changes: 0 additions & 543 deletions deploy/helm/crds/workloads.kubeblocks.io_instancesets.yaml

Large diffs are not rendered by default.

34 changes: 0 additions & 34 deletions docs/developer_docs/api-reference/cluster.md
Original file line number Diff line number Diff line change
Expand Up @@ -28104,23 +28104,6 @@ More info: <a href="https://kubernetes.io/docs/concepts/overview/working-with-ob
</tr>
<tr>
<td>
<code>service</code><br/>
<em>
<a href="https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.25/#service-v1-core">
Kubernetes core/v1.Service
</a>
</em>
</td>
<td>
<em>(Optional)</em>
<p>Defines the behavior of a service spec.
Provides read-write service.
<a href="https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#spec-and-status">https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#spec-and-status</a></p>
<p>Note: This field will be removed in future version.</p>
</td>
</tr>
<tr>
<td>
<code>template</code><br/>
<em>
<a href="https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.25/#podtemplatespec-v1-core">
Expand Down Expand Up @@ -28652,23 +28635,6 @@ More info: <a href="https://kubernetes.io/docs/concepts/overview/working-with-ob
</tr>
<tr>
<td>
<code>service</code><br/>
<em>
<a href="https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.25/#service-v1-core">
Kubernetes core/v1.Service
</a>
</em>
</td>
<td>
<em>(Optional)</em>
<p>Defines the behavior of a service spec.
Provides read-write service.
<a href="https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#spec-and-status">https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#spec-and-status</a></p>
<p>Note: This field will be removed in future version.</p>
</td>
</tr>
<tr>
<td>
<code>template</code><br/>
<em>
<a href="https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.25/#podtemplatespec-v1-core">
Expand Down
5 changes: 0 additions & 5 deletions pkg/controller/builder/builder_instance_set.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,11 +155,6 @@ func (builder *InstanceSetBuilder) SetRoleProbe(roleProbe *workloads.RoleProbe)
return builder
}

func (builder *InstanceSetBuilder) SetService(service *corev1.Service) *InstanceSetBuilder {
builder.get().Spec.Service = service
return builder
}

func (builder *InstanceSetBuilder) SetMembershipReconfiguration(reconfiguration *workloads.MembershipReconfiguration) *InstanceSetBuilder {
builder.get().Spec.MembershipReconfiguration = reconfiguration
return builder
Expand Down
14 changes: 0 additions & 14 deletions pkg/controller/builder/builder_instance_set_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,17 +129,6 @@ var _ = Describe("instance_set builder", func() {
Command: []string{"bar-2"},
}
memberUpdateStrategy := workloads.BestEffortParallelUpdateStrategy
service := &corev1.Service{
Spec: corev1.ServiceSpec{
Ports: []corev1.ServicePort{
{
Name: "foo",
Protocol: corev1.ProtocolTCP,
Port: port,
},
},
},
}
paused := true
credential := workloads.Credential{
Username: workloads.CredentialVar{Value: "foo"},
Expand Down Expand Up @@ -175,7 +164,6 @@ var _ = Describe("instance_set builder", func() {
SetCustomHandler(actions).
AddCustomHandler(action).
SetMemberUpdateStrategy(&memberUpdateStrategy).
SetService(service).
SetPaused(paused).
SetCredential(credential).
SetInstances(instances).
Expand Down Expand Up @@ -215,8 +203,6 @@ var _ = Describe("instance_set builder", func() {
Expect(its.Spec.RoleProbe.CustomHandler[1]).Should(Equal(action))
Expect(its.Spec.MemberUpdateStrategy).ShouldNot(BeNil())
Expect(*its.Spec.MemberUpdateStrategy).Should(Equal(memberUpdateStrategy))
Expect(its.Spec.Service).ShouldNot(BeNil())
Expect(its.Spec.Service).Should(BeEquivalentTo(service))
Expect(its.Spec.Paused).Should(Equal(paused))
Expect(its.Spec.Credential).ShouldNot(BeNil())
Expect(*its.Spec.Credential).Should(Equal(credential))
Expand Down
1 change: 0 additions & 1 deletion pkg/controller/instanceset/instance_util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ import (
var _ = Describe("instance util test", func() {
BeforeEach(func() {
its = builder.NewInstanceSetBuilder(namespace, name).
SetService(&corev1.Service{}).
SetReplicas(3).
SetTemplate(template).
SetVolumeClaimTemplates(volumeClaimTemplates...).
Expand Down
40 changes: 0 additions & 40 deletions pkg/controller/instanceset/object_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,21 +36,6 @@ import (
viper "github.com/apecloud/kubeblocks/pkg/viperx"
)

func buildSvc(its workloads.InstanceSet, labels, selectors map[string]string) *corev1.Service {
if its.Spec.Service == nil {
return nil
}
annotations := ParseAnnotationsOfScope(ServiceScope, its.Annotations)
return builder.NewServiceBuilder(its.Namespace, its.Name).
AddAnnotationsInMap(annotations).
AddLabelsInMap(labels).
AddLabelsInMap(its.Spec.Service.Labels).
AddSelectorsInMap(selectors).
AddPorts(its.Spec.Service.Spec.Ports...).
SetType(its.Spec.Service.Spec.Type).
GetObject()
}

func buildHeadlessSvc(its workloads.InstanceSet, labels, selectors map[string]string) *corev1.Service {
annotations := ParseAnnotationsOfScope(HeadlessServiceScope, its.Annotations)
hdlBuilder := builder.NewHeadlessServiceBuilder(its.Namespace, getHeadlessSvcName(its.Name)).
Expand Down Expand Up @@ -186,15 +171,6 @@ func injectRoleProbeBaseContainer(its *workloads.InstanceSet, template *corev1.P
Name: actionSvcListVarName,
Value: actionSvcList,
})
// find service port of th db engine
servicePort := findSvcPort(its)
if servicePort > 0 {
env = append(env,
corev1.EnvVar{
Name: servicePortVarName,
Value: strconv.Itoa(servicePort),
})
}

// inject role update mechanism env
env = append(env,
Expand Down Expand Up @@ -257,22 +233,6 @@ func injectRoleProbeBaseContainer(its *workloads.InstanceSet, template *corev1.P
template.Spec.Containers = append(template.Spec.Containers, *container)
}

func findSvcPort(its *workloads.InstanceSet) int {
if its.Spec.Service == nil || len(its.Spec.Service.Spec.Ports) == 0 {
return 0
}
port := its.Spec.Service.Spec.Ports[0]
for _, c := range its.Spec.Template.Spec.Containers {
for _, p := range c.Ports {
if port.TargetPort.Type == intstr.String && p.Name == port.TargetPort.StrVal ||
port.TargetPort.Type == intstr.Int && p.ContainerPort == port.TargetPort.IntVal {
return int(p.ContainerPort)
}
}
}
return 0
}

func injectCustomRoleProbeContainer(its *workloads.InstanceSet, template *corev1.PodTemplateSpec, actionSvcPorts []int32, credentialEnv []corev1.EnvVar) {
if its.Spec.RoleProbe == nil {
return
Expand Down
69 changes: 0 additions & 69 deletions pkg/controller/instanceset/object_builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import (
. "github.com/onsi/gomega"

corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/util/intstr"

workloads "github.com/apecloud/kubeblocks/apis/workloads/v1"
"github.com/apecloud/kubeblocks/pkg/constant"
Expand All @@ -39,25 +38,12 @@ var _ = Describe("object generation transformer test.", func() {
SetReplicas(3).
AddMatchLabelsInMap(selectors).
SetRoles(roles).
SetService(service).
SetCredential(credential).
SetTemplate(template).
SetCustomHandler(observeActions).
GetObject()
})

Context("well-known service labels", func() {
It("should work well", func() {
svc := buildSvc(*its, getMatchLabels(its.Name), getSvcSelector(its, false))
Expect(svc).ShouldNot(BeNil())
for k, ev := range service.Labels {
v, ok := svc.Labels[k]
Expect(ok).Should(BeTrue())
Expect(v).Should(Equal(ev))
}
})
})

Context("injectRoleProbeBaseContainer function", func() {
It("should reuse container 'kb-role-probe' if exists", func() {
templateCopy := template.DeepCopy()
Expand Down Expand Up @@ -180,59 +166,4 @@ var _ = Describe("object generation transformer test.", func() {
Expect(svc.Spec.Ports[1].Name).ShouldNot(Equal(port.Name))
})
})

Context("findSvcPort function", func() {
It("should work well", func() {
By("set port name")
its.Spec.Service.Spec.Ports = []corev1.ServicePort{
{
Name: "svc-port",
Protocol: corev1.ProtocolTCP,
Port: 12345,
TargetPort: intstr.FromString("my-service"),
},
}
containerPort := int32(54321)
container := corev1.Container{
Name: name,
Ports: []corev1.ContainerPort{
{
Name: "my-service",
Protocol: corev1.ProtocolTCP,
ContainerPort: containerPort,
},
},
}
pod := builder.NewPodBuilder(namespace, getPodName(name, 0)).
SetContainers([]corev1.Container{container}).
GetObject()
its.Spec.Template = corev1.PodTemplateSpec{
ObjectMeta: pod.ObjectMeta,
Spec: pod.Spec,
}
Expect(findSvcPort(its)).Should(BeEquivalentTo(containerPort))

By("set port number")
its.Spec.Service.Spec.Ports = []corev1.ServicePort{
{
Name: "svc-port",
Protocol: corev1.ProtocolTCP,
Port: 12345,
TargetPort: intstr.FromInt(int(containerPort)),
},
}
Expect(findSvcPort(its)).Should(BeEquivalentTo(containerPort))

By("set no matched port")
its.Spec.Service.Spec.Ports = []corev1.ServicePort{
{
Name: "svc-port",
Protocol: corev1.ProtocolTCP,
Port: 12345,
TargetPort: intstr.FromInt(int(containerPort - 1)),
},
}
Expect(findSvcPort(its)).Should(BeZero())
})
})
})
7 changes: 1 addition & 6 deletions pkg/controller/instanceset/reconciler_assistant_object.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,10 @@ func (a *assistantObjectReconciler) Reconcile(tree *kubebuilderx.ObjectTree) (ku

// generate objects by current spec
labels := getMatchLabels(its.Name)
selectors := getSvcSelector(its, false)
headlessSelectors := getSvcSelector(its, true)
headlessSelectors := getHeadlessSvcSelector(its)

svc := buildSvc(*its, labels, selectors)
headLessSvc := buildHeadlessSvc(*its, labels, headlessSelectors)
var objects []client.Object
if svc != nil {
objects = append(objects, svc)
}
objects = append(objects, headLessSvc)
for _, object := range objects {
if err := intctrlutil.SetOwnership(its, object, model.GetScheme(), finalizer); err != nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ import (
var _ = Describe("replicas alignment reconciler test", func() {
BeforeEach(func() {
its = builder.NewInstanceSetBuilder(namespace, name).
SetService(&corev1.Service{}).
SetReplicas(3).
SetTemplate(template).
SetVolumeClaimTemplates(volumeClaimTemplates...).
Expand Down
3 changes: 0 additions & 3 deletions pkg/controller/instanceset/reconciler_revision_update_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ import (
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"

corev1 "k8s.io/api/core/v1"

workloads "github.com/apecloud/kubeblocks/apis/workloads/v1"
"github.com/apecloud/kubeblocks/pkg/controller/builder"
"github.com/apecloud/kubeblocks/pkg/controller/kubebuilderx"
Expand All @@ -33,7 +31,6 @@ import (
var _ = Describe("revision update reconciler test", func() {
BeforeEach(func() {
its = builder.NewInstanceSetBuilder(namespace, name).
SetService(&corev1.Service{}).
SetReplicas(3).
SetTemplate(template).
SetVolumeClaimTemplates(volumeClaimTemplates...).
Expand Down
Loading

0 comments on commit 19fb216

Please sign in to comment.