From 88ffb7bf01a377b1cb93c8e159d4e16072cf9229 Mon Sep 17 00:00:00 2001 From: eoinfennessy Date: Sun, 31 Mar 2024 20:21:28 +0100 Subject: [PATCH] Add option types --- .gitignore | 1 + Makefile | 24 +++ README.md | 3 + go.mod | 26 +++ go.sum | 92 +++++++++ hack/boilerplate.go.txt | 15 ++ types/option/option.go | 263 ++++++++++++++++++++++++++ types/option/zz_generated.deepcopy.go | 148 +++++++++++++++ 8 files changed, 572 insertions(+) create mode 100644 .gitignore create mode 100644 Makefile create mode 100644 README.md create mode 100644 go.mod create mode 100644 go.sum create mode 100644 hack/boilerplate.go.txt create mode 100644 types/option/option.go create mode 100644 types/option/zz_generated.deepcopy.go diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..efa6632 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +bin/* \ No newline at end of file diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..9b84f9d --- /dev/null +++ b/Makefile @@ -0,0 +1,24 @@ +.PHONY: help +help: ## Display this help. + @awk 'BEGIN {FS = ":.*##"; printf "\nUsage:\n make \033[36m\033[0m\n"} /^[a-zA-Z_0-9-]+:.*?##/ { printf " \033[36m%-15s\033[0m %s\n", $$1, $$2 } /^##@/ { printf "\n\033[1m%s\033[0m\n", substr($$0, 5) } ' $(MAKEFILE_LIST) + +.PHONY: generate +generate: controller-gen ## Generate code containing DeepCopy, DeepCopyInto, and DeepCopyObject method implementations. + $(CONTROLLER_GEN) object:headerFile="hack/boilerplate.go.txt" paths="./..." + +## Location to install dependencies to +LOCALBIN ?= $(shell pwd)/bin +$(LOCALBIN): + mkdir -p $(LOCALBIN) + +## Tool Binaries +CONTROLLER_GEN ?= $(LOCALBIN)/controller-gen + +## Tool Versions +CONTROLLER_TOOLS_VERSION ?= v0.13.0 + +.PHONY: controller-gen +controller-gen: $(CONTROLLER_GEN) ## Download controller-gen locally if necessary. If wrong version is installed, it will be overwritten. +$(CONTROLLER_GEN): $(LOCALBIN) + test -s $(LOCALBIN)/controller-gen && $(LOCALBIN)/controller-gen --version | grep -q $(CONTROLLER_TOOLS_VERSION) || \ + GOBIN=$(LOCALBIN) go install sigs.k8s.io/controller-tools/cmd/controller-gen@$(CONTROLLER_TOOLS_VERSION) diff --git a/README.md b/README.md new file mode 100644 index 0000000..2b66e52 --- /dev/null +++ b/README.md @@ -0,0 +1,3 @@ +## Making changes + +Run `make generate` to generate `DeepCopy` methods after changing types \ No newline at end of file diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..3c99ab2 --- /dev/null +++ b/go.mod @@ -0,0 +1,26 @@ +module github.com/kraken-iac/common + +go 1.20 + +require github.com/kraken-iac/kraken v0.3.1 + +require ( + github.com/Jeffail/gabs/v2 v2.7.0 + github.com/go-logr/logr v1.2.4 // indirect + github.com/gogo/protobuf v1.3.2 // indirect + github.com/google/gofuzz v1.2.0 // indirect + github.com/json-iterator/go v1.1.12 // indirect + github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect + github.com/modern-go/reflect2 v1.0.2 // indirect + golang.org/x/net v0.17.0 // indirect + golang.org/x/text v0.13.0 // indirect + gopkg.in/inf.v0 v0.9.1 // indirect + gopkg.in/yaml.v2 v2.4.0 // indirect + k8s.io/apiextensions-apiserver v0.28.3 // indirect + k8s.io/apimachinery v0.28.3 // indirect + k8s.io/klog/v2 v2.100.1 // indirect + k8s.io/utils v0.0.0-20230406110748-d93618cff8a2 // indirect + sigs.k8s.io/controller-runtime v0.16.3 // indirect + sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd // indirect + sigs.k8s.io/structured-merge-diff/v4 v4.2.3 // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..e23cd8d --- /dev/null +++ b/go.sum @@ -0,0 +1,92 @@ +github.com/Jeffail/gabs/v2 v2.7.0 h1:Y2edYaTcE8ZpRsR2AtmPu5xQdFDIthFG0jYhu5PY8kg= +github.com/Jeffail/gabs/v2 v2.7.0/go.mod h1:dp5ocw1FvBBQYssgHsG7I1WYsiLRtkUaB1FEtSwvNUw= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/go-logr/logr v1.2.0/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A= +github.com/go-logr/logr v1.2.4 h1:g01GSCwiDw2xSZfjJ2/T9M+S6pFdcNtFYsp+Y43HYDQ= +github.com/go-logr/logr v1.2.4/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A= +github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572 h1:tfuBGBXKqDEevZMzYi5KSi8KkcZtzBcTgAUUtapy0OI= +github.com/gogo/protobuf v1.3.2 h1:Ov1cvc58UF3b5XjBnZv7+opcTcQFZebYjWzi34vdm4Q= +github.com/gogo/protobuf v1.3.2/go.mod h1:P1XiOD3dCwIKUDQYPy72D8LYyHL2YPYrpS2s69NZV8Q= +github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38= +github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= +github.com/google/gofuzz v1.2.0 h1:xRy4A+RhZaiKjJ1bPfwQ8sedCA+YS2YcCHW6ec7JMi0= +github.com/google/gofuzz v1.2.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= +github.com/google/pprof v0.0.0-20210720184732-4bb14d4b1be1 h1:K6RDEckDVWvDI9JAJYCmNdQXq6neHJOYx3V6jnqNEec= +github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM= +github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo= +github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI2bnpBCr8= +github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck= +github.com/kraken-iac/kraken v0.3.1 h1:k4o5dSMdjL3Ry/73/DGlZ+64GgBaVP7+bBvsVmTONKo= +github.com/kraken-iac/kraken v0.3.1/go.mod h1:DWYEW7ukNyx8y5y5mTKnIiKZOyc0xg8v/OhVqzekAk8= +github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= +github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg= +github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= +github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M= +github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk= +github.com/onsi/ginkgo/v2 v2.11.0 h1:WgqUCUt/lT6yXoQ8Wef0fsNn5cAuMK7+KT9UFRz2tcU= +github.com/onsi/gomega v1.27.10 h1:naR28SdDFlqrG6kScpT8VWpu1xWY5nJRCF3XaYyBjhI= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= +github.com/stretchr/testify v1.8.2 h1:+h33VjcLVPDHtOdpUCuF+7gSuG3yGIftsP1YvFihtJ8= +github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= +github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= +golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= +golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= +golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= +golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= +golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= +golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= +golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= +golang.org/x/net v0.0.0-20200226121028-0de0cce0169b/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= +golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU= +golang.org/x/net v0.17.0 h1:pVaXccu2ozPjCXewfr1S7xza/zcXTity9cCdXQYSjIM= +golang.org/x/net v0.17.0/go.mod h1:NxSsAGuq816PNPmqtQdLE42eU2Fs7NoRIZrHJAlaCOE= +golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.13.0 h1:Af8nKPmuFypiUBjVoU9V20FiaFXOcuZI21p0ycVYYGE= +golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= +golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= +golang.org/x/text v0.13.0 h1:ablQoSUd0tRdKxZewP80B+BaqeKJuVhuRxj/dkrun3k= +golang.org/x/text v0.13.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE= +golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= +golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= +golang.org/x/tools v0.0.0-20200619180055-7c47624df98f/go.mod h1:EkVYQZoAsY45+roYkvgYkIh4xh/qjgUK9TdY2XT94GE= +golang.org/x/tools v0.0.0-20210106214847-113979e3529a/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA= +golang.org/x/tools v0.9.3 h1:Gn1I8+64MsuTb/HpH+LmQtNas23LhUVr3rYZ0eKuaMM= +golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/inf.v0 v0.9.1 h1:73M5CoZyi3ZLMOyDlQh031Cx6N9NDJ2Vvfl76EDAgDc= +gopkg.in/inf.v0 v0.9.1/go.mod h1:cWUDdTG/fYaXco+Dcufb5Vnc6Gp2YChqWtbxRZE0mXw= +gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY= +gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +k8s.io/api v0.28.3 h1:Gj1HtbSdB4P08C8rs9AR94MfSGpRhJgsS+GF9V26xMM= +k8s.io/apiextensions-apiserver v0.28.3 h1:Od7DEnhXHnHPZG+W9I97/fSQkVpVPQx2diy+2EtmY08= +k8s.io/apiextensions-apiserver v0.28.3/go.mod h1:NE1XJZ4On0hS11aWWJUTNkmVB03j9LM7gJSisbRt8Lc= +k8s.io/apimachinery v0.28.3 h1:B1wYx8txOaCQG0HmYF6nbpU8dg6HvA06x5tEffvOe7A= +k8s.io/apimachinery v0.28.3/go.mod h1:uQTKmIqs+rAYaq+DFaoD2X7pcjLOqbQX2AOiO0nIpb8= +k8s.io/klog/v2 v2.100.1 h1:7WCHKK6K8fNhTqfBhISHQ97KrnJNFZMcQvKp7gP/tmg= +k8s.io/klog/v2 v2.100.1/go.mod h1:y1WjHnz7Dj687irZUWR/WLkLc5N1YHtjLdmgWjndZn0= +k8s.io/utils v0.0.0-20230406110748-d93618cff8a2 h1:qY1Ad8PODbnymg2pRbkyMT/ylpTrCM8P2RJ0yroCyIk= +k8s.io/utils v0.0.0-20230406110748-d93618cff8a2/go.mod h1:OLgZIPagt7ERELqWJFomSt595RzquPNLL48iOWgYOg0= +sigs.k8s.io/controller-runtime v0.16.3 h1:2TuvuokmfXvDUamSx1SuAOO3eTyye+47mJCigwG62c4= +sigs.k8s.io/controller-runtime v0.16.3/go.mod h1:j7bialYoSn142nv9sCOJmQgDXQXxnroFU4VnX/brVJ0= +sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd h1:EDPBXCAspyGV4jQlpZSudPeMmr1bNJefnuqLsRAsHZo= +sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd/go.mod h1:B8JuhiUyNFVKdsE8h686QcCxMaH6HrOAZj4vswFpcB0= +sigs.k8s.io/structured-merge-diff/v4 v4.2.3 h1:PRbqxJClWWYMNV1dhaG4NsibJbArud9kFxnAMREiWFE= +sigs.k8s.io/structured-merge-diff/v4 v4.2.3/go.mod h1:qjx8mGObPmV2aSZepjQjbmb2ihdVs8cGKBraizNC69E= +sigs.k8s.io/yaml v1.3.0 h1:a2VclLzOGrwOHDiV8EfBGhvjHvP46CtW5j6POvhYGGo= diff --git a/hack/boilerplate.go.txt b/hack/boilerplate.go.txt new file mode 100644 index 0000000..ff72ff2 --- /dev/null +++ b/hack/boilerplate.go.txt @@ -0,0 +1,15 @@ +/* +Copyright 2024. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ \ No newline at end of file diff --git a/types/option/option.go b/types/option/option.go new file mode 100644 index 0000000..4ea69ec --- /dev/null +++ b/types/option/option.go @@ -0,0 +1,263 @@ +// +kubebuilder:object:generate=true + +/* +Copyright 2024. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package option + +// TODO: Move this package into a shared types repo + +import ( + "errors" + "fmt" + "reflect" + "strconv" + + "github.com/Jeffail/gabs/v2" + krakenv1alpha1 "github.com/kraken-iac/kraken/api/v1alpha1" +) + +var ( + errValidationNoValueOrRef = errors.New("neither value reference nor concrete value provided") + errValidationBothValueAndRef = errors.New("both value reference and concrete value provided") +) + +type ValueFromConfigMap struct { + // +kubebuilder:validation:Required + Name string `json:"name"` + + // +kubebuilder:validation:Required + Key string `json:"key"` +} + +func (vfcm ValueFromConfigMap) ToConfigMapDependency() krakenv1alpha1.ConfigMapDependency { + return krakenv1alpha1.ConfigMapDependency{ + Name: vfcm.Name, + Key: vfcm.Key, + } +} + +func (vfcm ValueFromConfigMap) Validate() error { + if vfcm.Name == "" { + return errors.New("ConfigMap name cannot be empty") + } + if vfcm.Key == "" { + return errors.New("ConfigMap key cannot be empty") + } + return nil +} + +type ValueFromSecret struct { + // +kubebuilder:validation:Required + Name string `json:"name"` + + // +kubebuilder:validation:Required + Key string `json:"key"` +} + +func (vfs ValueFromSecret) ToSecretDependency() { + panic("Not implemented") +} + +type ValueFromKrakenResource struct { + // +kubebuilder:validation:Required + Kind string `json:"kind"` + + // +kubebuilder:validation:Required + Name string `json:"name"` + + // +kubebuilder:validation:Required + Path string `json:"path"` +} + +func (vfkr ValueFromKrakenResource) ToKrakenResourceDependency(kind reflect.Kind) krakenv1alpha1.KrakenResourceDependency { + return krakenv1alpha1.KrakenResourceDependency{ + Kind: vfkr.Kind, + Name: vfkr.Name, + Path: vfkr.Path, + ReflectKind: kind, + } +} + +type ValueFrom struct { + ConfigMap *ValueFromConfigMap `json:"configMap,omitempty"` + Secret *ValueFromSecret `json:"secret,omitempty"` + KrakenResource *ValueFromKrakenResource `json:"krakenResource,omitempty"` +} + +func (vf ValueFrom) AddToDependencyRequestSpec(dr *krakenv1alpha1.DependencyRequestSpec, kind reflect.Kind) { + if vf.KrakenResource != nil { + dr.KrakenResourceDependencies = append(dr.KrakenResourceDependencies, vf.KrakenResource.ToKrakenResourceDependency(kind)) + } + if vf.ConfigMap != nil { + dr.ConfigMapDependencies = append(dr.ConfigMapDependencies, vf.ConfigMap.ToConfigMapDependency()) + } + if vf.Secret != nil { + panic("Unimplemented") + } +} + +func (vf ValueFrom) Validate() error { + nonNilCount := 0 + if vf.ConfigMap != nil { + nonNilCount++ + } + if vf.Secret != nil { + nonNilCount++ + } + if vf.KrakenResource != nil { + nonNilCount++ + } + if nonNilCount != 1 { + return fmt.Errorf("expected a single value reference but received %d", nonNilCount) + } + return nil +} + +type String struct { + Value *string `json:"value,omitempty"` + ValueFrom *ValueFrom `json:"valueFrom,omitempty"` +} + +func (s String) ToApplicableValue(dv krakenv1alpha1.DependentValues) (*string, error) { + if s.Value != nil { + return s.Value, nil + } + if s.ValueFrom == nil { + return nil, nil + } + if s.ValueFrom.ConfigMap != nil { + return getValueFromConfigMap(s.ValueFrom.ConfigMap, dv.FromConfigMaps) + } + if s.ValueFrom.KrakenResource != nil { + return getValueFromKrakenResource[string](s.ValueFrom.KrakenResource, dv.FromKrakenResources) + } + return nil, errors.New("ValueFrom object is not nil but does not contain any non-nil pointer references") +} + +func (s String) Validate() error { + if s.Value != nil { + if s.ValueFrom != nil { + return errValidationBothValueAndRef + } + return nil + } else { + if s.ValueFrom == nil { + return errValidationNoValueOrRef + } + } + return s.ValueFrom.Validate() +} + +type Int struct { + Value *int `json:"value,omitempty"` + ValueFrom *ValueFrom `json:"valueFrom,omitempty"` +} + +func (i Int) ToApplicableValue(dv krakenv1alpha1.DependentValues) (*int, error) { + if i.Value != nil { + return i.Value, nil + } + if i.ValueFrom == nil { + return nil, nil + } + if i.ValueFrom.ConfigMap != nil { + valString, err := getValueFromConfigMap(i.ValueFrom.ConfigMap, dv.FromConfigMaps) + if err != nil { + return nil, err + } + val, err := strconv.Atoi(*valString) + if err != nil { + return nil, err + } + return &val, nil + } + if i.ValueFrom.KrakenResource != nil { + // Unmarshalled JSON numbers are of type float64 + valFloat, err := getValueFromKrakenResource[float64](i.ValueFrom.KrakenResource, dv.FromKrakenResources) + if err != nil { + return nil, err + } + val := int(*valFloat) + return &val, nil + } + return nil, errors.New("ValueFrom object is not nil but does not contain any non-nil pointer references") +} + +func (i Int) Validate() error { + if i.Value != nil { + if i.ValueFrom != nil { + return errValidationBothValueAndRef + } + return nil + } else { + if i.ValueFrom == nil { + return errValidationNoValueOrRef + } + } + return i.ValueFrom.Validate() +} + +func getValueFromConfigMap(cmRef *ValueFromConfigMap, cmVals krakenv1alpha1.DependentValuesFromConfigMaps) (*string, error) { + cm, exists := cmVals[cmRef.Name] + if !exists { + return nil, fmt.Errorf("ConfigMap \"%s\" does not exist in DependentValues", cmRef.Name) + } + val, exists := cm[cmRef.Key] + if !exists { + return nil, fmt.Errorf("key \"%s\" does not exist in DependentValues ConfigMap \"%s\"", cmRef.Key, cmRef.Name) + } + return &val, nil +} + +func getValueFromKrakenResource[T any]( + krRef *ValueFromKrakenResource, + krVals krakenv1alpha1.DependentValuesFromKrakenResources, +) (*T, error) { + kind, exists := krVals[krRef.Kind] + if !exists { + return nil, fmt.Errorf("no entry for kind \"%s\" in DependentValues", krRef.Kind) + } + resource, exists := kind[krRef.Name] + if !exists { + return nil, fmt.Errorf("no entry for resource \"%s\" in DependentValues", krRef.Name) + } + jsonVal, exists := resource[krRef.Path] + if !exists { + return nil, fmt.Errorf("no entry for path \"%s\" in DependentValues", krRef.Path) + } + + jsonContainer, err := gabs.ParseJSON(jsonVal.Raw) + if err != nil { + return nil, fmt.Errorf("error parsing JSON: %s", err) + } + data := jsonContainer.Data() + + var val T + expectedType := reflect.TypeOf(val).Kind() + actualType := reflect.TypeOf(data).Kind() + if actualType != expectedType { + return nil, fmt.Errorf( + "provided value \"%s\" is of type \"%s\"; expected type \"%s\"", + data, + actualType, + expectedType, + ) + } + + val = data.(T) + return &val, nil +} diff --git a/types/option/zz_generated.deepcopy.go b/types/option/zz_generated.deepcopy.go new file mode 100644 index 0000000..f6309f4 --- /dev/null +++ b/types/option/zz_generated.deepcopy.go @@ -0,0 +1,148 @@ +//go:build !ignore_autogenerated + +/* +Copyright 2024. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +// Code generated by controller-gen. DO NOT EDIT. + +package option + +import () + +// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. +func (in *Int) DeepCopyInto(out *Int) { + *out = *in + if in.Value != nil { + in, out := &in.Value, &out.Value + *out = new(int) + **out = **in + } + if in.ValueFrom != nil { + in, out := &in.ValueFrom, &out.ValueFrom + *out = new(ValueFrom) + (*in).DeepCopyInto(*out) + } +} + +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new Int. +func (in *Int) DeepCopy() *Int { + if in == nil { + return nil + } + out := new(Int) + in.DeepCopyInto(out) + return out +} + +// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. +func (in *String) DeepCopyInto(out *String) { + *out = *in + if in.Value != nil { + in, out := &in.Value, &out.Value + *out = new(string) + **out = **in + } + if in.ValueFrom != nil { + in, out := &in.ValueFrom, &out.ValueFrom + *out = new(ValueFrom) + (*in).DeepCopyInto(*out) + } +} + +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new String. +func (in *String) DeepCopy() *String { + if in == nil { + return nil + } + out := new(String) + in.DeepCopyInto(out) + return out +} + +// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. +func (in *ValueFrom) DeepCopyInto(out *ValueFrom) { + *out = *in + if in.ConfigMap != nil { + in, out := &in.ConfigMap, &out.ConfigMap + *out = new(ValueFromConfigMap) + **out = **in + } + if in.Secret != nil { + in, out := &in.Secret, &out.Secret + *out = new(ValueFromSecret) + **out = **in + } + if in.KrakenResource != nil { + in, out := &in.KrakenResource, &out.KrakenResource + *out = new(ValueFromKrakenResource) + **out = **in + } +} + +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new ValueFrom. +func (in *ValueFrom) DeepCopy() *ValueFrom { + if in == nil { + return nil + } + out := new(ValueFrom) + in.DeepCopyInto(out) + return out +} + +// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. +func (in *ValueFromConfigMap) DeepCopyInto(out *ValueFromConfigMap) { + *out = *in +} + +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new ValueFromConfigMap. +func (in *ValueFromConfigMap) DeepCopy() *ValueFromConfigMap { + if in == nil { + return nil + } + out := new(ValueFromConfigMap) + in.DeepCopyInto(out) + return out +} + +// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. +func (in *ValueFromKrakenResource) DeepCopyInto(out *ValueFromKrakenResource) { + *out = *in +} + +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new ValueFromKrakenResource. +func (in *ValueFromKrakenResource) DeepCopy() *ValueFromKrakenResource { + if in == nil { + return nil + } + out := new(ValueFromKrakenResource) + in.DeepCopyInto(out) + return out +} + +// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. +func (in *ValueFromSecret) DeepCopyInto(out *ValueFromSecret) { + *out = *in +} + +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new ValueFromSecret. +func (in *ValueFromSecret) DeepCopy() *ValueFromSecret { + if in == nil { + return nil + } + out := new(ValueFromSecret) + in.DeepCopyInto(out) + return out +}