-
Notifications
You must be signed in to change notification settings - Fork 78
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: watch support kubevela Application from core.oam.dev/v1beta1 (#238
) * add Application API from core.oam.dev/v1beta1 both github.com/kubevela/kubevela or github.com/kubevela/kubevela-core-api has replace statement in go.mod, so copy them into third party * feat: watch support kubevela Application from core.oam.dev/v1beta1
- Loading branch information
Showing
18 changed files
with
1,144 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package printers | ||
|
||
import ( | ||
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" | ||
"k8s.io/apimachinery/pkg/runtime" | ||
) | ||
|
||
type Convertor func(o *unstructured.Unstructured) runtime.Object | ||
|
||
var convertors []Convertor | ||
|
||
func RegisterConvertor(c Convertor) { | ||
convertors = append(convertors, c) | ||
} | ||
|
||
func Convert(o *unstructured.Unstructured) runtime.Object { | ||
for _, c := range convertors { | ||
if target := c(o); target != nil { | ||
return target | ||
} | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package kubevela | ||
|
||
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" | ||
) | ||
|
||
// APIs in core.oam.dev/v1beta1 | ||
const ( | ||
Application = "Application" | ||
) | ||
|
||
func init() { | ||
printers.RegisterConvertor(Convert) | ||
} | ||
|
||
func Convert(o *unstructured.Unstructured) runtime.Object { | ||
switch o.GroupVersionKind().GroupVersion() { | ||
case oamv1beta1.SchemeGroupVersion: | ||
return convertOamV1beta1(o) | ||
default: | ||
return nil | ||
} | ||
} | ||
|
||
func convertOamV1beta1(o *unstructured.Unstructured) runtime.Object { | ||
var target runtime.Object | ||
switch o.GetKind() { | ||
case Application: | ||
target = &oamv1beta1.Application{} | ||
default: | ||
return nil | ||
} | ||
|
||
if err := runtime.DefaultUnstructuredConverter.FromUnstructured(o.Object, target); err != nil { | ||
return nil | ||
} | ||
return target | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package kubevela | ||
|
||
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) { | ||
h.TableHandler(printApplication) | ||
} | ||
|
||
func printApplication(obj *oamv1beta1.Application) (string, bool) { | ||
// Component and Type | ||
components := obj.Spec.Components | ||
componentNames := make([]string, len(components)) | ||
componentTypes := make([]string, len(components)) | ||
for i := range components { | ||
componentNames[i] = components[i].Name | ||
componentTypes[i] = components[i].Type | ||
} | ||
componentStr := strings.Join(componentNames, ",") | ||
typeStr := strings.Join(componentTypes, ",") | ||
|
||
// Phase | ||
phase := obj.Status.Phase | ||
|
||
// Healthy and Status | ||
services := obj.Status.Services | ||
serviceHealths := make([]string, len(services)) | ||
serviceStatuses := make([]string, len(services)) | ||
for i := range services { | ||
serviceHealths[i] = strconv.FormatBool(services[i].Healthy) | ||
serviceStatuses[i] = services[i].Message | ||
} | ||
healthyStr := strings.Join(serviceHealths, ",") | ||
statusStr := strings.Join(serviceStatuses, ",") | ||
|
||
// Age | ||
age := translateTimestampSince(obj.CreationTimestamp) | ||
|
||
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)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
/* | ||
Copyright 2022 The KubeVela Authors. | ||
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. | ||
*/ | ||
|
||
// Package kubevela contains `Application` API | ||
// which is copied from [kubevela](https://github.com/oam-dev/kubevela) | ||
// and [workflow](https://github.com/kubevela/workflow). | ||
package kubevela |
Oops, something went wrong.