Skip to content

Commit

Permalink
Merge pull request #219 from bjwswang/dev
Browse files Browse the repository at this point in the history
fix: fully compatiable with original menus/component design
  • Loading branch information
bjwswang authored Aug 7, 2023
2 parents 2b6df16 + 00897f6 commit 0a02256
Show file tree
Hide file tree
Showing 16 changed files with 402 additions and 0 deletions.
10 changes: 10 additions & 0 deletions PROJECT
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,14 @@ resources:
kind: Component
path: github.com/kubebb/core/api/v1alpha1
version: v1alpha1
- api:
crdVersion: v1
domain: kubebb.k8s.com.cn
group: core
kind: Portal
path: github.com/kubebb/core/api/v1alpha1
version: v1alpha1
webhooks:
validation: true
webhookVersion: v1
version: "3"
3 changes: 3 additions & 0 deletions api/v1alpha1/menu_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,9 @@ type MenuSpec struct {
// 父菜单 ID
// +optional
Parent string `json:"parent,omitempty"`
// +optional
// FIXME: deprecate this when `AutoMenus` supported
ParentOwnerReferences metav1.OwnerReference `json:"parentOwnerReferences,omitempty"`
// menu 显示控制
// +optional
Disabled bool `json:"disabled,omitempty"`
Expand Down
64 changes: 64 additions & 0 deletions api/v1alpha1/portal_types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
Copyright 2023 The Kubebb Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package v1alpha1

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

// EDIT THIS FILE! THIS IS SCAFFOLDING FOR YOU TO OWN!
// NOTE: json tags are required. Any new fields you add must have json tags for the fields to be serialized.

// PortalSpec defines the desired state of Portal
type PortalSpec struct {
// the path for request acccessing
Path string `json:"path,omitempty"`
// the path of the static file
Entry string `json:"entry,omitempty"`
}

// PortalStatus defines the observed state of Portal
type PortalStatus struct {
// INSERT ADDITIONAL STATUS FIELD - define observed state of cluster
// Important: Run "make" to regenerate code after modifying this file
}

//+kubebuilder:object:root=true
//+kubebuilder:subresource:status
//+kubebuilder:resource:scope=Cluster

// Portal is the Schema for the portals API
type Portal struct {
metav1.TypeMeta `json:",inline"`
metav1.ObjectMeta `json:"metadata,omitempty"`

Spec PortalSpec `json:"spec,omitempty"`
Status PortalStatus `json:"status,omitempty"`
}

//+kubebuilder:object:root=true

// PortalList contains a list of Portal
type PortalList struct {
metav1.TypeMeta `json:",inline"`
metav1.ListMeta `json:"metadata,omitempty"`
Items []Portal `json:"items"`
}

func init() {
SchemeBuilder.Register(&Portal{}, &PortalList{})
}
64 changes: 64 additions & 0 deletions api/v1alpha1/portal_webhook.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
Copyright 2023 The Kubebb Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package v1alpha1

import (
"context"

"k8s.io/apimachinery/pkg/runtime"
ctrl "sigs.k8s.io/controller-runtime"
logf "sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/webhook"
)

// log is for logging in this package.
var portallog = logf.Log.WithName("portal-resource")

func (r *Portal) SetupWebhookWithManager(mgr ctrl.Manager) error {
return ctrl.NewWebhookManagedBy(mgr).
For(r).
Complete()
}

//+kubebuilder:webhook:path=/validate-core-kubebb-k8s-com-cn-v1alpha1-portal,mutating=false,failurePolicy=fail,sideEffects=None,groups=core.kubebb.k8s.com.cn,resources=portals,verbs=create;update,versions=v1alpha1,name=vportal.kb.io,admissionReviewVersions=v1

var _ webhook.CustomValidator = &Portal{}

// ValidateCreate implements webhook.Validator so a webhook will be registered for the type
func (r *Portal) ValidateCreate(ctx context.Context, obj runtime.Object) error {
portallog.Info("validate create", "name", r.Name)

// TODO: validate entry & path conflicts

return nil
}

// ValidateUpdate implements webhook.Validator so a webhook will be registered for the type
func (r *Portal) ValidateUpdate(ctx context.Context, oldObj runtime.Object, newObj runtime.Object) error {
portallog.Info("validate update", "name", r.Name)

// TODO: validate entry & path conflicts
return nil
}

// ValidateDelete implements webhook.Validator so a webhook will be registered for the type
func (r *Portal) ValidateDelete(ctx context.Context, obj runtime.Object) error {
portallog.Info("validate delete", "name", r.Name)

// TODO(user): fill in your validation logic upon object deletion.
return nil
}
3 changes: 3 additions & 0 deletions api/v1alpha1/webhook_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,9 @@ var _ = BeforeSuite(func() {
err = (&Subscription{}).SetupWebhookWithManager(mgr)
Expect(err).NotTo(HaveOccurred())

err = (&Portal{}).SetupWebhookWithManager(mgr)
Expect(err).NotTo(HaveOccurred())

//+kubebuilder:scaffold:webhook

go func() {
Expand Down
90 changes: 90 additions & 0 deletions api/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

34 changes: 34 additions & 0 deletions config/crd/bases/core.kubebb.k8s.com.cn_menus.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,40 @@ spec:
parent:
description: 父菜单 ID
type: string
parentOwnerReferences:
description: 'FIXME: deprecate this when `AutoMenus` supported'
properties:
apiVersion:
description: API version of the referent.
type: string
blockOwnerDeletion:
description: If true, AND if the owner has the "foregroundDeletion"
finalizer, then the owner cannot be deleted from the key-value
store until this reference is removed. See https://kubernetes.io/docs/concepts/architecture/garbage-collection/#foreground-deletion
for how the garbage collector interacts with this field and
enforces the foreground deletion. Defaults to false. To set
this field, a user needs "delete" permission of the owner, otherwise
422 (Unprocessable Entity) will be returned.
type: boolean
controller:
description: If true, this reference points to the managing controller.
type: boolean
kind:
description: 'Kind of the referent. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds'
type: string
name:
description: 'Name of the referent. More info: http://kubernetes.io/docs/user-guide/identifiers#names'
type: string
uid:
description: 'UID of the referent. More info: http://kubernetes.io/docs/user-guide/identifiers#uids'
type: string
required:
- apiVersion
- kind
- name
- uid
type: object
x-kubernetes-map-type: atomic
pathname:
description: 菜单路由
type: string
Expand Down
52 changes: 52 additions & 0 deletions config/crd/bases/core.kubebb.k8s.com.cn_portals.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
---
apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
annotations:
controller-gen.kubebuilder.io/version: v0.9.2
creationTimestamp: null
name: portals.core.kubebb.k8s.com.cn
spec:
group: core.kubebb.k8s.com.cn
names:
kind: Portal
listKind: PortalList
plural: portals
singular: portal
scope: Cluster
versions:
- name: v1alpha1
schema:
openAPIV3Schema:
description: Portal is the Schema for the portals API
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: PortalSpec defines the desired state of Portal
properties:
entry:
description: the path of the static file
type: string
path:
description: the path for request acccessing
type: string
type: object
status:
description: PortalStatus defines the observed state of Portal
type: object
type: object
served: true
storage: true
subresources:
status: {}
3 changes: 3 additions & 0 deletions config/crd/kustomization.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ resources:
- bases/core.kubebb.k8s.com.cn_subscriptions.yaml
- bases/core.kubebb.k8s.com.cn_componentplans.yaml
- bases/core.kubebb.k8s.com.cn_components.yaml
- bases/core.kubebb.k8s.com.cn_portals.yaml
#+kubebuilder:scaffold:crdkustomizeresource

patchesStrategicMerge:
Expand All @@ -16,6 +17,7 @@ patchesStrategicMerge:
#- patches/webhook_in_subscriptions.yaml
#- patches/webhook_in_componentplans.yaml
#- patches/webhook_in_components.yaml
#- patches/webhook_in_portals.yaml
#+kubebuilder:scaffold:crdkustomizewebhookpatch

# [CERTMANAGER] To enable cert-manager, uncomment all the sections with [CERTMANAGER] prefix.
Expand All @@ -24,6 +26,7 @@ patchesStrategicMerge:
#- patches/cainjection_in_subscriptions.yaml
#- patches/cainjection_in_componentplans.yaml
#- patches/cainjection_in_components.yaml
#- patches/cainjection_in_portals.yaml
#+kubebuilder:scaffold:crdkustomizecainjectionpatch

# the following config is for teaching kustomize how to do kustomization for CRDs.
Expand Down
7 changes: 7 additions & 0 deletions config/crd/patches/cainjection_in_portals.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# The following patch adds a directive for certmanager to inject CA into the CRD
apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
annotations:
cert-manager.io/inject-ca-from: $(CERTIFICATE_NAMESPACE)/$(CERTIFICATE_NAME)
name: portals.core.kubebb.k8s.com.cn
Loading

0 comments on commit 0a02256

Please sign in to comment.