// Copyright 2023 Volvo Car Corporation
// SPDX-License-Identifier: Apache-2.0

package api

import (
	"fmt"

	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

type Resource struct {
	Name       string
	ShortName  string
	APIVersion string
	Namespaced bool
	Kind       string
}
type Resources []Resource

func (k Resources) ByKind(kind string) (Resource, bool) {
	for _, v := range k {
		if v.Kind == kind {
			return v, true
		}
	}
	return Resource{}, false
}

func TypeMeta(kind string) metav1.TypeMeta {
	o, ok := KAPI.ByKind(kind)
	if !ok {
		panic(fmt.Sprintf("kind %q not implemented", kind))
	}
	return metav1.TypeMeta{
		APIVersion: o.APIVersion,
		Kind:       o.Kind,
	}
}

var KAPI = Resources{
	{
		APIVersion: "v1",
		Kind:       "Binding",
		Name:       "bindings",
		Namespaced: true,
		ShortName:  "bindings",
	}, {
		APIVersion: "v1",
		Kind:       "ComponentStatus",
		Name:       "componentstatuses",
		Namespaced: false,
		ShortName:  "cs",
	}, {
		APIVersion: "v1",
		Kind:       "ConfigMap",
		Name:       "configmaps",
		Namespaced: true,
		ShortName:  "cm",
	}, {
		APIVersion: "v1",
		Kind:       "Endpoints",
		Name:       "endpoints",
		Namespaced: true,
		ShortName:  "ep",
	}, {
		APIVersion: "v1",
		Kind:       "LimitRange",
		Name:       "limitranges",
		Namespaced: true,
		ShortName:  "limits",
	}, {
		APIVersion: "v1",
		Kind:       "Namespace",
		Name:       "namespaces",
		Namespaced: false,
		ShortName:  "ns",
	}, {
		APIVersion: "v1",
		Kind:       "Node",
		Name:       "nodes",
		Namespaced: false,
		ShortName:  "no",
	}, {
		APIVersion: "v1",
		Kind:       "PersistentVolumeClaim",
		Name:       "persistentvolumeclaims",
		Namespaced: true,
		ShortName:  "pvc",
	}, {
		APIVersion: "v1",
		Kind:       "PersistentVolume",
		Name:       "persistentvolumes",
		Namespaced: false,
		ShortName:  "pv",
	}, {
		APIVersion: "v1",
		Kind:       "Pod",
		Name:       "pods",
		Namespaced: true,
		ShortName:  "po",
	}, {
		APIVersion: "v1",
		Kind:       "PodTemplate",
		Name:       "podtemplates",
		Namespaced: true,
		ShortName:  "podtemplates",
	}, {
		APIVersion: "v1",
		Kind:       "ReplicationController",
		Name:       "replicationcontrollers",
		Namespaced: true,
		ShortName:  "rc",
	}, {
		APIVersion: "v1",
		Kind:       "ResourceQuota",
		Name:       "resourcequotas",
		Namespaced: true,
		ShortName:  "quota",
	}, {
		APIVersion: "v1",
		Kind:       "Secret",
		Name:       "secrets",
		Namespaced: true,
		ShortName:  "secrets",
	}, {
		APIVersion: "v1",
		Kind:       "ServiceAccount",
		Name:       "serviceaccounts",
		Namespaced: true,
		ShortName:  "sa",
	}, {
		APIVersion: "v1",
		Kind:       "Service",
		Name:       "services",
		Namespaced: true,
		ShortName:  "svc",
	}, {
		APIVersion: "admissionregistration.k8s.io/v1",
		Kind:       "MutatingWebhookConfiguration",
		Name:       "mutatingwebhookconfigurations",
		Namespaced: false,
		ShortName:  "mutatingwebhookconfigurations",
	}, {
		APIVersion: "admissionregistration.k8s.io/v1",
		Kind:       "ValidatingWebhookConfiguration",
		Name:       "validatingwebhookconfigurations",
		Namespaced: false,
		ShortName:  "validatingwebhookconfigurations",
	}, {
		APIVersion: "apiextensions.k8s.io/v1",
		Kind:       "CustomResourceDefinition",
		Name:       "customresourcedefinitions",
		Namespaced: false,
		ShortName:  "crd",
	}, {
		APIVersion: "apiregistration.k8s.io/v1",
		Kind:       "APIService",
		Name:       "apiservices",
		Namespaced: false,
		ShortName:  "apiservices",
	}, {
		APIVersion: "apps/v1",
		Kind:       "ControllerRevision",
		Name:       "controllerrevisions",
		Namespaced: true,
		ShortName:  "controllerrevisions",
	}, {
		APIVersion: "apps/v1",
		Kind:       "DaemonSet",
		Name:       "daemonsets",
		Namespaced: true,
		ShortName:  "ds",
	}, {
		APIVersion: "apps/v1",
		Kind:       "Deployment",
		Name:       "deployments",
		Namespaced: true,
		ShortName:  "deploy",
	}, {
		APIVersion: "apps/v1",
		Kind:       "ReplicaSet",
		Name:       "replicasets",
		Namespaced: true,
		ShortName:  "rs",
	}, {
		APIVersion: "apps/v1",
		Kind:       "StatefulSet",
		Name:       "statefulsets",
		Namespaced: true,
		ShortName:  "sts",
	}, {
		APIVersion: "authentication.k8s.io/v1",
		Kind:       "TokenReview",
		Name:       "tokenreviews",
		Namespaced: false,
		ShortName:  "tokenreviews",
	}, {
		APIVersion: "authorization.k8s.io/v1",
		Kind:       "LocalSubjectAccessReview",
		Name:       "localsubjectaccessreviews",
		Namespaced: true,
		ShortName:  "localsubjectaccessreviews",
	}, {
		APIVersion: "authorization.k8s.io/v1",
		Kind:       "SelfSubjectAccessReview",
		Name:       "selfsubjectaccessreviews",
		Namespaced: false,
		ShortName:  "selfsubjectaccessreviews",
	}, {
		APIVersion: "authorization.k8s.io/v1",
		Kind:       "SelfSubjectRulesReview",
		Name:       "selfsubjectrulesreviews",
		Namespaced: false,
		ShortName:  "selfsubjectrulesreviews",
	}, {
		APIVersion: "authorization.k8s.io/v1",
		Kind:       "SubjectAccessReview",
		Name:       "subjectaccessreviews",
		Namespaced: false,
		ShortName:  "subjectaccessreviews",
	}, {
		APIVersion: "autoscaling/v2",
		Kind:       "HorizontalPodAutoscaler",
		Name:       "horizontalpodautoscalers",
		Namespaced: true,
		ShortName:  "hpa",
	}, {
		APIVersion: "batch/v1",
		Kind:       "CronJob",
		Name:       "cronjobs",
		Namespaced: true,
		ShortName:  "cj",
	}, {
		APIVersion: "batch/v1",
		Kind:       "Job",
		Name:       "jobs",
		Namespaced: true,
		ShortName:  "jobs",
	}, {
		APIVersion: "certificates.k8s.io/v1",
		Kind:       "CertificateSigningRequest",
		Name:       "certificatesigningrequests",
		Namespaced: false,
		ShortName:  "csr",
	}, {
		APIVersion: "cilium.io/v2",
		Kind:       "CiliumClusterwideNetworkPolicy",
		Name:       "ciliumclusterwidenetworkpolicies",
		Namespaced: false,
		ShortName:  "ccnp",
	}, {
		APIVersion: "cilium.io/v2",
		Kind:       "CiliumEndpoint",
		Name:       "ciliumendpoints",
		Namespaced: true,
		ShortName:  "cep",
	}, {
		APIVersion: "cilium.io/v2",
		Kind:       "CiliumExternalWorkload",
		Name:       "ciliumexternalworkloads",
		Namespaced: false,
		ShortName:  "cew",
	}, {
		APIVersion: "cilium.io/v2",
		Kind:       "CiliumIdentity",
		Name:       "ciliumidentities",
		Namespaced: false,
		ShortName:  "ciliumid",
	}, {
		APIVersion: "cilium.io/v2",
		Kind:       "CiliumNetworkPolicy",
		Name:       "ciliumnetworkpolicies",
		Namespaced: true,
		ShortName:  "cnp",
	}, {
		APIVersion: "cilium.io/v2",
		Kind:       "CiliumNode",
		Name:       "ciliumnodes",
		Namespaced: false,
		ShortName:  "cn",
	}, {
		APIVersion: "coordination.k8s.io/v1",
		Kind:       "Lease",
		Name:       "leases",
		Namespaced: true,
		ShortName:  "leases",
	}, {
		APIVersion: "crd.k8s.amazonaws.com/v1alpha1",
		Kind:       "ENIConfig",
		Name:       "eniconfigs",
		Namespaced: false,
		ShortName:  "eniconfigs",
	}, {
		APIVersion: "discovery.k8s.io/v1",
		Kind:       "EndpointSlice",
		Name:       "endpointslices",
		Namespaced: true,
		ShortName:  "endpointslices",
	}, {
		APIVersion: "flowcontrol.apiserver.k8s.io/v1beta2",
		Kind:       "FlowSchema",
		Name:       "flowschemas",
		Namespaced: false,
		ShortName:  "flowschemas",
	}, {
		APIVersion: "flowcontrol.apiserver.k8s.io/v1beta2",
		Kind:       "PriorityLevelConfiguration",
		Name:       "prioritylevelconfigurations",
		Namespaced: false,
		ShortName:  "prioritylevelconfigurations",
	}, {
		APIVersion: "karpenter.k8s.aws/v1alpha1",
		Kind:       "AWSNodeTemplate",
		Name:       "awsnodetemplates",
		Namespaced: false,
		ShortName:  "awsnodetemplates",
	}, {
		APIVersion: "karpenter.sh/v1alpha5",
		Kind:       "Provisioner",
		Name:       "provisioners",
		Namespaced: false,
		ShortName:  "provisioners",
	}, {
		APIVersion: "networking.k8s.io/v1",
		Kind:       "IngressClass",
		Name:       "ingressclasses",
		Namespaced: false,
		ShortName:  "ingressclasses",
	}, {
		APIVersion: "networking.k8s.io/v1",
		Kind:       "Ingress",
		Name:       "ingresses",
		Namespaced: true,
		ShortName:  "ing",
	}, {
		APIVersion: "networking.k8s.io/v1",
		Kind:       "NetworkPolicy",
		Name:       "networkpolicies",
		Namespaced: true,
		ShortName:  "netpol",
	}, {
		APIVersion: "node.k8s.io/v1",
		Kind:       "RuntimeClass",
		Name:       "runtimeclasses",
		Namespaced: false,
		ShortName:  "runtimeclasses",
	}, {
		APIVersion: "policy/v1",
		Kind:       "PodDisruptionBudget",
		Name:       "poddisruptionbudgets",
		Namespaced: true,
		ShortName:  "pdb",
	}, {
		APIVersion: "policy/v1beta1",
		Kind:       "PodSecurityPolicy",
		Name:       "podsecuritypolicies",
		Namespaced: false,
		ShortName:  "psp",
	}, {
		APIVersion: "rbac.authorization.k8s.io/v1",
		Kind:       "ClusterRoleBinding",
		Name:       "clusterrolebindings",
		Namespaced: false,
		ShortName:  "crb",
	}, {
		APIVersion: "rbac.authorization.k8s.io/v1",
		Kind:       "ClusterRole",
		Name:       "clusterroles",
		Namespaced: false,
		ShortName:  "cr",
	}, {
		APIVersion: "rbac.authorization.k8s.io/v1",
		Kind:       "RoleBinding",
		Name:       "rolebindings",
		Namespaced: true,
		ShortName:  "rb",
	}, {
		APIVersion: "rbac.authorization.k8s.io/v1",
		Kind:       "Role",
		Name:       "roles",
		Namespaced: true,
		ShortName:  "role",
	}, {
		APIVersion: "scheduling.k8s.io/v1",
		Kind:       "PriorityClass",
		Name:       "priorityclasses",
		Namespaced: false,
		ShortName:  "pc",
	}, {
		APIVersion: "storage.k8s.io/v1",
		Kind:       "CSIDriver",
		Name:       "csidrivers",
		Namespaced: false,
		ShortName:  "csidrivers",
	}, {
		APIVersion: "storage.k8s.io/v1",
		Kind:       "CSINode",
		Name:       "csinodes",
		Namespaced: false,
		ShortName:  "csinodes",
	}, {
		APIVersion: "storage.k8s.io/v1beta1",
		Kind:       "CSIStorageCapacity",
		Name:       "csistoragecapacities",
		Namespaced: true,
		ShortName:  "csistoragecapacities",
	}, {
		APIVersion: "storage.k8s.io/v1",
		Kind:       "StorageClass",
		Name:       "storageclasses",
		Namespaced: false,
		ShortName:  "sc",
	}, {
		APIVersion: "storage.k8s.io/v1",
		Kind:       "VolumeAttachment",
		Name:       "volumeattachments",
		Namespaced: false,
		ShortName:  "volumeattachments",
	}, {
		APIVersion: "vpcresources.k8s.aws/v1beta1",
		Kind:       "SecurityGroupPolicy",
		Name:       "securitygrouppolicies",
		Namespaced: true,
		ShortName:  "sgp",
	},
}
