|
| 1 | +/* |
| 2 | + * Copyright 2025 Red Hat, Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package pfpstatus |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "fmt" |
| 22 | + "os" |
| 23 | + "sync" |
| 24 | + "time" |
| 25 | + |
| 26 | + "github.com/go-logr/logr" |
| 27 | + |
| 28 | + "github.com/k8stopologyawareschedwg/podfingerprint" |
| 29 | + |
| 30 | + "github.com/openshift-kni/debug-tools/pkg/pfpstatus/record" |
| 31 | +) |
| 32 | + |
| 33 | +const ( |
| 34 | + PFPStatusDumpEnvVar string = "PFP_STATUS_DUMP" |
| 35 | +) |
| 36 | + |
| 37 | +const ( |
| 38 | + DefaultDumpDirectory string = "/run/pfpstatus" |
| 39 | +) |
| 40 | + |
| 41 | +const ( |
| 42 | + defaultMaxNodes = 5000 |
| 43 | + defaultMaxSamplesPerNode = 10 |
| 44 | + defaultDumpPeriod = 10 * time.Second |
| 45 | +) |
| 46 | + |
| 47 | +type StorageParams struct { |
| 48 | + Enabled bool |
| 49 | + Directory string |
| 50 | + Period time.Duration |
| 51 | +} |
| 52 | + |
| 53 | +type Params struct { |
| 54 | + Storage StorageParams |
| 55 | +} |
| 56 | + |
| 57 | +type environ struct { |
| 58 | + mu sync.Mutex |
| 59 | + rec *record.Recorder |
| 60 | + lh logr.Logger |
| 61 | +} |
| 62 | + |
| 63 | +func DefaultParams() Params { |
| 64 | + return Params{ |
| 65 | + Storage: StorageParams{ |
| 66 | + Enabled: false, |
| 67 | + Directory: DefaultDumpDirectory, |
| 68 | + Period: 10 * time.Second, |
| 69 | + }, |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +func ParamsFromEnv(lh logr.Logger, params *Params) { |
| 74 | + dumpDir, ok := os.LookupEnv(PFPStatusDumpEnvVar) |
| 75 | + if !ok || dumpDir == "" { |
| 76 | + params.Storage.Enabled = false |
| 77 | + } else { |
| 78 | + params.Storage.Enabled = true |
| 79 | + params.Storage.Directory = dumpDir |
| 80 | + } |
| 81 | + |
| 82 | + // let's try to keep the amount of code we do in init() at minimum. |
| 83 | + // This may happen if the container didn't have the directory mounted |
| 84 | + if !existsBaseDirectory(dumpDir) { |
| 85 | + lh.Info("base directory not found, will discard everything", "baseDirectory", dumpDir) |
| 86 | + params.Storage.Enabled = false |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +func Setup(logh logr.Logger, params Params) { |
| 91 | + if !params.Storage.Enabled { |
| 92 | + logh.Info("no backend enabled, nothing to do") |
| 93 | + return |
| 94 | + } |
| 95 | + |
| 96 | + logh.Info("Setup in progress", "params", fmt.Sprintf("%+#v", params)) |
| 97 | + |
| 98 | + rec, err := record.NewRecorder(defaultMaxNodes, defaultMaxSamplesPerNode, time.Now) |
| 99 | + if err != nil { |
| 100 | + logh.Error(err, "cannot create a status recorder") |
| 101 | + return |
| 102 | + } |
| 103 | + |
| 104 | + ctx := context.Background() |
| 105 | + env := environ{ |
| 106 | + rec: rec, |
| 107 | + lh: logh, |
| 108 | + } |
| 109 | + |
| 110 | + ch := make(chan podfingerprint.Status) |
| 111 | + podfingerprint.SetCompletionSink(ch) |
| 112 | + go collectLoop(ctx, &env, ch) |
| 113 | + if params.Storage.Enabled { |
| 114 | + go dumpLoop(ctx, &env, params.Storage) |
| 115 | + } |
| 116 | +} |
| 117 | + |
| 118 | +func collectLoop(ctx context.Context, env *environ, updates <-chan podfingerprint.Status) { |
| 119 | + env.lh.V(4).Info("collect loop started") |
| 120 | + defer env.lh.V(4).Info("collect loop finished") |
| 121 | + for { |
| 122 | + select { |
| 123 | + case <-ctx.Done(): |
| 124 | + return |
| 125 | + case st := <-updates: |
| 126 | + env.mu.Lock() |
| 127 | + _ = env.rec.Push(st) // intentionally ignore error |
| 128 | + env.mu.Unlock() |
| 129 | + } |
| 130 | + } |
| 131 | +} |
0 commit comments