Skip to content

Commit

Permalink
adopt new framework of yurt-manager
Browse files Browse the repository at this point in the history
Signed-off-by: hxcGit <[email protected]>
  • Loading branch information
xavier-hou committed Mar 24, 2023
1 parent 8bf25d0 commit d1c63d7
Show file tree
Hide file tree
Showing 18 changed files with 322 additions and 395 deletions.
4 changes: 2 additions & 2 deletions charts/openyurt/templates/yurt-manager-auto-generated.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -936,7 +936,7 @@ webhooks:
service:
name: webhook-service
namespace: kube-system
path: /mutate-apps-openyurt-io-v1alpha1-staticpod
path: /mutate-apps-openyurt-io-staticpod
failurePolicy: Fail
name: mutate.apps.v1alpha1.staticpod.openyurt.io
rules:
Expand Down Expand Up @@ -1006,7 +1006,7 @@ webhooks:
service:
name: webhook-service
namespace: kube-system
path: /validate-apps-openyurt-io-v1alpha1-staticpod
path: /validate-apps-openyurt-io-staticpod
failurePolicy: Fail
name: validate.apps.v1alpha1.staticpod.openyurt.io
rules:
Expand Down
17 changes: 10 additions & 7 deletions cmd/yurt-manager/app/options/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,20 @@ import (

// YurtManagerOptions is the main context object for the yurt-manager.
type YurtManagerOptions struct {
Generic *GenericOptions
NodePoolController *NodePoolControllerOptions
GatewayController *GatewayControllerOptions
Generic *GenericOptions
NodePoolController *NodePoolControllerOptions
GatewayController *GatewayControllerOptions
StaticPodController *StaticPodControllerOptions
}

// NewYurtManagerOptions creates a new YurtManagerOptions with a default config.
func NewYurtManagerOptions() (*YurtManagerOptions, error) {

s := YurtManagerOptions{
Generic: NewGenericOptions(),
NodePoolController: NewNodePoolControllerOptions(),
GatewayController: NewGatewayControllerOptions(),
Generic: NewGenericOptions(),
NodePoolController: NewNodePoolControllerOptions(),
GatewayController: NewGatewayControllerOptions(),
StaticPodController: NewStaticPodControllerOptions(),
}

return &s, nil
Expand All @@ -46,7 +48,7 @@ func (y *YurtManagerOptions) Flags() cliflag.NamedFlagSets {
y.Generic.AddFlags(fss.FlagSet("generic"))
y.NodePoolController.AddFlags(fss.FlagSet("nodepool controller"))
y.GatewayController.AddFlags(fss.FlagSet("gateway controller"))

y.StaticPodController.AddFlags(fss.FlagSet("staticpod controller"))
// Please Add Other controller flags @kadisi

return fss
Expand All @@ -58,6 +60,7 @@ func (y *YurtManagerOptions) Validate() error {
errs = append(errs, y.Generic.Validate()...)
errs = append(errs, y.NodePoolController.Validate()...)
errs = append(errs, y.GatewayController.Validate()...)
errs = append(errs, y.StaticPodController.Validate()...)
return utilerrors.NewAggregate(errs)
}

Expand Down
60 changes: 60 additions & 0 deletions cmd/yurt-manager/app/options/staticpodcontroller.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 options

import (
"github.com/spf13/pflag"

"github.com/openyurtio/openyurt/pkg/controller/staticpod/config"
)

type StaticPodControllerOptions struct {
*config.StaticPodControllerConfiguration
}

func NewStaticPodControllerOptions() *StaticPodControllerOptions {
return &StaticPodControllerOptions{
&config.StaticPodControllerConfiguration{},
}
}

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

//fs.BoolVar(&n.CreateDefaultPool, "create-default-pool", n.CreateDefaultPool, "Create default cloud/edge pools if indicated.")
}

// ApplyTo fills up nodepool config with options.
func (o *StaticPodControllerOptions) ApplyTo(cfg *config.StaticPodControllerConfiguration) error {
if o == nil {
return nil
}

return nil
}

// Validate checks validation of StaticPodControllerOptions.
func (o *StaticPodControllerOptions) Validate() []error {
if o == nil {
return nil
}
errs := []error{}
return errs
}
2 changes: 0 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ require (
github.com/russross/blackfriday v1.5.2 // indirect
github.com/xlab/treeprint v0.0.0-20181112141820-a009c3971eca // indirect
go.starlark.net v0.0.0-20200306205701-8dd3e2ee1dd5 // indirect
k8s.io/cli-runtime v0.22.3
sigs.k8s.io/kustomize/api v0.8.11 // indirect
sigs.k8s.io/kustomize/kyaml v0.11.0 // indirect
)
Expand Down Expand Up @@ -100,7 +99,6 @@ require (
github.com/jmespath/go-jmespath v0.0.0-20180206201540-c2b33e8439af // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/liggitt/tabwriter v0.0.0-20181228230101-89fcab3d43de // indirect
github.com/mailru/easyjson v0.7.6 // indirect
github.com/mattn/go-isatty v0.0.14 // indirect
github.com/mattn/go-runewidth v0.0.7 // indirect
Expand Down
18 changes: 18 additions & 0 deletions pkg/apis/apps/v1alpha1/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ limitations under the License.

package v1alpha1

import "k8s.io/apimachinery/pkg/util/intstr"

// SetDefaultsNodePool set default values for NodePool.
func SetDefaultsNodePool(obj *NodePool) {
// example for set default value for NodePool
Expand All @@ -24,3 +26,19 @@ func SetDefaultsNodePool(obj *NodePool) {
}

}

// SetDefaultsStaticPod set default values for StaticPod.
func SetDefaultsStaticPod(obj *StaticPod) {
// Set default max-unavailable to "10%" in auto mode if it's not set
strategy := obj.Spec.UpgradeStrategy.DeepCopy()
if strategy != nil && strategy.Type == AutoStaticPodUpgradeStrategyType && strategy.MaxUnavailable == nil {
v := intstr.FromString("10%")
obj.Spec.UpgradeStrategy.MaxUnavailable = &v
}

// Set default RevisionHistoryLimit to 10
if obj.Spec.RevisionHistoryLimit == nil {
obj.Spec.RevisionHistoryLimit = new(int32)
*obj.Spec.RevisionHistoryLimit = 10
}
}
4 changes: 4 additions & 0 deletions pkg/controller/apis/config/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (

gatewayconfig "github.com/openyurtio/openyurt/pkg/controller/gateway/config"
nodepoolconfig "github.com/openyurtio/openyurt/pkg/controller/nodepool/config"
staticpodconfig "github.com/openyurtio/openyurt/pkg/controller/staticpod/config"
)

// YurtControllerManagerConfiguration contains elements describing yurt-controller manager.
Expand All @@ -46,6 +47,9 @@ type YurtManagerConfiguration struct {

// GatewayControllerConfiguration holds configuration for GatewayController related features.
GatewayController gatewayconfig.GatewayControllerConfiguration

// StaticPodControllerConfiguration holds configuration for StaticPodController related features.
StaticPodController staticpodconfig.StaticPodControllerConfiguration
}

type GenericConfiguration struct {
Expand Down
21 changes: 21 additions & 0 deletions pkg/controller/staticpod/config/types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
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 config

// StaticPodControllerConfiguration contains elements describing GatewayController.
type StaticPodControllerConfiguration struct {
}
20 changes: 12 additions & 8 deletions pkg/controller/staticpod/staticpod_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@ import (
"sigs.k8s.io/controller-runtime/pkg/reconcile"
"sigs.k8s.io/controller-runtime/pkg/source"

appconfig "github.com/openyurtio/openyurt/cmd/yurt-manager/app/config"
appsv1alpha1 "github.com/openyurtio/openyurt/pkg/apis/apps/v1alpha1"
"github.com/openyurtio/openyurt/pkg/controller/staticpod/config"
"github.com/openyurtio/openyurt/pkg/controller/staticpod/upgradeinfo"
"github.com/openyurtio/openyurt/pkg/controller/staticpod/util"
utilclient "github.com/openyurtio/openyurt/pkg/util/client"
Expand Down Expand Up @@ -129,28 +131,30 @@ func Format(format string, args ...interface{}) string {

// Add creates a new StaticPod 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(mgr manager.Manager) error {
func Add(c *appconfig.CompletedConfig, mgr manager.Manager) error {
if !utildiscovery.DiscoverGVK(controllerKind) {
return nil
}
return add(mgr, newReconciler(mgr))
return add(mgr, newReconciler(c, mgr))
}

var _ reconcile.Reconciler = &ReconcileStaticPod{}

// ReconcileStaticPod reconciles a StaticPod object
type ReconcileStaticPod struct {
client.Client
scheme *runtime.Scheme
recorder record.EventRecorder
scheme *runtime.Scheme
recorder record.EventRecorder
Configration config.StaticPodControllerConfiguration
}

// newReconciler returns a new reconcile.Reconciler
func newReconciler(mgr manager.Manager) reconcile.Reconciler {
func newReconciler(c *appconfig.CompletedConfig, mgr manager.Manager) reconcile.Reconciler {
return &ReconcileStaticPod{
Client: utilclient.NewClientFromManager(mgr, controllerName),
scheme: mgr.GetScheme(),
recorder: mgr.GetEventRecorderFor(controllerName),
Client: utilclient.NewClientFromManager(mgr, controllerName),
scheme: mgr.GetScheme(),
recorder: mgr.GetEventRecorderFor(controllerName),
Configration: c.ComponentConfig.StaticPodController,
}
}

Expand Down
11 changes: 5 additions & 6 deletions pkg/controller/staticpod/staticpod_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,13 +94,12 @@ func TestReconcile(t *testing.T) {
nodes := prepareNodes()
instance := &appsv1alpha1.StaticPod{
ObjectMeta: metav1.ObjectMeta{
Name: "foo",
Name: TestStaticPodName,
Namespace: metav1.NamespaceDefault,
},
Spec: appsv1alpha1.StaticPodSpec{
StaticPodNamespace: metav1.NamespaceDefault,
StaticPodName: "nginx",
StaticPodManifest: "nginx",
Template: corev1.PodTemplateSpec{},
StaticPodManifest: "nginx",
Template: corev1.PodTemplateSpec{},
},
}

Expand All @@ -116,7 +115,7 @@ func TestReconcile(t *testing.T) {
instance.Spec.UpgradeStrategy = s
c := fakeclint.NewClientBuilder().WithScheme(scheme).WithRuntimeObjects(instance).WithObjects(staticPods...).WithObjects(nodes...).Build()

var req = reconcile.Request{NamespacedName: types.NamespacedName{Name: "foo"}}
var req = reconcile.Request{NamespacedName: types.NamespacedName{Namespace: metav1.NamespaceDefault, Name: TestStaticPodName}}
rsp := ReconcileStaticPod{
Client: c,
scheme: scheme,
Expand Down
13 changes: 6 additions & 7 deletions pkg/controller/staticpod/upgradeinfo/upgrade_info_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,26 +66,25 @@ func newPods(nodes []string, namespace string, isStaticPod bool) []client.Object

func newStaticPod() *appsv1alpha1.StaticPod {
return &appsv1alpha1.StaticPod{
TypeMeta: metav1.TypeMeta{},
ObjectMeta: metav1.ObjectMeta{},
Spec: appsv1alpha1.StaticPodSpec{
StaticPodName: fakeStaticPodName,
},
TypeMeta: metav1.TypeMeta{},
ObjectMeta: metav1.ObjectMeta{
Name: fakeStaticPodName,
Namespace: metav1.NamespaceDefault},
Spec: appsv1alpha1.StaticPodSpec{},
Status: appsv1alpha1.StaticPodStatus{},
}
}

func Test_ConstructStaticPodsUpgradeInfoList(t *testing.T) {
staticPods := newPods(fakeStaticPodNodes, metav1.NamespaceDefault, true)
workerPods := newPods(fakeWorkerPodNodes, metav1.NamespaceSystem, false)
workerPods := newPods(fakeWorkerPodNodes, metav1.NamespaceDefault, false)
expect := map[string]*UpgradeInfo{
"node1": {
StaticPod: staticPods[0].(*corev1.Pod),
WorkerPod: workerPods[0].(*corev1.Pod),
},
"node2": {
StaticPod: staticPods[1].(*corev1.Pod),

WorkerPod: workerPods[1].(*corev1.Pod),
},
"node3": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,9 @@ limitations under the License.
package webhook

import (
"github.com/openyurtio/openyurt/pkg/webhook/staticpod/mutating"
"github.com/openyurtio/openyurt/pkg/webhook/staticpod/validating"
"github.com/openyurtio/openyurt/pkg/webhook/staticpod/v1alpha1"
)

func init() {
addHandlers(mutating.HandlerMap)
addHandlers(validating.HandlerMap)
addWebhook(&v1alpha1.StaticPodHandler{})
}
Loading

0 comments on commit d1c63d7

Please sign in to comment.