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

Add option to also log to a file #656

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
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
13 changes: 12 additions & 1 deletion helm/solr-operator/templates/_helpers.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,17 @@ Determine whether to use ClusterRoles or Roles
{{- end -}}
{{- end -}}

{{- define "solr-operator.volumeMounts" -}}
{{- include "solr-operator.mTLS.volumeMounts" . }}
{{- include "solr-operator.logging.volumeMounts" . }}
{{- end -}}

{{- define "solr-operator.volumes" -}}
{{- include "solr-operator.mTLS.volumes" . }}
{{- include "solr-operator.logging.volumes" . }}
{{- end -}}


{{/*
mTLS vars
*/}}
Expand Down Expand Up @@ -136,4 +147,4 @@ rootSolrCert.pem
path: {{ include "solr-operator.mTLS.caCertName" . }}
optional: false
{{- end -}}
{{- end -}}
{{- end -}}
79 changes: 79 additions & 0 deletions helm/solr-operator/templates/_log_helpers.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
{{/*
* 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.
*/}}

{{/* vim: set filetype=mustache: */}}


{{/*
log file
*/}}

{{- define "solr-operator.logging.fileMountDir" -}}
/var/logs/operator
{{- end -}}

{{- define "solr-operator.logging.file" -}}
{{- if .Values.logging.file.enabled -}}
{{ include "solr-operator.logging.fileMountDir" . }}/{{ .Values.logging.file.path }}
{{- end -}}
{{- end -}}


{{/*
log file
*/}}
{{- define "solr-operator.logging.volumes" -}}
{{- if .Values.logging.file.enabled -}}
- {{ .Values.logging.file.volume | toYaml | nindent 2 }}
{{ include "solr-operator.logging.rotation.volume" . }}
{{- end -}}
{{- end -}}

{{/*
log volume mounts
*/}}
{{- define "solr-operator.logging.volumeMounts" -}}
{{- if .Values.logging.file.enabled -}}
- name: {{ .Values.logging.file.volume.name }}
mountPath: {{ include "solr-operator.logging.fileMountDir" . }}
readOnly: false
{{ include "solr-operator.logging.rotation.volumeMount" . }}
{{- end -}}
{{- end -}}

{{/*
log rotation
*/}}
{{- define "solr-operator.logging.rotation.configMapName" -}}
log-rotate-conf
{{- end -}}

{{- define "solr-operator.logging.rotation.volumeMount" -}}
{{- if .Values.logging.file.enabled | and .Values.logging.file.rotation.enabled -}}
- name: {{ include "solr-operator.logging.rotation.configMapName" . }}
mountPath: /etc/logrotate.d/
readOnly: true
{{- end -}}

{{- end -}}
{{- define "solr-operator.logging.rotation.volume" -}}
{{- if .Values.logging.file.enabled | and .Values.logging.file.rotation.enabled -}}
- name: {{ include "solr-operator.logging.rotation.configMapName" . }}
configMap:
name: {{ include "solr-operator.logging.rotation.configMapName" . }}
{{- end -}}
{{- end -}}
17 changes: 13 additions & 4 deletions helm/solr-operator/templates/deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,11 @@ spec:
{{- if .Values.watchNamespaces }}
- --watch-namespaces={{- include "solr-operator.watchNamespaces" . -}}
{{- end }}
{{- with ( include "solr-operator.logging.file" .) }}
{{- if . }}
- --log-file={{- . -}}
{{- end }}
{{- end }}
{{- if .Values.mTLS.clientCertSecret }}
- --tls-client-cert-path={{- include "solr-operator.mTLS.clientCertDirectory" . -}}/tls.crt
- --tls-client-cert-key-path={{- include "solr-operator.mTLS.clientCertDirectory" . -}}/tls.key
Expand Down Expand Up @@ -106,19 +111,23 @@ spec:

resources:
{{- toYaml .Values.resources | nindent 10 }}
{{- if (include "solr-operator.mTLS.volumeMounts" .) }}
{{- with include "solr-operator.volumeMounts" . }}
{{- if . }}
volumeMounts:
{{- include "solr-operator.mTLS.volumeMounts" . | nindent 10 }}
{{- . | nindent 10 }}
{{- end }}
{{- end }}

{{- if .Values.metrics.enable }}
ports:
- containerPort: 8080
name: metrics
{{- end }}
{{- if (include "solr-operator.mTLS.volumes" .) }}
{{- with include "solr-operator.volumes" . }}
{{- if . }}
volumes:
{{- include "solr-operator.mTLS.volumes" . | nindent 8 }}
{{- . | nindent 8 }}
{{- end }}
{{- end }}

{{- if .Values.sidecarContainers }}
Expand Down
33 changes: 33 additions & 0 deletions helm/solr-operator/templates/log_rotation_configmap.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# 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.

{{- if .Values.logging.file.enabled | and .Values.logging.file.rotation.enabled }}
---
# config for doing log rotation in the Solr Operator pod
apiVersion: v1
kind: ConfigMap
metadata:
name: {{ include "solr-operator.logging.rotation.configMapName" . }}
namespace: {{ .Release.Namespace }}
data:
solr-operator: |
{{ include "solr-operator.logging.file" . }} {
rotate {{ .Values.logging.file.rotation.numOldFilesToKeep | default 0 }}
size {{ .Values.logging.file.rotation.maxFileSize | default "10m"}}
nocompress
missingok
notifempty
}
{{- end }}
25 changes: 25 additions & 0 deletions helm/solr-operator/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -90,3 +90,28 @@ mTLS:
# Enable metrics for the Solr Operator
metrics:
enable: true

# Logging settings for the Solr Operator
logging:
# Log to a file in addition to stdErr
file:
# Whether to enable logging to a file
enabled: true
# The path of the log file within the volume
path: "operator.log"

# The volume that will be used to store the log file(s)
volume:
name: logs
emptyDir:
sizeLimit: 100Mi

# The log rotation policy
rotation:
enabled: true
numOldFilesToKeep: 3
maxFileSize: 20M
emptyDir:
sizeLimit: 100Mi


20 changes: 20 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"github.com/apache/solr-operator/version"
"github.com/fsnotify/fsnotify"
zkApi "github.com/pravega/zookeeper-operator/api/v1beta1"
"io"
"io/ioutil"
"net/http"
"os"
Expand Down Expand Up @@ -60,6 +61,7 @@ var (

// Operator scope
watchNamespaces string
logFile string

// External Operator dependencies
useZookeeperCRD bool
Expand Down Expand Up @@ -90,6 +92,7 @@ func init() {

flag.BoolVar(&useZookeeperCRD, "zk-operator", true, "The operator will not use the zk operator & crd when this flag is set to false.")
flag.StringVar(&watchNamespaces, "watch-namespaces", "", "The comma-separated list of namespaces to watch. If an empty string (default) is provided, the operator will watch the entire Kubernetes cluster.")
flag.StringVar(&logFile, "log-file", "", "A location to write logs to, if provided. This will not stop logs from being printed to stdout.")

flag.BoolVar(&clientSkipVerify, "tls-skip-verify-server", true, "Controls whether a client verifies the server's certificate chain and host name. If true (insecure), TLS accepts any certificate presented by the server and any host name in that certificate.")
flag.StringVar(&clientCertPath, "tls-client-cert-path", "", "Path where a TLS client cert can be found")
Expand Down Expand Up @@ -118,9 +121,25 @@ func main() {
flag.BoolVar(&enableLeaderElection, "leader-elect", true,
"Enable leader election for controller manager. "+
"Enabling this will ensure there is only one active controller manager.")

opts := zap.Options{
Development: true,
}

// Log to a file if requested by the user
logFile = strings.TrimSpace(logFile)
if logFile != "" {
file, err := os.OpenFile(logFile, os.O_CREATE|os.O_APPEND|os.O_RDWR, 0644)
if err != nil {
panic(err)
}
opts.DestWriter = io.MultiWriter(os.Stderr, file)
defer func() {
file.Sync()
file.Close()
}()
}

opts.BindFlags(flag.CommandLine)
flag.Parse()

Expand All @@ -130,6 +149,7 @@ func main() {
if version.VersionSuffix != "" {
fullVersion += "-" + version.VersionSuffix
}
setupLog.Info("Writing logs to file", "path", logFile)
setupLog.Info(fmt.Sprintf("solr-operator Version: %v", fullVersion))
setupLog.Info(fmt.Sprintf("solr-operator Git SHA: %s", version.GitSHA))
setupLog.Info(fmt.Sprintf("solr-operator Build Time: %s", version.BuildTime))
Expand Down