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

refactor container builder to common pkg #305

Merged
merged 3 commits into from
Apr 11, 2023
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
160 changes: 160 additions & 0 deletions shardingsphere-operator/pkg/reconcile/common/container.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package common

import (
v1 "k8s.io/api/core/v1"
)

// ContainerBuilder is a common builder for Container
type ContainerBuilder interface {
SetName(name string) ContainerBuilder
SetImage(image string) ContainerBuilder
SetPorts(ports []v1.ContainerPort) ContainerBuilder
SetResources(res v1.ResourceRequirements) ContainerBuilder
SetLivenessProbe(probe *v1.Probe) ContainerBuilder
SetReadinessProbe(probe *v1.Probe) ContainerBuilder
SetStartupProbe(probe *v1.Probe) ContainerBuilder
SetEnv(envs []v1.EnvVar) ContainerBuilder
SetCommand(cmds []string) ContainerBuilder
SetVolumeMount(mount *v1.VolumeMount) ContainerBuilder
Build() *v1.Container
}

// NewContainerBuilder return a builder for Container
func NewContainerBuilder() ContainerBuilder {
return &containerBuilder{
container: DefaultContainer(),
}
}

type containerBuilder struct {
container *v1.Container
}

// SetName sets the name of the container
func (c *containerBuilder) SetName(name string) ContainerBuilder {
c.container.Name = name
return c
}

// SetImage sets the name of the container
func (c *containerBuilder) SetImage(image string) ContainerBuilder {
c.container.Image = image
return c
}

// SetPorts set the container port of the container
func (c *containerBuilder) SetPorts(ports []v1.ContainerPort) ContainerBuilder {
if ports == nil {
c.container.Ports = []v1.ContainerPort{}
}
if ports != nil {
c.container.Ports = ports
}
return c
}

// SetResources set the resources of the container
func (c *containerBuilder) SetResources(res v1.ResourceRequirements) ContainerBuilder {
c.container.Resources = res
return c
}

// SetLivenessProbe set the livenessProbe of the container
func (c *containerBuilder) SetLivenessProbe(probe *v1.Probe) ContainerBuilder {
if probe != nil {
if c.container.LivenessProbe == nil {
c.container.LivenessProbe = &v1.Probe{}
}
c.container.LivenessProbe = probe
}
return c
}

// SetReadinessProbe set the readinessProbe of the container
func (c *containerBuilder) SetReadinessProbe(probe *v1.Probe) ContainerBuilder {
if probe != nil {
if c.container.ReadinessProbe == nil {
c.container.ReadinessProbe = &v1.Probe{}
}
c.container.ReadinessProbe = probe
}
return c
}

// SetStartupProbe set the startupProbe of the container
func (c *containerBuilder) SetStartupProbe(probe *v1.Probe) ContainerBuilder {
if probe != nil {
if c.container.StartupProbe == nil {
c.container.StartupProbe = &v1.Probe{}
}
c.container.StartupProbe = probe
}
return c
}

// SetEnv set the env of the container
func (c *containerBuilder) SetEnv(envs []v1.EnvVar) ContainerBuilder {
if envs == nil {
c.container.Env = []v1.EnvVar{}
}
if envs != nil {
c.container.Env = envs
}
return c
}

// SetCommand set the command of the container
func (c *containerBuilder) SetCommand(cmds []string) ContainerBuilder {
if cmds != nil {
c.container.Command = cmds
}
return c
}

// SetVolumeMount set the mount point of the container
func (c *containerBuilder) SetVolumeMount(mount *v1.VolumeMount) ContainerBuilder {
if c.container.VolumeMounts == nil {
c.container.VolumeMounts = []v1.VolumeMount{*mount}
} else {
for idx := range c.container.VolumeMounts {
if c.container.VolumeMounts[idx].Name == mount.Name {
c.container.VolumeMounts[idx] = *mount
return c
}
}
c.container.VolumeMounts = append(c.container.VolumeMounts, *mount)
}

return c
}

// Build returns a Container
func (c *containerBuilder) Build() *v1.Container {
return c.container
}

// DefaultContainer returns a container with busybox
func DefaultContainer() *v1.Container {
con := &v1.Container{
Name: "default",
Image: "busybox:1.35.0",
}
return con
}
70 changes: 70 additions & 0 deletions shardingsphere-operator/pkg/reconcile/common/container_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package common

import (
corev1 "k8s.io/api/core/v1"
"testing"
)

func TestContainerBuilder_SetVolumeMount(t *testing.T) {
var found bool

// create a new container builder
c := &containerBuilder{
container: &corev1.Container{
VolumeMounts: []corev1.VolumeMount{},
},
}

// add a new volume mount
mount := &corev1.VolumeMount{
Name: "test-mount",
MountPath: "/test",
}
c.SetVolumeMount(mount)

// check if the volume mount has been added
for _, v := range c.container.VolumeMounts {
if v.Name == mount.Name {
found = true
break
}
}
if !found {
t.Errorf("SetVolumeMount() failed to add the VolumeMount")
}

// update an existing volume mount
updatedMount := &corev1.VolumeMount{
Name: "test-mount",
MountPath: "/new-test",
}
c.SetVolumeMount(updatedMount)

// check if the volume mount has been updated
for _, v := range c.container.VolumeMounts {
if v.MountPath == updatedMount.MountPath {
found = true
break
}
}
if !found {
t.Errorf("SetVolumeMount() failed to update the VolumeMount")
}
}
Loading