Skip to content

Commit

Permalink
add retry logic when get service, fix rbac.
Browse files Browse the repository at this point in the history
  • Loading branch information
lingdie committed Aug 31, 2024
1 parent d2eb60c commit 9204815
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 58 deletions.
4 changes: 4 additions & 0 deletions controllers/devbox/config/manager/manager.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,10 @@ spec:
args:
- --leader-elect
- --health-probe-bind-address=:8081
- --registry-addr={{ .registryAddr }}
- --registry-user={{ .registryUser }}
- --registry-password={{ .registryPassword }}
- --auth-addr={{ .authAddr }}
image: controller:latest
name: manager
securityContext:
Expand Down
43 changes: 9 additions & 34 deletions controllers/devbox/config/rbac/role.yaml
Original file line number Diff line number Diff line change
@@ -1,17 +1,3 @@
# Copyright © 2024 sealos.
#
# 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.

---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
Expand All @@ -21,15 +7,16 @@ rules:
- apiGroups:
- ""
resources:
- pods
- events
verbs:
- create
- delete
- get
- list
- patch
- update
- watch
- apiGroups:
- ""
resources:
- pods
verbs:
- '*'
- apiGroups:
- ""
resources:
Expand All @@ -43,25 +30,13 @@ rules:
resources:
- secrets
verbs:
- create
- delete
- get
- list
- patch
- update
- watch
- '*'
- apiGroups:
- ""
resources:
- services
verbs:
- create
- delete
- get
- list
- patch
- update
- watch
- '*'
- apiGroups:
- devbox.sealos.io
resources:
Expand Down
26 changes: 9 additions & 17 deletions controllers/devbox/deploy/manifests/deploy.yaml.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -574,15 +574,19 @@ rules:
- apiGroups:
- ""
resources:
- pods
- events
verbs:
- create
- delete
- get
- list
- patch
- update
- watch
- apiGroups:
- ""
resources:
- pods
verbs:
- '*'
- apiGroups:
- ""
resources:
Expand All @@ -596,25 +600,13 @@ rules:
resources:
- secrets
verbs:
- create
- delete
- get
- list
- patch
- update
- watch
- '*'
- apiGroups:
- ""
resources:
- services
verbs:
- create
- delete
- get
- list
- patch
- update
- watch
- '*'
- apiGroups:
- devbox.sealos.io
resources:
Expand Down
30 changes: 23 additions & 7 deletions controllers/devbox/internal/controller/devbox_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (
"k8s.io/apimachinery/pkg/util/intstr"
"k8s.io/apimachinery/pkg/util/rand"
"k8s.io/client-go/tools/record"
"k8s.io/client-go/util/retry"
"k8s.io/utils/ptr"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
Expand All @@ -58,10 +59,11 @@ type DevboxReconciler struct {
// +kubebuilder:rbac:groups=devbox.sealos.io,resources=devboxes,verbs=get;list;watch;create;update;patch;delete
// +kubebuilder:rbac:groups=devbox.sealos.io,resources=devboxes/status,verbs=get;update;patch
// +kubebuilder:rbac:groups=devbox.sealos.io,resources=devboxes/finalizers,verbs=update
// +kubebuilder:rbac:groups=core,resources=pods,verbs=get;list;watch;create;update;patch;delete
// +kubebuilder:rbac:groups=core,resources=pods/status,verbs=get;update;patch
// +kubebuilder:rbac:groups=core,resources=services,verbs=get;list;watch;create;update;patch;delete
// +kubebuilder:rbac:groups=core,resources=secrets,verbs=get;list;watch;create;update;patch;delete
// +kubebuilder:rbac:groups="",resources=pods,verbs=*
// +kubebuilder:rbac:groups="",resources=pods/status,verbs=get;update;patch
// +kubebuilder:rbac:groups="",resources=services,verbs=*
// +kubebuilder:rbac:groups="",resources=secrets,verbs=*
// +kubebuilder:rbac:groups="",resources=events,verbs=create;patch

func (r *DevboxReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
logger := log.FromContext(ctx, "devbox", req.NamespacedName)
Expand All @@ -84,14 +86,17 @@ func (r *DevboxReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctr
}
} else {
if devbox.Spec.State == devboxv1alpha1.DevboxStateRunning {
logger.Info("devbox deleted, set devbox state to stopped")
devbox.Spec.State = devboxv1alpha1.DevboxStateStopped
return ctrl.Result{}, r.Update(ctx, devbox)
}

logger.Info("devbox deleted, remove all resources")
if err := r.removeAll(ctx, devbox, recLabels); err != nil {
return ctrl.Result{}, err
}

logger.Info("devbox deleted, remove finalizer")
if controllerutil.RemoveFinalizer(devbox, FinalizerName) {
if err := r.Update(ctx, devbox); err != nil {
return ctrl.Result{}, err
Expand All @@ -103,12 +108,15 @@ func (r *DevboxReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctr
_ = r.Status().Update(ctx, devbox)

// create or update secret
logger.Info("create or update secret", "devbox", devbox.Name)
if err := r.syncSecret(ctx, devbox, recLabels); err != nil {
logger.Error(err, "create or update secret failed")
r.Recorder.Eventf(devbox, corev1.EventTypeWarning, "Create secret failed", "%v", err)
return ctrl.Result{}, err
}

// create or update pod
logger.Info("create or update pod", "devbox", devbox.Name)
if err := r.syncPod(ctx, devbox, recLabels); err != nil {
logger.Error(err, "sync pod failed")
r.Recorder.Eventf(devbox, corev1.EventTypeWarning, "Sync pod failed", "%v", err)
Expand All @@ -117,12 +125,14 @@ func (r *DevboxReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctr

// create service if network type is NodePort
if devbox.Spec.NetworkSpec.Type == devboxv1alpha1.NetworkTypeNodePort {
logger.Info("create service", "devbox", devbox.Name)
if err := r.syncService(ctx, devbox, recLabels); err != nil {
logger.Error(err, "Create service failed")
r.Recorder.Eventf(devbox, corev1.EventTypeWarning, "Create service failed", "%v", err)
return ctrl.Result{RequeueAfter: time.Second * 3}, err
}
}
logger.Info("create devbox success", "devbox", devbox.Name)
r.Recorder.Eventf(devbox, corev1.EventTypeNormal, "Created", "create devbox success: %v", devbox.ObjectMeta.Name)
return ctrl.Result{Requeue: false}, nil
}
Expand Down Expand Up @@ -449,7 +459,7 @@ func (r *DevboxReconciler) getLastSuccessCommitImageName(ctx context.Context, de
if err := r.Get(ctx, client.ObjectKey{Namespace: devbox.Namespace, Name: devbox.Spec.RuntimeRef.Name}, rt); err != nil {
return "", err
}
if devbox.Status.CommitHistory == nil || len(devbox.Status.CommitHistory) == 0 {
if len(devbox.Status.CommitHistory) == 0 {
return rt.Spec.Image, nil
}
// get image name from commit history, ues the latest commit history
Expand Down Expand Up @@ -503,8 +513,14 @@ func (r *DevboxReconciler) syncService(ctx context.Context, devbox *devboxv1alph

// Retrieve the updated Service to get the NodePort
var updatedService corev1.Service
if err := r.Client.Get(ctx, client.ObjectKey{Namespace: service.Namespace, Name: service.Name}, &updatedService); err != nil {
return err
err := retry.OnError(
retry.DefaultRetry,
func(err error) bool { return client.IgnoreNotFound(err) == nil },
func() error {
return r.Client.Get(ctx, client.ObjectKey{Namespace: service.Namespace, Name: service.Name}, &updatedService)
})
if err != nil {
return fmt.Errorf("failed to get updated service: %w", err)
}

// Extract the NodePort
Expand Down

0 comments on commit 9204815

Please sign in to comment.