|
| 1 | +// Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved. |
| 2 | +// See the file LICENSE for licensing terms. |
| 3 | + |
| 4 | +package tmpnet |
| 5 | + |
| 6 | +import ( |
| 7 | + "context" |
| 8 | + "fmt" |
| 9 | + "strings" |
| 10 | + |
| 11 | + "go.uber.org/zap" |
| 12 | + "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" |
| 13 | + "k8s.io/apimachinery/pkg/runtime/schema" |
| 14 | + "k8s.io/apimachinery/pkg/runtime/serializer/yaml" |
| 15 | + "k8s.io/client-go/dynamic" |
| 16 | + "k8s.io/client-go/kubernetes" |
| 17 | + |
| 18 | + _ "embed" |
| 19 | + |
| 20 | + "github.com/ava-labs/avalanchego/utils/logging" |
| 21 | + |
| 22 | + corev1 "k8s.io/api/core/v1" |
| 23 | + apierrors "k8s.io/apimachinery/pkg/api/errors" |
| 24 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 25 | +) |
| 26 | + |
| 27 | +//go:embed yaml/promtail-daemonset.yaml |
| 28 | +var promtailManifest []byte |
| 29 | + |
| 30 | +//go:embed yaml/prometheus-agent.yaml |
| 31 | +var prometheusManifest []byte |
| 32 | + |
| 33 | +// This must match the namespace defined in the manifests |
| 34 | +const monitoringNamespace = "ci-monitoring" |
| 35 | + |
| 36 | +type kubeCollectorConfig struct { |
| 37 | + name string |
| 38 | + secretPrefix string |
| 39 | + manifest []byte |
| 40 | +} |
| 41 | + |
| 42 | +// DeployKubeCollectors deploys collectors of logs and metrics to a Kubernetes cluster. |
| 43 | +func DeployKubeCollectors(ctx context.Context, log logging.Logger, configPath string, configContext string) error { |
| 44 | + log.Info("deploying kube collectors for logs and metrics") |
| 45 | + |
| 46 | + clientConfig, err := GetClientConfig(log, configPath, configContext) |
| 47 | + if err != nil { |
| 48 | + return fmt.Errorf("failed to get client config: %w", err) |
| 49 | + } |
| 50 | + clientset, err := kubernetes.NewForConfig(clientConfig) |
| 51 | + if err != nil { |
| 52 | + return fmt.Errorf("failed to create clientset: %w", err) |
| 53 | + } |
| 54 | + dynamicClient, err := dynamic.NewForConfig(clientConfig) |
| 55 | + if err != nil { |
| 56 | + return fmt.Errorf("failed to create dynamic client: %w", err) |
| 57 | + } |
| 58 | + |
| 59 | + namespace := &corev1.Namespace{ |
| 60 | + ObjectMeta: metav1.ObjectMeta{ |
| 61 | + Name: monitoringNamespace, |
| 62 | + }, |
| 63 | + } |
| 64 | + _, err = clientset.CoreV1().Namespaces().Create(ctx, namespace, metav1.CreateOptions{}) |
| 65 | + if err != nil && !apierrors.IsAlreadyExists(err) { |
| 66 | + return fmt.Errorf("failed to create namespace %s: %w", monitoringNamespace, err) |
| 67 | + } |
| 68 | + |
| 69 | + collectorConfigs := []kubeCollectorConfig{ |
| 70 | + { |
| 71 | + name: promtailCmd, |
| 72 | + secretPrefix: "loki", |
| 73 | + manifest: promtailManifest, |
| 74 | + }, |
| 75 | + { |
| 76 | + name: prometheusCmd, |
| 77 | + secretPrefix: prometheusCmd, |
| 78 | + manifest: prometheusManifest, |
| 79 | + }, |
| 80 | + } |
| 81 | + for _, collectorConfig := range collectorConfigs { |
| 82 | + if err := deployKubeCollector(ctx, log, clientset, dynamicClient, collectorConfig); err != nil { |
| 83 | + return err |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + return nil |
| 88 | +} |
| 89 | + |
| 90 | +// deployKubeCollector deploys a the named collector to a Kubernetes cluster via the provided manifest bytes. |
| 91 | +func deployKubeCollector( |
| 92 | + ctx context.Context, |
| 93 | + log logging.Logger, |
| 94 | + clientset *kubernetes.Clientset, |
| 95 | + dynamicClient dynamic.Interface, |
| 96 | + collectorConfig kubeCollectorConfig, |
| 97 | +) error { |
| 98 | + username, password, err := getCollectorCredentials(collectorConfig.name) |
| 99 | + if err != nil { |
| 100 | + return fmt.Errorf("failed to get credentials for %s: %w", collectorConfig.name, err) |
| 101 | + } |
| 102 | + |
| 103 | + if err := createCredentialSecret(ctx, log, clientset, collectorConfig.secretPrefix, username, password); err != nil { |
| 104 | + return fmt.Errorf("failed to create credential secret for %s: %w", collectorConfig.name, err) |
| 105 | + } |
| 106 | + |
| 107 | + if err := applyManifest(ctx, log, dynamicClient, collectorConfig.manifest); err != nil { |
| 108 | + return fmt.Errorf("failed to apply manifest for %s: %w", collectorConfig.name, err) |
| 109 | + } |
| 110 | + return nil |
| 111 | +} |
| 112 | + |
| 113 | +// createCredentialSecret creates a secret with the provided username and password for a collector |
| 114 | +func createCredentialSecret( |
| 115 | + ctx context.Context, |
| 116 | + log logging.Logger, |
| 117 | + clientset *kubernetes.Clientset, |
| 118 | + namePrefix string, |
| 119 | + username string, |
| 120 | + password string, |
| 121 | +) error { |
| 122 | + secretName := namePrefix + "-credentials" |
| 123 | + secret := &corev1.Secret{ |
| 124 | + ObjectMeta: metav1.ObjectMeta{ |
| 125 | + Name: secretName, |
| 126 | + }, |
| 127 | + StringData: map[string]string{ |
| 128 | + "username": username, |
| 129 | + "password": password, |
| 130 | + }, |
| 131 | + } |
| 132 | + _, err := clientset.CoreV1().Secrets(monitoringNamespace).Create(ctx, secret, metav1.CreateOptions{}) |
| 133 | + if err != nil { |
| 134 | + if apierrors.IsAlreadyExists(err) { |
| 135 | + log.Info("secret already exists", |
| 136 | + zap.String("namespace", monitoringNamespace), |
| 137 | + zap.String("name", secretName), |
| 138 | + ) |
| 139 | + return nil |
| 140 | + } |
| 141 | + return fmt.Errorf("failed to create secret %s/%s: %w", monitoringNamespace, secretName, err) |
| 142 | + } |
| 143 | + |
| 144 | + log.Info("created secret", |
| 145 | + zap.String("namespace", monitoringNamespace), |
| 146 | + zap.String("name", secretName), |
| 147 | + ) |
| 148 | + |
| 149 | + return nil |
| 150 | +} |
| 151 | + |
| 152 | +// applyManifest creates the resources defined by the provided manifest in a manner similar to `kubectl apply -f` |
| 153 | +func applyManifest( |
| 154 | + ctx context.Context, |
| 155 | + log logging.Logger, |
| 156 | + dynamicClient dynamic.Interface, |
| 157 | + manifest []byte, |
| 158 | +) error { |
| 159 | + // Split the manifest into individual resources |
| 160 | + decoder := yaml.NewDecodingSerializer(unstructured.UnstructuredJSONScheme) |
| 161 | + documents := strings.Split(string(manifest), "\n---\n") |
| 162 | + |
| 163 | + for _, doc := range documents { |
| 164 | + doc := strings.TrimSpace(doc) |
| 165 | + if strings.TrimSpace(doc) == "" || strings.HasPrefix(doc, "#") { |
| 166 | + continue |
| 167 | + } |
| 168 | + |
| 169 | + obj := &unstructured.Unstructured{} |
| 170 | + _, gvk, err := decoder.Decode([]byte(doc), nil, obj) |
| 171 | + if err != nil { |
| 172 | + return fmt.Errorf("failed to decode manifest: %w", err) |
| 173 | + } |
| 174 | + |
| 175 | + gvr := schema.GroupVersionResource{ |
| 176 | + Group: gvk.Group, |
| 177 | + Version: gvk.Version, |
| 178 | + Resource: strings.ToLower(gvk.Kind) + "s", |
| 179 | + } |
| 180 | + |
| 181 | + var resourceInterface dynamic.ResourceInterface |
| 182 | + if strings.HasPrefix(gvk.Kind, "Cluster") || gvk.Kind == "Namespace" { |
| 183 | + resourceInterface = dynamicClient.Resource(gvr) |
| 184 | + } else { |
| 185 | + resourceInterface = dynamicClient.Resource(gvr).Namespace(monitoringNamespace) |
| 186 | + } |
| 187 | + |
| 188 | + _, err = resourceInterface.Create(ctx, obj, metav1.CreateOptions{}) |
| 189 | + if err != nil { |
| 190 | + if apierrors.IsAlreadyExists(err) { |
| 191 | + log.Info("resource already exists", |
| 192 | + zap.String("kind", gvk.Kind), |
| 193 | + zap.String("namespace", monitoringNamespace), |
| 194 | + zap.String("name", obj.GetName()), |
| 195 | + ) |
| 196 | + continue |
| 197 | + } |
| 198 | + return fmt.Errorf("failed to create %s %s/%s: %w", gvk.Kind, monitoringNamespace, obj.GetName(), err) |
| 199 | + } |
| 200 | + log.Info("created resource", |
| 201 | + zap.String("kind", gvk.Kind), |
| 202 | + zap.String("namespace", monitoringNamespace), |
| 203 | + zap.String("name", obj.GetName()), |
| 204 | + ) |
| 205 | + } |
| 206 | + |
| 207 | + return nil |
| 208 | +} |
0 commit comments