From b1497713dc251e3e60391aa5031abb9123cacd8a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A5vard=20Elnan?= Date: Tue, 14 Jan 2025 14:40:33 +0100 Subject: [PATCH] implement v2rortypes and datacenter --- cmd/generator/main.go | 25 ++-- pkg/rorresources/fromstruct.go | 4 + pkg/rorresources/resource.go | 10 ++ pkg/rorresources/rordefs/defs.go | 138 ++++++------------ pkg/rorresources/rordefs/rordefs.go | 135 +++++++++++++++++ pkg/rorresources/rorkubernetes/k8s.go | 21 +++ pkg/rorresources/rorkubernetes/k8s_test.go | 19 +++ .../resource_input_filter_datacenter.go | 8 + .../rortypes/resource_interfaces.go | 6 + .../rortypes/resource_models_methods.go | 18 +++ .../resourcedef_internaldatacenter.go | 33 +++++ 11 files changed, 314 insertions(+), 103 deletions(-) create mode 100644 pkg/rorresources/rordefs/rordefs.go create mode 100644 pkg/rorresources/rortypes/resource_input_filter_datacenter.go create mode 100644 pkg/rorresources/rortypes/resourcedef_internaldatacenter.go diff --git a/cmd/generator/main.go b/cmd/generator/main.go index b7dc14b5..f8fba4c4 100644 --- a/cmd/generator/main.go +++ b/cmd/generator/main.go @@ -41,22 +41,23 @@ func main() { // - cmd/tanzu/agent/tanzuservice/schemas/schemas_generated.go //templateFile("cmd/tanzu/agent/tanzuservice/schemas/schemas_generated.go.tmpl", rordefs.GetResourcesByType(rordefs.ApiResourceTypeTanzuAgent)) - templateFile("pkg/apicontracts/apiresourcecontracts/resource_models_generated.go.tmpl", rordefs.Resourcedefs) - templateFile("pkg/apicontracts/apiresourcecontracts/resource_models_methods_generated.go.tmpl", rordefs.Resourcedefs) - - // Resource models - templateFile("pkg/rorresources/fromstruct.go.tmpl", rordefs.Resourcedefs) - templateFile("pkg/rorresources/resource.go.tmpl", rordefs.Resourcedefs) - templateFile("pkg/rorresources/rorkubernetes/k8s_test.go.tmpl", rordefs.Resourcedefs) - templateFile("pkg/rorresources/rorkubernetes/k8s.go.tmpl", rordefs.Resourcedefs) - templateFile("pkg/rorresources/rortypes/resource_interfaces.go.tmpl", rordefs.Resourcedefs) - templateFile("pkg/rorresources/rortypes/resource_models_methods.go.tmpl", rordefs.Resourcedefs) + // Resource models v1 + templateFile("pkg/apicontracts/apiresourcecontracts/resource_models_generated.go.tmpl", rordefs.Resourcedefs.GetResourcesByVersion(rordefs.ApiVersionV1)) + templateFile("pkg/apicontracts/apiresourcecontracts/resource_models_methods_generated.go.tmpl", rordefs.Resourcedefs.GetResourcesByVersion(rordefs.ApiVersionV1)) + + // Resource models v2 + templateFile("pkg/rorresources/fromstruct.go.tmpl", rordefs.Resourcedefs.GetResourcesByVersion(rordefs.ApiVersionV2)) + templateFile("pkg/rorresources/resource.go.tmpl", rordefs.Resourcedefs.GetResourcesByVersion(rordefs.ApiVersionV2)) + templateFile("pkg/rorresources/rorkubernetes/k8s_test.go.tmpl", rordefs.Resourcedefs.GetResourcesByVersion(rordefs.ApiVersionV2)) + templateFile("pkg/rorresources/rorkubernetes/k8s.go.tmpl", rordefs.Resourcedefs.GetResourcesByVersion(rordefs.ApiVersionV2)) + templateFile("pkg/rorresources/rortypes/resource_interfaces.go.tmpl", rordefs.Resourcedefs.GetResourcesByVersion(rordefs.ApiVersionV2)) + templateFile("pkg/rorresources/rortypes/resource_models_methods.go.tmpl", rordefs.Resourcedefs.GetResourcesByVersion(rordefs.ApiVersionV2)) // Resource models - input filters - for _, res := range rordefs.Resourcedefs { + for _, res := range rordefs.Resourcedefs.GetResourcesByVersion(rordefs.ApiVersionV2) { filepath := fmt.Sprintf("pkg/rorresources/rortypes/resource_input_filter_%s.go", res.GetKind()) filepath = strings.ToLower(filepath) - templateFileOnce(filepath, "/pkg/rorresources/rortypes/resource_models_input_filter.go.tmpl", res) + templateFileOnce(filepath, "pkg/rorresources/rortypes/resource_models_input_filter.go.tmpl", res) } } diff --git a/pkg/rorresources/fromstruct.go b/pkg/rorresources/fromstruct.go index fd90ca9f..35cc1dbc 100644 --- a/pkg/rorresources/fromstruct.go +++ b/pkg/rorresources/fromstruct.go @@ -182,6 +182,10 @@ func NewResourceFromStruct(res Resource) *Resource { r.SetNetworkPolicy(res.NetworkPolicyResource) r.SetCommonInterface(res.NetworkPolicyResource) + case "infrastructure.ror.internal/v1alpha1, Kind=Datacenter": + r.SetDatacenter(res.DatacenterResource) + r.SetCommonInterface(res.DatacenterResource) + default: rlog.Info("Unknown resource kind", rlog.String("gvk", gvk.String()), rlog.String("kind", res.Kind), rlog.String("apiVersion", res.APIVersion)) return nil diff --git a/pkg/rorresources/resource.go b/pkg/rorresources/resource.go index 1e32f7a4..d38c122b 100644 --- a/pkg/rorresources/resource.go +++ b/pkg/rorresources/resource.go @@ -49,6 +49,7 @@ type Resource struct { VirtualMachineResource *rortypes.ResourceVirtualMachine `json:"virtualmachine,omitempty" bson:"virtualmachine,omitempty"` EndpointsResource *rortypes.ResourceEndpoints `json:"endpoints,omitempty" bson:"endpoints,omitempty"` NetworkPolicyResource *rortypes.ResourceNetworkPolicy `json:"networkpolicy,omitempty" bson:"networkpolicy,omitempty"` + DatacenterResource *rortypes.ResourceDatacenter `json:"datacenter,omitempty" bson:"datacenter,omitempty"` common rortypes.CommonResourceInterface } @@ -215,6 +216,10 @@ func (r *Resource) SetNetworkPolicy(res *rortypes.ResourceNetworkPolicy) { r.NetworkPolicyResource = res } +func (r *Resource) SetDatacenter(res *rortypes.ResourceDatacenter) { + r.DatacenterResource = res +} + // Namespace is a wrapper for the underlying resource, it provides a Namespaceinterface to work with namespaces func (r *Resource) Namespace() rortypes.Namespaceinterface { return r.NamespaceResource @@ -395,6 +400,11 @@ func (r *Resource) NetworkPolicy() rortypes.NetworkPolicyinterface { return r.NetworkPolicyResource } +// Datacenter is a wrapper for the underlying resource, it provides a Datacenterinterface to work with datacenters +func (r *Resource) Datacenter() rortypes.Datacenterinterface { + return r.DatacenterResource +} + // (r *Resource) GetRorHash() returns the hash from the common interface func (r *Resource) GetRorHash() string { return r.common.GetRorHash() diff --git a/pkg/rorresources/rordefs/defs.go b/pkg/rorresources/rordefs/defs.go index bf4683bf..fa26268b 100644 --- a/pkg/rorresources/rordefs/defs.go +++ b/pkg/rorresources/rordefs/defs.go @@ -1,103 +1,14 @@ // The package provides the models and variables needed to generate code and endpoints for the implemented rorresources package rordefs // Package resourcegeneratormodels -import ( - "golang.org/x/exp/slices" - "golang.org/x/text/cases" - "golang.org/x/text/language" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - "k8s.io/apimachinery/pkg/runtime/schema" -) - -type ApiResourceType string - -const ( - ApiResourceTypeUnknown ApiResourceType = "" - ApiResourceTypeAgent ApiResourceType = "Agent" - ApiResourceTypeVmAgent ApiResourceType = "VmAgent" - ApiResourceTypeTanzuAgent ApiResourceType = "TanzuAgent" - ApiResourceTypeInternal ApiResourceType = "Internal" -) - -// ApiResource -// The type describing a resource implemented in ror -type ApiResource struct { - metav1.TypeMeta `json:",inline"` - Plural string - Namespaced bool - Types []ApiResourceType -} - -// GetApiVersion -// Generates the apiVersion from the resource object to match with kubernetes api resources -func (m ApiResource) GetApiVersion() string { - return m.APIVersion -} - -func (m ApiResource) GetVersion() string { - return m.GroupVersionKind().Version -} - -func (m ApiResource) GetGroup() string { - return m.GroupVersionKind().Group -} - -func (m ApiResource) GetKind() string { - return m.Kind -} - -func (m ApiResource) GetResource() string { - return m.Plural -} - -func (m ApiResource) GetGroupVersionKind() schema.GroupVersionKind { - return schema.GroupVersionKind{ - Group: m.GroupVersionKind().Group, - Version: m.GroupVersionKind().Version, - Kind: m.GetKind(), - } -} - -func (m ApiResource) GetGroupVersionResource() schema.GroupVersionResource { - return schema.GroupVersionResource{ - Group: m.GroupVersionKind().Group, - Version: m.GroupVersionKind().Version, - Resource: m.GetResource(), - } -} - -func (m ApiResource) PluralCapitalized() string { - caser := cases.Title(language.Und) - - return caser.String(m.Plural) -} - -func GetSchemasByType(resourceType ApiResourceType) []schema.GroupVersionResource { - var resources []schema.GroupVersionResource - for _, resource := range GetResourcesByType(resourceType) { - resources = append(resources, resource.GetGroupVersionResource()) - } - - return resources -} - -func GetResourcesByType(resourceType ApiResourceType) []ApiResource { - var resources []ApiResource - for _, resource := range Resourcedefs { - if slices.Contains(resource.Types, resourceType) { - resources = append(resources, resource) - } - } - - return resources -} +import metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" // Resources implemented in ror // // When changed the generator must be run, and the files generated checked in with the code. // // $ go run build/generator/main.go -var Resourcedefs = []ApiResource{ +var Resourcedefs = ApiResources{ { TypeMeta: metav1.TypeMeta{ Kind: "Namespace", @@ -106,6 +17,7 @@ var Resourcedefs = []ApiResource{ Plural: "namespaces", Namespaced: false, Types: []ApiResourceType{ApiResourceTypeAgent}, + Versions: []ApiVersions{ApiVersionV1, ApiVersionV2}, }, { TypeMeta: metav1.TypeMeta{ Kind: "Node", @@ -114,6 +26,7 @@ var Resourcedefs = []ApiResource{ Plural: "nodes", Namespaced: false, Types: []ApiResourceType{ApiResourceTypeAgent}, + Versions: []ApiVersions{ApiVersionV1, ApiVersionV2}, }, { TypeMeta: metav1.TypeMeta{ Kind: "PersistentVolumeClaim", @@ -122,6 +35,7 @@ var Resourcedefs = []ApiResource{ Plural: "persistentvolumeclaims", Namespaced: false, Types: []ApiResourceType{ApiResourceTypeAgent}, + Versions: []ApiVersions{ApiVersionV1, ApiVersionV2}, }, { TypeMeta: metav1.TypeMeta{ Kind: "Deployment", @@ -130,6 +44,7 @@ var Resourcedefs = []ApiResource{ Plural: "deployments", Namespaced: true, Types: []ApiResourceType{ApiResourceTypeAgent}, + Versions: []ApiVersions{ApiVersionV1, ApiVersionV2}, }, { TypeMeta: metav1.TypeMeta{ Kind: "StorageClass", @@ -138,6 +53,7 @@ var Resourcedefs = []ApiResource{ Plural: "storageclasses", Namespaced: false, Types: []ApiResourceType{ApiResourceTypeAgent}, + Versions: []ApiVersions{ApiVersionV1, ApiVersionV2}, }, { TypeMeta: metav1.TypeMeta{ Kind: "PolicyReport", @@ -146,6 +62,7 @@ var Resourcedefs = []ApiResource{ Plural: "policyreports", Namespaced: true, Types: []ApiResourceType{ApiResourceTypeAgent}, + Versions: []ApiVersions{ApiVersionV1, ApiVersionV2}, }, { TypeMeta: metav1.TypeMeta{ Kind: "Application", @@ -154,6 +71,7 @@ var Resourcedefs = []ApiResource{ Plural: "applications", Namespaced: true, Types: []ApiResourceType{ApiResourceTypeAgent}, + Versions: []ApiVersions{ApiVersionV1, ApiVersionV2}, }, { TypeMeta: metav1.TypeMeta{ Kind: "AppProject", @@ -162,6 +80,7 @@ var Resourcedefs = []ApiResource{ Plural: "appprojects", Namespaced: true, Types: []ApiResourceType{ApiResourceTypeAgent}, + Versions: []ApiVersions{ApiVersionV1, ApiVersionV2}, }, { TypeMeta: metav1.TypeMeta{ Kind: "Certificate", @@ -170,6 +89,7 @@ var Resourcedefs = []ApiResource{ Plural: "certificates", Namespaced: true, Types: []ApiResourceType{ApiResourceTypeAgent}, + Versions: []ApiVersions{ApiVersionV1, ApiVersionV2}, }, { TypeMeta: metav1.TypeMeta{ Kind: "Service", @@ -178,6 +98,7 @@ var Resourcedefs = []ApiResource{ Plural: "services", Namespaced: true, Types: []ApiResourceType{ApiResourceTypeAgent}, + Versions: []ApiVersions{ApiVersionV1, ApiVersionV2}, }, { TypeMeta: metav1.TypeMeta{ Kind: "Pod", @@ -186,6 +107,7 @@ var Resourcedefs = []ApiResource{ Plural: "pods", Namespaced: true, Types: []ApiResourceType{ApiResourceTypeAgent}, + Versions: []ApiVersions{ApiVersionV1, ApiVersionV2}, }, { TypeMeta: metav1.TypeMeta{ Kind: "ReplicaSet", @@ -194,6 +116,7 @@ var Resourcedefs = []ApiResource{ Plural: "replicasets", Namespaced: true, Types: []ApiResourceType{ApiResourceTypeAgent}, + Versions: []ApiVersions{ApiVersionV1, ApiVersionV2}, }, { TypeMeta: metav1.TypeMeta{ Kind: "StatefulSet", @@ -202,6 +125,7 @@ var Resourcedefs = []ApiResource{ Plural: "statefulsets", Namespaced: true, Types: []ApiResourceType{ApiResourceTypeAgent}, + Versions: []ApiVersions{ApiVersionV1, ApiVersionV2}, }, { TypeMeta: metav1.TypeMeta{ Kind: "DaemonSet", @@ -210,6 +134,7 @@ var Resourcedefs = []ApiResource{ Plural: "daemonsets", Namespaced: true, Types: []ApiResourceType{ApiResourceTypeAgent}, + Versions: []ApiVersions{ApiVersionV1, ApiVersionV2}, }, { TypeMeta: metav1.TypeMeta{ Kind: "Ingress", @@ -218,6 +143,7 @@ var Resourcedefs = []ApiResource{ Plural: "ingresses", Namespaced: true, Types: []ApiResourceType{ApiResourceTypeAgent}, + Versions: []ApiVersions{ApiVersionV1, ApiVersionV2}, }, { TypeMeta: metav1.TypeMeta{ Kind: "IngressClass", @@ -226,6 +152,7 @@ var Resourcedefs = []ApiResource{ Plural: "ingressclasses", Namespaced: true, Types: []ApiResourceType{ApiResourceTypeAgent}, + Versions: []ApiVersions{ApiVersionV1, ApiVersionV2}, }, { TypeMeta: metav1.TypeMeta{ Kind: "VulnerabilityReport", @@ -234,6 +161,7 @@ var Resourcedefs = []ApiResource{ Plural: "vulnerabilityreports", Namespaced: true, Types: []ApiResourceType{ApiResourceTypeAgent}, + Versions: []ApiVersions{ApiVersionV1, ApiVersionV2}, }, { TypeMeta: metav1.TypeMeta{ Kind: "ExposedSecretReport", @@ -242,6 +170,7 @@ var Resourcedefs = []ApiResource{ Plural: "exposedsecretreports", Namespaced: true, Types: []ApiResourceType{ApiResourceTypeAgent}, + Versions: []ApiVersions{ApiVersionV1, ApiVersionV2}, }, { TypeMeta: metav1.TypeMeta{ Kind: "ConfigAuditReport", @@ -250,6 +179,7 @@ var Resourcedefs = []ApiResource{ Plural: "configauditreports", Namespaced: true, Types: []ApiResourceType{ApiResourceTypeAgent}, + Versions: []ApiVersions{ApiVersionV1, ApiVersionV2}, }, { TypeMeta: metav1.TypeMeta{ Kind: "RbacAssessmentReport", @@ -258,6 +188,7 @@ var Resourcedefs = []ApiResource{ Plural: "rbacassessmentreports", Namespaced: true, Types: []ApiResourceType{ApiResourceTypeAgent}, + Versions: []ApiVersions{ApiVersionV1, ApiVersionV2}, }, { TypeMeta: metav1.TypeMeta{ Kind: "TanzuKubernetesCluster", @@ -266,6 +197,7 @@ var Resourcedefs = []ApiResource{ Plural: "tanzukubernetesclusters", Namespaced: true, Types: []ApiResourceType{ApiResourceTypeTanzuAgent}, + Versions: []ApiVersions{ApiVersionV1, ApiVersionV2}, }, { TypeMeta: metav1.TypeMeta{ Kind: "TanzuKubernetesRelease", @@ -274,6 +206,7 @@ var Resourcedefs = []ApiResource{ Plural: "tanzukubernetesreleases", Namespaced: false, Types: []ApiResourceType{ApiResourceTypeTanzuAgent}, + Versions: []ApiVersions{ApiVersionV1, ApiVersionV2}, }, { TypeMeta: metav1.TypeMeta{ Kind: "VirtualMachineClass", @@ -282,6 +215,7 @@ var Resourcedefs = []ApiResource{ Plural: "virtualmachineclasses", Namespaced: false, Types: []ApiResourceType{ApiResourceTypeTanzuAgent}, + Versions: []ApiVersions{ApiVersionV1, ApiVersionV2}, }, { TypeMeta: metav1.TypeMeta{ Kind: "VirtualMachineClassBinding", @@ -290,6 +224,7 @@ var Resourcedefs = []ApiResource{ Plural: "virtualmachineclassbindings", Namespaced: true, Types: []ApiResourceType{ApiResourceTypeTanzuAgent}, + Versions: []ApiVersions{ApiVersionV1, ApiVersionV2}, }, { TypeMeta: metav1.TypeMeta{ Kind: "KubernetesCluster", @@ -298,6 +233,7 @@ var Resourcedefs = []ApiResource{ Plural: "kubernetesclusters", Namespaced: true, Types: []ApiResourceType{ApiResourceTypeInternal}, + Versions: []ApiVersions{ApiVersionV1, ApiVersionV2}, }, { TypeMeta: metav1.TypeMeta{ Kind: "ClusterOrder", @@ -306,6 +242,7 @@ var Resourcedefs = []ApiResource{ Plural: "clusterorders", Namespaced: true, Types: []ApiResourceType{ApiResourceTypeInternal}, + Versions: []ApiVersions{ApiVersionV1, ApiVersionV2}, }, { TypeMeta: metav1.TypeMeta{ Kind: "Project", @@ -314,6 +251,7 @@ var Resourcedefs = []ApiResource{ Plural: "projects", Namespaced: true, Types: []ApiResourceType{ApiResourceTypeInternal}, + Versions: []ApiVersions{ApiVersionV1, ApiVersionV2}, }, { TypeMeta: metav1.TypeMeta{ Kind: "Configuration", @@ -322,6 +260,7 @@ var Resourcedefs = []ApiResource{ Plural: "configurations", Namespaced: true, Types: []ApiResourceType{ApiResourceTypeInternal}, + Versions: []ApiVersions{ApiVersionV1, ApiVersionV2}, }, { TypeMeta: metav1.TypeMeta{ Kind: "ClusterComplianceReport", @@ -330,6 +269,7 @@ var Resourcedefs = []ApiResource{ Plural: "clustercompliancereports", Namespaced: false, Types: []ApiResourceType{ApiResourceTypeAgent}, + Versions: []ApiVersions{ApiVersionV1, ApiVersionV2}, }, { TypeMeta: metav1.TypeMeta{ Kind: "ClusterVulnerabilityReport", @@ -338,6 +278,7 @@ var Resourcedefs = []ApiResource{ Plural: "clustervulnerabilityreports", Namespaced: false, Types: []ApiResourceType{ApiResourceTypeInternal}, + Versions: []ApiVersions{ApiVersionV1, ApiVersionV2}, }, { TypeMeta: metav1.TypeMeta{ Kind: "Route", @@ -346,6 +287,7 @@ var Resourcedefs = []ApiResource{ Plural: "routes", Namespaced: false, Types: []ApiResourceType{ApiResourceTypeInternal}, + Versions: []ApiVersions{ApiVersionV1, ApiVersionV2}, }, { TypeMeta: metav1.TypeMeta{ Kind: "SlackMessage", @@ -354,6 +296,7 @@ var Resourcedefs = []ApiResource{ Plural: "slackmessages", Namespaced: false, Types: []ApiResourceType{ApiResourceTypeInternal}, + Versions: []ApiVersions{ApiVersionV1, ApiVersionV2}, }, { TypeMeta: metav1.TypeMeta{ Kind: "VulnerabilityEvent", @@ -362,6 +305,7 @@ var Resourcedefs = []ApiResource{ Plural: "vulnerabilityevents", Namespaced: false, Types: []ApiResourceType{ApiResourceTypeInternal}, + Versions: []ApiVersions{ApiVersionV1, ApiVersionV2}, }, { TypeMeta: metav1.TypeMeta{ Kind: "VirtualMachine", @@ -370,6 +314,7 @@ var Resourcedefs = []ApiResource{ Plural: "VirtualMachines", Namespaced: false, Types: []ApiResourceType{ApiResourceTypeVmAgent}, + Versions: []ApiVersions{ApiVersionV1, ApiVersionV2}, }, { TypeMeta: metav1.TypeMeta{ @@ -379,6 +324,7 @@ var Resourcedefs = []ApiResource{ Plural: "endpoints", Namespaced: true, Types: []ApiResourceType{ApiResourceTypeAgent}, + Versions: []ApiVersions{ApiVersionV1, ApiVersionV2}, }, { TypeMeta: metav1.TypeMeta{ Kind: "NetworkPolicy", @@ -387,5 +333,15 @@ var Resourcedefs = []ApiResource{ Plural: "networkpolicies", Namespaced: true, Types: []ApiResourceType{ApiResourceTypeAgent}, + Versions: []ApiVersions{ApiVersionV1, ApiVersionV2}, + }, { + TypeMeta: metav1.TypeMeta{ + Kind: "Datacenter", + APIVersion: "infrastructure.ror.internal/v1alpha1", + }, + Plural: "datacenters", + Namespaced: true, + Types: []ApiResourceType{ApiResourceTypeInternal}, + Versions: []ApiVersions{ApiVersionV2}, }, } diff --git a/pkg/rorresources/rordefs/rordefs.go b/pkg/rorresources/rordefs/rordefs.go new file mode 100644 index 00000000..9a744600 --- /dev/null +++ b/pkg/rorresources/rordefs/rordefs.go @@ -0,0 +1,135 @@ +package rordefs + +import ( + "golang.org/x/exp/slices" + "golang.org/x/text/cases" + "golang.org/x/text/language" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/apimachinery/pkg/runtime/schema" +) + +type ApiVersions string + +const ( + ApiVersionV1 = "v1" + ApiVersionV2 = "v2" +) + +type ApiResourceType string + +const ( + ApiResourceTypeUnknown ApiResourceType = "" + ApiResourceTypeAgent ApiResourceType = "Agent" + ApiResourceTypeVmAgent ApiResourceType = "VmAgent" + ApiResourceTypeTanzuAgent ApiResourceType = "TanzuAgent" + ApiResourceTypeInternal ApiResourceType = "Internal" +) + +// ApiResources +// The type describing a list of resources implemented in ror +type ApiResources []ApiResource + +// ApiResource +// The type describing a resource implemented in ror +type ApiResource struct { + metav1.TypeMeta `json:",inline"` + Plural string + Namespaced bool + Types []ApiResourceType + Versions []ApiVersions +} + +// GetApiVersion +// Generates the apiVersion from the resource object to match with kubernetes api resources +func (m ApiResource) GetApiVersion() string { + return m.APIVersion +} + +func (m ApiResource) GetVersion() string { + return m.GroupVersionKind().Version +} + +func (m ApiResource) GetGroup() string { + return m.GroupVersionKind().Group +} + +func (m ApiResource) GetKind() string { + return m.Kind +} + +func (m ApiResource) GetResource() string { + return m.Plural +} + +func (m ApiResource) GetGroupVersionKind() schema.GroupVersionKind { + return schema.GroupVersionKind{ + Group: m.GroupVersionKind().Group, + Version: m.GroupVersionKind().Version, + Kind: m.GetKind(), + } +} + +func (m ApiResource) GetGroupVersionResource() schema.GroupVersionResource { + return schema.GroupVersionResource{ + Group: m.GroupVersionKind().Group, + Version: m.GroupVersionKind().Version, + Resource: m.GetResource(), + } +} + +func (m ApiResource) PluralCapitalized() string { + caser := cases.Title(language.Und) + + return caser.String(m.Plural) +} + +func (m ApiResources) GetSchemasByType(resourceType ApiResourceType) []schema.GroupVersionResource { + var resources []schema.GroupVersionResource + for _, resource := range m.GetResourcesByType(resourceType) { + resources = append(resources, resource.GetGroupVersionResource()) + } + + return resources +} + +func (m ApiResources) GetResourcesByType(resourceType ApiResourceType) ApiResources { + var resources ApiResources + for _, resource := range m { + if slices.Contains(resource.Types, resourceType) { + resources = append(resources, resource) + } + } + return resources +} + +func (m ApiResources) GetResourcesByVersion(version ApiVersions) ApiResources { + var resources ApiResources + for _, resource := range m { + if slices.Contains(resource.Versions, version) { + resources = append(resources, resource) + } + } + return resources +} + +// Deprecated: migrate to the ApiResources.GetSchemasByType +func GetSchemasByType(resourceType ApiResourceType) []schema.GroupVersionResource { + var resources []schema.GroupVersionResource + for _, resource := range GetResourcesByType(resourceType) { + resources = append(resources, resource.GetGroupVersionResource()) + } + + return resources +} + +// Deprecated: migrate to the ApiResources.GetResourcesByType +func GetResourcesByType(resourceType ApiResourceType) []ApiResource { + var resources []ApiResource + for _, resource := range Resourcedefs { + if slices.Contains(resource.Types, resourceType) { + resources = append(resources, resource) + } + } + + return resources +} diff --git a/pkg/rorresources/rorkubernetes/k8s.go b/pkg/rorresources/rorkubernetes/k8s.go index d5a967c5..d4e973c2 100644 --- a/pkg/rorresources/rorkubernetes/k8s.go +++ b/pkg/rorresources/rorkubernetes/k8s.go @@ -223,6 +223,11 @@ func NewResourceFromDynamicClient(input *unstructured.Unstructured) *rorresource r.SetNetworkPolicy(res) r.SetCommonInterface(res) + case "infrastructure.ror.internal/v1alpha1, Kind=Datacenter": + res := newDatacenterFromDynamicClient(input) + r.SetDatacenter(res) + r.SetCommonInterface(res) + default: rlog.Warn("could not create ResourceSet") return nil @@ -805,3 +810,19 @@ func newNetworkPolicyFromDynamicClient(obj *unstructured.Unstructured) *rortypes } return &nr } + +// newDatacenterFromDynamicClient creates the underlying resource from a unstructured.Unstructured type provided +// by the kubernetes universal client. +func newDatacenterFromDynamicClient(obj *unstructured.Unstructured) *rortypes.ResourceDatacenter { + nr := rortypes.ResourceDatacenter{} + nrjson, err := obj.MarshalJSON() + if err != nil { + rlog.Error("Could not mashal unstructired to json", err) + } + + err = json.Unmarshal(nrjson, &nr) + if err != nil { + rlog.Error("Could not unmarshal json to Datacenter", err) + } + return &nr +} diff --git a/pkg/rorresources/rorkubernetes/k8s_test.go b/pkg/rorresources/rorkubernetes/k8s_test.go index d9ec1795..6ba70821 100644 --- a/pkg/rorresources/rorkubernetes/k8s_test.go +++ b/pkg/rorresources/rorkubernetes/k8s_test.go @@ -697,6 +697,25 @@ func TestNewResourceSetFromDynamicClientNetworkPolicy(t *testing.T) { } } +func TestNewResourceSetFromDynamicClientDatacenter(t *testing.T) { + input := &unstructured.Unstructured{ + Object: map[string]interface{}{ + "kind": "Datacenter", + "apiVersion": "infrastructure.ror.internal/v1alpha1", + "metadata": map[string]interface{}{ + "name": "test-datacenter", + }, + }, + } + + expected := NewResourceFromDynamicClient(input) + result := NewResourceSetFromDynamicClient(input) + + if !reflect.DeepEqual(result.Get(), expected) { + t.Errorf("Expected %v, but got %v", expected, result) + } +} + func TestNewResourceSetFromDynamicClientWrong(t *testing.T) { input := &unstructured.Unstructured{ Object: map[string]interface{}{ diff --git a/pkg/rorresources/rortypes/resource_input_filter_datacenter.go b/pkg/rorresources/rortypes/resource_input_filter_datacenter.go new file mode 100644 index 00000000..7477bd16 --- /dev/null +++ b/pkg/rorresources/rortypes/resource_input_filter_datacenter.go @@ -0,0 +1,8 @@ +package rortypes + +import () + +// (r *ResourceDatacenter) ApplyInputFilter Applies the input filter to the resource +func (r *ResourceDatacenter) ApplyInputFilter(cr *CommonResource) error { + return nil +} diff --git a/pkg/rorresources/rortypes/resource_interfaces.go b/pkg/rorresources/rortypes/resource_interfaces.go index 1f02542a..2d6d6ec5 100644 --- a/pkg/rorresources/rortypes/resource_interfaces.go +++ b/pkg/rorresources/rortypes/resource_interfaces.go @@ -224,3 +224,9 @@ type NetworkPolicyinterface interface { CommonResourceInterface Get() *ResourceNetworkPolicy } + +// Datacenterinterface represents the interface for resources of the type datacenter +type Datacenterinterface interface { + CommonResourceInterface + Get() *ResourceDatacenter +} diff --git a/pkg/rorresources/rortypes/resource_models_methods.go b/pkg/rorresources/rortypes/resource_models_methods.go index 9d97e37b..866e4f56 100644 --- a/pkg/rorresources/rortypes/resource_models_methods.go +++ b/pkg/rorresources/rortypes/resource_models_methods.go @@ -656,3 +656,21 @@ func (r *ResourceNetworkPolicy) GetRorHash() string { func (r *ResourceNetworkPolicy) Get() *ResourceNetworkPolicy { return r } + +// (r *ResourceDatacenter) GetRorHash calculates the hash of the resource +// +// it uses the hashstructure library to calculate the hash of the resource +// fields can be ignored by adding the tag `hash:"ignore"` to the field +func (r *ResourceDatacenter) GetRorHash() string { + hash, err := hashstructure.Hash(r, hashstructure.FormatV2, nil) + if err != nil { + return "" + } + + return fmt.Sprintf("%d", hash) +} + +// (r ResourceDatacenter) Get returns a pointer to the resource of type ResourceDatacenter +func (r *ResourceDatacenter) Get() *ResourceDatacenter { + return r +} diff --git a/pkg/rorresources/rortypes/resourcedef_internaldatacenter.go b/pkg/rorresources/rortypes/resourcedef_internaldatacenter.go new file mode 100644 index 00000000..5d4bdda6 --- /dev/null +++ b/pkg/rorresources/rortypes/resourcedef_internaldatacenter.go @@ -0,0 +1,33 @@ +package rortypes + +import "github.com/NorskHelsenett/ror/pkg/models/providers" + +// K8s deployment struct +type ResourceDatacenter struct { + Spec ResourceDaemonSetStatus `json:"spec"` + Legacy Datacenter `json:"legacy"` +} + +type ResourceDatacenterSpec struct { +} + +type Datacenter struct { + ID string `json:"id" bson:"_id,omitempty"` + Name string `json:"name"` + Provider providers.ProviderType `json:"provider"` + Location DatacenterLocation `json:"location"` + APIEndpoint string `json:"apiEndpoint"` +} + +type DatacenterLocation struct { + ID string `json:"id" bson:"id,omitempty"` + Region string `json:"region"` + Country string `json:"country"` +} + +type Workspace struct { + ID string `json:"id" bson:"_id,omitempty"` + Name string `json:"name"` + DatacenterID string `json:"datacenterId" bson:"datacenterid,omitempty"` + Datacenter Datacenter `json:"datacenter"` +}