-
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 <[email protected]>
- 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -32,9 +32,9 @@ $(GENERATED): $(CRD_TYPE_SOURCE) | |
|
||
$(GENERATED_CRD): go.mod $(GENERATED) | ||
go run sigs.k8s.io/controller-tools/cmd/[email protected] crd:crdVersions=v1 paths=./apis/... output:crd:dir=. | ||
# workaround to add pattern to array items. Not supported by controller-gen | ||
# workaround to add validation to array items. Not supported by controller-gen | ||
# ref: https://github.com/kubernetes-sigs/controller-tools/issues/342 | ||
perl -i -p0e 's|(\s*)(hosts:.*?items:)|$$1$$2$$1 pattern: "^[a-z0-9]([-a-z0-9]*[a-z0-9])?([.][a-z0-9]([-a-z0-9]*[a-z0-9])?)*\$$"|sg' $(GENERATED_CRD) | ||
go run hack/crd/add_hosts_validation.go $(GENERATED_CRD) | ||
|
||
build.local: $(LOCAL_BINARIES) $(GENERATED_CRD) | ||
build.linux: $(LINUX_BINARIES) $(GENERATED_CRD) | ||
|
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