|
| 1 | +#!/usr/bin/env ruby |
| 2 | + |
| 3 | +require 'yaml' |
| 4 | + |
| 5 | +# Services to keep |
| 6 | +KEEP_SERVICES = %w[ |
| 7 | + accounting ad cart checkout currency email |
| 8 | + fraud-detection frontend frontend-proxy image-provider |
| 9 | + load-generator payment product-catalog quote |
| 10 | + recommendation shipping flagd flagd-ui kafka |
| 11 | + postgres postgresql valkey-cart |
| 12 | +].freeze |
| 13 | + |
| 14 | +# Resources to remove (observability stack) |
| 15 | +REMOVE_SERVICES = %w[ |
| 16 | + opensearch grafana jaeger prometheus |
| 17 | + otel-collector opentelemetry-collector |
| 18 | +].freeze |
| 19 | + |
| 20 | +def should_keep_resource?(resource) |
| 21 | + return false unless resource.is_a?(Hash) |
| 22 | + |
| 23 | + kind = resource['kind'] || '' |
| 24 | + metadata = resource['metadata'] || {} |
| 25 | + name = metadata['name'] || '' |
| 26 | + |
| 27 | + # Always keep namespace |
| 28 | + return true if kind == 'Namespace' |
| 29 | + |
| 30 | + # Check if it's an observability component to remove |
| 31 | + REMOVE_SERVICES.each do |remove_pattern| |
| 32 | + return false if name.include?(remove_pattern) |
| 33 | + end |
| 34 | + |
| 35 | + # Keep main service account |
| 36 | + return true if kind == 'ServiceAccount' && name == 'opentelemetry-demo' |
| 37 | + |
| 38 | + # Keep flagd and product catalog configs |
| 39 | + return true if kind == 'ConfigMap' && ['flagd-config', 'product-catalog-products'].include?(name) |
| 40 | + |
| 41 | + # For Services, Deployments, StatefulSets - check if in keep list |
| 42 | + if %w[Service Deployment StatefulSet].include?(kind) |
| 43 | + KEEP_SERVICES.each do |keep_svc| |
| 44 | + return true if name == keep_svc || name.start_with?("#{keep_svc}-") |
| 45 | + end |
| 46 | + return false |
| 47 | + end |
| 48 | + |
| 49 | + # Skip other resources by default (RBAC, etc.) |
| 50 | + false |
| 51 | +end |
| 52 | + |
| 53 | +def clean_deployment(deployment, service_name) |
| 54 | + # Update container images and remove OTEL env vars |
| 55 | + if deployment['spec'] && deployment['spec']['template'] && |
| 56 | + deployment['spec']['template']['spec'] && deployment['spec']['template']['spec']['containers'] |
| 57 | + |
| 58 | + deployment['spec']['template']['spec']['containers'].each do |container| |
| 59 | + # Update image reference |
| 60 | + if container['image'] && container['image'].include?('ghcr.io/open-telemetry/demo') |
| 61 | + container['image'] = "betterstack/opentelemetry-demo:latest-#{service_name}" |
| 62 | + end |
| 63 | + |
| 64 | + # Remove OTEL environment variables |
| 65 | + if container['env'] |
| 66 | + container['env'] = container['env'].reject do |env| |
| 67 | + env['name'] && env['name'].start_with?('OTEL_') |
| 68 | + end |
| 69 | + end |
| 70 | + end |
| 71 | + end |
| 72 | + |
| 73 | + deployment |
| 74 | +end |
| 75 | + |
| 76 | +def main |
| 77 | + input_file = 'kubernetes/opentelemetry-demo.yaml' |
| 78 | + output_file = 'kubernetes/opentelemetry-demo-cleaned.yaml' |
| 79 | + |
| 80 | + puts "Reading #{input_file}..." |
| 81 | + |
| 82 | + # Read all YAML documents |
| 83 | + documents = [] |
| 84 | + File.open(input_file, 'r') do |file| |
| 85 | + YAML.load_stream(file) do |doc| |
| 86 | + documents << doc if doc |
| 87 | + end |
| 88 | + end |
| 89 | + |
| 90 | + puts "Found #{documents.length} resources" |
| 91 | + |
| 92 | + # Filter and clean resources |
| 93 | + cleaned_docs = [] |
| 94 | + kept_count = 0 |
| 95 | + removed_count = 0 |
| 96 | + |
| 97 | + documents.each do |doc| |
| 98 | + next unless doc |
| 99 | + |
| 100 | + if should_keep_resource?(doc) |
| 101 | + # Clean deployments |
| 102 | + if doc['kind'] == 'Deployment' |
| 103 | + service_name = doc['metadata']['name'] |
| 104 | + doc = clean_deployment(doc, service_name) |
| 105 | + end |
| 106 | + |
| 107 | + cleaned_docs << doc |
| 108 | + kept_count += 1 |
| 109 | + puts " Keeping: #{doc['kind'] || 'Unknown'} - #{doc.dig('metadata', 'name') || 'unnamed'}" |
| 110 | + else |
| 111 | + removed_count += 1 |
| 112 | + kind = doc['kind'] || 'Unknown' |
| 113 | + name = doc.dig('metadata', 'name') || 'unnamed' |
| 114 | + if REMOVE_SERVICES.any? { |obs| name.include?(obs) } |
| 115 | + puts " Removing: #{kind} - #{name}" |
| 116 | + end |
| 117 | + end |
| 118 | + end |
| 119 | + |
| 120 | + puts "\nKept #{kept_count} resources, removed #{removed_count} resources" |
| 121 | + |
| 122 | + # Write cleaned manifest |
| 123 | + puts "\nWriting cleaned manifest to #{output_file}..." |
| 124 | + File.open(output_file, 'w') do |file| |
| 125 | + file.puts "# Copyright The OpenTelemetry Authors" |
| 126 | + file.puts "# SPDX-License-Identifier: Apache-2.0" |
| 127 | + file.puts "# Cleaned version without observability stack" |
| 128 | + |
| 129 | + cleaned_docs.each_with_index do |doc, index| |
| 130 | + file.puts "---" |
| 131 | + yaml_output = doc.to_yaml |
| 132 | + # Remove the leading --- that to_yaml adds |
| 133 | + yaml_output = yaml_output.sub(/\A---\s*\n/, '') |
| 134 | + file.puts yaml_output |
| 135 | + end |
| 136 | + end |
| 137 | + |
| 138 | + puts "Done! Cleaned manifest saved to #{output_file}" |
| 139 | +end |
| 140 | + |
| 141 | +main if __FILE__ == $0 |
0 commit comments