Skip to content

Commit

Permalink
chore: refactor printers code structure (#257)
Browse files Browse the repository at this point in the history
- make Convert() and Generate() public
- avoid import cycle
  • Loading branch information
howieyuen committed Feb 24, 2023
1 parent ca7a644 commit 2a26fec
Show file tree
Hide file tree
Showing 9 changed files with 52 additions and 65 deletions.
2 changes: 1 addition & 1 deletion pkg/engine/operation/watch.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ func (wo *WatchOperation) Watch(req *WatchRequest) error {
} else {
// Restore to actual type
target := printers.Convert(o)
detail, ready = printers.TG.GenerateTable(target)
detail, ready = printers.Generate(target)
}

// Mark ready for breaking loop
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,20 @@ package printers
import (
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"

"kusionstack.io/kusion/pkg/engine/printers/convertor"
)

func init() {
registerConvertor(convertor.ToK8s)
registerConvertor(convertor.ToOAM)
}

type Convertor func(o *unstructured.Unstructured) runtime.Object

var convertors []Convertor

func RegisterConvertor(c Convertor) {
func registerConvertor(c Convertor) {
convertors = append(convertors, c)
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package k8s
package convertor

import (
appsv1 "k8s.io/api/apps/v1"
Expand All @@ -7,8 +7,6 @@ import (
discoveryv1 "k8s.io/api/discovery/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"

"kusionstack.io/kusion/pkg/engine/printers"
)

// APIs in core/v1
Expand Down Expand Up @@ -50,11 +48,7 @@ const (
EndpointSlice = "EndpointSlice"
)

func init() {
printers.RegisterConvertor(Convert)
}

func Convert(o *unstructured.Unstructured) runtime.Object {
func ToK8s(o *unstructured.Unstructured) runtime.Object {
switch o.GroupVersionKind().GroupVersion() {
case corev1.SchemeGroupVersion:
return convertCoreV1(o)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package kubevela
package convertor

import (
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"

"kusionstack.io/kusion/pkg/engine/printers"
oamv1beta1 "kusionstack.io/kusion/third_party/kubevela/kubevela/apis/v1beta1"
)

Expand All @@ -13,11 +12,7 @@ const (
Application = "Application"
)

func init() {
printers.RegisterConvertor(Convert)
}

func Convert(o *unstructured.Unstructured) runtime.Object {
func ToOAM(o *unstructured.Unstructured) runtime.Object {
switch o.GroupVersionKind().GroupVersion() {
case oamv1beta1.SchemeGroupVersion:
return convertOamV1beta1(o)
Expand Down
18 changes: 18 additions & 0 deletions pkg/engine/printers/generate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package printers

import (
"k8s.io/apimachinery/pkg/runtime"

"kusionstack.io/kusion/pkg/engine/printers/printer"
)

var tg = printer.NewTableGenerator()

func init() {
tg.With(printer.AddK8sHandlers)
tg.With(printer.AddOAMHandlers)
}

func Generate(obj runtime.Object) (string, bool) {
return tg.GenerateTable(obj)
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package k8s
package printer

import (
"bytes"
Expand All @@ -19,8 +19,6 @@ import (
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/util/duration"
"k8s.io/apimachinery/pkg/util/sets"

"kusionstack.io/kusion/pkg/engine/printers"
)

const (
Expand All @@ -34,13 +32,9 @@ const (
nodeLabelRole = "kubernetes.io/role"
)

func init() {
printers.TG.With(AddHandlers)
}

// AddHandlers adds print handlers for default Kubernetes types dealing with internal versions.
// TODO: handle errors from Handler
func AddHandlers(h printers.PrintHandler) {
func AddK8sHandlers(h PrintHandler) {
// core/v1
h.TableHandler(printComponentStatus)
h.TableHandler(printConfigMap)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,14 @@
package kubevela
package printer

import (
"fmt"
"strconv"
"strings"
"time"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/duration"

"kusionstack.io/kusion/pkg/engine/printers"
oamv1beta1 "kusionstack.io/kusion/third_party/kubevela/kubevela/apis/v1beta1"
)

func init() {
printers.TG.With(AddHandlers)
}

func AddHandlers(h printers.PrintHandler) {
func AddOAMHandlers(h PrintHandler) {
h.TableHandler(printApplication)
}

Expand Down Expand Up @@ -53,13 +44,3 @@ func printApplication(obj *oamv1beta1.Application) (string, bool) {
return fmt.Sprintf("Component: %s, Type: %s, Phase: %s, Healthy: %s, Status: %s, Age: %s",
componentStr, typeStr, phase, healthyStr, statusStr, age), phase == "running" && obj.Status.Workflow.Finished
}

// translateTimestampSince returns the elapsed time since timestamp in
// human-readable approximation.
func translateTimestampSince(timestamp metav1.Time) string {
if timestamp.IsZero() {
return "<unknown>"
}

return duration.HumanDuration(time.Since(timestamp.Time))
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/
// ref: k8s.io/kubernetes/pkg/printers/tablegenerator.go
package printers
package printer

import (
"fmt"
Expand All @@ -24,8 +24,6 @@ import (
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
)

var TG = NewTableGenerator()

// TableGenerator - an interface for generating a message and ready flag provided a runtime.Object
type TableGenerator interface {
GenerateTable(obj runtime.Object) (string, bool)
Expand Down
32 changes: 16 additions & 16 deletions pkg/engine/runtime/kubernetes/kubernetes_runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import (

"kusionstack.io/kusion/pkg/engine"
"kusionstack.io/kusion/pkg/engine/models"
"kusionstack.io/kusion/pkg/engine/printers/k8s"
"kusionstack.io/kusion/pkg/engine/printers/convertor"
"kusionstack.io/kusion/pkg/engine/runtime"
"kusionstack.io/kusion/pkg/log"
"kusionstack.io/kusion/pkg/status"
Expand Down Expand Up @@ -226,7 +226,7 @@ func (k *KubernetesRuntime) Import(ctx context.Context, request *runtime.ImportR
}
} else {
// normalize resources
if k8s.Service == ur.GetKind() {
if convertor.Service == ur.GetKind() {
if err := normalizeService(ur); err != nil {
return &runtime.ImportResponse{
Resource: nil,
Expand Down Expand Up @@ -319,11 +319,11 @@ func (k *KubernetesRuntime) Watch(ctx context.Context, request *runtime.WatchReq
watchers := runtime.NewWatchers()
watchers.Insert(engine.BuildIDForKubernetes(reqObj), rootCh)

if reqObj.GetKind() == k8s.Service { // Watch Endpoints or EndpointSlice
if reqObj.GetKind() == convertor.Service { // Watch Endpoints or EndpointSlice
if gvk, err := k.mapper.KindFor(schema.GroupVersionResource{
Group: discoveryv1.SchemeGroupVersion.Group,
Version: discoveryv1.SchemeGroupVersion.Version,
Resource: k8s.EndpointSlice,
Resource: convertor.EndpointSlice,
}); gvk.Empty() || err != nil { // Watch Endpoints
log.Errorf("k8s runtime has no kind for EndpointSlice, err: %v", err)
namedGVK := getNamedGVK(reqObj.GroupVersionKind())
Expand Down Expand Up @@ -570,39 +570,39 @@ func namedBy(ep, svc *unstructured.Unstructured) bool {
func getDependentGVK(gvk schema.GroupVersionKind) schema.GroupVersionKind {
switch gvk.Kind {
// Deployment generates ReplicaSet
case k8s.Deployment:
case convertor.Deployment:
return schema.GroupVersionKind{
Group: appsv1.SchemeGroupVersion.Group,
Version: appsv1.SchemeGroupVersion.Version,
Kind: k8s.ReplicaSet,
Kind: convertor.ReplicaSet,
}
// DaemonSet and StatefulSet generate ControllerRevision
case k8s.DaemonSet, k8s.StatefulSet:
case convertor.DaemonSet, convertor.StatefulSet:
return schema.GroupVersionKind{
Group: appsv1.SchemeGroupVersion.Group,
Version: appsv1.SchemeGroupVersion.Version,
Kind: k8s.ControllerRevision,
Kind: convertor.ControllerRevision,
}
// CronJob generates Job
case k8s.CronJob:
case convertor.CronJob:
return schema.GroupVersionKind{
Group: batchv1.SchemeGroupVersion.Group,
Version: batchv1.SchemeGroupVersion.Version,
Kind: k8s.Job,
Kind: convertor.Job,
}
// ReplicaSet, ReplicationController, ControllerRevision and job generate Pod
case k8s.ReplicaSet, k8s.Job, k8s.ReplicationController, k8s.ControllerRevision:
case convertor.ReplicaSet, convertor.Job, convertor.ReplicationController, convertor.ControllerRevision:
return schema.GroupVersionKind{
Group: corev1.SchemeGroupVersion.Group,
Version: corev1.SchemeGroupVersion.Version,
Kind: k8s.Pod,
Kind: convertor.Pod,
}
// Service is the owner of EndpointSlice
case k8s.Service:
case convertor.Service:
return schema.GroupVersionKind{
Group: discoveryv1.SchemeGroupVersion.Group,
Version: discoveryv1.SchemeGroupVersion.Version,
Kind: k8s.EndpointSlice,
Kind: convertor.EndpointSlice,
}
default:
return schema.GroupVersionKind{}
Expand All @@ -611,11 +611,11 @@ func getDependentGVK(gvk schema.GroupVersionKind) schema.GroupVersionKind {

func getNamedGVK(gvk schema.GroupVersionKind) schema.GroupVersionKind {
switch gvk.Kind {
case k8s.Service:
case convertor.Service:
return schema.GroupVersionKind{
Group: corev1.SchemeGroupVersion.Group,
Version: corev1.SchemeGroupVersion.Version,
Kind: k8s.Endpoints,
Kind: convertor.Endpoints,
}
default:
return schema.GroupVersionKind{}
Expand Down

0 comments on commit 2a26fec

Please sign in to comment.