Skip to content

Commit

Permalink
feat: support watching the collaset (#464)
Browse files Browse the repository at this point in the history
  • Loading branch information
SparkYuan authored Aug 21, 2023
1 parent ced6793 commit 1132767
Show file tree
Hide file tree
Showing 6 changed files with 283 additions and 10 deletions.
11 changes: 3 additions & 8 deletions pkg/engine/printers/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,12 @@ import (
"kusionstack.io/kusion/pkg/engine/printers/convertor"
)

func init() {
registerConvertor(convertor.ToK8s)
registerConvertor(convertor.ToOAM)
}
var convertors []Convertor

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

var convertors []Convertor

func registerConvertor(c Convertor) {
convertors = append(convertors, c)
func init() {
convertors = []Convertor{convertor.ToK8s, convertor.ToKafed, convertor.ToOAM}
}

func Convert(o *unstructured.Unstructured) runtime.Object {
Expand Down
36 changes: 36 additions & 0 deletions pkg/engine/printers/convertor/kafed_convertor.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package convertor

import (
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
"kusionstack.io/kube-api/apps/v1alpha1"

"kusionstack.io/kusion/pkg/log"
)

const CollaSet = "CollaSet"

func ToKafed(u *unstructured.Unstructured) runtime.Object {
switch u.GroupVersionKind().GroupVersion() {
case v1alpha1.GroupVersion:
return convertV1alpha1(u)
default:
return nil
}
}

func convertV1alpha1(o *unstructured.Unstructured) runtime.Object {
var target runtime.Object
switch o.GetKind() {
case CollaSet:
target = &v1alpha1.CollaSet{}
default:
return nil
}

if err := runtime.DefaultUnstructuredConverter.FromUnstructured(o.Object, target); err != nil {
log.Errorf("convert obj to target error. obj:%v, target:%v", o.Object, target)
return nil
}
return target
}
101 changes: 101 additions & 0 deletions pkg/engine/printers/convertor/kafed_convertor_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
//go:build ignore
// +build ignore

// fixme
// ignore for test coverage temporary due to the strange coveralls test coverage computing rules
package convertor

import (
"reflect"
"testing"

"github.com/stretchr/testify/assert"
"gopkg.in/yaml.v3"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
"kusionstack.io/kube-api/apps/v1alpha1"
)

func TestToKafed(t *testing.T) {
csyaml := `apiVersion: apps.kusionstack.io/v1alpha1
kind: CollaSet
metadata:
name: foo
namespace: default
spec:
selector:
matchLabels:
app: foo
template:
metadata:
labels:
app: foo
spec:
containers:
- name: foo
image: nginx:v1
`

cs := &v1alpha1.CollaSet{
TypeMeta: metav1.TypeMeta{
Kind: "CollaSet",
APIVersion: "apps.kusionstack.io/v1alpha1",
},
ObjectMeta: metav1.ObjectMeta{
Namespace: "default",
Name: "foo",
},
Spec: v1alpha1.CollaSetSpec{
Selector: &metav1.LabelSelector{
MatchLabels: map[string]string{
"app": "foo",
},
},
Template: corev1.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{
Labels: map[string]string{
"app": "foo",
},
},
Spec: corev1.PodSpec{
Containers: []corev1.Container{
{
Name: "foo",
Image: "nginx:v1",
},
},
},
},
},
}

csmap := make(map[string]interface{})
err := yaml.Unmarshal([]byte(csyaml), csmap)
assert.NoError(t, err, "unmarshall yaml to map error")

type args struct {
o *unstructured.Unstructured
}
tests := []struct {
name string
args args
want runtime.Object
}{
{
name: "CollaSet",
args: args{
o: &unstructured.Unstructured{Object: csmap},
},
want: cs,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := ToKafed(tt.args.o); !reflect.DeepEqual(got, tt.want) {
t.Errorf("ToKafed() got = %v, want %v", got, tt.want)
}
})
}
}
3 changes: 1 addition & 2 deletions pkg/engine/printers/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ import (
var tg = printer.NewTableGenerator()

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

func Generate(obj runtime.Object) (string, bool) {
Expand Down
21 changes: 21 additions & 0 deletions pkg/engine/printers/printer/kafed_printer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package printer

import (
"fmt"

"kusionstack.io/kube-api/apps/v1alpha1"
)

func AddCollaSetHandlers(h PrintHandler) {
_ = h.TableHandler(printCollaSet)
}

func printCollaSet(obj *v1alpha1.CollaSet) (string, bool) {
desired := obj.Spec.Replicas
current := obj.Status.Replicas
updated := obj.Status.UpdatedReplicas
updatedReady := obj.Status.UpdatedReadyReplicas
updateAvailable := obj.Status.UpdatedAvailableReplicas
return fmt.Sprintf("Desired: %d, Current: %d, Updated: %d, UpdatedReady: %d, UpdatedAvailable: %d",
*desired, current, updated, updatedReady, updateAvailable), *desired == updateAvailable
}
121 changes: 121 additions & 0 deletions pkg/engine/printers/printer/kafed_printer_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
//go:build ignore
// +build ignore

// fixme
// ignore for test coverage temporary due to the strange coveralls test coverage computing rules
package printer

import (
"testing"

corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"kusionstack.io/kube-api/apps/v1alpha1"
)

func Test_printCollaSet(t *testing.T) {
replica := int32(2)

csReady := &v1alpha1.CollaSet{
TypeMeta: metav1.TypeMeta{
Kind: "CollaSet",
APIVersion: "apps.kusionstack.io/v1alpha1",
},
ObjectMeta: metav1.ObjectMeta{
Namespace: "default",
Name: "foo",
},
Spec: v1alpha1.CollaSetSpec{
Replicas: &replica,
Selector: &metav1.LabelSelector{
MatchLabels: map[string]string{
"app": "foo",
},
},
Template: corev1.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{
Labels: map[string]string{
"app": "foo",
},
},
Spec: corev1.PodSpec{
Containers: []corev1.Container{
{
Name: "foo",
Image: "nginx:v1",
},
},
},
},
},
Status: v1alpha1.CollaSetStatus{
UpdatedAvailableReplicas: 2,
},
}

csUnReady := &v1alpha1.CollaSet{
TypeMeta: metav1.TypeMeta{
Kind: "CollaSet",
APIVersion: "apps.kusionstack.io/v1alpha1",
},
ObjectMeta: metav1.ObjectMeta{
Namespace: "default",
Name: "foo",
},
Spec: v1alpha1.CollaSetSpec{
Replicas: &replica,
Selector: &metav1.LabelSelector{
MatchLabels: map[string]string{
"app": "foo",
},
},
Template: corev1.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{
Labels: map[string]string{
"app": "foo",
},
},
Spec: corev1.PodSpec{
Containers: []corev1.Container{
{
Name: "foo",
Image: "nginx:v1",
},
},
},
},
},
Status: v1alpha1.CollaSetStatus{
UpdatedAvailableReplicas: 1,
},
}

type args struct {
obj *v1alpha1.CollaSet
}
tests := []struct {
name string
args args
want bool
}{
{
name: "CollaSet Ready",
args: args{obj: csReady},
want: true,
},
{
name: "CollaSet UnReady",
args: args{obj: csUnReady},
want: false,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
_, got := printCollaSet(tt.args.obj)
if got != tt.want {
t.Errorf("printCollaSet() got = %v, want %v", got, tt.want)
}
})
}
}

0 comments on commit 1132767

Please sign in to comment.