Răsfoiți Sursa

create development enviornment with tilt.dev

Signed-off-by: saweber <saweber@gmail.com>
saweber 2 ani în urmă
părinte
comite
47bdbbfd13
4 a modificat fișierele cu 271 adăugiri și 0 ștergeri
  1. 2 0
      .gitignore
  2. 21 0
      Dockerfile.debug
  3. 130 0
      Tiltfile
  4. 118 0
      tilt-values.yaml

+ 2 - 0
.gitignore

@@ -9,6 +9,8 @@ ui/node_modules/
 cmd/costmodel/costmodel
 cmd/costmodel/costmodel-amd64
 cmd/costmodel/costmodel-arm64
+cmd/costmodel/costmodel-tilt
+
 pkg/cloud/azureorphan_test.go
 
 # VS Code

+ 21 - 0
Dockerfile.debug

@@ -0,0 +1,21 @@
+# This dockerfile is for development purposes only; do not use this for production deployments
+FROM golang:alpine
+# The prebuilt binary path. This Dockerfile assumes the binary will be built
+# outside of Docker.
+ARG binary_path
+
+WORKDIR /app
+RUN apk add --update --no-cache ca-certificates
+RUN go install github.com/go-delve/delve/cmd/dlv@latest
+
+ADD --chmod=644 ./configs/default.json /models/default.json
+ADD --chmod=644 ./configs/azure.json /models/azure.json
+ADD --chmod=644 ./configs/aws.json /models/aws.json
+ADD --chmod=644 ./configs/gcp.json /models/gcp.json
+ADD --chmod=644 ./configs/alibaba.json /models/alibaba.json
+
+RUN echo "binary_path"
+COPY ${binary_path} main
+
+ENTRYPOINT ["/go/bin/dlv exec --listen=:40000 --api-version=2 --headless=true --accept-multiclient --log --continue /app/main"]
+EXPOSE 9003 40000

+ 130 - 0
Tiltfile

@@ -0,0 +1,130 @@
+load('ext://helm_resource', 'helm_resource', 'helm_repo')
+load('ext://restart_process', 'docker_build_with_restart')
+
+# WARNING: this allows any k8s context for deployment
+#allow_k8s_contexts(k8s_context())
+# To allow a specific context for deployment:
+# allow_k8s_contexts('kubectl-context')
+# See https://docs.tilt.dev/api.html#api.allow_k8s_contexts for default allowed contexts
+
+config.define_string('arch', args=False, usage='amd64')
+config.define_string('docker-repo', args=False, usage='')
+cfg = config.parse()
+
+arch = cfg.get('arch')
+
+docker_platform = "linux/amd64"
+go_arch = "amd64"
+if arch == "arm64":
+    docker_platform = "linux/aarch64"
+    go_arch = "arm64"
+
+docker_repo = cfg.get('docker-repo')
+if docker_repo == None:
+    docker_repo = ''
+else:
+    docker_repo = docker_repo + "/"
+
+# Build and update opencost back end binary when code changes
+local_resource(
+    name='build-costmodel',
+    dir='.',
+    cmd='CGO_ENABLED=0 GOOS=linux GOARCH='+go_arch+' go build -o ./cmd/costmodel/costmodel-tilt ./cmd/costmodel/main.go',
+    deps=[
+        './cmd/costmodel/main.go',
+        './pkg',
+    ],
+    allow_parallel=True,
+    resource_deps=['build-go-mod-download'],
+)
+
+# Build back end docker container
+# If the binary is updated, update the running container and restart binary in dlv
+docker_build_with_restart(
+    ref=docker_repo+'opencost-costmodel',
+    context='.',
+    # remove --continue flag to make dlv wait until debugger is attached to start
+    entrypoint='/go/bin/dlv exec --listen=:40000 --api-version=2 --headless=true --accept-multiclient --log --continue /app/main',
+    dockerfile='Dockerfile.debug',
+    platform=docker_platform,
+
+    build_args={'binary_path':'./cmd/costmodel/costmodel-tilt'},
+    only=[
+        'cmd/costmodel/costmodel-tilt',
+        'configs',
+    ],
+    live_update=[
+       sync('./cmd/costmodel/costmodel-tilt', '/app/main'),
+    ],
+)
+
+# npm install if package.json changes
+local_resource(
+    name='build-npm-install',
+    dir='./ui',
+    cmd='npm install',
+    deps=[
+        './ui/package.json',
+    ],
+    allow_parallel=True,
+)
+
+# Build FE locally when code changes
+local_resource(
+    name='build-ui',
+    dir='./ui',
+    cmd='npx parcel build src/index.html',
+    deps=[
+        './ui/src',
+        './ui/package.json',
+    ],
+    allow_parallel=True,
+    resource_deps=['build-npm-install'],
+)
+
+# update container when relevant files change
+docker_build(
+    ref=docker_repo+'opencost-ui',
+    context='./ui',
+    dockerfile='./ui/Dockerfile.cross',
+    only=[
+        'dist',
+        'nginx.conf',
+        'default.nginx.conf',
+        'docker-entrypoint.sh',
+    ],
+    live_update=[
+       sync('./ui/dist', '/var/www'),
+    ],
+)
+
+# build yaml for deployment to k8s
+yaml = helm(
+    '../opencost-helm-chart/charts/opencost',
+    name='opencost',
+    values=['./tilt-values.yaml'],
+    # configuring opencost to also use the kubecost prometheus server below
+    set=[
+        'opencost.ui.image.fullImageName='+docker_repo+'opencost-ui',
+        'opencost.exporter.image.fullImageName='+docker_repo+'opencost-costmodel',
+        'opencost.prometheus.internal.namespaceName='+k8s_namespace(),
+    ]
+)
+k8s_yaml(yaml) # put resulting yaml into k8s
+k8s_resource(workload='opencost', port_forwards=['9003:9003','9090:9090','40000:40000'])
+
+helm_resource(
+    name='prometheus',
+    chart='prometheus-community/prometheus')
+k8s_resource(workload='prometheus', port_forwards=['9080:9090'])
+
+local_resource(
+    name='costmodel-test',
+    dir='.',
+    cmd='go test ./...',
+    deps=[
+        './pkg',
+    ],
+    allow_parallel=True,
+    resource_deps=['opencost'], # run tests after build to speed up deployment
+)

+ 118 - 0
tilt-values.yaml

@@ -0,0 +1,118 @@
+# DO NOT USE FOR DEPLOYMENT. This file is intended to be used with a Tiltfile
+# and for development purposes only. Please refer to
+# https://github.com/opencost/opencost-helm-chart
+service:
+  enabled: true
+  # --  Kubernetes Service type
+  type: ClusterIP
+  # -- extra ports.  Useful for sidecar pods such as oauth-proxy
+  extraPorts:
+    - name: debug
+      port: 40000
+      targetPort: 40000
+
+opencost:
+  exporter:
+    # -- The GCP Pricing API requires a key. This is supplied just for evaluation.
+    cloudProviderApiKey: ""
+    # -- Default cluster ID to use if cluster_id is not set in Prometheus metrics.
+    defaultClusterId: 'tilt-cluster'
+  livenessProbe:
+    # -- Whether probe is enabled
+    enabled: true
+    # -- Number of seconds before probe is initiated
+    initialDelaySeconds: 120
+    # -- Probe frequency in seconds
+    periodSeconds: 10
+    # -- Number of failures for probe to be considered failed
+    failureThreshold: 3
+  # Readiness probe configuration
+  readinessProbe:
+    # -- Whether probe is enabled
+    enabled: true
+    # -- Number of seconds before probe is initiated
+    initialDelaySeconds: 120
+    # -- Probe frequency in seconds
+    periodSeconds: 10
+    # -- Number of failures for probe to be considered failed
+    failureThreshold: 3
+
+  # Persistent volume claim for storing the data. eg: csv file
+  persistence:
+    enabled: false
+
+  aws:
+    # -- AWS secret access key
+    secret_access_key: ""
+    # -- AWS secret key id
+    access_key_id: ""
+
+  customPricing:
+    # -- Enables custom pricing configuration
+    enabled: false
+    # -- Customize the configmap name used for custom pricing
+    configmapName: custom-pricing-model
+    # -- Path for the pricing configuration.
+    configPath: /tmp/custom-config
+    # -- Configures the pricing model provided in the values file.
+    createConfigmap: true
+    # -- Sets the provider type for the custom pricing file.
+    provider: custom
+    # -- More information about these values here: https://www.opencost.io/docs/configuration/on-prem#custom-pricing-using-the-opencost-helm-chart
+    costModel:
+      description: Modified pricing configuration.
+      CPU: 1.25
+      spotCPU: 0.006655
+      RAM: 0.50
+      spotRAM: 0.000892
+      GPU: 0.95
+      storage: 0.25
+      zoneNetworkEgress: 0.01
+      regionNetworkEgress: 0.01
+      internetNetworkEgress: 0.12
+
+  dataRetention:
+    dailyResolutionDays: 15
+
+  cloudCost:
+    # -- Enable cloud cost ingestion and querying, dependant on valid integration credentials
+    enabled: false
+    # -- Number of hours between each run of the Cloud Cost pipeline
+    refreshRateHours: 6
+    # -- Number of days into the past that a Cloud Cost standard run will query for
+    runWindowDays: 3
+    # -- The number of standard runs before a Month-to-Date run occurs
+    monthToDateInterval: 6
+    # -- The max number of days that any single query will be made to construct Cloud Costs
+    queryWindowDays: 7
+
+  metrics:
+    serviceMonitor:
+      # -- Create ServiceMonitor resource for scraping metrics using PrometheusOperator
+      enabled: false
+      # -- Additional labels to add to the ServiceMonitor
+      additionalLabels: {}
+      # -- Specify if the ServiceMonitor will be deployed into a different namespace (blank deploys into same namespace as chart)
+      namespace: ""
+      # -- Interval at which metrics should be scraped
+      scrapeInterval: 30s
+      # -- Timeout after which the scrape is ended
+      scrapeTimeout: 10s
+      # -- HonorLabels chooses the metric's labels on collisions with target labels
+      honorLabels: true
+      # -- RelabelConfigs to apply to samples before scraping. Prometheus Operator automatically adds relabelings for a few standard Kubernetes fields
+      relabelings: []
+      # -- MetricRelabelConfigs to apply to samples before ingestion
+      metricRelabelings: []
+      # -- HTTP scheme used for scraping. Defaults to `http`
+      scheme: http
+  prometheus:
+    internal:
+      enabled: true
+      # -- Service name of in-cluster Prometheus
+      serviceName: prometheus-server
+      # -- Service port of in-cluster Prometheus
+      port: 80
+  ui:
+    # -- Enable OpenCost UI
+    enabled: true