diff --git a/api/v1alpha1/clusterconfig_types.go b/api/v1alpha1/clusterconfig_types.go index df17b8dfa..c242f1e1c 100644 --- a/api/v1alpha1/clusterconfig_types.go +++ b/api/v1alpha1/clusterconfig_types.go @@ -215,11 +215,6 @@ type CNI struct { func (CNI) VariableSchema() clusterv1.VariableSchema { supportedCNIProviders := []string{CNIProviderCalico} - cniProviderEnumVals, err := variables.ValuesToEnumJSON(supportedCNIProviders...) - if err != nil { - panic(err) - } - return clusterv1.VariableSchema{ OpenAPIV3Schema: clusterv1.JSONSchemaProps{ Type: "object", @@ -227,7 +222,7 @@ func (CNI) VariableSchema() clusterv1.VariableSchema { "provider": { Description: "CNI provider to deploy", Type: "string", - Enum: cniProviderEnumVals, + Enum: variables.MustMarshalValuesToEnumJSON(supportedCNIProviders...), }, }, Required: []string{"provider"}, diff --git a/cmd/main.go b/cmd/main.go index ac83efbf3..aafd60ce9 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -27,15 +27,15 @@ import ( "github.com/d2iq-labs/capi-runtime-extensions/common/pkg/capi/clustertopology/handlers" "github.com/d2iq-labs/capi-runtime-extensions/common/pkg/capi/clustertopology/handlers/mutation" "github.com/d2iq-labs/capi-runtime-extensions/common/pkg/server" - "github.com/d2iq-labs/capi-runtime-extensions/pkg/handlers/auditpolicy" - "github.com/d2iq-labs/capi-runtime-extensions/pkg/handlers/clusterconfig" - "github.com/d2iq-labs/capi-runtime-extensions/pkg/handlers/cni/calico" - "github.com/d2iq-labs/capi-runtime-extensions/pkg/handlers/etcd" - "github.com/d2iq-labs/capi-runtime-extensions/pkg/handlers/extraapiservercertsans" - "github.com/d2iq-labs/capi-runtime-extensions/pkg/handlers/httpproxy" - "github.com/d2iq-labs/capi-runtime-extensions/pkg/handlers/kubernetesimagerepository" - "github.com/d2iq-labs/capi-runtime-extensions/pkg/handlers/nfd" - "github.com/d2iq-labs/capi-runtime-extensions/pkg/handlers/servicelbgc" + "github.com/d2iq-labs/capi-runtime-extensions/pkg/handlers/generic/clusterconfig" + "github.com/d2iq-labs/capi-runtime-extensions/pkg/handlers/generic/lifecycle/cni/calico" + "github.com/d2iq-labs/capi-runtime-extensions/pkg/handlers/generic/lifecycle/nfd" + "github.com/d2iq-labs/capi-runtime-extensions/pkg/handlers/generic/lifecycle/servicelbgc" + "github.com/d2iq-labs/capi-runtime-extensions/pkg/handlers/generic/mutation/auditpolicy" + "github.com/d2iq-labs/capi-runtime-extensions/pkg/handlers/generic/mutation/etcd" + "github.com/d2iq-labs/capi-runtime-extensions/pkg/handlers/generic/mutation/extraapiservercertsans" + "github.com/d2iq-labs/capi-runtime-extensions/pkg/handlers/generic/mutation/httpproxy" + "github.com/d2iq-labs/capi-runtime-extensions/pkg/handlers/generic/mutation/kubernetesimagerepository" ) // Flags. diff --git a/common/pkg/capi/clustertopology/variables/enums.go b/common/pkg/capi/clustertopology/variables/enums.go index e76c149e3..49a0387dc 100644 --- a/common/pkg/capi/clustertopology/variables/enums.go +++ b/common/pkg/capi/clustertopology/variables/enums.go @@ -4,22 +4,15 @@ package variables import ( - "encoding/json" - "fmt" - apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1" ) -func ValuesToEnumJSON[T any](vals ...T) ([]apiextensionsv1.JSON, error) { +func MustMarshalValuesToEnumJSON[T any](vals ...T) []apiextensionsv1.JSON { enumJSON := make([]apiextensionsv1.JSON, 0, len(vals)) for _, v := range vals { - enumVal, err := json.Marshal(v) - if err != nil { - return nil, fmt.Errorf("failed to marshal enum value: %v", v) - } - enumJSON = append(enumJSON, apiextensionsv1.JSON{Raw: enumVal}) + enumJSON = append(enumJSON, *MustMarshal(v)) } - return enumJSON, nil + return enumJSON } diff --git a/common/pkg/capi/clustertopology/variables/eums_test.go b/common/pkg/capi/clustertopology/variables/eums_test.go index 83ad69784..3a059f107 100644 --- a/common/pkg/capi/clustertopology/variables/eums_test.go +++ b/common/pkg/capi/clustertopology/variables/eums_test.go @@ -58,8 +58,7 @@ func TestValuesToEnumJSON(t *testing.T) { g := gomega.NewWithT(t) // Call the function under test - jsonData, err := ValuesToEnumJSON(tt.input...) - g.Expect(err).NotTo(gomega.HaveOccurred()) + jsonData := MustMarshalValuesToEnumJSON(tt.input...) // Assert the result g.Expect(jsonData).To(gomega.HaveLen(len(tt.expected))) diff --git a/common/pkg/capi/clustertopology/variables/json.go b/common/pkg/capi/clustertopology/variables/json.go new file mode 100644 index 000000000..c7bafd148 --- /dev/null +++ b/common/pkg/capi/clustertopology/variables/json.go @@ -0,0 +1,20 @@ +// Copyright 2023 D2iQ, Inc. All rights reserved. +// SPDX-License-Identifier: Apache-2.0 + +package variables + +import ( + "encoding/json" + "fmt" + + apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1" +) + +func MustMarshal(val any) *apiextensionsv1.JSON { + marshaled, err := json.Marshal(val) + if err != nil { + panic(fmt.Errorf("failed to marshal enum value: %w", err)) + } + + return &apiextensionsv1.JSON{Raw: marshaled} +} diff --git a/pkg/handlers/clusterconfig/variables.go b/pkg/handlers/generic/clusterconfig/variables.go similarity index 100% rename from pkg/handlers/clusterconfig/variables.go rename to pkg/handlers/generic/clusterconfig/variables.go diff --git a/pkg/handlers/clusterconfig/variables_test.go b/pkg/handlers/generic/clusterconfig/variables_test.go similarity index 100% rename from pkg/handlers/clusterconfig/variables_test.go rename to pkg/handlers/generic/clusterconfig/variables_test.go diff --git a/pkg/handlers/cni/calico/doc.go b/pkg/handlers/generic/lifecycle/cni/calico/doc.go similarity index 100% rename from pkg/handlers/cni/calico/doc.go rename to pkg/handlers/generic/lifecycle/cni/calico/doc.go diff --git a/pkg/handlers/cni/calico/handler.go b/pkg/handlers/generic/lifecycle/cni/calico/handler.go similarity index 99% rename from pkg/handlers/cni/calico/handler.go rename to pkg/handlers/generic/lifecycle/cni/calico/handler.go index 0fe36b466..fb3e7639d 100644 --- a/pkg/handlers/cni/calico/handler.go +++ b/pkg/handlers/generic/lifecycle/cni/calico/handler.go @@ -30,7 +30,7 @@ import ( "github.com/d2iq-labs/capi-runtime-extensions/common/pkg/k8s/client" "github.com/d2iq-labs/capi-runtime-extensions/common/pkg/k8s/parser" "github.com/d2iq-labs/capi-runtime-extensions/pkg/handlers" - "github.com/d2iq-labs/capi-runtime-extensions/pkg/handlers/cni" + "github.com/d2iq-labs/capi-runtime-extensions/pkg/handlers/generic/lifecycle/cni" ) const ( diff --git a/pkg/handlers/cni/cluster.go b/pkg/handlers/generic/lifecycle/cni/cluster.go similarity index 100% rename from pkg/handlers/cni/cluster.go rename to pkg/handlers/generic/lifecycle/cni/cluster.go diff --git a/pkg/handlers/cni/cluster_test.go b/pkg/handlers/generic/lifecycle/cni/cluster_test.go similarity index 100% rename from pkg/handlers/cni/cluster_test.go rename to pkg/handlers/generic/lifecycle/cni/cluster_test.go diff --git a/pkg/handlers/cni/constants.go b/pkg/handlers/generic/lifecycle/cni/constants.go similarity index 100% rename from pkg/handlers/cni/constants.go rename to pkg/handlers/generic/lifecycle/cni/constants.go diff --git a/pkg/handlers/nfd/handler.go b/pkg/handlers/generic/lifecycle/nfd/handler.go similarity index 100% rename from pkg/handlers/nfd/handler.go rename to pkg/handlers/generic/lifecycle/nfd/handler.go diff --git a/pkg/handlers/servicelbgc/deleter.go b/pkg/handlers/generic/lifecycle/servicelbgc/deleter.go similarity index 100% rename from pkg/handlers/servicelbgc/deleter.go rename to pkg/handlers/generic/lifecycle/servicelbgc/deleter.go diff --git a/pkg/handlers/servicelbgc/deleter_test.go b/pkg/handlers/generic/lifecycle/servicelbgc/deleter_test.go similarity index 100% rename from pkg/handlers/servicelbgc/deleter_test.go rename to pkg/handlers/generic/lifecycle/servicelbgc/deleter_test.go diff --git a/pkg/handlers/servicelbgc/doc.go b/pkg/handlers/generic/lifecycle/servicelbgc/doc.go similarity index 100% rename from pkg/handlers/servicelbgc/doc.go rename to pkg/handlers/generic/lifecycle/servicelbgc/doc.go diff --git a/pkg/handlers/servicelbgc/handler.go b/pkg/handlers/generic/lifecycle/servicelbgc/handler.go similarity index 100% rename from pkg/handlers/servicelbgc/handler.go rename to pkg/handlers/generic/lifecycle/servicelbgc/handler.go diff --git a/pkg/handlers/auditpolicy/embedded/apiserver-audit-policy.yaml b/pkg/handlers/generic/mutation/auditpolicy/embedded/apiserver-audit-policy.yaml similarity index 100% rename from pkg/handlers/auditpolicy/embedded/apiserver-audit-policy.yaml rename to pkg/handlers/generic/mutation/auditpolicy/embedded/apiserver-audit-policy.yaml diff --git a/pkg/handlers/auditpolicy/inject.go b/pkg/handlers/generic/mutation/auditpolicy/inject.go similarity index 100% rename from pkg/handlers/auditpolicy/inject.go rename to pkg/handlers/generic/mutation/auditpolicy/inject.go diff --git a/pkg/handlers/auditpolicy/inject_test.go b/pkg/handlers/generic/mutation/auditpolicy/inject_test.go similarity index 100% rename from pkg/handlers/auditpolicy/inject_test.go rename to pkg/handlers/generic/mutation/auditpolicy/inject_test.go diff --git a/pkg/handlers/etcd/inject.go b/pkg/handlers/generic/mutation/etcd/inject.go similarity index 100% rename from pkg/handlers/etcd/inject.go rename to pkg/handlers/generic/mutation/etcd/inject.go diff --git a/pkg/handlers/etcd/inject_test.go b/pkg/handlers/generic/mutation/etcd/inject_test.go similarity index 100% rename from pkg/handlers/etcd/inject_test.go rename to pkg/handlers/generic/mutation/etcd/inject_test.go diff --git a/pkg/handlers/etcd/variables.go b/pkg/handlers/generic/mutation/etcd/variables.go similarity index 100% rename from pkg/handlers/etcd/variables.go rename to pkg/handlers/generic/mutation/etcd/variables.go diff --git a/pkg/handlers/etcd/variables_test.go b/pkg/handlers/generic/mutation/etcd/variables_test.go similarity index 100% rename from pkg/handlers/etcd/variables_test.go rename to pkg/handlers/generic/mutation/etcd/variables_test.go diff --git a/pkg/handlers/extraapiservercertsans/inject.go b/pkg/handlers/generic/mutation/extraapiservercertsans/inject.go similarity index 100% rename from pkg/handlers/extraapiservercertsans/inject.go rename to pkg/handlers/generic/mutation/extraapiservercertsans/inject.go diff --git a/pkg/handlers/extraapiservercertsans/inject_test.go b/pkg/handlers/generic/mutation/extraapiservercertsans/inject_test.go similarity index 100% rename from pkg/handlers/extraapiservercertsans/inject_test.go rename to pkg/handlers/generic/mutation/extraapiservercertsans/inject_test.go diff --git a/pkg/handlers/extraapiservercertsans/variables.go b/pkg/handlers/generic/mutation/extraapiservercertsans/variables.go similarity index 100% rename from pkg/handlers/extraapiservercertsans/variables.go rename to pkg/handlers/generic/mutation/extraapiservercertsans/variables.go diff --git a/pkg/handlers/extraapiservercertsans/variables_test.go b/pkg/handlers/generic/mutation/extraapiservercertsans/variables_test.go similarity index 100% rename from pkg/handlers/extraapiservercertsans/variables_test.go rename to pkg/handlers/generic/mutation/extraapiservercertsans/variables_test.go diff --git a/pkg/handlers/httpproxy/doc.go b/pkg/handlers/generic/mutation/httpproxy/doc.go similarity index 100% rename from pkg/handlers/httpproxy/doc.go rename to pkg/handlers/generic/mutation/httpproxy/doc.go diff --git a/pkg/handlers/httpproxy/inject.go b/pkg/handlers/generic/mutation/httpproxy/inject.go similarity index 100% rename from pkg/handlers/httpproxy/inject.go rename to pkg/handlers/generic/mutation/httpproxy/inject.go diff --git a/pkg/handlers/httpproxy/inject_test.go b/pkg/handlers/generic/mutation/httpproxy/inject_test.go similarity index 100% rename from pkg/handlers/httpproxy/inject_test.go rename to pkg/handlers/generic/mutation/httpproxy/inject_test.go diff --git a/pkg/handlers/httpproxy/systemd_proxy_config.go b/pkg/handlers/generic/mutation/httpproxy/systemd_proxy_config.go similarity index 100% rename from pkg/handlers/httpproxy/systemd_proxy_config.go rename to pkg/handlers/generic/mutation/httpproxy/systemd_proxy_config.go diff --git a/pkg/handlers/httpproxy/systemd_proxy_config_test.go b/pkg/handlers/generic/mutation/httpproxy/systemd_proxy_config_test.go similarity index 100% rename from pkg/handlers/httpproxy/systemd_proxy_config_test.go rename to pkg/handlers/generic/mutation/httpproxy/systemd_proxy_config_test.go diff --git a/pkg/handlers/httpproxy/templates/systemd.conf.tmpl b/pkg/handlers/generic/mutation/httpproxy/templates/systemd.conf.tmpl similarity index 100% rename from pkg/handlers/httpproxy/templates/systemd.conf.tmpl rename to pkg/handlers/generic/mutation/httpproxy/templates/systemd.conf.tmpl diff --git a/pkg/handlers/httpproxy/variables.go b/pkg/handlers/generic/mutation/httpproxy/variables.go similarity index 100% rename from pkg/handlers/httpproxy/variables.go rename to pkg/handlers/generic/mutation/httpproxy/variables.go diff --git a/pkg/handlers/httpproxy/variables_test.go b/pkg/handlers/generic/mutation/httpproxy/variables_test.go similarity index 100% rename from pkg/handlers/httpproxy/variables_test.go rename to pkg/handlers/generic/mutation/httpproxy/variables_test.go diff --git a/pkg/handlers/kubernetesimagerepository/inject.go b/pkg/handlers/generic/mutation/kubernetesimagerepository/inject.go similarity index 100% rename from pkg/handlers/kubernetesimagerepository/inject.go rename to pkg/handlers/generic/mutation/kubernetesimagerepository/inject.go diff --git a/pkg/handlers/kubernetesimagerepository/inject_test.go b/pkg/handlers/generic/mutation/kubernetesimagerepository/inject_test.go similarity index 100% rename from pkg/handlers/kubernetesimagerepository/inject_test.go rename to pkg/handlers/generic/mutation/kubernetesimagerepository/inject_test.go diff --git a/pkg/handlers/kubernetesimagerepository/variables.go b/pkg/handlers/generic/mutation/kubernetesimagerepository/variables.go similarity index 100% rename from pkg/handlers/kubernetesimagerepository/variables.go rename to pkg/handlers/generic/mutation/kubernetesimagerepository/variables.go diff --git a/pkg/handlers/kubernetesimagerepository/variables_test.go b/pkg/handlers/generic/mutation/kubernetesimagerepository/variables_test.go similarity index 100% rename from pkg/handlers/kubernetesimagerepository/variables_test.go rename to pkg/handlers/generic/mutation/kubernetesimagerepository/variables_test.go