2019-06-14 15:07:06 +02:00
|
|
|
/*
|
|
|
|
|
Copyright 2019 The Rook Authors. All rights reserved.
|
|
|
|
|
|
|
|
|
|
Licensed 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.
|
|
|
|
|
*/
|
|
|
|
|
|
2022-12-07 16:22:57 -07:00
|
|
|
package nodedaemon
|
2019-06-14 15:07:06 +02:00
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"fmt"
|
|
|
|
|
"path"
|
|
|
|
|
|
2019-10-08 12:51:36 +02:00
|
|
|
"github.com/pkg/errors"
|
2020-01-06 16:20:24 -07:00
|
|
|
cephv1 "github.com/rook/rook/pkg/apis/ceph.rook.io/v1"
|
2019-06-14 15:07:06 +02:00
|
|
|
"github.com/rook/rook/pkg/operator/ceph/config"
|
|
|
|
|
"github.com/rook/rook/pkg/operator/ceph/config/keyring"
|
2020-02-25 18:07:01 +01:00
|
|
|
"github.com/rook/rook/pkg/operator/ceph/controller"
|
2021-05-13 14:01:28 +05:30
|
|
|
|
|
|
|
|
cephver "github.com/rook/rook/pkg/operator/ceph/version"
|
2019-06-14 15:07:06 +02:00
|
|
|
"github.com/rook/rook/pkg/operator/k8sutil"
|
|
|
|
|
appsv1 "k8s.io/api/apps/v1"
|
|
|
|
|
corev1 "k8s.io/api/core/v1"
|
|
|
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
2020-01-06 16:20:24 -07:00
|
|
|
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
|
2019-06-14 15:07:06 +02:00
|
|
|
)
|
|
|
|
|
|
2019-12-03 16:34:42 +01:00
|
|
|
const (
|
|
|
|
|
crashCollectorKeyringUsername = "client.crash"
|
2020-01-07 19:24:56 +05:30
|
|
|
crashCollectorKeyName = "rook-ceph-crash-collector-keyring"
|
2020-10-08 10:54:01 -04:00
|
|
|
// pruneSchedule is scheduled to run every day at midnight.
|
|
|
|
|
pruneSchedule = "0 0 * * *"
|
2019-12-03 16:34:42 +01:00
|
|
|
)
|
|
|
|
|
|
2019-06-14 15:07:06 +02:00
|
|
|
// createOrUpdateCephCrash is a wrapper around controllerutil.CreateOrUpdate
|
2021-05-13 14:01:28 +05:30
|
|
|
func (r *ReconcileNode) createOrUpdateCephCrash(node corev1.Node, tolerations []corev1.Toleration, cephCluster cephv1.CephCluster, cephVersion *cephver.CephVersion) (controllerutil.OperationResult, error) {
|
2019-06-14 15:07:06 +02:00
|
|
|
// Create or Update the deployment default/foo
|
|
|
|
|
nodeHostnameLabel, ok := node.ObjectMeta.Labels[corev1.LabelHostname]
|
|
|
|
|
if !ok {
|
2019-10-08 12:51:36 +02:00
|
|
|
return controllerutil.OperationResultNone, errors.Errorf("label key %q does not exist on node %q", corev1.LabelHostname, node.GetName())
|
2019-06-14 15:07:06 +02:00
|
|
|
}
|
|
|
|
|
deploy := &appsv1.Deployment{
|
|
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
2023-09-05 18:25:27 +05:30
|
|
|
Name: k8sutil.TruncateNodeName(fmt.Sprintf("%s-%%s", CrashCollectorAppName), nodeHostnameLabel),
|
2021-02-05 07:01:43 +00:00
|
|
|
Namespace: cephCluster.GetNamespace(),
|
2019-06-14 15:07:06 +02:00
|
|
|
},
|
|
|
|
|
}
|
2021-02-05 07:01:43 +00:00
|
|
|
err := controllerutil.SetControllerReference(&cephCluster, deploy, r.scheme)
|
|
|
|
|
if err != nil {
|
2021-02-18 13:10:11 +00:00
|
|
|
return controllerutil.OperationResultNone, errors.Errorf("failed to set owner reference of crashcollector deployment %q", deploy.Name)
|
2021-02-05 07:01:43 +00:00
|
|
|
}
|
2019-06-14 15:07:06 +02:00
|
|
|
|
2023-01-09 15:47:51 +05:30
|
|
|
volumes := controller.DaemonVolumesBase(config.NewDatalessDaemonDataPathMap(cephCluster.GetNamespace(), cephCluster.Spec.DataDirHostPath), "", cephCluster.Spec.DataDirHostPath)
|
2020-02-11 17:20:06 +01:00
|
|
|
volumes = append(volumes, keyring.Volume().CrashCollector())
|
2019-12-03 16:34:42 +01:00
|
|
|
|
2019-06-14 15:07:06 +02:00
|
|
|
mutateFunc := func() error {
|
|
|
|
|
|
|
|
|
|
// labels for the pod, the deployment, and the deploymentSelector
|
|
|
|
|
deploymentLabels := map[string]string{
|
|
|
|
|
corev1.LabelHostname: nodeHostnameLabel,
|
2023-09-05 18:25:27 +05:30
|
|
|
k8sutil.AppAttr: CrashCollectorAppName,
|
2019-06-14 15:07:06 +02:00
|
|
|
NodeNameLabel: node.GetName(),
|
|
|
|
|
}
|
2020-03-31 19:09:39 +02:00
|
|
|
deploymentLabels[config.CrashType] = "crash"
|
2021-02-11 17:31:55 -07:00
|
|
|
deploymentLabels[controller.DaemonIDLabel] = "crash"
|
2020-03-12 11:43:20 +01:00
|
|
|
deploymentLabels[k8sutil.ClusterAttr] = cephCluster.GetNamespace()
|
2019-06-14 15:07:06 +02:00
|
|
|
|
2019-12-10 15:01:21 +01:00
|
|
|
selectorLabels := map[string]string{
|
|
|
|
|
corev1.LabelHostname: nodeHostnameLabel,
|
2023-09-05 18:25:27 +05:30
|
|
|
k8sutil.AppAttr: CrashCollectorAppName,
|
2019-12-10 15:01:21 +01:00
|
|
|
NodeNameLabel: node.GetName(),
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-14 15:07:06 +02:00
|
|
|
nodeSelector := map[string]string{corev1.LabelHostname: nodeHostnameLabel}
|
|
|
|
|
|
|
|
|
|
// Deployment selector is immutable so we set this value only if
|
|
|
|
|
// a new object is going to be created
|
|
|
|
|
if deploy.ObjectMeta.CreationTimestamp.IsZero() {
|
|
|
|
|
deploy.Spec.Selector = &metav1.LabelSelector{
|
2019-12-10 15:01:21 +01:00
|
|
|
MatchLabels: selectorLabels,
|
2019-06-14 15:07:06 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
deploy.ObjectMeta.Labels = deploymentLabels
|
2021-10-27 12:27:07 +02:00
|
|
|
cephv1.GetCrashCollectorLabels(cephCluster.Spec.Labels).ApplyToObjectMeta(&deploy.ObjectMeta)
|
2019-11-22 22:47:55 -05:00
|
|
|
if cephVersion != nil {
|
2020-02-25 18:07:01 +01:00
|
|
|
controller.AddCephVersionLabelToDeployment(*cephVersion, deploy)
|
2019-11-22 22:47:55 -05:00
|
|
|
}
|
2023-02-26 13:46:59 +08:00
|
|
|
|
|
|
|
|
// make a copy labels for pod to avoid rook version gets added to pod spec
|
|
|
|
|
podLabels := map[string]string{}
|
|
|
|
|
for key, value := range deploymentLabels {
|
|
|
|
|
podLabels[key] = value
|
|
|
|
|
}
|
|
|
|
|
k8sutil.AddRookVersionLabelToDeployment(deploy)
|
|
|
|
|
|
2019-06-14 15:07:06 +02:00
|
|
|
deploy.Spec.Template = corev1.PodTemplateSpec{
|
|
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
2023-02-26 13:46:59 +08:00
|
|
|
Labels: podLabels,
|
2019-06-14 15:07:06 +02:00
|
|
|
},
|
|
|
|
|
Spec: corev1.PodSpec{
|
|
|
|
|
NodeSelector: nodeSelector,
|
|
|
|
|
InitContainers: []corev1.Container{
|
|
|
|
|
getCrashDirInitContainer(cephCluster),
|
|
|
|
|
getCrashChownInitContainer(cephCluster),
|
|
|
|
|
},
|
|
|
|
|
Containers: []corev1.Container{
|
2019-12-03 16:34:42 +01:00
|
|
|
getCrashDaemonContainer(cephCluster, *cephVersion),
|
2019-06-14 15:07:06 +02:00
|
|
|
},
|
2023-12-14 18:30:11 +05:30
|
|
|
Tolerations: tolerations,
|
|
|
|
|
RestartPolicy: corev1.RestartPolicyAlways,
|
|
|
|
|
HostNetwork: cephCluster.Spec.Network.IsHost(),
|
|
|
|
|
Volumes: volumes,
|
|
|
|
|
PriorityClassName: cephv1.GetCrashCollectorPriorityClassName(cephCluster.Spec.PriorityClassNames),
|
|
|
|
|
ServiceAccountName: k8sutil.DefaultServiceAccount,
|
2019-06-14 15:07:06 +02:00
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-06 17:46:01 +02:00
|
|
|
return controllerutil.CreateOrUpdate(r.opManagerContext, r.client, deploy, mutateFunc)
|
2019-06-14 15:07:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func getCrashDirInitContainer(cephCluster cephv1.CephCluster) corev1.Container {
|
|
|
|
|
dataPathMap := config.NewDatalessDaemonDataPathMap(cephCluster.GetNamespace(), cephCluster.Spec.DataDirHostPath)
|
|
|
|
|
crashPostedDir := path.Join(dataPathMap.ContainerCrashDir(), "posted")
|
|
|
|
|
|
|
|
|
|
container := corev1.Container{
|
|
|
|
|
Name: "make-container-crash-dir",
|
|
|
|
|
Command: []string{
|
|
|
|
|
"mkdir",
|
|
|
|
|
"-p",
|
|
|
|
|
},
|
|
|
|
|
Args: []string{
|
|
|
|
|
crashPostedDir,
|
|
|
|
|
},
|
|
|
|
|
Image: cephCluster.Spec.CephVersion.Image,
|
2022-09-13 16:02:03 +05:30
|
|
|
ImagePullPolicy: controller.GetContainerImagePullPolicy(cephCluster.Spec.CephVersion.ImagePullPolicy),
|
2020-11-19 17:58:18 +01:00
|
|
|
SecurityContext: controller.PodSecurityContext(),
|
2019-06-14 15:07:06 +02:00
|
|
|
Resources: cephv1.GetCrashCollectorResources(cephCluster.Spec.Resources),
|
2023-01-09 15:47:51 +05:30
|
|
|
VolumeMounts: controller.DaemonVolumeMounts(dataPathMap, "", cephCluster.Spec.DataDirHostPath),
|
2019-06-14 15:07:06 +02:00
|
|
|
}
|
|
|
|
|
return container
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func getCrashChownInitContainer(cephCluster cephv1.CephCluster) corev1.Container {
|
|
|
|
|
dataPathMap := config.NewDatalessDaemonDataPathMap(cephCluster.GetNamespace(), cephCluster.Spec.DataDirHostPath)
|
|
|
|
|
|
2020-02-25 18:07:01 +01:00
|
|
|
return controller.ChownCephDataDirsInitContainer(
|
2019-06-14 15:07:06 +02:00
|
|
|
*dataPathMap,
|
|
|
|
|
cephCluster.Spec.CephVersion.Image,
|
2022-09-13 16:02:03 +05:30
|
|
|
controller.GetContainerImagePullPolicy(cephCluster.Spec.CephVersion.ImagePullPolicy),
|
2023-01-09 15:47:51 +05:30
|
|
|
controller.DaemonVolumeMounts(dataPathMap, "", cephCluster.Spec.DataDirHostPath),
|
2019-06-14 15:07:06 +02:00
|
|
|
cephv1.GetCrashCollectorResources(cephCluster.Spec.Resources),
|
2020-11-19 17:58:18 +01:00
|
|
|
controller.PodSecurityContext(),
|
2023-01-09 15:47:51 +05:30
|
|
|
"",
|
2019-06-14 15:07:06 +02:00
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-13 14:01:28 +05:30
|
|
|
func getCrashDaemonContainer(cephCluster cephv1.CephCluster, cephVersion cephver.CephVersion) corev1.Container {
|
2019-06-14 15:07:06 +02:00
|
|
|
dataPathMap := config.NewDatalessDaemonDataPathMap(cephCluster.GetNamespace(), cephCluster.Spec.DataDirHostPath)
|
2020-02-11 17:20:06 +01:00
|
|
|
crashEnvVar := generateCrashEnvVar()
|
2023-08-25 19:58:46 +05:30
|
|
|
envVars := append(controller.DaemonEnvVars(&cephCluster.Spec), crashEnvVar)
|
2023-01-09 15:47:51 +05:30
|
|
|
volumeMounts := controller.DaemonVolumeMounts(dataPathMap, "", cephCluster.Spec.DataDirHostPath)
|
2020-02-11 17:20:06 +01:00
|
|
|
volumeMounts = append(volumeMounts, keyring.VolumeMount().CrashCollector())
|
2019-06-14 15:07:06 +02:00
|
|
|
|
|
|
|
|
container := corev1.Container{
|
|
|
|
|
Name: "ceph-crash",
|
|
|
|
|
Command: []string{
|
|
|
|
|
"ceph-crash",
|
|
|
|
|
},
|
2023-08-25 19:58:46 +05:30
|
|
|
Image: cephCluster.Spec.CephVersion.Image,
|
2022-09-13 16:02:03 +05:30
|
|
|
ImagePullPolicy: controller.GetContainerImagePullPolicy(cephCluster.Spec.CephVersion.ImagePullPolicy),
|
2020-05-26 16:33:48 +02:00
|
|
|
Env: envVars,
|
|
|
|
|
VolumeMounts: volumeMounts,
|
|
|
|
|
Resources: cephv1.GetCrashCollectorResources(cephCluster.Spec.Resources),
|
2022-10-26 16:34:20 -06:00
|
|
|
// Initialize the security context with the ceph user since the ceph-crash script does not have an argument
|
|
|
|
|
// to run as the ceph user
|
|
|
|
|
SecurityContext: controller.CephSecurityContext(),
|
2019-06-14 15:07:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return container
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-11 17:20:06 +01:00
|
|
|
func generateCrashEnvVar() corev1.EnvVar {
|
|
|
|
|
val := fmt.Sprintf("-m $(ROOK_CEPH_MON_HOST) -k %s", keyring.VolumeMount().CrashCollectorKeyringFilePath())
|
2019-12-03 16:34:42 +01:00
|
|
|
env := corev1.EnvVar{Name: "CEPH_ARGS", Value: val}
|
2020-02-11 17:20:06 +01:00
|
|
|
|
2019-12-03 16:34:42 +01:00
|
|
|
return env
|
|
|
|
|
}
|