|
| 1 | +/* |
| 2 | +Copyright 2025 The Kubernetes Authors. |
| 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 main |
| 18 | + |
| 19 | +import ( |
| 20 | + "bytes" |
| 21 | + "flag" |
| 22 | + "fmt" |
| 23 | + "html/template" |
| 24 | + "io/fs" |
| 25 | + "log" |
| 26 | + "os" |
| 27 | + "path/filepath" |
| 28 | + "slices" |
| 29 | + "sort" |
| 30 | + "strconv" |
| 31 | + "strings" |
| 32 | + |
| 33 | + "sigs.k8s.io/yaml" |
| 34 | + |
| 35 | + gep "sigs.k8s.io/gateway-api/pkg/gep" |
| 36 | +) |
| 37 | + |
| 38 | +var ( |
| 39 | + GEPSDir string |
| 40 | + MKDocsTemplate string |
| 41 | + SkipGEPNumber string |
| 42 | +) |
| 43 | + |
| 44 | +// Those are the GEPs that will be included in the final navigation bar |
| 45 | +// The order established below will be the order that the statuses will be shown |
| 46 | +var includeGEPStatus = []gep.GEPStatus{ |
| 47 | + gep.GEPStatusImplementable, |
| 48 | + gep.GEPStatusExperimental, |
| 49 | + gep.GEPStatusStandard, |
| 50 | + gep.GEPStatusMemorandum, |
| 51 | +} |
| 52 | + |
| 53 | +type GEPArray []GEPList |
| 54 | + |
| 55 | +type GEPList struct { |
| 56 | + GepType string |
| 57 | + Geps []uint |
| 58 | +} |
| 59 | + |
| 60 | +type TemplateData struct { |
| 61 | + GEPData GEPArray |
| 62 | +} |
| 63 | + |
| 64 | +const kindDetails = "GEPDetails" |
| 65 | + |
| 66 | +func main() { |
| 67 | + flag.StringVar(&GEPSDir, "g", "", "Defines the absolute path of the directory containing the GEPs") |
| 68 | + flag.StringVar(&MKDocsTemplate, "t", "", "Defines the absolute path of mkdocs.yaml file") |
| 69 | + flag.StringVar(&SkipGEPNumber, "s", "696", "Defines GEPs number to be skipped, should be comma-separated") |
| 70 | + flag.Parse() |
| 71 | + |
| 72 | + if GEPSDir == "" || MKDocsTemplate == "" { |
| 73 | + log.Fatal("-g and -c are mandatory arguments") |
| 74 | + } |
| 75 | + |
| 76 | + if strings.Contains(SkipGEPNumber, " ") { |
| 77 | + log.Fatal("-s flag should not contain spaces") |
| 78 | + } |
| 79 | + |
| 80 | + skipGep := strings.Split(SkipGEPNumber, ",") |
| 81 | + |
| 82 | + geps, err := walkGEPs(GEPSDir, skipGep) |
| 83 | + if err != nil { |
| 84 | + panic(err) |
| 85 | + } |
| 86 | + |
| 87 | + tmpl, err := template.ParseFiles(MKDocsTemplate) |
| 88 | + if err != nil { |
| 89 | + log.Fatalf("error reading mkdocs template: %s", err) |
| 90 | + } |
| 91 | + |
| 92 | + tmplData := TemplateData{ |
| 93 | + GEPData: geps, |
| 94 | + } |
| 95 | + |
| 96 | + buf := &bytes.Buffer{} |
| 97 | + |
| 98 | + if err := tmpl.Execute(buf, tmplData); err != nil { |
| 99 | + panic(err) |
| 100 | + } |
| 101 | + fmt.Print(buf.String()) |
| 102 | +} |
| 103 | + |
| 104 | +func walkGEPs(dir string, skipGEPs []string) (GEPArray, error) { |
| 105 | + gepArray := make(GEPArray, 0) |
| 106 | + tmpMap := make(map[gep.GEPStatus]GEPList) |
| 107 | + |
| 108 | + err := filepath.WalkDir(dir, func(path string, d fs.DirEntry, err error) error { |
| 109 | + if err != nil { |
| 110 | + return fmt.Errorf("error accessing %s: %w", path, err) |
| 111 | + } |
| 112 | + if d.IsDir() || d.Name() != "metadata.yaml" { |
| 113 | + return nil |
| 114 | + } |
| 115 | + |
| 116 | + content, err := os.ReadFile(path) |
| 117 | + if err != nil { |
| 118 | + return err |
| 119 | + } |
| 120 | + |
| 121 | + gepDetail := &gep.GEPDetail{} |
| 122 | + log.Printf("checking %s", path) |
| 123 | + if err := yaml.Unmarshal(content, gepDetail); err != nil { |
| 124 | + return err |
| 125 | + } |
| 126 | + |
| 127 | + if gepDetail.Kind != kindDetails { |
| 128 | + return nil |
| 129 | + } |
| 130 | + |
| 131 | + // Skip the GEPs types we don't care |
| 132 | + if !slices.Contains(includeGEPStatus, gepDetail.Status) { |
| 133 | + return nil |
| 134 | + } |
| 135 | + |
| 136 | + // Skip the GEPs numbers we don't care |
| 137 | + if slices.Contains(skipGEPs, strconv.FormatUint(uint64(gepDetail.Number), 10)) { |
| 138 | + return nil |
| 139 | + } |
| 140 | + |
| 141 | + // Add the GEP to a map indexed by GEP types, so we can provide the sorted array |
| 142 | + // easily later |
| 143 | + _, ok := tmpMap[gepDetail.Status] |
| 144 | + if !ok { |
| 145 | + tmpMap[gepDetail.Status] = GEPList{ |
| 146 | + GepType: string(gepDetail.Status), |
| 147 | + Geps: make([]uint, 0), |
| 148 | + } |
| 149 | + } |
| 150 | + |
| 151 | + item := tmpMap[gepDetail.Status] |
| 152 | + item.Geps = append(item.Geps, gepDetail.Number) |
| 153 | + tmpMap[gepDetail.Status] = item |
| 154 | + return nil |
| 155 | + }) |
| 156 | + if err != nil { |
| 157 | + return nil, err |
| 158 | + } |
| 159 | + |
| 160 | + // Include the GEPs toc on the desired order |
| 161 | + for _, v := range includeGEPStatus { |
| 162 | + if geps, ok := tmpMap[v]; ok { |
| 163 | + gepArray = append(gepArray, geps) |
| 164 | + } |
| 165 | + } |
| 166 | + |
| 167 | + for i := range gepArray { |
| 168 | + sort.SliceStable(gepArray[i].Geps, func(x, y int) bool { |
| 169 | + return gepArray[i].Geps[x] < gepArray[i].Geps[y] |
| 170 | + }) |
| 171 | + } |
| 172 | + |
| 173 | + return gepArray, nil |
| 174 | +} |
0 commit comments