package render import ( "fmt" "strings" appsv1 "k8s.io/api/apps/v1" corev1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/runtime" buildinfo "example.com/monok8s/pkg/buildinfo" ) const cmmName = "cmm" type CMMConf struct { Namespace string Image string ImagePullSecrets []string Labels map[string]string } func RenderCMMDaemonSets(conf CMMConf) (string, error) { objs, err := buildCMMDaemonSetObjects(conf) if err != nil { return "", err } return renderObjects(objs) } func buildCMMDaemonSetObjects(conf CMMConf) ([]runtime.Object, error) { if strings.TrimSpace(conf.Namespace) == "" { return nil, fmt.Errorf("namespace is required") } conf.Labels = map[string]string{ "app.kubernetes.io/name": cmmName, "app.kubernetes.io/component": "hardware-offload", "app.kubernetes.io/part-of": "monok8s", "app.kubernetes.io/managed-by": "monok8s", } return []runtime.Object{ buildCMMServiceAccount(conf), buildCMMDaemonSet(conf), }, nil } func buildCMMServiceAccount(conf CMMConf) *corev1.ServiceAccount { return &corev1.ServiceAccount{ TypeMeta: metav1.TypeMeta{ APIVersion: "v1", Kind: "ServiceAccount", }, ObjectMeta: metav1.ObjectMeta{ Name: cmmName, Namespace: conf.Namespace, Labels: copyStringMap(conf.Labels), }, } } func buildCMMDaemonSet(conf CMMConf) *appsv1.DaemonSet { privileged := true dsLabels := map[string]string{ "app.kubernetes.io/name": cmmName, "app.kubernetes.io/component": "hardware-offload", "app.kubernetes.io/part-of": "monok8s", "app.kubernetes.io/managed-by": "monok8s", } image, pullPolicy := cmmImage(conf) return &appsv1.DaemonSet{ TypeMeta: metav1.TypeMeta{ APIVersion: "apps/v1", Kind: "DaemonSet", }, ObjectMeta: metav1.ObjectMeta{ Name: cmmName, Namespace: conf.Namespace, Labels: copyStringMap(conf.Labels), }, Spec: appsv1.DaemonSetSpec{ Selector: &metav1.LabelSelector{ MatchLabels: map[string]string{ "app.kubernetes.io/name": cmmName, }, }, Template: corev1.PodTemplateSpec{ ObjectMeta: metav1.ObjectMeta{ Labels: dsLabels, }, Spec: corev1.PodSpec{ ServiceAccountName: cmmName, HostNetwork: true, DNSPolicy: corev1.DNSClusterFirstWithHostNet, ImagePullSecrets: imagePullSecrets(conf.ImagePullSecrets), Tolerations: []corev1.Toleration{ {Operator: corev1.TolerationOpExists}, }, InitContainers: []corev1.Container{ { Name: "dpa-app", Image: image, ImagePullPolicy: pullPolicy, Command: []string{"/init_dpa.sh"}, Env: cdxEnv(), SecurityContext: &corev1.SecurityContext{ Privileged: &privileged, }, VolumeMounts: append( []corev1.VolumeMount{ { Name: "host-run-cmm", MountPath: "/host/run/monok8s/cmm", }, }, cdxVolumeMounts()..., ), }, }, Containers: []corev1.Container{ { Name: cmmName, Image: image, ImagePullPolicy: pullPolicy, Env: cmmEnv(), SecurityContext: &corev1.SecurityContext{ Privileged: &privileged, }, VolumeMounts: cdxVolumeMounts(), }, }, Volumes: cmmVolumes(), NodeSelector: map[string]string{ "node.kubernetes.io/instance-type": "mono-gateway", }, }, }, }, } } func cdxEnv() []corev1.EnvVar { return []corev1.EnvVar{ { Name: "CDX_CFG_FILE", Value: "/etc/dpa/cdx_cfg.xml", }, { Name: "CDX_PCD_FILE", Value: "/etc/dpa/cdx_pcd.xml", }, { Name: "CDX_PDL_FILE", Value: "/etc/fmc/config/hxs_pdl_v3.xml", }, { Name: "CDX_SP_FILE", Value: "/etc/dpa/cdx_sp.xml", }, } } func cmmEnv() []corev1.EnvVar { return []corev1.EnvVar{ { Name: "CMM_CONFIG", Value: "/etc/cmm/cmm.conf", }, { Name: "CMM_MAX_CONNECTIONS", Value: "131072", }, } } func cdxVolumeMounts() []corev1.VolumeMount { return []corev1.VolumeMount{ { Name: "cdx-ctrl", MountPath: "/dev/cdx_ctrl", }, } } func cmmVolumes() []corev1.Volume { return []corev1.Volume{ { Name: "cdx-ctrl", VolumeSource: corev1.VolumeSource{ HostPath: &corev1.HostPathVolumeSource{ Path: "/dev/cdx_ctrl", Type: hostPathType(corev1.HostPathCharDev), }, }, }, { Name: "host-run-cmm", VolumeSource: corev1.VolumeSource{ HostPath: &corev1.HostPathVolumeSource{ Path: "/run/monok8s/cmm", Type: hostPathType(corev1.HostPathDirectoryOrCreate), }, }, }, } } func cmmImage(conf CMMConf) (string, corev1.PullPolicy) { if conf.Image != "" { return conf.Image, corev1.PullIfNotPresent } return fmt.Sprintf("localhost/monok8s/cmm:%s", buildinfo.Version), corev1.PullNever }