diff --git a/app/kumactl/cmd/install/testdata/install-control-plane.dump-values.yaml b/app/kumactl/cmd/install/testdata/install-control-plane.dump-values.yaml index 613065e337dd..790420d55392 100644 --- a/app/kumactl/cmd/install/testdata/install-control-plane.dump-values.yaml +++ b/app/kumactl/cmd/install/testdata/install-control-plane.dump-values.yaml @@ -721,6 +721,7 @@ postgres: # @ignored for helm-docs plugins: + resources: {} policies: meshaccesslogs: true meshcircuitbreakers: true diff --git a/app/kumactl/cmd/root.go b/app/kumactl/cmd/root.go index 94979fcb83d1..18647ee65fe1 100644 --- a/app/kumactl/cmd/root.go +++ b/app/kumactl/cmd/root.go @@ -23,6 +23,8 @@ import ( kumactl_errors "github.com/kumahq/kuma/app/kumactl/pkg/errors" kuma_cmd "github.com/kumahq/kuma/pkg/cmd" "github.com/kumahq/kuma/pkg/core" + "github.com/kumahq/kuma/pkg/core/plugins" + core_apis "github.com/kumahq/kuma/pkg/core/resources/apis" kuma_log "github.com/kumahq/kuma/pkg/log" "github.com/kumahq/kuma/pkg/plugins/policies" // Register gateway resources. @@ -114,5 +116,6 @@ func Execute() { } func init() { - policies.InitAllPolicies() + plugins.InitAll(core_apis.NameToModule) + plugins.InitAll(policies.NameToModule) } diff --git a/deployments/charts/kuma/templates/cp-rbac.yaml b/deployments/charts/kuma/templates/cp-rbac.yaml index d3efa8842044..905007e3f03b 100644 --- a/deployments/charts/kuma/templates/cp-rbac.yaml +++ b/deployments/charts/kuma/templates/cp-rbac.yaml @@ -151,6 +151,11 @@ rules: {{- if $v }} - {{ $policy }} {{- end}} + {{- end}} + {{- range $policy, $v := .Values.plugins.resources }} + {{- if $v }} + - {{ $policy }} + {{- end}} {{- end}} verbs: - get diff --git a/deployments/charts/kuma/templates/cp-webhooks-and-secrets.yaml b/deployments/charts/kuma/templates/cp-webhooks-and-secrets.yaml index f17b028557b5..31bf3e75f6f2 100644 --- a/deployments/charts/kuma/templates/cp-webhooks-and-secrets.yaml +++ b/deployments/charts/kuma/templates/cp-webhooks-and-secrets.yaml @@ -84,6 +84,11 @@ webhooks: - {{ $policy }} {{- end}} {{- end}} + {{- range $policy, $v := .Values.plugins.resources }} + {{- if $v }} + - {{ $policy }} + {{- end}} + {{- end}} sideEffects: None - name: owner-reference.kuma-admission.kuma.io admissionReviewVersions: ["v1"] @@ -127,6 +132,11 @@ webhooks: - {{ $policy }} {{- end}} {{- end}} + {{- range $policy, $v := .Values.plugins.resources }} + {{- if $v }} + - {{ $policy }} + {{- end}} + {{- end}} {{ .Values.controlPlane.webhooks.ownerReference.additionalRules | nindent 6 }} sideEffects: None {{- if ne .Values.controlPlane.mode "global" }} @@ -241,6 +251,11 @@ webhooks: - {{ $policy }} {{- end}} {{- end}} + {{- range $policy, $v := .Values.plugins.resources }} + {{- if $v }} + - {{ $policy }} + {{- end}} + {{- end}} {{ .Values.controlPlane.webhooks.validator.additionalRules | nindent 6 }} sideEffects: None {{- if ne .Values.controlPlane.mode "global" }} diff --git a/deployments/charts/kuma/values.yaml b/deployments/charts/kuma/values.yaml index 613065e337dd..790420d55392 100644 --- a/deployments/charts/kuma/values.yaml +++ b/deployments/charts/kuma/values.yaml @@ -721,6 +721,7 @@ postgres: # @ignored for helm-docs plugins: + resources: {} policies: meshaccesslogs: true meshcircuitbreakers: true diff --git a/docs/generated/raw/helm-values.yaml b/docs/generated/raw/helm-values.yaml index 613065e337dd..790420d55392 100644 --- a/docs/generated/raw/helm-values.yaml +++ b/docs/generated/raw/helm-values.yaml @@ -721,6 +721,7 @@ postgres: # @ignored for helm-docs plugins: + resources: {} policies: meshaccesslogs: true meshcircuitbreakers: true diff --git a/mk/generate.mk b/mk/generate.mk index 2372e8a886f7..e9d4667927d6 100644 --- a/mk/generate.mk +++ b/mk/generate.mk @@ -29,7 +29,7 @@ clean/protos: ## Dev: Remove auto-generated Protobuf files find $(PROTO_DIRS) -name '*.pb.validate.go' -delete .PHONY: generate -generate: generate/protos $(if $(findstring ./api,$(PROTO_DIRS)),resources/type generate/builtin-crds) generate/policies generate/oas $(EXTRA_GENERATE_DEPS_TARGETS) ## Dev: Run all code generation +generate: generate/protos generate/resources $(if $(findstring ./api,$(PROTO_DIRS)),resources/type generate/builtin-crds) generate/policies generate/oas $(EXTRA_GENERATE_DEPS_TARGETS) ## Dev: Run all code generation $(POLICY_GEN): cd $(KUMA_DIR) && go build -o ./build/tools-${GOOS}-${GOARCH}/policy-gen/generator ./tools/policy-gen/generator/main.go @@ -46,12 +46,18 @@ resources/type: $(RESOURCE_GEN) clean/resources: find pkg -name 'zz_generated.*.go' -delete -POLICIES_DIR := pkg/plugins/policies +POLICIES_DIR ?= pkg/plugins/policies COMMON_DIR := api/common -policies = $(foreach dir,$(shell find pkg/plugins/policies -maxdepth 1 -mindepth 1 -type d | grep -v -e core | sort),$(notdir $(dir))) +policies = $(foreach dir,$(shell find $(POLICIES_DIR) -maxdepth 1 -mindepth 1 -type d | grep -v -e '/core$$' | grep -v -e '/system$$' | grep -v -e '/mesh$$' | sort),$(notdir $(dir))) kuma_policies = $(foreach dir,$(shell find $(KUMA_DIR)/pkg/plugins/policies -maxdepth 1 -mindepth 1 -type d | grep -v -e core | sort),$(notdir $(dir))) +generate/resources: POLICIES_DIR=pkg/core/resources/apis +generate/resources: + POLICIES_DIR=$(POLICIES_DIR) $(MAKE) $(addprefix generate/policy/,$(policies)) + POLICIES_DIR=$(POLICIES_DIR) $(MAKE) generate/policy-import + POLICIES_DIR=$(POLICIES_DIR) HELM_VALUES_FILE_POLICY_PATH=".plugins.resources" $(MAKE) generate/policy-helm + generate/policies: generate/deep-copy/common $(addprefix generate/policy/,$(policies)) generate/policy-import generate/policy-config generate/policy-defaults generate/policy-helm ## Generate all policies written as plugins .PHONY: clean/policies @@ -72,11 +78,11 @@ generate/policy/%: generate/schema/% generate/schema/%: generate/controller-gen/% for version in $(foreach dir,$(wildcard $(POLICIES_DIR)/$*/api/*),$(notdir $(dir))); do \ - PATH=$(CI_TOOLS_BIN_DIR):$$PATH $(TOOLS_DIR)/policy-gen/crd-extract-openapi.sh $* $$version $(TOOLS_DIR) ; \ + PATH=$(CI_TOOLS_BIN_DIR):$$PATH $(TOOLS_DIR)/policy-gen/crd-extract-openapi.sh $* $$version $(TOOLS_DIR) $(POLICIES_DIR); \ done generate/policy-import: - ./tools/policy-gen/generate-policy-import.sh $(GO_MODULE) $(policies) + ./tools/policy-gen/generate-policy-import.sh $(GO_MODULE) $(POLICIES_DIR) $(policies) generate/policy-config: ./tools/policy-gen/generate-policy-config.sh $(policies) @@ -85,7 +91,7 @@ generate/policy-defaults: ./tools/policy-gen/generate-policy-defaults.sh $(KUMA_DIR) $(kuma_policies) $(policies) generate/policy-helm: - PATH=$(CI_TOOLS_BIN_DIR):$$PATH $(TOOLS_DIR)/policy-gen/generate-policy-helm.sh $(HELM_VALUES_FILE) $(HELM_CRD_DIR) $(HELM_VALUES_FILE_POLICY_PATH) $(policies) + PATH=$(CI_TOOLS_BIN_DIR):$$PATH $(TOOLS_DIR)/policy-gen/generate-policy-helm.sh $(HELM_VALUES_FILE) $(HELM_CRD_DIR) $(HELM_VALUES_FILE_POLICY_PATH) $(POLICIES_DIR) $(policies) generate/controller-gen/%: generate/kumapolicy-gen/% # touch is a fix for controller-gen complaining that there is no schema.yaml file diff --git a/pkg/config/plugins/policies/policies.go b/pkg/config/plugins/policies/zz_generated.policies.go similarity index 87% rename from pkg/config/plugins/policies/policies.go rename to pkg/config/plugins/policies/zz_generated.policies.go index 59568ae348a3..3112d1470388 100644 --- a/pkg/config/plugins/policies/policies.go +++ b/pkg/config/plugins/policies/zz_generated.policies.go @@ -1,3 +1,6 @@ +// Generated by tools/policy-gen +// Run "make generate" to update this file. + package policies import "golang.org/x/exp/slices" diff --git a/pkg/core/bootstrap/bootstrap.go b/pkg/core/bootstrap/bootstrap.go index 33885ddf609e..69462beaf91a 100644 --- a/pkg/core/bootstrap/bootstrap.go +++ b/pkg/core/bootstrap/bootstrap.go @@ -26,6 +26,7 @@ import ( "github.com/kumahq/kuma/pkg/core/managers/apis/zoneinsight" core_plugins "github.com/kumahq/kuma/pkg/core/plugins" resources_access "github.com/kumahq/kuma/pkg/core/resources/access" + core_apis "github.com/kumahq/kuma/pkg/core/resources/apis" "github.com/kumahq/kuma/pkg/core/resources/apis/mesh" "github.com/kumahq/kuma/pkg/core/resources/apis/system" core_manager "github.com/kumahq/kuma/pkg/core/resources/manager" @@ -74,7 +75,8 @@ func buildRuntime(appCtx context.Context, cfg kuma_cp.Config) (core_runtime.Runt if err != nil { return nil, err } - policies.InitPolicies(cfg.Policies.PluginPoliciesEnabled) + core_plugins.InitAll(core_apis.NameToModule) + core_plugins.Init(cfg.Policies.PluginPoliciesEnabled, policies.NameToModule) builder.WithMultitenancy(multitenant.SingleTenant) builder.WithPgxConfigCustomizationFn(config.NoopPgxConfigCustomizationFn) for _, plugin := range core_plugins.Plugins().BootstrapPlugins() { diff --git a/pkg/core/plugins/initializer.go b/pkg/core/plugins/initializer.go new file mode 100644 index 000000000000..3bb26a91c0d2 --- /dev/null +++ b/pkg/core/plugins/initializer.go @@ -0,0 +1,27 @@ +package plugins + +type PluginInitializer struct { + InitFn func() + Initialized bool +} + +func InitAll(plugins map[string]*PluginInitializer) { + for _, initializer := range plugins { + if !initializer.Initialized { + initializer.InitFn() + initializer.Initialized = true + } + } +} + +func Init(enabledPlugins []string, plugins map[string]*PluginInitializer) { + for _, policy := range enabledPlugins { + initializer, ok := plugins[policy] + if ok && !initializer.Initialized { + initializer.InitFn() + initializer.Initialized = true + } else { + panic("plugin " + policy + " not found") + } + } +} diff --git a/pkg/core/resources/apis/donothingresource/api/v1alpha1/donothingresource.go b/pkg/core/resources/apis/donothingresource/api/v1alpha1/donothingresource.go new file mode 100644 index 000000000000..d932ea0562fb --- /dev/null +++ b/pkg/core/resources/apis/donothingresource/api/v1alpha1/donothingresource.go @@ -0,0 +1,7 @@ +// +kubebuilder:object:generate=true +package v1alpha1 + +// DoNothingResource +// +kuma:policy:skip_registration=true +// +kuma:policy:is_policy=false +type DoNothingResource struct{} diff --git a/pkg/core/resources/apis/donothingresource/api/v1alpha1/rest.yaml b/pkg/core/resources/apis/donothingresource/api/v1alpha1/rest.yaml new file mode 100644 index 000000000000..b5818296b38d --- /dev/null +++ b/pkg/core/resources/apis/donothingresource/api/v1alpha1/rest.yaml @@ -0,0 +1,122 @@ +openapi: 3.0.3 +info: + version: v1alpha1 + title: Kuma API + description: Kuma API + x-ref-schema-name: "DoNothingResource" + +paths: + /meshes/{mesh}/donothingresources/{name}: + get: + summary: Returns DoNothingResource entity + tags: [ "DoNothingResource" ] + parameters: + - in: path + name: mesh + schema: + type: string + required: true + description: name of the mesh + - in: path + name: name + schema: + type: string + required: true + description: name of the DoNothingResource + responses: + '200': + $ref: "#/components/responses/DoNothingResourceItem" + + put: + summary: Creates or Updates DoNothingResource entity + tags: [ "DoNothingResource" ] + parameters: + - in: path + name: mesh + schema: + type: string + required: true + description: name of the mesh + - in: path + name: name + schema: + type: string + required: true + description: name of the DoNothingResource + requestBody: + description: Put request + required: true + content: + application/json: + schema: + $ref: '#/components/schemas/DoNothingResourceItem' + responses: + '200': + description: Updated + '201': + description: Created + + delete: + summary: Deletes DoNothingResource entity + tags: [ "DoNothingResource" ] + parameters: + - in: path + name: mesh + schema: + type: string + required: true + description: name of the mesh + - in: path + name: name + schema: + type: string + required: true + description: name of the DoNothingResource + responses: + '200': + description: Successful response + + + /meshes/{mesh}/donothingresources: + get: + summary: Returns a list of DoNothingResource in the mesh. + tags: [ "DoNothingResource" ] + parameters: + - in: path + name: mesh + schema: + type: string + required: true + description: name of the mesh + responses: + '200': + $ref: "#/components/responses/DoNothingResourceList" + +components: + schemas: + DoNothingResourceItem: + $ref: 'schema.yaml' + responses: + DoNothingResourceItem: + description: Successful response + content: + application/json: + schema: + $ref: '#/components/schemas/DoNothingResourceItem' + DoNothingResourceList: + description: List + content: + application/json: + schema: + type: object + properties: + items: + type: array + items: + $ref: "#/components/schemas/DoNothingResourceItem" + total: + type: number + description: The total number of entities + next: + type: string + description: URL to the next page diff --git a/pkg/core/resources/apis/donothingresource/api/v1alpha1/schema.yaml b/pkg/core/resources/apis/donothingresource/api/v1alpha1/schema.yaml new file mode 100644 index 000000000000..af40ed086bf6 --- /dev/null +++ b/pkg/core/resources/apis/donothingresource/api/v1alpha1/schema.yaml @@ -0,0 +1,17 @@ +type: object +properties: + type: + description: 'the type of the resource' + type: string + enum: + - DoNothingResource + mesh: + description: 'Mesh is the name of the Kuma mesh this resource belongs to. It may be omitted for cluster-scoped resources.' + type: string + default: default + name: + description: 'Name of the Kuma resource' + type: string + spec: + description: Spec is the specification of the Kuma DoNothingResource resource. + type: object diff --git a/pkg/core/resources/apis/donothingresource/api/v1alpha1/validator.go b/pkg/core/resources/apis/donothingresource/api/v1alpha1/validator.go new file mode 100644 index 000000000000..c7a0c540b4d5 --- /dev/null +++ b/pkg/core/resources/apis/donothingresource/api/v1alpha1/validator.go @@ -0,0 +1,11 @@ +package v1alpha1 + +import ( + "github.com/kumahq/kuma/pkg/core/validators" +) + +func (r *DoNothingResourceResource) validate() error { + var verr validators.ValidationError + _ = validators.RootedAt("spec") + return verr.OrNil() +} diff --git a/pkg/core/resources/apis/donothingresource/api/v1alpha1/zz_generated.deepcopy.go b/pkg/core/resources/apis/donothingresource/api/v1alpha1/zz_generated.deepcopy.go new file mode 100644 index 000000000000..45b3cb2e7e37 --- /dev/null +++ b/pkg/core/resources/apis/donothingresource/api/v1alpha1/zz_generated.deepcopy.go @@ -0,0 +1,22 @@ +//go:build !ignore_autogenerated + +// Code generated by controller-gen. DO NOT EDIT. + +package v1alpha1 + +import () + +// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. +func (in *DoNothingResource) DeepCopyInto(out *DoNothingResource) { + *out = *in +} + +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new DoNothingResource. +func (in *DoNothingResource) DeepCopy() *DoNothingResource { + if in == nil { + return nil + } + out := new(DoNothingResource) + in.DeepCopyInto(out) + return out +} diff --git a/pkg/core/resources/apis/donothingresource/api/v1alpha1/zz_generated.resource.go b/pkg/core/resources/apis/donothingresource/api/v1alpha1/zz_generated.resource.go new file mode 100644 index 000000000000..34ab11a91b6e --- /dev/null +++ b/pkg/core/resources/apis/donothingresource/api/v1alpha1/zz_generated.resource.go @@ -0,0 +1,142 @@ +// Generated by tools/policy-gen. +// Run "make generate" to update this file. + +// nolint:whitespace +package v1alpha1 + +import ( + _ "embed" + "fmt" + + "k8s.io/kube-openapi/pkg/validation/spec" + "sigs.k8s.io/yaml" + + "github.com/kumahq/kuma/pkg/core/resources/model" +) + +//go:embed schema.yaml +var rawSchema []byte + +func init() { + var schema spec.Schema + if err := yaml.Unmarshal(rawSchema, &schema); err != nil { + panic(err) + } + rawSchema = nil + DoNothingResourceResourceTypeDescriptor.Schema = &schema +} + +const ( + DoNothingResourceType model.ResourceType = "DoNothingResource" +) + +var _ model.Resource = &DoNothingResourceResource{} + +type DoNothingResourceResource struct { + Meta model.ResourceMeta + Spec *DoNothingResource +} + +func NewDoNothingResourceResource() *DoNothingResourceResource { + return &DoNothingResourceResource{ + Spec: &DoNothingResource{}, + } +} + +func (t *DoNothingResourceResource) GetMeta() model.ResourceMeta { + return t.Meta +} + +func (t *DoNothingResourceResource) SetMeta(m model.ResourceMeta) { + t.Meta = m +} + +func (t *DoNothingResourceResource) GetSpec() model.ResourceSpec { + return t.Spec +} + +func (t *DoNothingResourceResource) SetSpec(spec model.ResourceSpec) error { + protoType, ok := spec.(*DoNothingResource) + if !ok { + return fmt.Errorf("invalid type %T for Spec", spec) + } else { + if protoType == nil { + t.Spec = &DoNothingResource{} + } else { + t.Spec = protoType + } + return nil + } +} + +func (t *DoNothingResourceResource) Descriptor() model.ResourceTypeDescriptor { + return DoNothingResourceResourceTypeDescriptor +} + +func (t *DoNothingResourceResource) Validate() error { + if v, ok := interface{}(t).(interface{ validate() error }); !ok { + return nil + } else { + return v.validate() + } +} + +var _ model.ResourceList = &DoNothingResourceResourceList{} + +type DoNothingResourceResourceList struct { + Items []*DoNothingResourceResource + Pagination model.Pagination +} + +func (l *DoNothingResourceResourceList) GetItems() []model.Resource { + res := make([]model.Resource, len(l.Items)) + for i, elem := range l.Items { + res[i] = elem + } + return res +} + +func (l *DoNothingResourceResourceList) GetItemType() model.ResourceType { + return DoNothingResourceType +} + +func (l *DoNothingResourceResourceList) NewItem() model.Resource { + return NewDoNothingResourceResource() +} + +func (l *DoNothingResourceResourceList) AddItem(r model.Resource) error { + if trr, ok := r.(*DoNothingResourceResource); ok { + l.Items = append(l.Items, trr) + return nil + } else { + return model.ErrorInvalidItemType((*DoNothingResourceResource)(nil), r) + } +} + +func (l *DoNothingResourceResourceList) GetPagination() *model.Pagination { + return &l.Pagination +} + +func (l *DoNothingResourceResourceList) SetPagination(p model.Pagination) { + l.Pagination = p +} + +var DoNothingResourceResourceTypeDescriptor = model.ResourceTypeDescriptor{ + Name: DoNothingResourceType, + Resource: NewDoNothingResourceResource(), + ResourceList: &DoNothingResourceResourceList{}, + Scope: model.ScopeMesh, + KDSFlags: model.GlobalToAllZonesFlag | model.ZoneToGlobalFlag, + WsPath: "donothingresources", + KumactlArg: "donothingresource", + KumactlListArg: "donothingresources", + AllowToInspect: false, + IsPolicy: false, + IsExperimental: false, + SingularDisplayName: "Do Nothing Resource", + PluralDisplayName: "Do Nothing Resources", + IsPluginOriginated: false, + IsTargetRefBased: false, + HasToTargetRef: false, + HasFromTargetRef: false, +} diff --git a/pkg/core/resources/apis/donothingresource/k8s/crd/kuma.io_donothingresources.yaml b/pkg/core/resources/apis/donothingresource/k8s/crd/kuma.io_donothingresources.yaml new file mode 100644 index 000000000000..120fcce6664f --- /dev/null +++ b/pkg/core/resources/apis/donothingresource/k8s/crd/kuma.io_donothingresources.yaml @@ -0,0 +1,45 @@ +--- +apiVersion: apiextensions.k8s.io/v1 +kind: CustomResourceDefinition +metadata: + annotations: + controller-gen.kubebuilder.io/version: v0.14.0 + name: donothingresources.kuma.io +spec: + group: kuma.io + names: + categories: + - kuma + kind: DoNothingResource + listKind: DoNothingResourceList + plural: donothingresources + singular: donothingresource + scope: Namespaced + versions: + - name: v1alpha1 + schema: + openAPIV3Schema: + properties: + apiVersion: + description: |- + APIVersion defines the versioned schema of this representation of an object. + Servers should convert recognized schemas to the latest internal value, and + may reject unrecognized values. + More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources + type: string + kind: + description: |- + Kind is a string value representing the REST resource this object represents. + Servers may infer this from the endpoint the client submits requests to. + Cannot be updated. + In CamelCase. + More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds + type: string + metadata: + type: object + spec: + description: Spec is the specification of the Kuma DoNothingResource resource. + type: object + type: object + served: true + storage: true diff --git a/pkg/core/resources/apis/donothingresource/k8s/v1alpha1/groupversion_info.go b/pkg/core/resources/apis/donothingresource/k8s/v1alpha1/groupversion_info.go new file mode 100644 index 000000000000..f87708f0cd5b --- /dev/null +++ b/pkg/core/resources/apis/donothingresource/k8s/v1alpha1/groupversion_info.go @@ -0,0 +1,19 @@ +// Package v1alpha1 contains API Schema definitions for the mesh v1alpha1 API group +// +groupName=kuma.io +package v1alpha1 + +import ( + "k8s.io/apimachinery/pkg/runtime/schema" + "sigs.k8s.io/controller-runtime/pkg/scheme" +) + +var ( + // GroupVersion is group version used to register these objects + GroupVersion = schema.GroupVersion{Group: "kuma.io", Version: "v1alpha1"} + + // SchemeBuilder is used to add go types to the GroupVersionKind scheme + SchemeBuilder = &scheme.Builder{GroupVersion: GroupVersion} + + // AddToScheme adds the types in this group-version to the given scheme. + AddToScheme = SchemeBuilder.AddToScheme +) diff --git a/pkg/core/resources/apis/donothingresource/k8s/v1alpha1/zz_generated.deepcopy.go b/pkg/core/resources/apis/donothingresource/k8s/v1alpha1/zz_generated.deepcopy.go new file mode 100644 index 000000000000..b03c5f724225 --- /dev/null +++ b/pkg/core/resources/apis/donothingresource/k8s/v1alpha1/zz_generated.deepcopy.go @@ -0,0 +1,72 @@ +//go:build !ignore_autogenerated + +// Code generated by controller-gen. DO NOT EDIT. + +package v1alpha1 + +import ( + apiv1alpha1 "github.com/kumahq/kuma/pkg/core/resources/apis/donothingresource/api/v1alpha1" + runtime "k8s.io/apimachinery/pkg/runtime" +) + +// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. +func (in *DoNothingResource) DeepCopyInto(out *DoNothingResource) { + *out = *in + out.TypeMeta = in.TypeMeta + in.ObjectMeta.DeepCopyInto(&out.ObjectMeta) + if in.Spec != nil { + in, out := &in.Spec, &out.Spec + *out = new(apiv1alpha1.DoNothingResource) + **out = **in + } +} + +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new DoNothingResource. +func (in *DoNothingResource) DeepCopy() *DoNothingResource { + if in == nil { + return nil + } + out := new(DoNothingResource) + in.DeepCopyInto(out) + return out +} + +// DeepCopyObject is an autogenerated deepcopy function, copying the receiver, creating a new runtime.Object. +func (in *DoNothingResource) DeepCopyObject() runtime.Object { + if c := in.DeepCopy(); c != nil { + return c + } + return nil +} + +// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. +func (in *DoNothingResourceList) DeepCopyInto(out *DoNothingResourceList) { + *out = *in + out.TypeMeta = in.TypeMeta + in.ListMeta.DeepCopyInto(&out.ListMeta) + if in.Items != nil { + in, out := &in.Items, &out.Items + *out = make([]DoNothingResource, len(*in)) + for i := range *in { + (*in)[i].DeepCopyInto(&(*out)[i]) + } + } +} + +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new DoNothingResourceList. +func (in *DoNothingResourceList) DeepCopy() *DoNothingResourceList { + if in == nil { + return nil + } + out := new(DoNothingResourceList) + in.DeepCopyInto(out) + return out +} + +// DeepCopyObject is an autogenerated deepcopy function, copying the receiver, creating a new runtime.Object. +func (in *DoNothingResourceList) DeepCopyObject() runtime.Object { + if c := in.DeepCopy(); c != nil { + return c + } + return nil +} diff --git a/pkg/core/resources/apis/donothingresource/k8s/v1alpha1/zz_generated.types.go b/pkg/core/resources/apis/donothingresource/k8s/v1alpha1/zz_generated.types.go new file mode 100644 index 000000000000..97515ffda399 --- /dev/null +++ b/pkg/core/resources/apis/donothingresource/k8s/v1alpha1/zz_generated.types.go @@ -0,0 +1,87 @@ +// Generated by tools/policy-gen +// Run "make generate" to update this file. + +// nolint:whitespace +package v1alpha1 + +import ( + "fmt" + + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + + policy "github.com/kumahq/kuma/pkg/core/resources/apis/donothingresource/api/v1alpha1" + core_model "github.com/kumahq/kuma/pkg/core/resources/model" + "github.com/kumahq/kuma/pkg/plugins/resources/k8s/native/pkg/model" + "github.com/kumahq/kuma/pkg/plugins/runtime/k8s/metadata" +) + +// +kubebuilder:object:root=true +// +kubebuilder:resource:categories=kuma,scope=Namespaced +type DoNothingResource struct { + metav1.TypeMeta `json:",inline"` + metav1.ObjectMeta `json:"metadata,omitempty"` + + // Spec is the specification of the Kuma DoNothingResource resource. + // +kubebuilder:validation:Optional + Spec *policy.DoNothingResource `json:"spec,omitempty"` +} + +// +kubebuilder:object:root=true +// +kubebuilder:resource:scope=Namespaced +type DoNothingResourceList struct { + metav1.TypeMeta `json:",inline"` + metav1.ListMeta `json:"metadata,omitempty"` + Items []DoNothingResource `json:"items"` +} + +func (cb *DoNothingResource) GetObjectMeta() *metav1.ObjectMeta { + return &cb.ObjectMeta +} + +func (cb *DoNothingResource) SetObjectMeta(m *metav1.ObjectMeta) { + cb.ObjectMeta = *m +} + +func (cb *DoNothingResource) GetMesh() string { + if mesh, ok := cb.ObjectMeta.Labels[metadata.KumaMeshLabel]; ok { + return mesh + } else { + return core_model.DefaultMesh + } +} + +func (cb *DoNothingResource) SetMesh(mesh string) { + if cb.ObjectMeta.Labels == nil { + cb.ObjectMeta.Labels = map[string]string{} + } + cb.ObjectMeta.Labels[metadata.KumaMeshLabel] = mesh +} + +func (cb *DoNothingResource) GetSpec() (core_model.ResourceSpec, error) { + return cb.Spec, nil +} + +func (cb *DoNothingResource) SetSpec(spec core_model.ResourceSpec) { + if spec == nil { + cb.Spec = nil + return + } + + if _, ok := spec.(*policy.DoNothingResource); !ok { + panic(fmt.Sprintf("unexpected protobuf message type %T", spec)) + } + + cb.Spec = spec.(*policy.DoNothingResource) +} + +func (cb *DoNothingResource) Scope() model.Scope { + return model.ScopeNamespace +} + +func (l *DoNothingResourceList) GetItems() []model.KubernetesObject { + result := make([]model.KubernetesObject, len(l.Items)) + for i := range l.Items { + result[i] = &l.Items[i] + } + return result +} diff --git a/pkg/core/resources/apis/zz_generated.modules.go b/pkg/core/resources/apis/zz_generated.modules.go new file mode 100644 index 000000000000..d253e071d36a --- /dev/null +++ b/pkg/core/resources/apis/zz_generated.modules.go @@ -0,0 +1,9 @@ +// Generated by tools/policy-gen +// Run "make generate" to update this file. +package policies + +import ( + "github.com/kumahq/kuma/pkg/core/plugins" +) + +var NameToModule = map[string]*plugins.PluginInitializer{} diff --git a/pkg/core/resources/registry/k8s_schema.go b/pkg/core/resources/registry/k8s_schema.go new file mode 100644 index 000000000000..9760b51d4480 --- /dev/null +++ b/pkg/core/resources/registry/k8s_schema.go @@ -0,0 +1,18 @@ +package registry + +import "k8s.io/apimachinery/pkg/runtime" + +var AllKubeSchemes []func(*runtime.Scheme) error + +func AddKubeScheme(fn func(scheme *runtime.Scheme) error) { + AllKubeSchemes = append(AllKubeSchemes, fn) +} + +func AddToScheme(s *runtime.Scheme) error { + for i := range AllKubeSchemes { + if err := AllKubeSchemes[i](s); err != nil { + return err + } + } + return nil +} diff --git a/pkg/plugins/bootstrap/k8s/scheme.go b/pkg/plugins/bootstrap/k8s/scheme.go index c734049fc868..8103b023ec85 100644 --- a/pkg/plugins/bootstrap/k8s/scheme.go +++ b/pkg/plugins/bootstrap/k8s/scheme.go @@ -7,7 +7,7 @@ import ( kube_client_scheme "k8s.io/client-go/kubernetes/scheme" gatewayapi "sigs.k8s.io/gateway-api/apis/v1beta1" - "github.com/kumahq/kuma/pkg/plugins/policies" + "github.com/kumahq/kuma/pkg/core/resources/registry" mesh_k8s "github.com/kumahq/kuma/pkg/plugins/resources/k8s/native/api/v1alpha1" k8scnicncfio "github.com/kumahq/kuma/pkg/plugins/runtime/k8s/apis/k8s.cni.cncf.io" ) @@ -30,7 +30,7 @@ func NewScheme() (*kube_runtime.Scheme, error) { if err := gatewayapi.Install(s); err != nil { return nil, errors.Wrapf(err, "could not add %q to scheme", gatewayapi.SchemeGroupVersion) } - if err := policies.AddToScheme(s); err != nil { + if err := registry.AddToScheme(s); err != nil { return nil, err } return s, nil diff --git a/pkg/plugins/policies/meshaccesslog/zz_generated.plugin.go b/pkg/plugins/policies/meshaccesslog/zz_generated.plugin.go index b00bca401bc1..a3fe0a519900 100644 --- a/pkg/plugins/policies/meshaccesslog/zz_generated.plugin.go +++ b/pkg/plugins/policies/meshaccesslog/zz_generated.plugin.go @@ -1,16 +1,15 @@ package meshaccesslog import ( - "github.com/kumahq/kuma/pkg/plugins/policies/core" + "github.com/kumahq/kuma/pkg/core/plugins" + "github.com/kumahq/kuma/pkg/core/resources/registry" api_v1alpha1 "github.com/kumahq/kuma/pkg/plugins/policies/meshaccesslog/api/v1alpha1" k8s_v1alpha1 "github.com/kumahq/kuma/pkg/plugins/policies/meshaccesslog/k8s/v1alpha1" plugin_v1alpha1 "github.com/kumahq/kuma/pkg/plugins/policies/meshaccesslog/plugin/v1alpha1" ) func InitPlugin() { - core.Register( - api_v1alpha1.MeshAccessLogResourceTypeDescriptor, - k8s_v1alpha1.AddToScheme, - plugin_v1alpha1.NewPlugin(), - ) + registry.AddKubeScheme(k8s_v1alpha1.AddToScheme) + registry.RegisterType(api_v1alpha1.MeshAccessLogResourceTypeDescriptor) + plugins.Register(plugins.PluginName(api_v1alpha1.MeshAccessLogResourceTypeDescriptor.KumactlArg), plugin_v1alpha1.NewPlugin()) } diff --git a/pkg/plugins/policies/meshcircuitbreaker/zz_generated.plugin.go b/pkg/plugins/policies/meshcircuitbreaker/zz_generated.plugin.go index ed7e1c3c5ae9..b798136dd459 100644 --- a/pkg/plugins/policies/meshcircuitbreaker/zz_generated.plugin.go +++ b/pkg/plugins/policies/meshcircuitbreaker/zz_generated.plugin.go @@ -1,16 +1,15 @@ package meshcircuitbreaker import ( - "github.com/kumahq/kuma/pkg/plugins/policies/core" + "github.com/kumahq/kuma/pkg/core/plugins" + "github.com/kumahq/kuma/pkg/core/resources/registry" api_v1alpha1 "github.com/kumahq/kuma/pkg/plugins/policies/meshcircuitbreaker/api/v1alpha1" k8s_v1alpha1 "github.com/kumahq/kuma/pkg/plugins/policies/meshcircuitbreaker/k8s/v1alpha1" plugin_v1alpha1 "github.com/kumahq/kuma/pkg/plugins/policies/meshcircuitbreaker/plugin/v1alpha1" ) func InitPlugin() { - core.Register( - api_v1alpha1.MeshCircuitBreakerResourceTypeDescriptor, - k8s_v1alpha1.AddToScheme, - plugin_v1alpha1.NewPlugin(), - ) + registry.AddKubeScheme(k8s_v1alpha1.AddToScheme) + registry.RegisterType(api_v1alpha1.MeshCircuitBreakerResourceTypeDescriptor) + plugins.Register(plugins.PluginName(api_v1alpha1.MeshCircuitBreakerResourceTypeDescriptor.KumactlArg), plugin_v1alpha1.NewPlugin()) } diff --git a/pkg/plugins/policies/meshfaultinjection/zz_generated.plugin.go b/pkg/plugins/policies/meshfaultinjection/zz_generated.plugin.go index a9e65b95d149..9e7792023565 100644 --- a/pkg/plugins/policies/meshfaultinjection/zz_generated.plugin.go +++ b/pkg/plugins/policies/meshfaultinjection/zz_generated.plugin.go @@ -1,16 +1,15 @@ package meshfaultinjection import ( - "github.com/kumahq/kuma/pkg/plugins/policies/core" + "github.com/kumahq/kuma/pkg/core/plugins" + "github.com/kumahq/kuma/pkg/core/resources/registry" api_v1alpha1 "github.com/kumahq/kuma/pkg/plugins/policies/meshfaultinjection/api/v1alpha1" k8s_v1alpha1 "github.com/kumahq/kuma/pkg/plugins/policies/meshfaultinjection/k8s/v1alpha1" plugin_v1alpha1 "github.com/kumahq/kuma/pkg/plugins/policies/meshfaultinjection/plugin/v1alpha1" ) func InitPlugin() { - core.Register( - api_v1alpha1.MeshFaultInjectionResourceTypeDescriptor, - k8s_v1alpha1.AddToScheme, - plugin_v1alpha1.NewPlugin(), - ) + registry.AddKubeScheme(k8s_v1alpha1.AddToScheme) + registry.RegisterType(api_v1alpha1.MeshFaultInjectionResourceTypeDescriptor) + plugins.Register(plugins.PluginName(api_v1alpha1.MeshFaultInjectionResourceTypeDescriptor.KumactlArg), plugin_v1alpha1.NewPlugin()) } diff --git a/pkg/plugins/policies/meshhealthcheck/zz_generated.plugin.go b/pkg/plugins/policies/meshhealthcheck/zz_generated.plugin.go index 04d98493c374..bd8da287c447 100644 --- a/pkg/plugins/policies/meshhealthcheck/zz_generated.plugin.go +++ b/pkg/plugins/policies/meshhealthcheck/zz_generated.plugin.go @@ -1,16 +1,15 @@ package meshhealthcheck import ( - "github.com/kumahq/kuma/pkg/plugins/policies/core" + "github.com/kumahq/kuma/pkg/core/plugins" + "github.com/kumahq/kuma/pkg/core/resources/registry" api_v1alpha1 "github.com/kumahq/kuma/pkg/plugins/policies/meshhealthcheck/api/v1alpha1" k8s_v1alpha1 "github.com/kumahq/kuma/pkg/plugins/policies/meshhealthcheck/k8s/v1alpha1" plugin_v1alpha1 "github.com/kumahq/kuma/pkg/plugins/policies/meshhealthcheck/plugin/v1alpha1" ) func InitPlugin() { - core.Register( - api_v1alpha1.MeshHealthCheckResourceTypeDescriptor, - k8s_v1alpha1.AddToScheme, - plugin_v1alpha1.NewPlugin(), - ) + registry.AddKubeScheme(k8s_v1alpha1.AddToScheme) + registry.RegisterType(api_v1alpha1.MeshHealthCheckResourceTypeDescriptor) + plugins.Register(plugins.PluginName(api_v1alpha1.MeshHealthCheckResourceTypeDescriptor.KumactlArg), plugin_v1alpha1.NewPlugin()) } diff --git a/pkg/plugins/policies/meshhttproute/zz_generated.plugin.go b/pkg/plugins/policies/meshhttproute/zz_generated.plugin.go index e781d0882a14..8cb1e33b4296 100644 --- a/pkg/plugins/policies/meshhttproute/zz_generated.plugin.go +++ b/pkg/plugins/policies/meshhttproute/zz_generated.plugin.go @@ -1,16 +1,15 @@ package meshhttproute import ( - "github.com/kumahq/kuma/pkg/plugins/policies/core" + "github.com/kumahq/kuma/pkg/core/plugins" + "github.com/kumahq/kuma/pkg/core/resources/registry" api_v1alpha1 "github.com/kumahq/kuma/pkg/plugins/policies/meshhttproute/api/v1alpha1" k8s_v1alpha1 "github.com/kumahq/kuma/pkg/plugins/policies/meshhttproute/k8s/v1alpha1" plugin_v1alpha1 "github.com/kumahq/kuma/pkg/plugins/policies/meshhttproute/plugin/v1alpha1" ) func InitPlugin() { - core.Register( - api_v1alpha1.MeshHTTPRouteResourceTypeDescriptor, - k8s_v1alpha1.AddToScheme, - plugin_v1alpha1.NewPlugin(), - ) + registry.AddKubeScheme(k8s_v1alpha1.AddToScheme) + registry.RegisterType(api_v1alpha1.MeshHTTPRouteResourceTypeDescriptor) + plugins.Register(plugins.PluginName(api_v1alpha1.MeshHTTPRouteResourceTypeDescriptor.KumactlArg), plugin_v1alpha1.NewPlugin()) } diff --git a/pkg/plugins/policies/meshloadbalancingstrategy/zz_generated.plugin.go b/pkg/plugins/policies/meshloadbalancingstrategy/zz_generated.plugin.go index 9add6f7e6110..ec96abda77f0 100644 --- a/pkg/plugins/policies/meshloadbalancingstrategy/zz_generated.plugin.go +++ b/pkg/plugins/policies/meshloadbalancingstrategy/zz_generated.plugin.go @@ -1,16 +1,15 @@ package meshloadbalancingstrategy import ( - "github.com/kumahq/kuma/pkg/plugins/policies/core" + "github.com/kumahq/kuma/pkg/core/plugins" + "github.com/kumahq/kuma/pkg/core/resources/registry" api_v1alpha1 "github.com/kumahq/kuma/pkg/plugins/policies/meshloadbalancingstrategy/api/v1alpha1" k8s_v1alpha1 "github.com/kumahq/kuma/pkg/plugins/policies/meshloadbalancingstrategy/k8s/v1alpha1" plugin_v1alpha1 "github.com/kumahq/kuma/pkg/plugins/policies/meshloadbalancingstrategy/plugin/v1alpha1" ) func InitPlugin() { - core.Register( - api_v1alpha1.MeshLoadBalancingStrategyResourceTypeDescriptor, - k8s_v1alpha1.AddToScheme, - plugin_v1alpha1.NewPlugin(), - ) + registry.AddKubeScheme(k8s_v1alpha1.AddToScheme) + registry.RegisterType(api_v1alpha1.MeshLoadBalancingStrategyResourceTypeDescriptor) + plugins.Register(plugins.PluginName(api_v1alpha1.MeshLoadBalancingStrategyResourceTypeDescriptor.KumactlArg), plugin_v1alpha1.NewPlugin()) } diff --git a/pkg/plugins/policies/meshmetric/zz_generated.plugin.go b/pkg/plugins/policies/meshmetric/zz_generated.plugin.go index 3850c9bcf2a8..8acc3d83abad 100644 --- a/pkg/plugins/policies/meshmetric/zz_generated.plugin.go +++ b/pkg/plugins/policies/meshmetric/zz_generated.plugin.go @@ -1,16 +1,15 @@ package meshmetric import ( - "github.com/kumahq/kuma/pkg/plugins/policies/core" + "github.com/kumahq/kuma/pkg/core/plugins" + "github.com/kumahq/kuma/pkg/core/resources/registry" api_v1alpha1 "github.com/kumahq/kuma/pkg/plugins/policies/meshmetric/api/v1alpha1" k8s_v1alpha1 "github.com/kumahq/kuma/pkg/plugins/policies/meshmetric/k8s/v1alpha1" plugin_v1alpha1 "github.com/kumahq/kuma/pkg/plugins/policies/meshmetric/plugin/v1alpha1" ) func InitPlugin() { - core.Register( - api_v1alpha1.MeshMetricResourceTypeDescriptor, - k8s_v1alpha1.AddToScheme, - plugin_v1alpha1.NewPlugin(), - ) + registry.AddKubeScheme(k8s_v1alpha1.AddToScheme) + registry.RegisterType(api_v1alpha1.MeshMetricResourceTypeDescriptor) + plugins.Register(plugins.PluginName(api_v1alpha1.MeshMetricResourceTypeDescriptor.KumactlArg), plugin_v1alpha1.NewPlugin()) } diff --git a/pkg/plugins/policies/meshproxypatch/zz_generated.plugin.go b/pkg/plugins/policies/meshproxypatch/zz_generated.plugin.go index 229cc227bd7f..5e6e3bb88f57 100644 --- a/pkg/plugins/policies/meshproxypatch/zz_generated.plugin.go +++ b/pkg/plugins/policies/meshproxypatch/zz_generated.plugin.go @@ -1,16 +1,15 @@ package meshproxypatch import ( - "github.com/kumahq/kuma/pkg/plugins/policies/core" + "github.com/kumahq/kuma/pkg/core/plugins" + "github.com/kumahq/kuma/pkg/core/resources/registry" api_v1alpha1 "github.com/kumahq/kuma/pkg/plugins/policies/meshproxypatch/api/v1alpha1" k8s_v1alpha1 "github.com/kumahq/kuma/pkg/plugins/policies/meshproxypatch/k8s/v1alpha1" plugin_v1alpha1 "github.com/kumahq/kuma/pkg/plugins/policies/meshproxypatch/plugin/v1alpha1" ) func InitPlugin() { - core.Register( - api_v1alpha1.MeshProxyPatchResourceTypeDescriptor, - k8s_v1alpha1.AddToScheme, - plugin_v1alpha1.NewPlugin(), - ) + registry.AddKubeScheme(k8s_v1alpha1.AddToScheme) + registry.RegisterType(api_v1alpha1.MeshProxyPatchResourceTypeDescriptor) + plugins.Register(plugins.PluginName(api_v1alpha1.MeshProxyPatchResourceTypeDescriptor.KumactlArg), plugin_v1alpha1.NewPlugin()) } diff --git a/pkg/plugins/policies/meshratelimit/zz_generated.plugin.go b/pkg/plugins/policies/meshratelimit/zz_generated.plugin.go index be24590cd177..5bc77ba6768a 100644 --- a/pkg/plugins/policies/meshratelimit/zz_generated.plugin.go +++ b/pkg/plugins/policies/meshratelimit/zz_generated.plugin.go @@ -1,16 +1,15 @@ package meshratelimit import ( - "github.com/kumahq/kuma/pkg/plugins/policies/core" + "github.com/kumahq/kuma/pkg/core/plugins" + "github.com/kumahq/kuma/pkg/core/resources/registry" api_v1alpha1 "github.com/kumahq/kuma/pkg/plugins/policies/meshratelimit/api/v1alpha1" k8s_v1alpha1 "github.com/kumahq/kuma/pkg/plugins/policies/meshratelimit/k8s/v1alpha1" plugin_v1alpha1 "github.com/kumahq/kuma/pkg/plugins/policies/meshratelimit/plugin/v1alpha1" ) func InitPlugin() { - core.Register( - api_v1alpha1.MeshRateLimitResourceTypeDescriptor, - k8s_v1alpha1.AddToScheme, - plugin_v1alpha1.NewPlugin(), - ) + registry.AddKubeScheme(k8s_v1alpha1.AddToScheme) + registry.RegisterType(api_v1alpha1.MeshRateLimitResourceTypeDescriptor) + plugins.Register(plugins.PluginName(api_v1alpha1.MeshRateLimitResourceTypeDescriptor.KumactlArg), plugin_v1alpha1.NewPlugin()) } diff --git a/pkg/plugins/policies/meshretry/zz_generated.plugin.go b/pkg/plugins/policies/meshretry/zz_generated.plugin.go index 239ad51e2d06..78e7cd9bb6bf 100644 --- a/pkg/plugins/policies/meshretry/zz_generated.plugin.go +++ b/pkg/plugins/policies/meshretry/zz_generated.plugin.go @@ -1,16 +1,15 @@ package meshretry import ( - "github.com/kumahq/kuma/pkg/plugins/policies/core" + "github.com/kumahq/kuma/pkg/core/plugins" + "github.com/kumahq/kuma/pkg/core/resources/registry" api_v1alpha1 "github.com/kumahq/kuma/pkg/plugins/policies/meshretry/api/v1alpha1" k8s_v1alpha1 "github.com/kumahq/kuma/pkg/plugins/policies/meshretry/k8s/v1alpha1" plugin_v1alpha1 "github.com/kumahq/kuma/pkg/plugins/policies/meshretry/plugin/v1alpha1" ) func InitPlugin() { - core.Register( - api_v1alpha1.MeshRetryResourceTypeDescriptor, - k8s_v1alpha1.AddToScheme, - plugin_v1alpha1.NewPlugin(), - ) + registry.AddKubeScheme(k8s_v1alpha1.AddToScheme) + registry.RegisterType(api_v1alpha1.MeshRetryResourceTypeDescriptor) + plugins.Register(plugins.PluginName(api_v1alpha1.MeshRetryResourceTypeDescriptor.KumactlArg), plugin_v1alpha1.NewPlugin()) } diff --git a/pkg/plugins/policies/meshtcproute/zz_generated.plugin.go b/pkg/plugins/policies/meshtcproute/zz_generated.plugin.go index 7ec5cbebe831..7048d0dbc8ec 100644 --- a/pkg/plugins/policies/meshtcproute/zz_generated.plugin.go +++ b/pkg/plugins/policies/meshtcproute/zz_generated.plugin.go @@ -1,16 +1,15 @@ package meshtcproute import ( - "github.com/kumahq/kuma/pkg/plugins/policies/core" + "github.com/kumahq/kuma/pkg/core/plugins" + "github.com/kumahq/kuma/pkg/core/resources/registry" api_v1alpha1 "github.com/kumahq/kuma/pkg/plugins/policies/meshtcproute/api/v1alpha1" k8s_v1alpha1 "github.com/kumahq/kuma/pkg/plugins/policies/meshtcproute/k8s/v1alpha1" plugin_v1alpha1 "github.com/kumahq/kuma/pkg/plugins/policies/meshtcproute/plugin/v1alpha1" ) func InitPlugin() { - core.Register( - api_v1alpha1.MeshTCPRouteResourceTypeDescriptor, - k8s_v1alpha1.AddToScheme, - plugin_v1alpha1.NewPlugin(), - ) + registry.AddKubeScheme(k8s_v1alpha1.AddToScheme) + registry.RegisterType(api_v1alpha1.MeshTCPRouteResourceTypeDescriptor) + plugins.Register(plugins.PluginName(api_v1alpha1.MeshTCPRouteResourceTypeDescriptor.KumactlArg), plugin_v1alpha1.NewPlugin()) } diff --git a/pkg/plugins/policies/meshtimeout/zz_generated.plugin.go b/pkg/plugins/policies/meshtimeout/zz_generated.plugin.go index f5b20f030609..36fa83cff934 100644 --- a/pkg/plugins/policies/meshtimeout/zz_generated.plugin.go +++ b/pkg/plugins/policies/meshtimeout/zz_generated.plugin.go @@ -1,16 +1,15 @@ package meshtimeout import ( - "github.com/kumahq/kuma/pkg/plugins/policies/core" + "github.com/kumahq/kuma/pkg/core/plugins" + "github.com/kumahq/kuma/pkg/core/resources/registry" api_v1alpha1 "github.com/kumahq/kuma/pkg/plugins/policies/meshtimeout/api/v1alpha1" k8s_v1alpha1 "github.com/kumahq/kuma/pkg/plugins/policies/meshtimeout/k8s/v1alpha1" plugin_v1alpha1 "github.com/kumahq/kuma/pkg/plugins/policies/meshtimeout/plugin/v1alpha1" ) func InitPlugin() { - core.Register( - api_v1alpha1.MeshTimeoutResourceTypeDescriptor, - k8s_v1alpha1.AddToScheme, - plugin_v1alpha1.NewPlugin(), - ) + registry.AddKubeScheme(k8s_v1alpha1.AddToScheme) + registry.RegisterType(api_v1alpha1.MeshTimeoutResourceTypeDescriptor) + plugins.Register(plugins.PluginName(api_v1alpha1.MeshTimeoutResourceTypeDescriptor.KumactlArg), plugin_v1alpha1.NewPlugin()) } diff --git a/pkg/plugins/policies/meshtrace/zz_generated.plugin.go b/pkg/plugins/policies/meshtrace/zz_generated.plugin.go index ef7eef501ab3..f2274d17be4c 100644 --- a/pkg/plugins/policies/meshtrace/zz_generated.plugin.go +++ b/pkg/plugins/policies/meshtrace/zz_generated.plugin.go @@ -1,16 +1,15 @@ package meshtrace import ( - "github.com/kumahq/kuma/pkg/plugins/policies/core" + "github.com/kumahq/kuma/pkg/core/plugins" + "github.com/kumahq/kuma/pkg/core/resources/registry" api_v1alpha1 "github.com/kumahq/kuma/pkg/plugins/policies/meshtrace/api/v1alpha1" k8s_v1alpha1 "github.com/kumahq/kuma/pkg/plugins/policies/meshtrace/k8s/v1alpha1" plugin_v1alpha1 "github.com/kumahq/kuma/pkg/plugins/policies/meshtrace/plugin/v1alpha1" ) func InitPlugin() { - core.Register( - api_v1alpha1.MeshTraceResourceTypeDescriptor, - k8s_v1alpha1.AddToScheme, - plugin_v1alpha1.NewPlugin(), - ) + registry.AddKubeScheme(k8s_v1alpha1.AddToScheme) + registry.RegisterType(api_v1alpha1.MeshTraceResourceTypeDescriptor) + plugins.Register(plugins.PluginName(api_v1alpha1.MeshTraceResourceTypeDescriptor.KumactlArg), plugin_v1alpha1.NewPlugin()) } diff --git a/pkg/plugins/policies/meshtrafficpermission/zz_generated.plugin.go b/pkg/plugins/policies/meshtrafficpermission/zz_generated.plugin.go index 91da29f3ec0f..ea44205e1a11 100644 --- a/pkg/plugins/policies/meshtrafficpermission/zz_generated.plugin.go +++ b/pkg/plugins/policies/meshtrafficpermission/zz_generated.plugin.go @@ -1,16 +1,15 @@ package meshtrafficpermission import ( - "github.com/kumahq/kuma/pkg/plugins/policies/core" + "github.com/kumahq/kuma/pkg/core/plugins" + "github.com/kumahq/kuma/pkg/core/resources/registry" api_v1alpha1 "github.com/kumahq/kuma/pkg/plugins/policies/meshtrafficpermission/api/v1alpha1" k8s_v1alpha1 "github.com/kumahq/kuma/pkg/plugins/policies/meshtrafficpermission/k8s/v1alpha1" plugin_v1alpha1 "github.com/kumahq/kuma/pkg/plugins/policies/meshtrafficpermission/plugin/v1alpha1" ) func InitPlugin() { - core.Register( - api_v1alpha1.MeshTrafficPermissionResourceTypeDescriptor, - k8s_v1alpha1.AddToScheme, - plugin_v1alpha1.NewPlugin(), - ) + registry.AddKubeScheme(k8s_v1alpha1.AddToScheme) + registry.RegisterType(api_v1alpha1.MeshTrafficPermissionResourceTypeDescriptor) + plugins.Register(plugins.PluginName(api_v1alpha1.MeshTrafficPermissionResourceTypeDescriptor.KumactlArg), plugin_v1alpha1.NewPlugin()) } diff --git a/pkg/plugins/policies/scheme.go b/pkg/plugins/policies/scheme.go deleted file mode 100644 index 1656ac7cb66f..000000000000 --- a/pkg/plugins/policies/scheme.go +++ /dev/null @@ -1,16 +0,0 @@ -package policies - -import ( - "k8s.io/apimachinery/pkg/runtime" - - "github.com/kumahq/kuma/pkg/plugins/policies/core" -) - -func AddToScheme(s *runtime.Scheme) error { - for i := range core.AllSchemes { - if err := core.AllSchemes[i](s); err != nil { - return err - } - } - return nil -} diff --git a/pkg/plugins/policies/imports.go b/pkg/plugins/policies/zz_generated.modules.go similarity index 78% rename from pkg/plugins/policies/imports.go rename to pkg/plugins/policies/zz_generated.modules.go index a42711c77c11..b72bcfbd0f78 100644 --- a/pkg/plugins/policies/imports.go +++ b/pkg/plugins/policies/zz_generated.modules.go @@ -1,6 +1,9 @@ +// Generated by tools/policy-gen +// Run "make generate" to update this file. package policies import ( + "github.com/kumahq/kuma/pkg/core/plugins" "github.com/kumahq/kuma/pkg/plugins/policies/meshaccesslog" "github.com/kumahq/kuma/pkg/plugins/policies/meshcircuitbreaker" "github.com/kumahq/kuma/pkg/plugins/policies/meshfaultinjection" @@ -17,12 +20,7 @@ import ( "github.com/kumahq/kuma/pkg/plugins/policies/meshtrafficpermission" ) -type PluginInitializer struct { - InitFn func() - Initialized bool -} - -var NameToModule = map[string]*PluginInitializer{ +var NameToModule = map[string]*plugins.PluginInitializer{ "meshaccesslogs": {InitFn: meshaccesslog.InitPlugin, Initialized: false}, "meshcircuitbreakers": {InitFn: meshcircuitbreaker.InitPlugin, Initialized: false}, "meshfaultinjections": {InitFn: meshfaultinjection.InitPlugin, Initialized: false}, @@ -38,24 +36,3 @@ var NameToModule = map[string]*PluginInitializer{ "meshtraces": {InitFn: meshtrace.InitPlugin, Initialized: false}, "meshtrafficpermissions": {InitFn: meshtrafficpermission.InitPlugin, Initialized: false}, } - -func InitAllPolicies() { - for _, initializer := range NameToModule { - if !initializer.Initialized { - initializer.InitFn() - initializer.Initialized = true - } - } -} - -func InitPolicies(enabledPluginPolicies []string) { - for _, policy := range enabledPluginPolicies { - initializer, ok := NameToModule[policy] - if ok && !initializer.Initialized { - initializer.InitFn() - initializer.Initialized = true - } else { - panic("policy " + policy + " not found") - } - } -} diff --git a/pkg/test/ginkgo.go b/pkg/test/ginkgo.go index 766fcad92c29..d34767960127 100644 --- a/pkg/test/ginkgo.go +++ b/pkg/test/ginkgo.go @@ -15,12 +15,15 @@ import ( "sigs.k8s.io/controller-runtime/pkg/log/zap" "github.com/kumahq/kuma/pkg/core" + "github.com/kumahq/kuma/pkg/core/plugins" + core_apis "github.com/kumahq/kuma/pkg/core/resources/apis" "github.com/kumahq/kuma/pkg/plugins/policies" ) // RunSpecs wraps ginkgo+gomega test suite initialization. func RunSpecs(t *testing.T, description string) { - policies.InitAllPolicies() + plugins.InitAll(core_apis.NameToModule) + plugins.InitAll(policies.NameToModule) format.TruncatedDiff = false if strings.HasPrefix(description, "E2E") { panic("Use RunE2ESpecs for e2e tests!") @@ -29,7 +32,8 @@ func RunSpecs(t *testing.T, description string) { } func RunE2ESpecs(t *testing.T, description string) { - policies.InitAllPolicies() + plugins.InitAll(core_apis.NameToModule) + plugins.InitAll(policies.NameToModule) gomega.SetDefaultConsistentlyDuration(time.Second * 5) gomega.SetDefaultConsistentlyPollingInterval(time.Millisecond * 200) gomega.SetDefaultEventuallyPollingInterval(time.Millisecond * 500) diff --git a/tools/policy-gen/bootstrap/root.go b/tools/policy-gen/bootstrap/root.go index 305b08ecdf06..4529ec308881 100644 --- a/tools/policy-gen/bootstrap/root.go +++ b/tools/policy-gen/bootstrap/root.go @@ -24,6 +24,7 @@ type config struct { generateTargetRef bool generateTo bool generateFrom bool + isPolicy bool } func (c config) policyPath() string { @@ -57,12 +58,14 @@ var rootCmd = &cobra.Command{ if err := generateType(cfg); err != nil { return err } - _, _ = fmt.Fprintf(cmd.OutOrStdout(), "Generating plugin file\n") - if err := generatePlugin(cfg); err != nil { - return err + if cfg.isPolicy { + _, _ = fmt.Fprintf(cmd.OutOrStdout(), "Generating plugin file\n") + if err := generatePlugin(cfg); err != nil { + return err + } } path := fmt.Sprintf("generate/policy/%s", cfg.lowercase()) - if err := exec.Command("make", path).Run(); err != nil { + if err := exec.Command("make", path, "POLICIES_DIR="+cfg.basePath).Run(); err != nil { return err } _, _ = cmd.OutOrStdout().Write([]byte(fmt.Sprintf(` @@ -100,6 +103,7 @@ func generateType(c config) error { "generateTargetRef": c.generateTargetRef, "generateTo": c.generateTo, "generateFrom": c.generateFrom, + "isPolicy": c.isPolicy, }) if err != nil { return err @@ -117,6 +121,7 @@ func generateType(c config) error { "generateTargetRef": c.generateTargetRef, "generateTo": c.generateTo, "generateFrom": c.generateFrom, + "isPolicy": c.isPolicy, }) } @@ -158,6 +163,7 @@ func init() { rootCmd.Flags().BoolVar(&cfg.generateTargetRef, "generate-target-ref", true, "Generate top-level TargetRef for dataplane proxy matching") rootCmd.Flags().BoolVar(&cfg.generateTo, "generate-to", false, "Generate 'to' array for outgoing traffic configuration") rootCmd.Flags().BoolVar(&cfg.generateFrom, "generate-from", false, "Generate 'from' array for incoming traffic configuration") + rootCmd.Flags().BoolVar(&cfg.isPolicy, "is-policy", false, "Resource is a policy") } var typeTemplate = template.Must(template.New("").Option("missingkey=error").Parse( @@ -172,6 +178,7 @@ import ( // {{ .name }} // +kuma:policy:skip_registration=true +// +kuma:policy:is_policy={{ .isPolicy }} type {{ .name }} struct { {{- if .generateTargetRef }} // TargetRef is a reference to the resource the policy takes an effect on. @@ -213,9 +220,11 @@ type From struct { {{- end}} +{{- if .isPolicy }} type Conf struct { // TODO add configuration fields } +{{- end}} `)) var pluginTemplate = template.Must(template.New("").Option("missingkey=error").Parse( @@ -342,9 +351,11 @@ func validateTo(to []To) validators.ValidationError { } {{- end }} +{{- if .isPolicy }} func validateDefault(conf Conf) validators.ValidationError { var verr validators.ValidationError // TODO add default conf validation return verr } +{{- end }} `)) diff --git a/tools/policy-gen/crd-extract-openapi.sh b/tools/policy-gen/crd-extract-openapi.sh index 695fea50c509..423ba8036517 100755 --- a/tools/policy-gen/crd-extract-openapi.sh +++ b/tools/policy-gen/crd-extract-openapi.sh @@ -8,8 +8,8 @@ set -e POLICY=$1 VERSION=$2 TOOLS_LOCATION=$3 +POLICIES_DIR=$4 -POLICIES_DIR=pkg/plugins/policies POLICIES_API_DIR="${POLICIES_DIR}/${POLICY}/api/${VERSION}" POLICIES_CRD_DIR="${POLICIES_DIR}/${POLICY}/k8s/crd" diff --git a/tools/policy-gen/generate-policy-config.sh b/tools/policy-gen/generate-policy-config.sh index af92c7f55b5c..3825755f47a1 100755 --- a/tools/policy-gen/generate-policy-config.sh +++ b/tools/policy-gen/generate-policy-config.sh @@ -2,7 +2,7 @@ set -e -POLICIES_FILE="pkg/config/plugins/policies/policies.go" +POLICIES_FILE="pkg/config/plugins/policies/zz_generated.policies.go" policies=$(for i in "${@:1}"; do if [[ -f pkg/plugins/policies/${i}/zz_generated.plugin.go ]]; then @@ -19,7 +19,10 @@ if [[ $policies == "" ]]; then exit 0 fi -echo "package policies +echo "// Generated by tools/policy-gen +// Run \"make generate\" to update this file. + +package policies import \"golang.org/x/exp/slices\" diff --git a/tools/policy-gen/generate-policy-helm.sh b/tools/policy-gen/generate-policy-helm.sh index 1e3ce2988f1b..588ef71170c3 100755 --- a/tools/policy-gen/generate-policy-helm.sh +++ b/tools/policy-gen/generate-policy-helm.sh @@ -7,14 +7,20 @@ set -e HELM_VALUES_FILE=$1 HELM_CRD_DIR=$2 VALUES_FILE_POLICY_PATH=$3 +POLICIES_DIR=$4 policies="" -for policy in "${@:4}"; do +for policy in "${@:5}"; do - policy_dir="pkg/plugins/policies/${policy}" + policy_dir="${POLICIES_DIR}/${policy}" policy_crd_dir="${policy_dir}/k8s/crd" + if [ ! -f "${policy_dir}/zz_generated.plugin.go" ]; then + echo "Policy ${policy} has skip registration, not updating helm" + continue + fi + if [ "$(find "${policy_crd_dir}" -type f | wc -l | xargs echo)" != 1 ]; then echo "More than 1 file in crd directory" exit 1 @@ -23,11 +29,6 @@ for policy in "${@:4}"; do policy_crd_file="$(find "${policy_crd_dir}" -type f)" rm -f "${HELM_CRD_DIR}/$(basename "${policy_crd_file}")" - if [ ! -f "${policy_dir}/zz_generated.plugin.go" ]; then - echo "Policy ${policy} has skip registration, not updating helm" - continue - fi - cp "${policy_crd_file}" "${HELM_CRD_DIR}" plural=$(yq e '.spec.names.plural' "${policy_crd_file}") @@ -36,6 +37,10 @@ for policy in "${@:4}"; do done +if [ -z "$policies" ]; then + exit 0 +fi + # yq_patch preserves indentation and blank lines of the original file cp "${HELM_VALUES_FILE}" "${HELM_VALUES_FILE}.noblank" # shellcheck disable=SC2016 diff --git a/tools/policy-gen/generate-policy-import.sh b/tools/policy-gen/generate-policy-import.sh index c8d7d1654234..3691f34b97b3 100755 --- a/tools/policy-gen/generate-policy-import.sh +++ b/tools/policy-gen/generate-policy-import.sh @@ -2,18 +2,20 @@ set -e GO_MODULE=$1 +POLICIES_DIR=$2 # pkg/plugins/policies or pkg/core/resources/apis +IMPORTS_FILE="${POLICIES_DIR}/zz_generated.modules.go" + +imports=$( + for i in "${@:3}"; do + if [[ -f "${POLICIES_DIR}/${i}/zz_generated.plugin.go" ]]; then + echo "\"${GO_MODULE}/${POLICIES_DIR}/${i}\"" + fi + done +) -IMPORTS_FILE="pkg/plugins/policies/imports.go" - -imports=$(for i in "${@:2}"; do [[ -f pkg/plugins/policies/${i}/zz_generated.plugin.go ]] && echo "\"${GO_MODULE}/pkg/plugins/policies/${i}\""; done) -if [[ $imports == "" ]]; then - rm -f "${IMPORTS_FILE}" - exit 0 -fi - -mappings=$(for i in "${@:2}"; do - if [[ -f pkg/plugins/policies/${i}/zz_generated.plugin.go ]]; then - policy_dir="pkg/plugins/policies/${i}" +mappings=$(for i in "${@:3}"; do + if [[ -f ${POLICIES_DIR}/${i}/zz_generated.plugin.go ]]; then + policy_dir="${POLICIES_DIR}/${i}" policy_crd_dir="${policy_dir}/k8s/crd" policy_crd_file="$(find "${policy_crd_dir}" -type f)" plural=$(yq e '.spec.names.plural' "$policy_crd_file") @@ -21,41 +23,18 @@ mappings=$(for i in "${@:2}"; do fi done) -echo "package policies +echo "// Generated by tools/policy-gen +// Run \"make generate\" to update this file. +package policies import ( - $imports + \"github.com/kumahq/kuma/pkg/core/plugins\" + $imports ) -type PluginInitializer struct { - InitFn func() - Initialized bool -} - -var NameToModule = map[string]*PluginInitializer{ +var NameToModule = map[string]*plugins.PluginInitializer{ $mappings } - -func InitAllPolicies() { - for _, initializer := range NameToModule { - if !initializer.Initialized { - initializer.InitFn() - initializer.Initialized = true - } - } -} - -func InitPolicies(enabledPluginPolicies []string) { - for _, policy := range enabledPluginPolicies { - initializer, ok := NameToModule[policy] - if ok && !initializer.Initialized { - initializer.InitFn() - initializer.Initialized = true - } else { - panic(\"policy \" + policy + \" not found\") - } - } -} " > "${IMPORTS_FILE}" gofmt -w "${IMPORTS_FILE}" diff --git a/tools/policy-gen/generator/cmd/core_resource.go b/tools/policy-gen/generator/cmd/core_resource.go index 40c01fb669de..2ec486dcfb2e 100644 --- a/tools/policy-gen/generator/cmd/core_resource.go +++ b/tools/policy-gen/generator/cmd/core_resource.go @@ -170,13 +170,13 @@ var {{.Name}}ResourceTypeDescriptor = model.ResourceTypeDescriptor{ WsPath: "{{.Path}}", KumactlArg: "{{index .AlternativeNames 0}}", KumactlListArg: "{{.Path}}", - AllowToInspect: true, - IsPolicy: true, + AllowToInspect: {{.IsPolicy}}, + IsPolicy: {{.IsPolicy}}, IsExperimental: false, SingularDisplayName: "{{.SingularDisplayName}}", PluralDisplayName: "{{.PluralDisplayName}}", - IsPluginOriginated: true, - IsTargetRefBased: true, + IsPluginOriginated: {{.IsPolicy}}, + IsTargetRefBased: {{.IsPolicy}}, HasToTargetRef: {{.HasTo}}, HasFromTargetRef: {{.HasFrom}}, } diff --git a/tools/policy-gen/generator/cmd/helpers.go b/tools/policy-gen/generator/cmd/helpers.go index 2998a86332d3..4c892755d2eb 100644 --- a/tools/policy-gen/generator/cmd/helpers.go +++ b/tools/policy-gen/generator/cmd/helpers.go @@ -28,6 +28,10 @@ func newHelpers(rootArgs *args) *cobra.Command { return err } + if !pconfig.IsPolicy { + return nil + } + outPath := filepath.Join(filepath.Dir(policyPath), "zz_generated.helpers.go") return save.GoTemplate(helpersTemplate, map[string]interface{}{ "name": pconfig.Name, diff --git a/tools/policy-gen/generator/cmd/k8s_resource.go b/tools/policy-gen/generator/cmd/k8s_resource.go index f4fd0b832fcd..88ccc6e7aed2 100644 --- a/tools/policy-gen/generator/cmd/k8s_resource.go +++ b/tools/policy-gen/generator/cmd/k8s_resource.go @@ -29,6 +29,7 @@ func newK8sResource(rootArgs *args) *cobra.Command { } pconfig.GoModule = rootArgs.goModule + pconfig.ResourceDir = rootArgs.pluginDir k8sPath := filepath.Join(rootArgs.pluginDir, "k8s", rootArgs.version) if err := os.MkdirAll(k8sPath, 0o755); err != nil { @@ -68,7 +69,7 @@ import ( metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" core_model "github.com/kumahq/kuma/pkg/core/resources/model" - policy "{{.GoModule}}/pkg/plugins/policies/{{.NameLower}}/api/{{.Package}}" + policy "{{.GoModule}}/{{.ResourceDir}}/api/{{.Package}}" "github.com/kumahq/kuma/pkg/plugins/resources/k8s/native/pkg/model" {{- if not .SkipRegistration }} "github.com/kumahq/kuma/pkg/plugins/resources/k8s/native/pkg/registry" @@ -78,8 +79,10 @@ import ( // +kubebuilder:object:root=true // +kubebuilder:resource:categories=kuma,scope=Namespaced +{{- if .IsPolicy }} // +kubebuilder:printcolumn:name="TargetRef Kind",type="string",JSONPath=".spec.targetRef.kind" // +kubebuilder:printcolumn:name="TargetRef Name",type="string",JSONPath=".spec.targetRef.name" +{{- end }} type {{.Name}} struct { metav1.TypeMeta {{ $tk }}json:",inline"{{ $tk }} metav1.ObjectMeta {{ $tk }}json:"metadata,omitempty"{{ $tk }} diff --git a/tools/policy-gen/generator/cmd/pluginfile.go b/tools/policy-gen/generator/cmd/pluginfile.go index cd5b8fc66e2d..247585301176 100644 --- a/tools/policy-gen/generator/cmd/pluginfile.go +++ b/tools/policy-gen/generator/cmd/pluginfile.go @@ -47,15 +47,19 @@ func newPluginFile(rootArgs *args) *cobra.Command { outPath := filepath.Join(rootArgs.pluginDir, "zz_generated.plugin.go") return save.GoTemplate(pluginGoTemplate, struct { - Package string - Versions []string - Name string - GoModule string + Package string + Versions []string + Name string + GoModule string + ResourceDir string + IsPolicy bool }{ - Package: strings.ToLower(pconfig.Name), - Name: pconfig.Name, - Versions: versions, - GoModule: rootArgs.goModule, + Package: strings.ToLower(pconfig.Name), + Name: pconfig.Name, + Versions: versions, + GoModule: rootArgs.goModule, + ResourceDir: rootArgs.pluginDir, + IsPolicy: pconfig.IsPolicy, }, outPath) }, } @@ -66,26 +70,32 @@ func newPluginFile(rootArgs *args) *cobra.Command { var pluginGoTemplate = template.Must(template.New("plugin-go").Parse(` package {{ .Package }} -{{ $pkg := .Package }} +{{ $pkg := .ResourceDir }} {{ $name := .Name }} {{ $gomodule := .GoModule }} +{{ $isPolicy := .IsPolicy }} import ( - "github.com/kumahq/kuma/pkg/plugins/policies/core" +{{- if $isPolicy }} + "github.com/kumahq/kuma/pkg/core/plugins" +{{- end}} + "github.com/kumahq/kuma/pkg/core/resources/registry" {{- range $idx, $version := .Versions}} - api_{{ $version }} "{{ $gomodule }}/pkg/plugins/policies/{{ $pkg }}/api/{{ $version }}" - k8s_{{ $version }} "{{ $gomodule }}/pkg/plugins/policies/{{ $pkg }}/k8s/{{ $version }}" - plugin_{{ $version }} "{{ $gomodule }}/pkg/plugins/policies/{{ $pkg }}/plugin/{{ $version }}" + api_{{ $version }} "{{ $gomodule }}/{{ $pkg }}/api/{{ $version }}" + k8s_{{ $version }} "{{ $gomodule }}/{{ $pkg }}/k8s/{{ $version }}" +{{- if $isPolicy }} + plugin_{{ $version }} "{{ $gomodule }}/{{ $pkg }}/plugin/{{ $version }}" +{{- end }} {{- end}} ) func InitPlugin() { {{- range $idx, $version := .Versions}} - core.Register( - api_{{ $version }}.{{ $name }}ResourceTypeDescriptor, - k8s_{{ $version }}.AddToScheme, - plugin_{{ $version }}.NewPlugin(), - ) + registry.AddKubeScheme(k8s_{{ $version }}.AddToScheme) + registry.RegisterType(api_{{ $version }}.{{ $name }}ResourceTypeDescriptor) +{{- if $isPolicy }} + plugins.Register(plugins.PluginName(api_{{ $version }}.{{ $name }}ResourceTypeDescriptor.KumactlArg), plugin_{{ $version }}.NewPlugin()) +{{- end }} {{- end}} } `)) diff --git a/tools/policy-gen/generator/pkg/parse/policyconfig.go b/tools/policy-gen/generator/pkg/parse/policyconfig.go index 350f0f619d0f..39167695b77c 100644 --- a/tools/policy-gen/generator/pkg/parse/policyconfig.go +++ b/tools/policy-gen/generator/pkg/parse/policyconfig.go @@ -27,6 +27,8 @@ type PolicyConfig struct { HasTo bool HasFrom bool GoModule string + ResourceDir string + IsPolicy bool } func Policy(path string) (PolicyConfig, error) { @@ -124,6 +126,7 @@ func newPolicyConfig(pkg, name string, markers map[string]string, hasTo, hasFrom AlternativeNames: []string{strings.ToLower(name)}, HasTo: hasTo, HasFrom: hasFrom, + IsPolicy: true, } if v, ok := parseBool(markers, "kuma:policy:skip_registration"); ok { @@ -132,6 +135,9 @@ func newPolicyConfig(pkg, name string, markers map[string]string, hasTo, hasFrom if v, ok := parseBool(markers, "kuma:policy:skip_get_default"); ok { res.SkipGetDefault = v } + if v, ok := parseBool(markers, "kuma:policy:is_policy"); ok { + res.IsPolicy = v + } if v, ok := markers["kuma:policy:singular_display_name"]; ok { res.SingularDisplayName = v