-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
controller-gen does not support validating internal list items on list types, see kubernetes-sigs/controller-tools#342 To add host pattern we used perl hack that is hard to extend to multiple validations. Also #16 added optional tls spec that contains hosts field for which perl hack worked by accident, see #16 (comment) This change replaces perl hack for a go hack and adds max length constraint. Signed-off-by: Alexander Yastrebov <alexander.yastrebov@zalando.de>
- Loading branch information
1 parent
d0c44d0
commit b67155e
Showing
4 changed files
with
100 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
// This program add host validation to CRD yaml. | ||
// | ||
// # Why | ||
// | ||
// controller-gen does not support validating internal list items on list types, | ||
// see https://github.com/kubernetes-sigs/controller-tools/issues/342 | ||
package main | ||
|
||
import ( | ||
"log" | ||
"os" | ||
|
||
"sigs.k8s.io/yaml" | ||
) | ||
|
||
const ( | ||
hostPattern = `^[a-z0-9]([-a-z0-9]*[a-z0-9])?([.][a-z0-9]([-a-z0-9]*[a-z0-9])?)*$` | ||
hostMaxLength = 255 // https://datatracker.ietf.org/doc/html/rfc1035#section-2.3.4 | ||
) | ||
|
||
func must(err error) { | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
} | ||
|
||
func mustGet(v []byte, err error) []byte { | ||
must(err) | ||
return v | ||
} | ||
|
||
type setter struct { | ||
o interface{} | ||
} | ||
|
||
func (s *setter) field(name string) *setter { | ||
return &setter{s.o.(map[string]interface{})[name]} | ||
} | ||
|
||
func (s *setter) item(i int) *setter { | ||
return &setter{s.o.([]interface{})[i]} | ||
} | ||
|
||
func (s *setter) setField(name string, value interface{}) *setter { | ||
s.o.(map[string]interface{})[name] = value | ||
return s | ||
} | ||
|
||
func main() { | ||
if len(os.Args) != 2 { | ||
log.Fatalf("CRD filename required") | ||
} | ||
crdFilename := os.Args[1] | ||
|
||
yamlBytes := mustGet(os.ReadFile(crdFilename)) | ||
|
||
o := make(map[string]interface{}) | ||
must(yaml.Unmarshal(yamlBytes, &o)) | ||
|
||
s := &setter{o} | ||
|
||
s.field("spec"). | ||
field("versions"). | ||
item(0). | ||
field("schema"). | ||
field("openAPIV3Schema"). | ||
field("properties"). | ||
field("spec"). | ||
field("properties"). | ||
field("hosts"). | ||
field("items"). | ||
setField("pattern", hostPattern). | ||
setField("maxLength", hostMaxLength) | ||
|
||
s.field("spec"). | ||
field("versions"). | ||
item(0). | ||
field("schema"). | ||
field("openAPIV3Schema"). | ||
field("properties"). | ||
field("spec"). | ||
field("properties"). | ||
field("tls"). | ||
field("items"). | ||
field("properties"). | ||
field("hosts"). | ||
field("items"). | ||
setField("pattern", hostPattern). | ||
setField("maxLength", hostMaxLength) | ||
|
||
outYaml := mustGet(yaml.Marshal(o)) | ||
must(os.WriteFile(crdFilename, outYaml, 0664)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters