-
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: support watching the collaset (#464)
- Loading branch information
Showing
6 changed files
with
283 additions
and
10 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,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 | ||
} |
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,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) | ||
} | ||
}) | ||
} | ||
} |
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,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 | ||
} |
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,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) | ||
} | ||
}) | ||
} | ||
} |