-
Notifications
You must be signed in to change notification settings - Fork 190
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: add builder for oteld (#4821)
- Loading branch information
Showing
11 changed files
with
389 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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")) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
}) | ||
}) |
Oops, something went wrong.