Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: webhook overwrite deployment owned by yad/yas #1851

Merged
merged 1 commit into from
Dec 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
}

// ApplyTemplate updates the object to the latest revision, depending on the YurtAppDaemon.
func (d *DeploymentControllor) applyTemplate(scheme *runtime.Scheme, yad *v1alpha1.YurtAppDaemon, nodepool v1alpha1.NodePool, revision string, set *appsv1.Deployment) error {
func (d *DeploymentControllor) ApplyTemplate(scheme *runtime.Scheme, yad *v1alpha1.YurtAppDaemon, nodepool v1alpha1.NodePool, revision string, set *appsv1.Deployment) error {

if set.Labels == nil {
set.Labels = map[string]string{}
Expand Down Expand Up @@ -129,7 +129,7 @@
return getError
}

if err := d.applyTemplate(d.Scheme, yad, nodepool, revision, deploy); err != nil {
if err := d.ApplyTemplate(d.Scheme, yad, nodepool, revision, deploy); err != nil {

Check warning on line 132 in pkg/yurtmanager/controller/yurtappdaemon/workloadcontroller/deployment_controller.go

View check run for this annotation

Codecov / codecov/patch

pkg/yurtmanager/controller/yurtappdaemon/workloadcontroller/deployment_controller.go#L132

Added line #L132 was not covered by tests
return err
}
updateError = d.Client.Update(context.TODO(), deploy)
Expand All @@ -145,7 +145,7 @@
klog.Infof("YurtAppDaemon[%s/%s] prepare create new deployment by nodepool %s ", yad.GetNamespace(), yad.GetName(), nodepool.GetName())

deploy := appsv1.Deployment{}
if err := d.applyTemplate(d.Scheme, yad, nodepool, revision, &deploy); err != nil {
if err := d.ApplyTemplate(d.Scheme, yad, nodepool, revision, &deploy); err != nil {
klog.Errorf("YurtAppDaemon[%s/%s] could not apply template, when create deployment: %v", yad.GetNamespace(),
yad.GetName(), err)
return err
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ func TestApplyTemplate(t *testing.T) {

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
get := dc.applyTemplate(tt.scheme, tt.yad, tt.nodepool, tt.revision, tt.set)
get := dc.ApplyTemplate(tt.scheme, tt.yad, tt.nodepool, tt.revision, tt.set)
t.Logf("expect: %v, get: %v", tt.expect, get)
if !reflect.DeepEqual(get, tt.expect) {
t.Fatalf("\t%s\texpect %v, but get %v", failed, tt.expect, get)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
"sigs.k8s.io/controller-runtime/pkg/client"

"github.com/openyurtio/openyurt/pkg/apis/apps/v1alpha1"
"github.com/openyurtio/openyurt/pkg/yurtmanager/controller/yurtappdaemon/workloadcontroller"
"github.com/openyurtio/openyurt/pkg/yurtmanager/controller/yurtappset/adapter"
)

Expand Down Expand Up @@ -57,6 +58,16 @@
if !contain(deployment.OwnerReferences[0].Kind, resources) {
return nil
}

// Get nodepool of deployment
np := &v1alpha1.NodePool{}
npName := deployment.Labels["apps.openyurt.io/pool-name"]
if err := webhook.Client.Get(ctx, client.ObjectKey{
Name: npName,
}, np); err != nil {
return err
}

Check warning on line 69 in pkg/yurtmanager/webhook/deploymentrender/v1alpha1/deploymentrender_default.go

View check run for this annotation

Codecov / codecov/patch

pkg/yurtmanager/webhook/deploymentrender/v1alpha1/deploymentrender_default.go#L68-L69

Added lines #L68 - L69 were not covered by tests

// Get YurtAppSet/YurtAppDaemon resource of this deployment
app := deployment.OwnerReferences[0]
var instance client.Object
Expand All @@ -74,10 +85,8 @@
}, instance); err != nil {
return err
}
// Get nodepool of deployment
nodepool := deployment.Labels["apps.openyurt.io/pool-name"]

// resume deployment
// restore deployment
switch app.Kind {
case "YurtAppSet":
var replicas int32
Expand All @@ -86,21 +95,29 @@
if yas.Spec.WorkloadTemplate.DeploymentTemplate != nil && yas.Spec.WorkloadTemplate.DeploymentTemplate.Spec.Replicas != nil {
replicas = *yas.Spec.WorkloadTemplate.DeploymentTemplate.Spec.Replicas
}
deploymentAdapter := adapter.DeploymentAdapter{
yasDeployController := adapter.DeploymentAdapter{
Client: webhook.Client,
Scheme: webhook.Scheme,
}
for _, pool := range yas.Spec.Topology.Pools {
if pool.Name == nodepool {
if pool.Name == npName {
replicas = *pool.Replicas
}
}
if err := deploymentAdapter.ApplyPoolTemplate(yas, nodepool, revision, replicas, deployment); err != nil {
if err := yasDeployController.ApplyPoolTemplate(yas, npName, revision, replicas, deployment); err != nil {
return err
}
case "YurtAppDaemon":
yad := instance.(*v1alpha1.YurtAppDaemon)
yad.Spec.WorkloadTemplate.DeploymentTemplate.Spec.DeepCopyInto(&deployment.Spec)
revision := yad.Status.CurrentRevision
yadDeployController := workloadcontroller.DeploymentControllor{
Client: webhook.Client,
Scheme: webhook.Scheme,
}
if err := yadDeployController.ApplyTemplate(webhook.Scheme, yad, *np, revision, deployment); err != nil {
return err
}

Check warning on line 119 in pkg/yurtmanager/webhook/deploymentrender/v1alpha1/deploymentrender_default.go

View check run for this annotation

Codecov / codecov/patch

pkg/yurtmanager/webhook/deploymentrender/v1alpha1/deploymentrender_default.go#L118-L119

Added lines #L118 - L119 were not covered by tests

}

// Get YurtAppOverrider resource of app(1 to 1)
Expand All @@ -125,16 +142,16 @@

for _, entry := range render.Entries {
for _, pool := range entry.Pools {
if pool[0] == '-' && pool[1:] == nodepool {
if pool[0] == '-' && pool[1:] == npName {
continue
}
if pool == nodepool || pool == "*" {
if pool == npName || pool == "*" {
// Replace items
replaceItems(deployment, entry.Items)
// json patch
for i, patch := range entry.Patches {
if strings.Contains(string(patch.Value.Raw), "{{nodepool}}") {
newPatchString := strings.ReplaceAll(string(patch.Value.Raw), "{{nodepool}}", nodepool)
newPatchString := strings.ReplaceAll(string(patch.Value.Raw), "{{nodepool}}", npName)
entry.Patches[i].Value = apiextensionsv1.JSON{Raw: []byte(newPatchString)}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,13 @@ var defaultAppSet = &v1alpha1.YurtAppSet{
},
}

var defaultNodePool = &v1alpha1.NodePool{
ObjectMeta: metav1.ObjectMeta{
Name: "nodepool-test",
},
Spec: v1alpha1.NodePoolSpec{},
}

var defaultAppDaemon = &v1alpha1.YurtAppDaemon{
ObjectMeta: metav1.ObjectMeta{
Name: "yurtappdaemon",
Expand Down Expand Up @@ -312,7 +319,7 @@ func TestDeploymentRenderHandler_Default(t *testing.T) {
for _, tcase := range tcases {
t.Run("", func(t *testing.T) {
webhook := &DeploymentRenderHandler{
Client: fakeclient.NewClientBuilder().WithScheme(scheme).WithObjects(defaultAppSet, daemonDeployment, defaultDeployment, defaultAppDaemon, tcase.overrider).Build(),
Client: fakeclient.NewClientBuilder().WithScheme(scheme).WithObjects(defaultAppSet, daemonDeployment, defaultNodePool, defaultDeployment, defaultAppDaemon, tcase.overrider).Build(),
Scheme: scheme,
}
if err := webhook.Default(context.TODO(), defaultDeployment); err != nil {
Expand Down
Loading