Describe sts on rollout history if the revision
Signed-off-by: Dinesh <dineshudt17@gmail.com>
This commit is contained in:
parent
22772d06a8
commit
2749dbdc9a
@ -95,6 +95,7 @@ go_test(
|
|||||||
"//staging/src/k8s.io/apimachinery/pkg/runtime/schema:go_default_library",
|
"//staging/src/k8s.io/apimachinery/pkg/runtime/schema:go_default_library",
|
||||||
"//staging/src/k8s.io/apimachinery/pkg/types:go_default_library",
|
"//staging/src/k8s.io/apimachinery/pkg/types:go_default_library",
|
||||||
"//staging/src/k8s.io/apimachinery/pkg/util/diff:go_default_library",
|
"//staging/src/k8s.io/apimachinery/pkg/util/diff:go_default_library",
|
||||||
|
"//staging/src/k8s.io/apimachinery/pkg/util/json:go_default_library",
|
||||||
"//staging/src/k8s.io/apimachinery/pkg/watch:go_default_library",
|
"//staging/src/k8s.io/apimachinery/pkg/watch:go_default_library",
|
||||||
"//staging/src/k8s.io/client-go/kubernetes/fake:go_default_library",
|
"//staging/src/k8s.io/client-go/kubernetes/fake:go_default_library",
|
||||||
"//staging/src/k8s.io/client-go/testing:go_default_library",
|
"//staging/src/k8s.io/client-go/testing:go_default_library",
|
||||||
|
@ -233,9 +233,8 @@ type StatefulSetHistoryViewer struct {
|
|||||||
|
|
||||||
// ViewHistory returns a list of the revision history of a statefulset
|
// ViewHistory returns a list of the revision history of a statefulset
|
||||||
// TODO: this should be a describer
|
// TODO: this should be a describer
|
||||||
// TODO: needs to implement detailed revision view
|
|
||||||
func (h *StatefulSetHistoryViewer) ViewHistory(namespace, name string, revision int64) (string, error) {
|
func (h *StatefulSetHistoryViewer) ViewHistory(namespace, name string, revision int64) (string, error) {
|
||||||
_, history, err := statefulSetHistory(h.c.AppsV1(), namespace, name)
|
sts, history, err := statefulSetHistory(h.c.AppsV1(), namespace, name)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
@ -243,9 +242,26 @@ func (h *StatefulSetHistoryViewer) ViewHistory(namespace, name string, revision
|
|||||||
if len(history) <= 0 {
|
if len(history) <= 0 {
|
||||||
return "No rollout history found.", nil
|
return "No rollout history found.", nil
|
||||||
}
|
}
|
||||||
revisions := make([]int64, 0, len(history))
|
|
||||||
for _, revision := range history {
|
historyInfo := make(map[int64]*appsv1.ControllerRevision)
|
||||||
revisions = append(revisions, revision.Revision)
|
for _, h := range history {
|
||||||
|
historyInfo[h.Revision] = h
|
||||||
|
}
|
||||||
|
if revision != 0 {
|
||||||
|
value, ok := historyInfo[revision]
|
||||||
|
if !ok {
|
||||||
|
return "", fmt.Errorf("unable to find the specified revision")
|
||||||
|
}
|
||||||
|
sts, err := applyStatefulSetHistory(sts, value)
|
||||||
|
if err != nil {
|
||||||
|
return "", fmt.Errorf("error unmarshelling sts: %w", err)
|
||||||
|
}
|
||||||
|
return printTemplate(&sts.Spec.Template)
|
||||||
|
}
|
||||||
|
|
||||||
|
revisions := make([]int64, 0, len(historyInfo))
|
||||||
|
for revision := range historyInfo {
|
||||||
|
revisions = append(revisions, revision)
|
||||||
}
|
}
|
||||||
sliceutil.SortInts64(revisions)
|
sliceutil.SortInts64(revisions)
|
||||||
|
|
||||||
@ -365,6 +381,24 @@ func applyDaemonSetHistory(ds *appsv1.DaemonSet, history *appsv1.ControllerRevis
|
|||||||
return result, nil
|
return result, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// applyStatefulSetHistory returns a specific revision of StatefulSet by applying the given history to a copy of the given StatefulSet
|
||||||
|
func applyStatefulSetHistory(sts *appsv1.StatefulSet, history *appsv1.ControllerRevision) (*appsv1.StatefulSet, error) {
|
||||||
|
stsBytes, err := json.Marshal(sts)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
patched, err := strategicpatch.StrategicMergePatch(stsBytes, history.Data.Raw, sts)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
result := &appsv1.StatefulSet{}
|
||||||
|
err = json.Unmarshal(patched, result)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return result, nil
|
||||||
|
}
|
||||||
|
|
||||||
// TODO: copied here until this becomes a describer
|
// TODO: copied here until this becomes a describer
|
||||||
func tabbedString(f func(io.Writer) error) (string, error) {
|
func tabbedString(f func(io.Writer) error) (string, error) {
|
||||||
out := new(tabwriter.Writer)
|
out := new(tabwriter.Writer)
|
||||||
|
@ -27,6 +27,7 @@ import (
|
|||||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||||
"k8s.io/apimachinery/pkg/runtime"
|
"k8s.io/apimachinery/pkg/runtime"
|
||||||
"k8s.io/apimachinery/pkg/runtime/schema"
|
"k8s.io/apimachinery/pkg/runtime/schema"
|
||||||
|
"k8s.io/apimachinery/pkg/util/json"
|
||||||
"k8s.io/client-go/kubernetes/fake"
|
"k8s.io/client-go/kubernetes/fake"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -71,21 +72,25 @@ func TestViewHistory(t *testing.T) {
|
|||||||
},
|
},
|
||||||
Spec: appsv1.StatefulSetSpec{Selector: &metav1.LabelSelector{MatchLabels: podStub.ObjectMeta.Labels}, Replicas: &replicas, Template: podStub},
|
Spec: appsv1.StatefulSetSpec{Selector: &metav1.LabelSelector{MatchLabels: podStub.ObjectMeta.Labels}, Replicas: &replicas, Template: podStub},
|
||||||
}
|
}
|
||||||
|
)
|
||||||
ssStub1 = &appsv1.ControllerRevision{
|
stsRawData, err := json.Marshal(ssStub)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("error creating sts raw data: %v", err)
|
||||||
|
}
|
||||||
|
ssStub1 := &appsv1.ControllerRevision{
|
||||||
ObjectMeta: metav1.ObjectMeta{
|
ObjectMeta: metav1.ObjectMeta{
|
||||||
Name: "moons",
|
Name: "moons",
|
||||||
Namespace: "default",
|
Namespace: "default",
|
||||||
Labels: map[string]string{"foo": "bar"},
|
Labels: map[string]string{"foo": "bar"},
|
||||||
OwnerReferences: []metav1.OwnerReference{{"apps/v1", "StatefulSet", "moons", "1993", &trueVar, nil}},
|
OwnerReferences: []metav1.OwnerReference{{"apps/v1", "StatefulSet", "moons", "1993", &trueVar, nil}},
|
||||||
},
|
},
|
||||||
|
Data: runtime.RawExtension{Raw: stsRawData},
|
||||||
TypeMeta: metav1.TypeMeta{Kind: "StatefulSet", APIVersion: "apps/v1"},
|
TypeMeta: metav1.TypeMeta{Kind: "StatefulSet", APIVersion: "apps/v1"},
|
||||||
Revision: 1,
|
Revision: 1,
|
||||||
}
|
}
|
||||||
)
|
|
||||||
|
|
||||||
fakeClientSet := fake.NewSimpleClientset(ssStub)
|
fakeClientSet := fake.NewSimpleClientset(ssStub)
|
||||||
_, err := fakeClientSet.AppsV1().ControllerRevisions("default").Create(context.TODO(), ssStub1, metav1.CreateOptions{})
|
_, err = fakeClientSet.AppsV1().ControllerRevisions("default").Create(context.TODO(), ssStub1, metav1.CreateOptions{})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("create controllerRevisions error %v occurred ", err)
|
t.Fatalf("create controllerRevisions error %v occurred ", err)
|
||||||
}
|
}
|
||||||
@ -94,7 +99,8 @@ func TestViewHistory(t *testing.T) {
|
|||||||
fakeClientSet,
|
fakeClientSet,
|
||||||
}
|
}
|
||||||
|
|
||||||
result, err := sts.ViewHistory("default", "moons", 1)
|
t.Run("should show revisions list if the revision is not specified", func(t *testing.T) {
|
||||||
|
result, err := sts.ViewHistory("default", "moons", 0)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("error getting ViewHistory for a StatefulSets moons: %v", err)
|
t.Fatalf("error getting ViewHistory for a StatefulSets moons: %v", err)
|
||||||
}
|
}
|
||||||
@ -106,7 +112,30 @@ func TestViewHistory(t *testing.T) {
|
|||||||
if result != expected {
|
if result != expected {
|
||||||
t.Fatalf("unexpected output (%v was expected but got %v)", expected, result)
|
t.Fatalf("unexpected output (%v was expected but got %v)", expected, result)
|
||||||
}
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("should describe the revision if revision is specified", func(t *testing.T) {
|
||||||
|
result, err := sts.ViewHistory("default", "moons", 1)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("error getting ViewHistory for a StatefulSets moons: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
expected := `Pod Template:
|
||||||
|
Labels: foo=bar
|
||||||
|
Containers:
|
||||||
|
test:
|
||||||
|
Image: nginx
|
||||||
|
Port: <none>
|
||||||
|
Host Port: <none>
|
||||||
|
Environment: <none>
|
||||||
|
Mounts: <none>
|
||||||
|
Volumes: <none>
|
||||||
|
`
|
||||||
|
|
||||||
|
if result != expected {
|
||||||
|
t.Fatalf("unexpected output (%v was expected but got %v)", expected, result)
|
||||||
|
}
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestApplyDaemonSetHistory(t *testing.T) {
|
func TestApplyDaemonSetHistory(t *testing.T) {
|
||||||
|
Loading…
Reference in New Issue
Block a user