Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: add builder for oteld (#4821) #5807

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
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
5 changes: 5 additions & 0 deletions pkg/controller/builder/builder_container.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,11 @@ func (builder *ContainerBuilder) SetReadinessProbe(probe corev1.Probe) *Containe
return builder
}

func (builder *ContainerBuilder) SetLivenessProbe(probe corev1.Probe) *ContainerBuilder {
builder.get().LivenessProbe = &probe
return builder
}

func (builder *ContainerBuilder) SetStartupProbe(probe corev1.Probe) *ContainerBuilder {
builder.get().StartupProbe = &probe
return builder
Expand Down
10 changes: 10 additions & 0 deletions pkg/controller/builder/builder_container_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,13 @@ var _ = Describe("container builder", func() {
},
},
}
livenessProbe := corev1.Probe{
ProbeHandler: corev1.ProbeHandler{
Exec: &corev1.ExecAction{
Command: []string{},
},
},
}
container := NewContainerBuilder(name).
AddCommands(commands...).
AddArgs(args...).
Expand All @@ -123,6 +130,7 @@ var _ = Describe("container builder", func() {
AddPorts(ports...).
SetReadinessProbe(readinessProbe).
SetStartupProbe(startupProbe).
SetLivenessProbe(livenessProbe).
GetObject()

Expect(container.Name).Should(Equal(name))
Expand All @@ -140,5 +148,7 @@ var _ = Describe("container builder", func() {
Expect(*container.ReadinessProbe).Should(Equal(readinessProbe))
Expect(container.StartupProbe).ShouldNot(BeNil())
Expect(*container.StartupProbe).Should(Equal(startupProbe))
Expect(container.LivenessProbe).ShouldNot(BeNil())
Expect(*container.LivenessProbe).Should(Equal(livenessProbe))
})
})
80 changes: 80 additions & 0 deletions pkg/controller/builder/builder_daemon_set.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/*
Copyright (C) 2022-2023 ApeCloud Co., Ltd

This file is part of KubeBlocks project

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.

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 builder

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

type DaemonSetBuilder struct {
BaseBuilder[appsv1.DaemonSet, *appsv1.DaemonSet, DaemonSetBuilder]
}

func NewDaemonSetBuilder(namespace, name string) *DaemonSetBuilder {
builder := &DaemonSetBuilder{}
builder.init(namespace, name, &appsv1.DaemonSet{}, builder)
return builder
}

func (builder *DaemonSetBuilder) SetSelector(selector *metav1.LabelSelector) *DaemonSetBuilder {
builder.get().Spec.Selector = selector
return builder
}

func (builder *DaemonSetBuilder) SetTemplate(template corev1.PodTemplateSpec) *DaemonSetBuilder {
builder.get().Spec.Template = template
return builder
}

func (builder *DaemonSetBuilder) SetUpdateStrategy(strategy appsv1.DaemonSetUpdateStrategy) *DaemonSetBuilder {
builder.get().Spec.UpdateStrategy = strategy
return builder
}

func (builder *DaemonSetBuilder) AddLabelsInMap(labels map[string]string) *DaemonSetBuilder {
l := builder.object.GetLabels()
if l == nil {
l = make(map[string]string)
}
for k, v := range labels {
l[k] = v
}
builder.object.SetLabels(l)
return builder.concreteBuilder
}

func (builder *DaemonSetBuilder) AddMatchLabelsInMap(labels map[string]string) *DaemonSetBuilder {
selector := builder.get().Spec.Selector
if selector == nil {
selector = &metav1.LabelSelector{}
builder.get().Spec.Selector = selector
}
matchLabels := builder.get().Spec.Selector.MatchLabels
if matchLabels == nil {
matchLabels = make(map[string]string, len(labels))
}
for k, v := range labels {
matchLabels[k] = v
}
builder.get().Spec.Selector.MatchLabels = matchLabels
return builder
}
102 changes: 102 additions & 0 deletions pkg/controller/builder/builder_daemon_set_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
/*
Copyright (C) 2022-2023 ApeCloud Co., Ltd

This file is part of KubeBlocks project

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.

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 builder

import (
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"

appv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/intstr"

cfgutil "github.com/apecloud/kubeblocks/pkg/configuration/util"
"github.com/apecloud/kubeblocks/pkg/constant"
)

var _ = Describe("pdb builder", func() {
It("should work well", func() {
const (
name = "foo"
ns = "default"
)

commonLabels := map[string]string{
constant.AppManagedByLabelKey: constant.AppName,
constant.AppNameLabelKey: "apecloudoteld",
constant.AppInstanceLabelKey: "apecloudoteld",
}

labelSelector := &metav1.LabelSelector{
MatchLabels: commonLabels,
}

podTemplate := corev1.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{
Labels: commonLabels,
},
Spec: NewPodBuilder("", "").
AddServiceAccount("oteld-controller").
AddContainer(corev1.Container{}).
AddVolumes(corev1.Volume{
Name: "oteldlog",
VolumeSource: corev1.VolumeSource{
HostPath: &corev1.HostPathVolumeSource{
Path: "/var/log/oteld",
Type: cfgutil.ToPointer(corev1.HostPathDirectoryOrCreate),
}},
}).
AddVolumes(corev1.Volume{
Name: "root",
VolumeSource: corev1.VolumeSource{
HostPath: &corev1.HostPathVolumeSource{Path: "/"}},
}).
SetSecurityContext(corev1.PodSecurityContext{
RunAsUser: cfgutil.ToPointer(int64(0)),
RunAsGroup: cfgutil.ToPointer(int64(0)),
FSGroup: cfgutil.ToPointer(int64(65534)),
RunAsNonRoot: cfgutil.ToPointer(false),
}).
GetObject().Spec,
}

daemonset := NewDaemonSetBuilder(ns, name).
SetTemplate(podTemplate).
AddLabelsInMap(commonLabels).
AddMatchLabelsInMap(commonLabels).
SetSelector(labelSelector).
SetUpdateStrategy(appv1.DaemonSetUpdateStrategy{
Type: appv1.RollingUpdateDaemonSetStrategyType,
RollingUpdate: &appv1.RollingUpdateDaemonSet{
MaxUnavailable: cfgutil.ToPointer(intstr.FromInt32(10)),
}}).
GetObject()

Expect(daemonset.Name).Should(Equal(name))
Expect(daemonset.Namespace).Should(Equal(ns))
Expect(daemonset.Spec.Template).Should(BeEquivalentTo(podTemplate))
Expect(daemonset.Spec.Selector.MatchLabels).Should(BeEquivalentTo(commonLabels))
Expect(daemonset.Labels).Should(BeEquivalentTo(commonLabels))
Expect(daemonset.Spec.UpdateStrategy.Type).Should(BeEquivalentTo(appv1.RollingUpdateDaemonSetStrategyType))
Expect(daemonset.Spec.UpdateStrategy.RollingUpdate).ShouldNot(BeNil())
Expect(daemonset.Spec.UpdateStrategy.RollingUpdate.MaxUnavailable.String()).Should(BeEquivalentTo("10"))
})
})
75 changes: 75 additions & 0 deletions pkg/controller/builder/builder_deployment.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/*
Copyright (C) 2022-2023 ApeCloud Co., Ltd

This file is part of KubeBlocks project

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.

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 builder

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

type DeploymentBuilder struct {
BaseBuilder[appsv1.Deployment, *appsv1.Deployment, DeploymentBuilder]
}

func NewDeploymentBuilder(namespace, name string) *DeploymentBuilder {
builder := &DeploymentBuilder{}
builder.init(namespace, name, &appsv1.Deployment{}, builder)
return builder
}

func (builder *DeploymentBuilder) SetSelector(selector *metav1.LabelSelector) *DeploymentBuilder {
builder.get().Spec.Selector = selector
return builder
}

func (builder *DeploymentBuilder) SetTemplate(template corev1.PodTemplateSpec) *DeploymentBuilder {
builder.get().Spec.Template = template
return builder
}

func (builder *DeploymentBuilder) AddLabelsInMap(labels map[string]string) *DeploymentBuilder {
l := builder.object.GetLabels()
if l == nil {
l = make(map[string]string)
}
for k, v := range labels {
l[k] = v
}
builder.object.SetLabels(l)
return builder.concreteBuilder
}

func (builder *DeploymentBuilder) AddMatchLabelsInMap(labels map[string]string) *DeploymentBuilder {
selector := builder.get().Spec.Selector
if selector == nil {
selector = &metav1.LabelSelector{}
builder.get().Spec.Selector = selector
}
matchLabels := builder.get().Spec.Selector.MatchLabels
if matchLabels == nil {
matchLabels = make(map[string]string, len(labels))
}
for k, v := range labels {
matchLabels[k] = v
}
builder.get().Spec.Selector.MatchLabels = matchLabels
return builder
}
92 changes: 92 additions & 0 deletions pkg/controller/builder/builder_deployment_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/*
Copyright (C) 2022-2023 ApeCloud Co., Ltd

This file is part of KubeBlocks project

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.

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 builder

import (
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"

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

cfgutil "github.com/apecloud/kubeblocks/pkg/configuration/util"
"github.com/apecloud/kubeblocks/pkg/constant"
)

var _ = Describe("pdb builder", func() {
It("should work well", func() {
const (
name = "foo"
ns = "default"
)

commonLabels := map[string]string{
constant.AppManagedByLabelKey: constant.AppName,
constant.AppNameLabelKey: "apecloudoteld",
constant.AppInstanceLabelKey: "apecloudoteld",
}

labelSelector := &metav1.LabelSelector{
MatchLabels: commonLabels,
}

podTemplate := corev1.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{
Labels: commonLabels,
},
Spec: NewPodBuilder("", "").
AddServiceAccount("oteld-controller").
AddContainer(corev1.Container{}).
AddVolumes(corev1.Volume{
Name: "oteldlog",
VolumeSource: corev1.VolumeSource{
HostPath: &corev1.HostPathVolumeSource{
Path: "/var/log/oteld",
Type: cfgutil.ToPointer(corev1.HostPathDirectoryOrCreate),
}},
}).
AddVolumes(corev1.Volume{
Name: "root",
VolumeSource: corev1.VolumeSource{
HostPath: &corev1.HostPathVolumeSource{Path: "/"}},
}).
SetSecurityContext(corev1.PodSecurityContext{
RunAsUser: cfgutil.ToPointer(int64(0)),
RunAsGroup: cfgutil.ToPointer(int64(0)),
FSGroup: cfgutil.ToPointer(int64(65534)),
RunAsNonRoot: cfgutil.ToPointer(false),
}).
GetObject().Spec,
}

deployment := NewDeploymentBuilder(ns, name).
SetTemplate(podTemplate).
AddLabelsInMap(commonLabels).
AddMatchLabelsInMap(commonLabels).
SetSelector(labelSelector).
GetObject()

Expect(deployment.Name).Should(BeEquivalentTo(name))
Expect(deployment.Namespace).Should(BeEquivalentTo(ns))
Expect(deployment.Spec.Template).Should(BeEquivalentTo(podTemplate))
Expect(deployment.Spec.Selector.MatchLabels).Should(BeEquivalentTo(commonLabels))
Expect(deployment.Labels).Should(BeEquivalentTo(commonLabels))
})
})
Loading