Skip to content

Commit

Permalink
add logrotate & conf for etcd, mds, chunkserver.
Browse files Browse the repository at this point in the history
  • Loading branch information
Zero3River committed Sep 26, 2023
1 parent 4e3a6a5 commit 15b8d7c
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 0 deletions.
11 changes: 11 additions & 0 deletions config/samples/logrotate.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/logs/* {
rotate 5
missingok
compress
copytruncate
dateext
createolddir
olddir /logs/old
size 10m
notifempty
}
6 changes: 6 additions & 0 deletions pkg/chunkserver/spec.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

"github.com/opencurve/curve-operator/pkg/config"
"github.com/opencurve/curve-operator/pkg/daemon"
"github.com/opencurve/curve-operator/pkg/logrotate"
"github.com/opencurve/curve-operator/pkg/topology"
)

Expand All @@ -19,6 +20,10 @@ func (c *Cluster) makeDeployment(csConfig *chunkserverConfig) (*apps.Deployment,
volumes = append(volumes, vols...)
labels := daemon.CephDaemonAppLabels(AppName, c.Namespace, "chunkserver", csConfig.DaemonId, c.Kind)

// add log config volume
logConfCMVolSource := &v1.ConfigMapVolumeSource{LocalObjectReference: v1.LocalObjectReference{Name: "log-conf"}}
volumes = append(volumes, v1.Volume{Name: "log-conf", VolumeSource: v1.VolumeSource{ConfigMap: logConfCMVolSource}})

podSpec := v1.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{
Name: csConfig.ResourceName,
Expand All @@ -27,6 +32,7 @@ func (c *Cluster) makeDeployment(csConfig *chunkserverConfig) (*apps.Deployment,
Spec: v1.PodSpec{
Containers: []v1.Container{
c.makeCSDaemonContainer(csConfig),
logrotate.MakeLogrotateContainer(),
},
NodeName: csConfig.NodeName,
RestartPolicy: v1.RestartPolicyAlways,
Expand Down
6 changes: 6 additions & 0 deletions pkg/controllers/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/opencurve/curve-operator/pkg/chunkserver"
"github.com/opencurve/curve-operator/pkg/daemon"
"github.com/opencurve/curve-operator/pkg/etcd"
"github.com/opencurve/curve-operator/pkg/logrotate"
"github.com/opencurve/curve-operator/pkg/mds"
"github.com/opencurve/curve-operator/pkg/metaserver"
"github.com/opencurve/curve-operator/pkg/monitor"
Expand Down Expand Up @@ -57,6 +58,11 @@ func reconcileSharedServer(c *daemon.Cluster) ([]daemon.NodeInfo, []*topology.De
return nil, nil, err
}

err = logrotate.CreateLogrotateConfigMap(c)
if err != nil {
return nil, nil, err
}

// Start etcd cluster
etcds := etcd.New(c)
dcs, err := etcds.Start(nodesInfo)
Expand Down
6 changes: 6 additions & 0 deletions pkg/etcd/spec.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (

"github.com/opencurve/curve-operator/pkg/config"
"github.com/opencurve/curve-operator/pkg/daemon"
"github.com/opencurve/curve-operator/pkg/logrotate"
)

// createOverrideConfigMap create configMap override to record the endpoints of etcd for mds use
Expand Down Expand Up @@ -58,6 +59,10 @@ func (c *Cluster) makeDeployment(nodeName string, ip string, etcdConfig *etcdCon
volumes := daemon.DaemonVolumes(config.EtcdConfigMapDataKey, etcdConfig.ConfigMapMountPath, etcdConfig.DataPathMap, etcdConfig.CurrentConfigMapName)
labels := daemon.CephDaemonAppLabels(AppName, c.Namespace, "etcd", etcdConfig.DaemonID, c.Kind)

// add log config volume
logConfCMVolSource := &v1.ConfigMapVolumeSource{LocalObjectReference: v1.LocalObjectReference{Name: "log-conf"}}
volumes = append(volumes, v1.Volume{Name: "log-conf", VolumeSource: v1.VolumeSource{ConfigMap: logConfCMVolSource}})

podSpec := v1.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{
Name: etcdConfig.ResourceName,
Expand All @@ -69,6 +74,7 @@ func (c *Cluster) makeDeployment(nodeName string, ip string, etcdConfig *etcdCon
},
Containers: []v1.Container{
c.makeEtcdDaemonContainer(nodeName, ip, etcdConfig, etcdConfig.ClusterEtcdHttpAddr),
logrotate.MakeLogrotateContainer(),
},
NodeName: nodeName,
RestartPolicy: v1.RestartPolicyAlways,
Expand Down
66 changes: 66 additions & 0 deletions pkg/logrotate/logrotate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package logrotate

import (
"os"

"github.com/pkg/errors"
v1 "k8s.io/api/core/v1"
kerrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

"github.com/opencurve/curve-operator/pkg/daemon"
)

func CreateLogrotateConfigMap(c *daemon.Cluster) error {

cm := &v1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Name: "log-conf",
Namespace: c.Namespace,
},
Data: map[string]string{
"logrotate.conf": loadConfigFile("config/samples/logrotate.conf"),
},
}

err := c.OwnerInfo.SetControllerReference(cm)
if err != nil {
return err
}

_, err = c.Context.Clientset.CoreV1().ConfigMaps(c.Namespace).Create(cm)
if err != nil && !kerrors.IsAlreadyExists(err) {
return errors.Wrapf(err, "failed to create %s configmap in namespace %s", cm.Name, c.Namespace)
}

return nil
}

func loadConfigFile(filepath string) string {

content, err := os.ReadFile(filepath)
if err != nil {
panic(err)
}

return string(content)
}

func MakeLogrotateContainer() v1.Container {
container := v1.Container{
Name: "logrotate",
Image: "linkyard/logrotate:1.0.0",
VolumeMounts: []v1.VolumeMount{
{
Name: "log-volume",
MountPath: "/logs",
},
{
Name: "log-conf",
MountPath: "/etc/logrotate.conf",
SubPath: "logrotate.conf",
},
},
}
return container
}
6 changes: 6 additions & 0 deletions pkg/mds/spec.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (

"github.com/opencurve/curve-operator/pkg/config"
"github.com/opencurve/curve-operator/pkg/daemon"
"github.com/opencurve/curve-operator/pkg/logrotate"
)

// createOverrideMdsCM create mds-endpoints-override configmap to record mds endpoints
Expand Down Expand Up @@ -61,6 +62,10 @@ func (c *Cluster) makeDeployment(nodeName string, nodeIP string, mdsConfig *mdsC
volumes := daemon.DaemonVolumes(config.MdsConfigMapDataKey, mdsConfig.ConfigMapMountPath, mdsConfig.DataPathMap, mdsConfig.CurrentConfigMapName)
labels := daemon.CephDaemonAppLabels(AppName, c.Namespace, "mds", mdsConfig.DaemonID, c.Kind)

// add log config volume
logConfCMVolSource := &v1.ConfigMapVolumeSource{LocalObjectReference: v1.LocalObjectReference{Name: "log-conf"}}
volumes = append(volumes, v1.Volume{Name: "log-conf", VolumeSource: v1.VolumeSource{ConfigMap: logConfCMVolSource}})

podSpec := v1.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{
Name: mdsConfig.ResourceName,
Expand All @@ -69,6 +74,7 @@ func (c *Cluster) makeDeployment(nodeName string, nodeIP string, mdsConfig *mdsC
Spec: v1.PodSpec{
Containers: []v1.Container{
c.makeMdsDaemonContainer(nodeIP, mdsConfig),
logrotate.MakeLogrotateContainer(),
},
NodeName: nodeName,
RestartPolicy: v1.RestartPolicyAlways,
Expand Down

0 comments on commit 15b8d7c

Please sign in to comment.