github.com/caos/orbos@v1.5.14-0.20221103111702-e6cd0cea7ad4/internal/operator/boom/application/applications/systemdmetricsexporter/yaml/build.go (about)

     1  package yaml
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/caos/orbos/pkg/kubernetes/k8s"
     7  	"gopkg.in/yaml.v3"
     8  )
     9  
    10  func Build(resources *k8s.Resources) interface{} {
    11  
    12  	ds, err := yaml.Marshal(map[string]interface{}{
    13  		"kind":       "DaemonSet",
    14  		"apiVersion": "apps/v1",
    15  		"metadata": map[string]interface{}{
    16  			"name":      "systemd-exporter",
    17  			"namespace": "caos-system",
    18  			"labels": map[string]string{
    19  				"app": "systemd-exporter",
    20  			},
    21  		},
    22  		"spec": map[string]interface{}{
    23  			"selector": map[string]interface{}{
    24  				"matchLabels": map[string]string{
    25  					"app": "systemd-exporter",
    26  				},
    27  			},
    28  			"updateStrategy": map[string]interface{}{
    29  				"rollingUpdate": map[string]string{
    30  					"maxUnavailable": "100%",
    31  				},
    32  				"type": "RollingUpdate",
    33  			},
    34  			"template": map[string]interface{}{
    35  				"metadata": map[string]interface{}{
    36  					"labels": map[string]string{
    37  						"app": "systemd-exporter",
    38  					},
    39  					"annotations": map[string]string{
    40  						"prometheus.io/scrape": "true",
    41  						"prometheus.io/path":   "/metrics",
    42  						"prometheus.io/port":   "9558",
    43  					},
    44  				},
    45  				"spec": map[string]interface{}{
    46  					"tolerations": []map[string]string{{
    47  						"effect":   "NoSchedule",
    48  						"operator": "Exists",
    49  					}},
    50  					"securityContext": map[string]uint8{
    51  						"runAsUser": 0,
    52  					},
    53  					"hostPID": true,
    54  					"containers": []map[string]interface{}{{
    55  						"name":  "systemd-exporter",
    56  						"image": "quay.io/povilasv/systemd_exporter:v0.2.0",
    57  						"securityContext": map[string]bool{
    58  							"privileged": true,
    59  						},
    60  						"args": []string{
    61  							"--log.level=info",
    62  							"--path.procfs=/host/proc",
    63  							"--web.disable-exporter-metrics",
    64  							"--collector.unit-whitelist=kubelet.service|docker.service|node-agentd.service|firewalld.service|keepalived.service|nginx.service|sshd.service",
    65  						},
    66  						"ports": []map[string]interface{}{{
    67  							"name":          "metrics",
    68  							"containerPort": 9558,
    69  							"hostPort":      9558,
    70  						}},
    71  						"volumeMounts": []*volumeMount{{
    72  							Name:      "proc",
    73  							MountPath: "/host/proc",
    74  							ReadOnly:  true,
    75  						}, {
    76  							Name:      "systemd",
    77  							MountPath: "/run/systemd",
    78  							ReadOnly:  true,
    79  						}},
    80  						"resources": resources,
    81  					}},
    82  					"volumes": []*volume{{
    83  						Name: "proc",
    84  						HostPath: hostPath{
    85  							Path: "/proc",
    86  						},
    87  					}, {
    88  						Name: "systemd",
    89  						HostPath: hostPath{
    90  							Path: "/run/systemd",
    91  						},
    92  					}},
    93  				},
    94  			},
    95  		},
    96  	})
    97  
    98  	if err != nil {
    99  		panic(err)
   100  	}
   101  
   102  	svc, err := yaml.Marshal(map[string]interface{}{
   103  		"kind":       "Service",
   104  		"apiVersion": "v1",
   105  		"metadata": map[string]interface{}{
   106  			"name":      "systemd-exporter",
   107  			"namespace": "caos-system",
   108  			"labels": map[string]string{
   109  				"app.kubernetes.io/managed-by": "boom.caos.ch",
   110  				"boom.caos.ch/instance":        "boom",
   111  				"boom.caos.ch/part-of":         "boom",
   112  			},
   113  		},
   114  		"spec": map[string]interface{}{
   115  			"ports": []map[string]interface{}{{
   116  				"name":       "metrics",
   117  				"port":       9558,
   118  				"protocol":   "TCP",
   119  				"targetPort": 9558,
   120  			}},
   121  			"selector": map[string]string{
   122  				"app": "systemd-exporter",
   123  			},
   124  		},
   125  	})
   126  	if err != nil {
   127  		panic(err)
   128  	}
   129  
   130  	return fmt.Sprintf(`%s
   131  ---
   132  %s`, string(ds), string(svc))
   133  }
   134  
   135  type volumeMount struct {
   136  	Name      string `yaml:"name,omitempty"`
   137  	MountPath string `yaml:"mountPath,omitempty"`
   138  	ReadOnly  bool   `yaml:"readOnly,omitempty"`
   139  }
   140  
   141  type volume struct {
   142  	Name     string   `yaml:"name,omitempty"`
   143  	HostPath hostPath `yaml:"hostPath,omitempty"`
   144  }
   145  
   146  type hostPath struct {
   147  	Path string `yaml:"path,omitempty"`
   148  }