diff --git a/config/samples/logrotate.conf b/config/samples/logrotate.conf new file mode 100644 index 00000000..6b28a680 --- /dev/null +++ b/config/samples/logrotate.conf @@ -0,0 +1,11 @@ +/logs/* { + rotate 5 + missingok + compress + copytruncate + dateext + createolddir + olddir /logs/old + size 10m + notifempty +} diff --git a/pkg/chunkserver/spec.go b/pkg/chunkserver/spec.go index 4629eb49..15dc9bbc 100644 --- a/pkg/chunkserver/spec.go +++ b/pkg/chunkserver/spec.go @@ -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" ) @@ -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, @@ -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, diff --git a/pkg/controllers/cluster.go b/pkg/controllers/cluster.go index a0ac3b8e..1cc125c0 100644 --- a/pkg/controllers/cluster.go +++ b/pkg/controllers/cluster.go @@ -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" @@ -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) diff --git a/pkg/etcd/spec.go b/pkg/etcd/spec.go index cc2be72b..4f4fba13 100644 --- a/pkg/etcd/spec.go +++ b/pkg/etcd/spec.go @@ -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 @@ -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, @@ -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, diff --git a/pkg/logrotate/logrotate.go b/pkg/logrotate/logrotate.go new file mode 100644 index 00000000..0d60b3af --- /dev/null +++ b/pkg/logrotate/logrotate.go @@ -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 +} diff --git a/pkg/mds/spec.go b/pkg/mds/spec.go index 3a1a627a..188372df 100644 --- a/pkg/mds/spec.go +++ b/pkg/mds/spec.go @@ -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 @@ -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, @@ -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,