Skip to content

Commit

Permalink
add nodebucket crd and controller
Browse files Browse the repository at this point in the history
  • Loading branch information
rambohe-ch committed Jan 17, 2024
1 parent 81d21b7 commit ff928df
Show file tree
Hide file tree
Showing 17 changed files with 1,651 additions and 280 deletions.
77 changes: 77 additions & 0 deletions charts/yurt-manager/crds/apps.openyurt.io_nodebuckets.yaml
Original file line number Diff line number Diff line change
@@ -0,0 1,77 @@
apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
annotations:
controller-gen.kubebuilder.io/version: v0.7.0
creationTimestamp: null
name: nodebuckets.apps.openyurt.io
spec:
group: apps.openyurt.io
names:
categories:
- all
kind: NodeBucket
listKind: NodeBucketList
plural: nodebuckets
shortNames:
- nb
singular: nodebucket
scope: Cluster
versions:
- additionalPrinterColumns:
- description: NumNodes represents the number of nodes in the NodeBucket.
jsonPath: .numNodes
name: NUM-NODES
type: integer
- description: CreationTimestamp is a timestamp representing the server time when
this object was created. It is not guaranteed to be set in happens-before
order across separate operations. Clients may not set this value. It is represented
in RFC3339 form and is in UTC.
jsonPath: .metadata.creationTimestamp
name: AGE
type: date
name: v1alpha1
schema:
openAPIV3Schema:
description: NodeBucket is the Schema for the samples 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
nodes:
description: Nodes represents a subset nodes in the nodepool
items:
description: Node represents a specified node in the nodepool
properties:
name:
description: Name is the name of node
type: string
type: object
type: array
numNodes:
description: NumNodes represents the number of nodes in the nodebucket
format: int32
type: integer
required:
- nodes
- numNodes
type: object
served: true
storage: true
subresources:
status: {}
status:
acceptedNames:
kind: ""
plural: ""
conditions: []
storedVersions: []
12 changes: 12 additions & 0 deletions charts/yurt-manager/templates/yurt-manager-auto-generated.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 135,18 @@ rules:
- get
- patch
- update
- apiGroups:
- apps.openyurt.io
resources:
- nodebuckets
verbs:
- create
- delete
- get
- list
- patch
- update
- watch
- apiGroups:
- apps.openyurt.io
resources:
Expand Down
70 changes: 70 additions & 0 deletions cmd/yurt-manager/app/options/nodebucketcontroller.go
Original file line number Diff line number Diff line change
@@ -0,0 1,70 @@
/*
Copyright 2023 The OpenYurt 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 options

import (
"fmt"

"github.com/spf13/pflag"

"github.com/openyurtio/openyurt/pkg/yurtmanager/controller/nodebucket/config"
)

type NodeBucketControllerOptions struct {
*config.NodeBucketControllerConfiguration
}

func NewNodeBucketControllerOptions() *NodeBucketControllerOptions {
return &NodeBucketControllerOptions{
&config.NodeBucketControllerConfiguration{
MaxNodesPerBucket: 100,
},
}
}

// AddFlags adds flags related to nodebucket for yurt-manager to the specified FlagSet.
func (n *NodeBucketControllerOptions) AddFlags(fs *pflag.FlagSet) {
if n == nil {
return
}

fs.Int32Var(&n.MaxNodesPerBucket, "max-nodes-per-bucket", n.MaxNodesPerBucket, "The maximum number of nodes that will be added to a NodeBucket. More nodes per bucket will result in less node buckets, but larger resources. Defaults to 100.")
}

// ApplyTo fills up nodebucket config with options.
func (o *NodeBucketControllerOptions) ApplyTo(cfg *config.NodeBucketControllerConfiguration) error {
if o == nil {
return nil
}

cfg.MaxNodesPerBucket = o.MaxNodesPerBucket

return nil
}

// Validate checks validation of NodeBucketControllerOptions.
func (o *NodeBucketControllerOptions) Validate() []error {
if o == nil {
return nil
}

errs := []error{}
if o.MaxNodesPerBucket <= 0 {
errs = append(errs, fmt.Errorf("max-nodes-per-bucket(%d) is invalid, should greater than 0", o.MaxNodesPerBucket))
}
return errs
}
8 changes: 7 additions & 1 deletion cmd/yurt-manager/app/options/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 37,7 @@ type YurtManagerOptions struct {
PlatformAdminController *PlatformAdminControllerOptions
YurtAppOverriderController *YurtAppOverriderControllerOptions
NodeLifeCycleController *NodeLifecycleControllerOptions
NodeBucketController *NodeBucketControllerOptions
}

// NewYurtManagerOptions creates a new YurtManagerOptions with a default config.
Expand All @@ -55,6 56,7 @@ func NewYurtManagerOptions() (*YurtManagerOptions, error) {
PlatformAdminController: NewPlatformAdminControllerOptions(),
YurtAppOverriderController: NewYurtAppOverriderControllerOptions(),
NodeLifeCycleController: NewNodeLifecycleControllerOptions(),
NodeBucketController: NewNodeBucketControllerOptions(),
}

return &s, nil
Expand All @@ -73,7 75,7 @@ func (y *YurtManagerOptions) Flags(allControllers, disabledByDefaultControllers
y.PlatformAdminController.AddFlags(fss.FlagSet("iot controller"))
y.YurtAppOverriderController.AddFlags(fss.FlagSet("yurtappoverrider controller"))
y.NodeLifeCycleController.AddFlags(fss.FlagSet("nodelifecycle controller"))

y.NodeBucketController.AddFlags(fss.FlagSet("nodebucket controller"))
return fss
}

Expand All @@ -91,6 93,7 @@ func (y *YurtManagerOptions) Validate(allControllers []string, controllerAliases
errs = append(errs, y.PlatformAdminController.Validate()...)
errs = append(errs, y.YurtAppOverriderController.Validate()...)
errs = append(errs, y.NodeLifeCycleController.Validate()...)
errs = append(errs, y.NodeBucketController.Validate()...)
return utilerrors.NewAggregate(errs)
}

Expand Down Expand Up @@ -129,6 132,9 @@ func (y *YurtManagerOptions) ApplyTo(c *config.Config, controllerAliases map[str
if err := y.NodeLifeCycleController.ApplyTo(&c.ComponentConfig.NodeLifeCycleController); err != nil {
return err
}
if err := y.NodeBucketController.ApplyTo(&c.ComponentConfig.NodeBucketController); err != nil {
return err
}
return nil
}

Expand Down
2 changes: 2 additions & 0 deletions cmd/yurt-manager/names/controller_names.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 35,7 @@ const (
GatewayPublicServiceController = "gateway-public-service-controller"
GatewayDNSController = "gateway-dns-controller"
NodeLifeCycleController = "node-life-cycle-controller"
NodeBucketController = "node-bucket-controller"
)

func YurtManagerControllerAliases() map[string]string {
Expand All @@ -58,5 59,6 @@ func YurtManagerControllerAliases() map[string]string {
"gatewaypublicservice": GatewayPublicServiceController,
"gatewaydns": GatewayDNSController,
"nodelifecycle": NodeLifeCycleController,
"nodebucket": NodeBucketController,
}
}
2 changes: 1 addition & 1 deletion hack/make-rules/add_controller.sh
Original file line number Diff line number Diff line change
Expand Up @@ -421,7 421,7 @@ func Format(format string, args ...interface{}) string {
// Add creates a new ${KIND_FIRST_UPPER} Controller and adds it to the Manager with default RBAC. The Manager will set fields on the Controller
// and Start it when the Manager is Started.
func Add(c *appconfig.CompletedConfig, mgr manager.Manager) error {
func Add(ctx context.Context, c *appconfig.CompletedConfig, mgr manager.Manager) error {
klog.Infof(Format("${KIND_ALL_LOWER}-controller add controller %s", controllerKind.String()))
return add(mgr, newReconciler(c, mgr))
}
Expand Down
1 change: 1 addition & 0 deletions hack/make-rules/kustomize_to_chart.sh
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 196,7 @@ EOF
mv ${crd_dir}/apiextensions.k8s.io_v1_customresourcedefinition_yurtappoverriders.apps.openyurt.io.yaml ${crd_dir}/apps.openyurt.io_yurtappoverriders.yaml
mv ${crd_dir}/apiextensions.k8s.io_v1_customresourcedefinition_gateways.raven.openyurt.io.yaml ${crd_dir}/raven.openyurt.io_gateways.yaml
mv ${crd_dir}/apiextensions.k8s.io_v1_customresourcedefinition_platformadmins.iot.openyurt.io.yaml ${crd_dir}/iot.openyurt.io_platformadmins.yaml
mv ${crd_dir}/apiextensions.k8s.io_v1_customresourcedefinition_nodebuckets.apps.openyurt.io.yaml ${crd_dir}/apps.openyurt.io_nodebuckets.yaml
# TODO: In the future, the crd generation process of yurt-manager and yurt-iot-dock will be split. For now, manually remove it from the yurt-manager script
# mv ${crd_dir}/apiextensions.k8s.io_v1_customresourcedefinition_devices.iot.openyurt.io.yaml ${crd_dir}/iot.openyurt.io_devices.yaml
# mv ${crd_dir}/apiextensions.k8s.io_v1_customresourcedefinition_deviceservices.iot.openyurt.io.yaml ${crd_dir}/iot.openyurt.io_deviceservices.yaml
Expand Down
6 changes: 6 additions & 0 deletions pkg/apis/apps/v1alpha1/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -256,3 256,9 @@ func SetDefaultsYurtAppDaemon(obj *YurtAppDaemon) {
SetDefaultPodSpec(&obj.Spec.WorkloadTemplate.DeploymentTemplate.Spec.Template.Spec)
}
}

// SetDefaultsNodeBucket set default values for NodeBucket.
func SetDefaultsNodeBucket(obj *NodeBucket) {
// example for set default value for NodeBucket

}
60 changes: 60 additions & 0 deletions pkg/apis/apps/v1alpha1/nodebucket_types.go
Original file line number Diff line number Diff line change
@@ -0,0 1,60 @@
/*
Copyright 2023 The OpenYurt 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"
)

// Node represents a specified node in the nodepool
type Node struct {
// Name is the name of node
Name string `json:"name,omitempty"`
}

// genclient
// k8s:openapi-gen=true
// kubebuilder:object:root=true
// kubebuilder:subresource:status
// kubebuilder:resource:scope=Cluster,path=nodebuckets,shortName=nb,categories=all
// kubebuilder:printcolumn:name="NUM-NODES",type="integer",JSONPath=".numNodes",description="NumNodes represents the number of nodes in the NodeBucket."
// kubebuilder:printcolumn:name="AGE",type="date",JSONPath=".metadata.creationTimestamp",description="CreationTimestamp is a timestamp representing the server time when this object was created. It is not guaranteed to be set in happens-before order across separate operations. Clients may not set this value. It is represented in RFC3339 form and is in UTC."

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

// NumNodes represents the number of nodes in the nodebucket
NumNodes int32 `json:"numNodes"`

// Nodes represents a subset nodes in the nodepool
Nodes []Node `json:"nodes"`
}

// kubebuilder:object:root=true

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

func init() {
SchemeBuilder.Register(&NodeBucket{}, &NodeBucketList{})
}
4 changes: 2 additions & 2 deletions pkg/apis/apps/v1alpha1/nodepool_conversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 37,7 @@ func (src *NodePool) ConvertTo(dstRaw conversion.Hub) error {
dst.Status.UnreadyNodeNum = src.Status.UnreadyNodeNum
dst.Status.Nodes = src.Status.Nodes

klog.Infof("convert from v1alpha1 to v1beta1 for nodepool %s", dst.Name)
klog.V(4).Infof("convert from v1alpha1 to v1beta1 for nodepool %s", dst.Name)

return nil
}
Expand All @@ -56,6 56,6 @@ func (dst *NodePool) ConvertFrom(srcRaw conversion.Hub) error {
dst.Status.UnreadyNodeNum = src.Status.UnreadyNodeNum
dst.Status.Nodes = src.Status.Nodes

klog.Infof("convert from v1beta1 to v1alpha1 for nodepool %s", dst.Name)
klog.V(4).Infof("convert from v1beta1 to v1alpha1 for nodepool %s", dst.Name)
return nil
}
Loading

0 comments on commit ff928df

Please sign in to comment.