# 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.features.pulsarMonitor.enabled }}

apiVersion: v1
kind: Service
metadata:
  name: pulsar
spec:
  selector:
    app: pulsar
  ports:
    - protocol: TCP
      port: 6650
      name: pulsar
    - protocol: TCP
      port: 8080
      name: pulsar-admin

---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: pulsar
spec:
  replicas: 1
  selector:
    matchLabels:
      app: pulsar
  template:
    metadata:
      labels:
        app: pulsar
      annotations:
        sidecar.istio.io/inject: "false"
    spec:
      containers:
        - name: pulsar
          image: apachepulsar/pulsar-all:3.1.1
          imagePullPolicy: IfNotPresent
          env:
            - name: PULSAR_MEM
              value: "-Xms512m -Xmx512m -XX:MaxDirectMemorySize=256m"
          ports:
            - containerPort: 8080
              name: metrics
            - containerPort: 6650
          command: [ "/bin/sh" ]
          args: [ "-c", "/pulsar/bin/pulsar standalone" ]

---
apiVersion: batch/v1
kind: Job
metadata:
  name: pulsar-consumer-pref
spec:
  template:
    metadata:
      name: pulsar-consumer-pref
      annotations:
        sidecar.istio.io/inject: "false"
    spec:
      restartPolicy: Never
      containers:
        - image: apachepulsar/pulsar-all:3.1.1
          imagePullPolicy: IfNotPresent
          name: pulsar-consumer-pref
          command: [ "/bin/sh" ]
          args: [ "-c", "bin/pulsar-perf consume --service-url pulsar://pulsar:6650 sw-topic" ]
      initContainers:
        - name: wait-for-pulsar
          image: busybox
          command: [ "sh", "-c", "until nslookup pulsar.{{ .Release.Namespace }}.svc.cluster.local; do echo waiting for pulsar; sleep 2; done;" ]

---
apiVersion: batch/v1
kind: CronJob
metadata:
  name: pulsar-producer-perf-test
spec:
  schedule: "*/10 * * * *"
  successfulJobsHistoryLimit: 1
  failedJobsHistoryLimit: 1
  jobTemplate:
    spec:
      template:
        spec:
          restartPolicy: Never
          containers:
            - name: pulsar-producer-perf-test
              image: apachepulsar/pulsar-all:3.1.1
              imagePullPolicy: IfNotPresent
              command: [ "sh", "-c", "bin/pulsar-perf produce --service-url pulsar://pulsar.{{ .Release.Namespace }}.svc.cluster.local:6650 -m 1 sw-topic" ]

{{- end }}