Skip to content

Commit

Permalink
add additionalPrinterColumns for devbox. (labring#5020)
Browse files Browse the repository at this point in the history
* add additionalPrinterColumns for devbox.

* add ResourceVersionChangedPredicate for devbox pod change status to reconcile devbox.
  • Loading branch information
lingdie authored Aug 31, 2024
1 parent e3c9b62 commit 3d62549
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 11 deletions.
5 changes: 5 additions & 0 deletions controllers/devbox/api/v1alpha1/devbox_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,11 @@ type DevboxStatus struct {

// +kubebuilder:object:root=true
// +kubebuilder:subresource:status
// +kubebuilder:printcolumn:name="State",type="string",JSONPath=".spec.state"
// +kubebuilder:printcolumn:name="RuntimeRef",type="string",JSONPath=".spec.runtimeRef.name"
// +kubebuilder:printcolumn:name="PodPhase",type="string",JSONPath=".status.podPhase"
// +kubebuilder:printcolumn:name="NetworkType",type="string",JSONPath=".status.network.type"
// +kubebuilder:printcolumn:name="NodePort",type="integer",JSONPath=".status.network.nodePort"

// Devbox is the Schema for the devboxes API
type Devbox struct {
Expand Down
19 changes: 17 additions & 2 deletions controllers/devbox/config/crd/bases/devbox.sealos.io_devboxes.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,23 @@ spec:
singular: devbox
scope: Namespaced
versions:
- name: v1alpha1
- additionalPrinterColumns:
- jsonPath: .spec.state
name: State
type: string
- jsonPath: .spec.runtimeRef.name
name: RuntimeRef
type: string
- jsonPath: .status.podPhase
name: PodPhase
type: string
- jsonPath: .status.network.type
name: NetworkType
type: string
- jsonPath: .status.network.nodePort
name: NodePort
type: integer
name: v1alpha1
schema:
openAPIV3Schema:
description: Devbox is the Schema for the devboxes API
Expand Down Expand Up @@ -131,7 +147,6 @@ spec:
- state
type: object
status:
description: DevboxStatus defines the observed state of Devbox
properties:
commitHistory:
items:
Expand Down
3 changes: 1 addition & 2 deletions controllers/devbox/config/rbac/role.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ rules:
resources:
- events
verbs:
- create
- patch
- '*'
- apiGroups:
- ""
resources:
Expand Down
38 changes: 32 additions & 6 deletions controllers/devbox/deploy/manifests/deploy.yaml.tmpl
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
# 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: v1
kind: Namespace
metadata:
Expand All @@ -22,7 +36,23 @@ spec:
singular: devbox
scope: Namespaced
versions:
- name: v1alpha1
- additionalPrinterColumns:
- jsonPath: .spec.state
name: State
type: string
- jsonPath: .spec.runtimeRef.name
name: RuntimeRef
type: string
- jsonPath: .status.podPhase
name: PodPhase
type: string
- jsonPath: .status.network.type
name: NetworkType
type: string
- jsonPath: .status.network.nodePort
name: NodePort
type: integer
name: v1alpha1
schema:
openAPIV3Schema:
description: Devbox is the Schema for the devboxes API
Expand Down Expand Up @@ -576,11 +606,7 @@ rules:
resources:
- events
verbs:
- create
- delete
- get
- list
- patch
- '*'
- apiGroups:
- ""
resources:
Expand Down
10 changes: 9 additions & 1 deletion controllers/devbox/internal/controller/devbox_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,12 @@ import (
"k8s.io/client-go/util/retry"
"k8s.io/utils/ptr"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/builder"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
"sigs.k8s.io/controller-runtime/pkg/handler"
"sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/predicate"
)

const (
Expand All @@ -63,7 +66,7 @@ type DevboxReconciler struct {
// +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
// +kubebuilder:rbac:groups="",resources=events,verbs=*

func (r *DevboxReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
logger := log.FromContext(ctx, "devbox", req.NamespacedName)
Expand Down Expand Up @@ -586,5 +589,10 @@ func (r *DevboxReconciler) SetupWithManager(mgr ctrl.Manager) error {
For(&devboxv1alpha1.Devbox{}).
Owns(&corev1.Pod{}).
Owns(&corev1.Service{}).
Watches(
&corev1.Pod{},
handler.EnqueueRequestForOwner(mgr.GetScheme(), mgr.GetRESTMapper(), &devboxv1alpha1.Devbox{}),
builder.WithPredicates(predicate.ResourceVersionChangedPredicate{}),
).
Complete(r)
}

0 comments on commit 3d62549

Please sign in to comment.