Skip to content

Commit

Permalink
add logrotate
Browse files Browse the repository at this point in the history
add logrotate for etcd, mds, chunkserver, metaserver & snapshotclone
  • Loading branch information
Zero3River authored and caoxianfei1 committed Oct 8, 2023
1 parent 286296e commit bc6e7f4
Show file tree
Hide file tree
Showing 7 changed files with 101 additions and 0 deletions.
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 @@ -10,6 +10,7 @@ import (
"github.com/opencurve/curve-operator/pkg/daemon"
"github.com/opencurve/curve-operator/pkg/etcd"
"github.com/opencurve/curve-operator/pkg/k8sutil"
"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 @@ -59,6 +60,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
65 changes: 65 additions & 0 deletions pkg/logrotate/logrotate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package logrotate

import (
"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 {

logrotateConfMapData := `/logs/* {
rotate 5
missingok
compress
copytruncate
dateext
createolddir
olddir /logs/old
size 10m
notifempty
}`
cm := &v1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Name: "log-conf",
Namespace: c.Namespace,
},
Data: map[string]string{
"logrotate.conf": logrotateConfMapData,
},
}

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 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
6 changes: 6 additions & 0 deletions pkg/metaserver/spec.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,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"
apps "k8s.io/api/apps/v1"
v1 "k8s.io/api/core/v1"
Expand All @@ -20,6 +21,10 @@ func (c *Cluster) makeDeployment(metaserverConfig *metaserverConfig, nodeName st
volumes = append(volumes, vols...)
labels := daemon.CephDaemonAppLabels(AppName, c.Namespace, "metaserver", metaserverConfig.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: metaserverConfig.ResourceName,
Expand All @@ -28,6 +33,7 @@ func (c *Cluster) makeDeployment(metaserverConfig *metaserverConfig, nodeName st
Spec: v1.PodSpec{
Containers: []v1.Container{
c.makeMSDaemonContainer(nodeIP, metaserverConfig),
logrotate.MakeLogrotateContainer(),
},
NodeName: nodeName,
RestartPolicy: v1.RestartPolicyAlways,
Expand Down
6 changes: 6 additions & 0 deletions pkg/snapshotclone/spec.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,18 @@ import (

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

// makeDeployment make snapshotclone deployment to run snapshotclone daemon
func (c *Cluster) makeDeployment(nodeName string, nodeIP string, snapConfig *snapConfig) (*apps.Deployment, error) {
volumes := SnapDaemonVolumes(snapConfig)
labels := daemon.CephDaemonAppLabels(AppName, c.Namespace, "snapshotclone", snapConfig.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}})

// for debug
// log.Infof("snapConfig %+v", snapConfig)

Expand All @@ -33,6 +38,7 @@ func (c *Cluster) makeDeployment(nodeName string, nodeIP string, snapConfig *sna
Spec: v1.PodSpec{
Containers: []v1.Container{
c.makeSnapshotDaemonContainer(nodeIP, snapConfig),
logrotate.MakeLogrotateContainer(),
},
NodeName: nodeName,
RestartPolicy: v1.RestartPolicyAlways,
Expand Down

0 comments on commit bc6e7f4

Please sign in to comment.