Skip to content

Commit f4a96f5

Browse files
committed
fix patch
1 parent c2c1385 commit f4a96f5

1 file changed

Lines changed: 27 additions & 22 deletions

File tree

internal/controller/codeserverdeployment_controller.go

Lines changed: 27 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,17 @@ import (
2020
"context"
2121
"fmt"
2222

23+
csv1alpha1 "github.com/walnuts1018/code-server-operator/api/v1alpha1"
2324
"github.com/walnuts1018/code-server-operator/util/random"
2425
"k8s.io/apimachinery/pkg/api/errors"
26+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
27+
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
2528
"k8s.io/apimachinery/pkg/labels"
2629
"k8s.io/apimachinery/pkg/runtime"
30+
"k8s.io/utils/ptr"
2731
ctrl "sigs.k8s.io/controller-runtime"
2832
"sigs.k8s.io/controller-runtime/pkg/client"
29-
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
3033
"sigs.k8s.io/controller-runtime/pkg/log"
31-
32-
csv1alpha1 "github.com/walnuts1018/code-server-operator/api/v1alpha1"
3334
)
3435

3536
// CodeServerDeploymentReconciler reconciles a CodeServerDeployment object
@@ -125,32 +126,36 @@ func (r *CodeServerDeploymentReconciler) reconcileCodeServer(ctx context.Context
125126
codeServer.Name = codeServerDeployments.Name + "-" + suffix
126127
codeServer.Namespace = codeServerDeployments.Namespace
127128

128-
op, err := ctrl.CreateOrUpdate(ctx, r.Client, codeServer, func() error {
129-
codeServer.Spec = codeServerDeployments.Spec.Template.Spec
130-
131-
if codeServer.Labels == nil {
132-
codeServer.Labels = make(map[string]string)
133-
}
134-
135-
codeServer.Labels["app.kubernetes.io/name"] = CodeServer
136-
codeServer.Labels["app.kubernetes.io/instance"] = codeServer.Name
137-
codeServer.Labels["app.kubernetes.io/created-by"] = CodeServerManager
138-
codeServer.Labels["cs.walnuts.dev/codeserverdeployment"] = codeServerDeployments.Name
139-
140-
return ctrl.SetControllerReference(codeServerDeployments, codeServer, r.Scheme)
129+
patch := &unstructured.Unstructured{}
130+
patch.SetGroupVersionKind(csv1alpha1.GroupVersion.WithKind("CodeServer"))
131+
patch.SetNamespace(codeServerDeployments.Namespace)
132+
patch.SetName(codeServer.Name)
133+
patch.SetLabels(map[string]string{
134+
"app.kubernetes.io/name": CodeServer,
135+
"app.kubernetes.io/instance": codeServer.Name,
136+
"app.kubernetes.io/created-by": CodeServerManager,
137+
"cs.walnuts.dev/codeserverdeployment": codeServerDeployments.Name,
138+
})
139+
patch.UnstructuredContent()["spec"] = codeServerDeployments.Spec.Template.Spec
140+
patch.SetOwnerReferences([]metav1.OwnerReference{
141+
{
142+
APIVersion: codeServerDeployments.APIVersion,
143+
Kind: codeServerDeployments.Kind,
144+
Name: codeServerDeployments.Name,
145+
UID: codeServerDeployments.UID,
146+
Controller: func(b bool) *bool { return &b }(true),
147+
BlockOwnerDeletion: func(b bool) *bool { return &b }(true),
148+
},
141149
})
142150

143-
if err != nil {
144-
return fmt.Errorf("failed to reconcile CodeServer: %w", err)
145-
}
146-
147-
if op != controllerutil.OperationResultNone {
148-
logger.Info("Reconciled CodeServer", "operation", op)
151+
if err := r.Patch(ctx, patch, client.Apply, &client.PatchOptions{FieldManager: CodeServerManager, Force: ptr.To(true)}); err != nil {
152+
return fmt.Errorf("failed to apply CodeServer: %w", err)
149153
}
150154

151155
continue
152156
}
153157

158+
logger.Info("Reconcile CodeServer successfully")
154159
return nil
155160

156161
}

0 commit comments

Comments
 (0)