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(operator): introduce PodBuilder and ShardingSphereDeploymentBuilder #428

Merged
merged 3 commits into from
Jul 5, 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
294 changes: 164 additions & 130 deletions shardingsphere-operator/pkg/kubernetes/deployment/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,15 +156,13 @@ type DeploymentBuilder interface {
SetNamespace(namespace string) DeploymentBuilder
SetLabelsAndSelectors(labels map[string]string, selectors *metav1.LabelSelector) DeploymentBuilder
SetAnnotations(annos map[string]string) DeploymentBuilder
SetShardingSphereProxyPodTemplate(tpl *corev1.PodTemplateSpec) DeploymentBuilder
SetShardingSphereProxyPodTemplateLabels(labels map[string]string) DeploymentBuilder
SetShardingSphereProxyPodTemplateAnnotations(annos map[string]string) DeploymentBuilder
SetShardingSphereProxyContainer(con *corev1.Container) DeploymentBuilder
SetMySQLConnector(scb common.ContainerBuilder, cn *v1alpha1.ComputeNode) DeploymentBuilder
SetAgentBin(scb common.ContainerBuilder, cn *v1alpha1.ComputeNode) DeploymentBuilder
SetInitContainer(con *corev1.Container) DeploymentBuilder

SetPodTemplateLabels(labels map[string]string) DeploymentBuilder
SetPodTemplateAnnotations(annos map[string]string) DeploymentBuilder
SetPodTemplateSpec(tpl *corev1.PodTemplateSpec) DeploymentBuilder
SetVolume(volume *corev1.Volume) DeploymentBuilder
SetReplicas(r *int32) DeploymentBuilder

Build() *appsv1.Deployment
}

Expand Down Expand Up @@ -212,26 +210,76 @@ func (d *deploymentBuilder) SetReplicas(r *int32) DeploymentBuilder {
return d
}

// SetShardingSphereProxyPodTemplate sets Deployment PodTemplateSpec for ShardingSphereProxy Pod
func (d *deploymentBuilder) SetShardingSphereProxyPodTemplate(tpl *corev1.PodTemplateSpec) DeploymentBuilder {
// SetPodTemplate sets Deployment PodTemplateSpec for ShardingSphereProxy Pod
func (d *deploymentBuilder) SetPodTemplateSpec(tpl *corev1.PodTemplateSpec) DeploymentBuilder {
d.deployment.Spec.Template = *tpl
return d
}

// SetShardingSphereProxyPodTemplateAnnotations sets annotations for ShardingSphereProxy Pod
func (d *deploymentBuilder) SetShardingSphereProxyPodTemplateAnnotations(annotations map[string]string) DeploymentBuilder {
// SetPodTemplateAnnotations sets annotations for ShardingSphereProxy Pod
func (d *deploymentBuilder) SetPodTemplateAnnotations(annotations map[string]string) DeploymentBuilder {
d.deployment.Spec.Template.Annotations = annotations
return d
}

// SetShardingSphereProxyPodTemplateLabels sets labels for ShardingSphereProxy Pod
func (d *deploymentBuilder) SetShardingSphereProxyPodTemplateLabels(labels map[string]string) DeploymentBuilder {
// SetPodTemplateLabels sets labels for ShardingSphereProxy Pod
func (d *deploymentBuilder) SetPodTemplateLabels(labels map[string]string) DeploymentBuilder {
d.deployment.Spec.Template.Labels = labels
return d
}

// SetVolume sets a volume for Deployment
func (d *deploymentBuilder) SetVolume(vol *corev1.Volume) DeploymentBuilder {
if d.deployment.Spec.Template.Spec.Volumes == nil {
d.deployment.Spec.Template.Spec.Volumes = []corev1.Volume{*vol}
}

for idx := range d.deployment.Spec.Template.Spec.Volumes {
if d.deployment.Spec.Template.Spec.Volumes[idx].Name == vol.Name {
d.deployment.Spec.Template.Spec.Volumes[idx] = *vol
return d
}
}

d.deployment.Spec.Template.Spec.Volumes = append(d.deployment.Spec.Template.Spec.Volumes, *vol)
return d
}

// Build returns a Deployment
func (d *deploymentBuilder) Build() *appsv1.Deployment {
return d.deployment
}

type ShardingSphereDeploymentBuilder interface {
DeploymentBuilder

// FIXME: this should be refactored into PodBuilder
SetContainer(con *corev1.Container) ShardingSphereDeploymentBuilder
SetInitContainer(con *corev1.Container) ShardingSphereDeploymentBuilder

SetMySQLConnector(scb common.ContainerBuilder, cn *v1alpha1.ComputeNode) ShardingSphereDeploymentBuilder
SetAgentBin(scb common.ContainerBuilder, cn *v1alpha1.ComputeNode) ShardingSphereDeploymentBuilder
}

// NewShardingSphereDeploymentBuilder creates a new ShardingSphereDeploymentBuilder
func NewShardingSphereDeploymentBuilder(meta metav1.Object, gvk schema.GroupVersionKind) ShardingSphereDeploymentBuilder {
dp := DefaultDeployment(meta, gvk)

return &shardingsphereDeploymentBuilder{
DeploymentBuilder: &deploymentBuilder{
deployment: dp,
},
deployment: dp,
}
}

type shardingsphereDeploymentBuilder struct {
DeploymentBuilder
deployment *appsv1.Deployment
}

// SetShardingSphereProxyContainer sets a container for ShardingSphereProxy
func (d *deploymentBuilder) SetShardingSphereProxyContainer(proxy *corev1.Container) DeploymentBuilder {
func (d *shardingsphereDeploymentBuilder) SetContainer(proxy *corev1.Container) ShardingSphereDeploymentBuilder {
if d.deployment.Spec.Template.Spec.Containers == nil {
d.deployment.Spec.Template.Spec.Containers = []corev1.Container{*proxy}
}
Expand All @@ -248,7 +296,7 @@ func (d *deploymentBuilder) SetShardingSphereProxyContainer(proxy *corev1.Contai
}

// SetInitContainer sets the a init container for bootstrapping
func (d *deploymentBuilder) SetInitContainer(init *corev1.Container) DeploymentBuilder {
func (d *shardingsphereDeploymentBuilder) SetInitContainer(init *corev1.Container) ShardingSphereDeploymentBuilder {
if d.deployment.Spec.Template.Spec.InitContainers == nil {
d.deployment.Spec.Template.Spec.InitContainers = []corev1.Container{}
}
Expand All @@ -265,6 +313,93 @@ func (d *deploymentBuilder) SetInitContainer(init *corev1.Container) DeploymentB
return d
}

// SetMySQLConnector will set an init container to download mysql jar and mount files for proxy container.
func (d *shardingsphereDeploymentBuilder) SetMySQLConnector(scb common.ContainerBuilder, cn *v1alpha1.ComputeNode) ShardingSphereDeploymentBuilder {
scb.AppendEnv([]corev1.EnvVar{
{
Name: defaultMySQLDriverEnvName,
Value: cn.Spec.StorageNodeConnector.Version,
},
})

vb := NewSharedVolumeAndMountBuilder().
SetVolumeMountSize(2).
SetName(defaultMySQLDriverVolumeName).
SetVolumeSourceEmptyDir().
SetMountPath(0, defaultExtlibPath).
SetMountPath(1, absoluteMySQLDriverMountName(defaultExtlibPath, cn.Spec.StorageNodeConnector.Version)).
SetSubPath(1, relativeMySQLDriverMountName(cn.Spec.StorageNodeConnector.Version))

v, vms := vb.Build()
d.SetVolume(v)
scb.SetVolumeMount(vms[1])

cb := NewBootstrapContainerBuilderForMysqlJar().SetVolumeMount(vms[0]).AppendEnv([]corev1.EnvVar{
{
Name: defaultMySQLDriverEnvName,
Value: cn.Spec.StorageNodeConnector.Version,
},
})
con := cb.Build()
d.SetInitContainer(con)

sc := scb.Build()
d.SetContainer(sc)

return d
}

// SetAgentBin set `agent bin` for ShardingSphereProxy with [observability](https://shardingsphere.apache.org/document/current/en/user-manual/shardingsphere-proxy/observability/)
func (d *shardingsphereDeploymentBuilder) SetAgentBin(scb common.ContainerBuilder, cn *v1alpha1.ComputeNode) ShardingSphereDeploymentBuilder {
// set env JAVA_TOOL_OPTIONS to proxy container, make sure proxy will apply agent-bin.jar
// agent-bin's version is always equals to shardingsphere proxy image's version

scb.AppendEnv([]corev1.EnvVar{
{
Name: defaultJavaToolOptionsName,
Value: fmt.Sprintf(defaultJavaAgentEnvValue, cn.Spec.ServerVersion),
},
})

// mount agent-bin dir
vbAgent := NewSharedVolumeAndMountBuilder().
SetVolumeMountSize(1).
SetName(defaultJavaAgentVolumeName).
SetVolumeSourceEmptyDir().
SetMountPath(0, defaultJavaAgentVolumeMountPath)
va, vma := vbAgent.Build()
d.SetVolume(va)
scb.SetVolumeMount(vma[0])

// mount agent config to overwrite agent-bin's config
vbAgentConf := NewSharedVolumeAndMountBuilder().
SetVolumeMountSize(1).
SetName(defaultJavaAgentConfigVolumeName).
SetVolumeSourceConfigMap(cn.Name, corev1.KeyToPath{Key: configmap.ConfigDataKeyForAgent, Path: configmap.ConfigDataKeyForAgent}).
SetMountPath(0, defaultJavaAgentConfigVolumeMountPath)
vc, vmc := vbAgentConf.Build()
d.SetVolume(vc)
scb.SetVolumeMount(vmc[0])

cb := NewBootstrapContainerBuilderForAgentBin().SetVolumeMount(vma[0]).AppendEnv([]corev1.EnvVar{
{
Name: defaultAgentBinVersionEnvName,
Value: cn.Spec.ServerVersion,
},
})
con := cb.Build()
d.SetInitContainer(con)

sc := scb.Build()
d.SetContainer(sc)

return d
}

func (d *shardingsphereDeploymentBuilder) Build() *appsv1.Deployment {
return d.DeploymentBuilder.Build()
}

// SharedVolumeAndMountBuilder build a Volume which could be mounted by different containers
type SharedVolumeAndMountBuilder interface {
SetName(name string) SharedVolumeAndMountBuilder
Expand Down Expand Up @@ -415,32 +550,13 @@ func (b *volumeAndMountBuilder) Build() (*corev1.Volume, *corev1.VolumeMount) {
return b.volume, b.volumemount
}

// SetVolume sets a volume for Deployment
func (d *deploymentBuilder) SetVolume(vol *corev1.Volume) DeploymentBuilder {
if d.deployment.Spec.Template.Spec.Volumes == nil {
d.deployment.Spec.Template.Spec.Volumes = []corev1.Volume{*vol}
}

for idx := range d.deployment.Spec.Template.Spec.Volumes {
if d.deployment.Spec.Template.Spec.Volumes[idx].Name == vol.Name {
d.deployment.Spec.Template.Spec.Volumes[idx] = *vol
return d
}
}

d.deployment.Spec.Template.Spec.Volumes = append(d.deployment.Spec.Template.Spec.Volumes, *vol)
return d
}

// Build returns a Deployment
func (d *deploymentBuilder) Build() *appsv1.Deployment {
return d.deployment
}

// NewDeployment creates a new Deployment
func NewDeployment(cn *v1alpha1.ComputeNode) *appsv1.Deployment {
builder := NewDeploymentBuilder(cn.GetObjectMeta(), cn.GetObjectKind().GroupVersionKind())
builder.SetName(cn.Name).SetNamespace(cn.Namespace).SetLabelsAndSelectors(cn.Labels, cn.Spec.Selector).SetAnnotations(cn.Annotations).SetReplicas(&cn.Spec.Replicas)
// builder := NewDeploymentBuilder(cn.GetObjectMeta(), cn.GetObjectKind().GroupVersionKind())
ssbuilder := NewShardingSphereDeploymentBuilder(cn.GetObjectMeta(), cn.GetObjectKind().GroupVersionKind())
// builder := ssbuilder.DeploymentBuilder
// builder.SetName(cn.Name).SetNamespace(cn.Namespace).SetLabelsAndSelectors(cn.Labels, cn.Spec.Selector).SetAnnotations(cn.Annotations).SetReplicas(&cn.Spec.Replicas)
ssbuilder.SetName(cn.Name).SetNamespace(cn.Namespace).SetLabelsAndSelectors(cn.Labels, cn.Spec.Selector).SetAnnotations(cn.Annotations).SetReplicas(&cn.Spec.Replicas)

ports := []corev1.ContainerPort{}
for idx := range cn.Spec.PortBindings {
Expand All @@ -466,20 +582,21 @@ func NewDeployment(cn *v1alpha1.ComputeNode) *appsv1.Deployment {
SetMountPath(0, defaultConfigVolumeMountPath)
vc, vmc := vcb.Build()

builder.SetVolume(vc)
ssbuilder.SetVolume(vc)
scb.SetVolumeMount(vmc[0])

// set agent for proxy
if enabled, ok := cn.Annotations[DefaultAnnotationJavaAgentEnabled]; ok && enabled == "true" {
builder.SetAgentBin(scb, cn)
ssbuilder.SetAgentBin(scb, cn)

metricsAnnos := map[string]string{}
metricsAnnos[commonAnnotationPrometheusMetricsPath] = cn.Annotations[commonAnnotationPrometheusMetricsPath]
metricsAnnos[commonAnnotationPrometheusMetricsPort] = cn.Annotations[commonAnnotationPrometheusMetricsPort]
metricsAnnos[commonAnnotationPrometheusMetricsScrape] = cn.Annotations[commonAnnotationPrometheusMetricsScrape]
metricsAnnos[commonAnnotationPrometheusMetricsScheme] = cn.Annotations[commonAnnotationPrometheusMetricsScheme]

builder.SetShardingSphereProxyPodTemplateAnnotations(metricsAnnos)
// builder.SetShardingSphereProxyPodTemplateAnnotations(metricsAnnos)
ssbuilder.SetPodTemplateAnnotations(metricsAnnos)

if cn.Spec.ServerVersion == "5.3.2" {
sv := NewSharedVolumeAndMountBuilder().
Expand All @@ -488,28 +605,28 @@ func NewDeployment(cn *v1alpha1.ComputeNode) *appsv1.Deployment {
SetVolumeSourceEmptyDir().
SetMountPath(0, "/opt/shardingsphere-proxy/bin")
va, vma := sv.Build()
builder.SetVolume(va)
ssbuilder.SetVolume(va)
scb.SetVolumeMount(vma[0])

// NOTE: This mountpath is not same with init container
vma[0].MountPath = "/opt/shardingsphere-proxy/tmpbin"
cb := NewBootstrapContainerBuilderForStartScripts().SetVolumeMount(vma[0])
con := cb.Build()
builder.SetInitContainer(con)
ssbuilder.SetInitContainer(con)
}
}

if cn.Spec.StorageNodeConnector != nil {
switch cn.Spec.StorageNodeConnector.Type {
case v1alpha1.ConnectorTypeMySQL:
builder.SetMySQLConnector(scb, cn)
ssbuilder.SetMySQLConnector(scb, cn)
case v1alpha1.ConnectorTypePostgreSQL:
sc := scb.Build()
builder.SetShardingSphereProxyContainer(sc)
ssbuilder.SetContainer(sc)
}
}

return builder.Build()
return ssbuilder.Build()
}

func setProbes(scb common.ContainerBuilder, cn *v1alpha1.ComputeNode) {
Expand All @@ -528,89 +645,6 @@ func setProbes(scb common.ContainerBuilder, cn *v1alpha1.ComputeNode) {
}
}

// SetMySQLConnector will set an init container to download mysql jar and mount files for proxy container.
func (d *deploymentBuilder) SetMySQLConnector(scb common.ContainerBuilder, cn *v1alpha1.ComputeNode) DeploymentBuilder {
scb.AppendEnv([]corev1.EnvVar{
{
Name: defaultMySQLDriverEnvName,
Value: cn.Spec.StorageNodeConnector.Version,
},
})

vb := NewSharedVolumeAndMountBuilder().
SetVolumeMountSize(2).
SetName(defaultMySQLDriverVolumeName).
SetVolumeSourceEmptyDir().
SetMountPath(0, defaultExtlibPath).
SetMountPath(1, absoluteMySQLDriverMountName(defaultExtlibPath, cn.Spec.StorageNodeConnector.Version)).
SetSubPath(1, relativeMySQLDriverMountName(cn.Spec.StorageNodeConnector.Version))

v, vms := vb.Build()
d.SetVolume(v)
scb.SetVolumeMount(vms[1])

cb := NewBootstrapContainerBuilderForMysqlJar().SetVolumeMount(vms[0]).AppendEnv([]corev1.EnvVar{
{
Name: defaultMySQLDriverEnvName,
Value: cn.Spec.StorageNodeConnector.Version,
},
})
con := cb.Build()
d.SetInitContainer(con)

sc := scb.Build()
d.SetShardingSphereProxyContainer(sc)

return d
}

// SetAgentBin set `agent bin` for ShardingSphereProxy with [observability](https://shardingsphere.apache.org/document/current/en/user-manual/shardingsphere-proxy/observability/)
func (d *deploymentBuilder) SetAgentBin(scb common.ContainerBuilder, cn *v1alpha1.ComputeNode) DeploymentBuilder {
// set env JAVA_TOOL_OPTIONS to proxy container, make sure proxy will apply agent-bin.jar
// agent-bin's version is always equals to shardingsphere proxy image's version

scb.AppendEnv([]corev1.EnvVar{
{
Name: defaultJavaToolOptionsName,
Value: fmt.Sprintf(defaultJavaAgentEnvValue, cn.Spec.ServerVersion),
},
})

// mount agent-bin dir
vbAgent := NewSharedVolumeAndMountBuilder().
SetVolumeMountSize(1).
SetName(defaultJavaAgentVolumeName).
SetVolumeSourceEmptyDir().
SetMountPath(0, defaultJavaAgentVolumeMountPath)
va, vma := vbAgent.Build()
d.SetVolume(va)
scb.SetVolumeMount(vma[0])

// mount agent config to overwrite agent-bin's config
vbAgentConf := NewSharedVolumeAndMountBuilder().
SetVolumeMountSize(1).
SetName(defaultJavaAgentConfigVolumeName).
SetVolumeSourceConfigMap(cn.Name, corev1.KeyToPath{Key: configmap.ConfigDataKeyForAgent, Path: configmap.ConfigDataKeyForAgent}).
SetMountPath(0, defaultJavaAgentConfigVolumeMountPath)
vc, vmc := vbAgentConf.Build()
d.SetVolume(vc)
scb.SetVolumeMount(vmc[0])

cb := NewBootstrapContainerBuilderForAgentBin().SetVolumeMount(vma[0]).AppendEnv([]corev1.EnvVar{
{
Name: defaultAgentBinVersionEnvName,
Value: cn.Spec.ServerVersion,
},
})
con := cb.Build()
d.SetInitContainer(con)

sc := scb.Build()
d.SetShardingSphereProxyContainer(sc)

return d
}

// DefaultDeployment describes the default deployment
func DefaultDeployment(meta metav1.Object, gvk schema.GroupVersionKind) *appsv1.Deployment {
defaultMaxUnavailable := intstr.FromInt(0)
Expand Down
Loading
Loading