2016-01-20 23:48:52 +00:00
|
|
|
/*
|
2016-06-03 00:25:58 +00:00
|
|
|
Copyright 2016 The Kubernetes Authors.
|
2016-01-20 23:48:52 +00:00
|
|
|
|
|
|
|
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 kubectl
|
|
|
|
|
|
|
|
import (
|
2016-06-10 16:31:29 +00:00
|
|
|
"bytes"
|
2016-01-20 23:48:52 +00:00
|
|
|
"fmt"
|
|
|
|
"io"
|
2017-02-19 22:56:11 +00:00
|
|
|
"text/tabwriter"
|
2016-01-20 23:48:52 +00:00
|
|
|
|
2017-06-22 18:24:23 +00:00
|
|
|
appsv1beta1 "k8s.io/api/apps/v1beta1"
|
2017-06-22 17:25:57 +00:00
|
|
|
"k8s.io/api/core/v1"
|
2017-06-22 18:24:23 +00:00
|
|
|
extensionsv1beta1 "k8s.io/api/extensions/v1beta1"
|
2017-01-11 14:09:48 +00:00
|
|
|
"k8s.io/apimachinery/pkg/api/meta"
|
|
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
|
|
|
"k8s.io/apimachinery/pkg/runtime"
|
|
|
|
"k8s.io/apimachinery/pkg/runtime/schema"
|
2017-06-06 18:31:04 +00:00
|
|
|
"k8s.io/apimachinery/pkg/util/json"
|
|
|
|
"k8s.io/apimachinery/pkg/util/strategicpatch"
|
2017-07-21 11:52:39 +00:00
|
|
|
clientappsv1beta1 "k8s.io/client-go/kubernetes/typed/apps/v1beta1"
|
|
|
|
clientextensionsv1beta1 "k8s.io/client-go/kubernetes/typed/extensions/v1beta1"
|
2016-01-20 23:48:52 +00:00
|
|
|
"k8s.io/kubernetes/pkg/api"
|
2017-07-29 08:47:14 +00:00
|
|
|
apiv1 "k8s.io/kubernetes/pkg/api/v1"
|
2017-01-10 16:25:32 +00:00
|
|
|
"k8s.io/kubernetes/pkg/apis/apps"
|
2016-01-20 23:48:52 +00:00
|
|
|
"k8s.io/kubernetes/pkg/apis/extensions"
|
2016-02-05 21:58:03 +00:00
|
|
|
clientset "k8s.io/kubernetes/pkg/client/clientset_generated/internalclientset"
|
2016-07-05 07:29:09 +00:00
|
|
|
deploymentutil "k8s.io/kubernetes/pkg/controller/deployment/util"
|
2017-06-29 23:07:31 +00:00
|
|
|
sliceutil "k8s.io/kubernetes/pkg/kubectl/util/slice"
|
2017-02-19 22:56:11 +00:00
|
|
|
printersinternal "k8s.io/kubernetes/pkg/printers/internalversion"
|
2016-01-20 23:48:52 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
ChangeCauseAnnotation = "kubernetes.io/change-cause"
|
|
|
|
)
|
|
|
|
|
2016-06-10 16:31:29 +00:00
|
|
|
// HistoryViewer provides an interface for resources have historical information.
|
2016-01-20 23:48:52 +00:00
|
|
|
type HistoryViewer interface {
|
2016-06-10 16:31:29 +00:00
|
|
|
ViewHistory(namespace, name string, revision int64) (string, error)
|
2016-01-20 23:48:52 +00:00
|
|
|
}
|
|
|
|
|
2016-11-21 02:55:31 +00:00
|
|
|
func HistoryViewerFor(kind schema.GroupKind, c clientset.Interface) (HistoryViewer, error) {
|
2016-01-20 23:48:52 +00:00
|
|
|
switch kind {
|
2017-01-10 16:25:32 +00:00
|
|
|
case extensions.Kind("Deployment"), apps.Kind("Deployment"):
|
2016-01-20 23:48:52 +00:00
|
|
|
return &DeploymentHistoryViewer{c}, nil
|
2017-06-04 22:31:23 +00:00
|
|
|
case apps.Kind("StatefulSet"):
|
|
|
|
return &StatefulSetHistoryViewer{c}, nil
|
2017-07-18 02:02:18 +00:00
|
|
|
case extensions.Kind("DaemonSet"), apps.Kind("DaemonSet"):
|
2017-05-18 22:46:20 +00:00
|
|
|
return &DaemonSetHistoryViewer{c}, nil
|
2016-01-20 23:48:52 +00:00
|
|
|
}
|
|
|
|
return nil, fmt.Errorf("no history viewer has been implemented for %q", kind)
|
|
|
|
}
|
|
|
|
|
|
|
|
type DeploymentHistoryViewer struct {
|
|
|
|
c clientset.Interface
|
|
|
|
}
|
|
|
|
|
2016-06-10 16:31:29 +00:00
|
|
|
// ViewHistory returns a revision-to-replicaset map as the revision history of a deployment
|
2017-02-19 22:56:11 +00:00
|
|
|
// TODO: this should be a describer
|
2016-06-10 16:31:29 +00:00
|
|
|
func (h *DeploymentHistoryViewer) ViewHistory(namespace, name string, revision int64) (string, error) {
|
2017-07-21 11:52:39 +00:00
|
|
|
versionedExtensionsClient := versionedExtensionsClientV1beta1(h.c)
|
|
|
|
deployment, err := versionedExtensionsClient.Deployments(namespace).Get(name, metav1.GetOptions{})
|
2016-01-20 23:48:52 +00:00
|
|
|
if err != nil {
|
2016-06-10 16:31:29 +00:00
|
|
|
return "", fmt.Errorf("failed to retrieve deployment %s: %v", name, err)
|
2016-01-20 23:48:52 +00:00
|
|
|
}
|
2017-07-21 11:52:39 +00:00
|
|
|
_, allOldRSs, newRS, err := deploymentutil.GetAllReplicaSets(deployment, versionedExtensionsClient)
|
2016-01-20 23:48:52 +00:00
|
|
|
if err != nil {
|
2016-06-10 16:31:29 +00:00
|
|
|
return "", fmt.Errorf("failed to retrieve replica sets from deployment %s: %v", name, err)
|
2016-01-20 23:48:52 +00:00
|
|
|
}
|
2016-05-07 14:06:13 +00:00
|
|
|
allRSs := allOldRSs
|
|
|
|
if newRS != nil {
|
|
|
|
allRSs = append(allRSs, newRS)
|
|
|
|
}
|
2016-06-10 16:31:29 +00:00
|
|
|
|
2016-11-18 20:58:22 +00:00
|
|
|
historyInfo := make(map[int64]*v1.PodTemplateSpec)
|
2016-02-08 23:13:48 +00:00
|
|
|
for _, rs := range allRSs {
|
|
|
|
v, err := deploymentutil.Revision(rs)
|
2016-01-20 23:48:52 +00:00
|
|
|
if err != nil {
|
2016-02-02 01:34:48 +00:00
|
|
|
continue
|
2016-01-20 23:48:52 +00:00
|
|
|
}
|
2016-06-10 16:31:29 +00:00
|
|
|
historyInfo[v] = &rs.Spec.Template
|
2016-02-08 23:13:48 +00:00
|
|
|
changeCause := getChangeCause(rs)
|
2016-06-10 16:31:29 +00:00
|
|
|
if historyInfo[v].Annotations == nil {
|
|
|
|
historyInfo[v].Annotations = make(map[string]string)
|
2016-01-20 23:48:52 +00:00
|
|
|
}
|
2016-03-21 13:45:31 +00:00
|
|
|
if len(changeCause) > 0 {
|
2016-06-10 16:31:29 +00:00
|
|
|
historyInfo[v].Annotations[ChangeCauseAnnotation] = changeCause
|
2016-03-21 13:45:31 +00:00
|
|
|
}
|
2016-01-20 23:48:52 +00:00
|
|
|
}
|
|
|
|
|
2016-06-10 16:31:29 +00:00
|
|
|
if len(historyInfo) == 0 {
|
|
|
|
return "No rollout history found.", nil
|
2016-01-20 23:48:52 +00:00
|
|
|
}
|
2016-06-10 16:31:29 +00:00
|
|
|
|
|
|
|
if revision > 0 {
|
|
|
|
// Print details of a specific revision
|
|
|
|
template, ok := historyInfo[revision]
|
|
|
|
if !ok {
|
|
|
|
return "", fmt.Errorf("unable to find the specified revision")
|
|
|
|
}
|
2017-05-18 22:46:20 +00:00
|
|
|
return printTemplate(template)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sort the revisionToChangeCause map by revision
|
|
|
|
revisions := make([]int64, 0, len(historyInfo))
|
|
|
|
for r := range historyInfo {
|
|
|
|
revisions = append(revisions, r)
|
|
|
|
}
|
|
|
|
sliceutil.SortInts64(revisions)
|
|
|
|
|
|
|
|
return tabbedString(func(out io.Writer) error {
|
|
|
|
fmt.Fprintf(out, "REVISION\tCHANGE-CAUSE\n")
|
|
|
|
for _, r := range revisions {
|
|
|
|
// Find the change-cause of revision r
|
|
|
|
changeCause := historyInfo[r].Annotations[ChangeCauseAnnotation]
|
|
|
|
if len(changeCause) == 0 {
|
|
|
|
changeCause = "<none>"
|
|
|
|
}
|
|
|
|
fmt.Fprintf(out, "%d\t%s\n", r, changeCause)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func printTemplate(template *v1.PodTemplateSpec) (string, error) {
|
|
|
|
buf := bytes.NewBuffer([]byte{})
|
|
|
|
internalTemplate := &api.PodTemplateSpec{}
|
2017-07-29 08:47:14 +00:00
|
|
|
if err := apiv1.Convert_v1_PodTemplateSpec_To_api_PodTemplateSpec(template, internalTemplate, nil); err != nil {
|
2017-05-18 22:46:20 +00:00
|
|
|
return "", fmt.Errorf("failed to convert podtemplate, %v", err)
|
|
|
|
}
|
|
|
|
w := printersinternal.NewPrefixWriter(buf)
|
|
|
|
printersinternal.DescribePodTemplate(internalTemplate, w)
|
|
|
|
return buf.String(), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type DaemonSetHistoryViewer struct {
|
|
|
|
c clientset.Interface
|
|
|
|
}
|
|
|
|
|
|
|
|
// ViewHistory returns a revision-to-history map as the revision history of a deployment
|
|
|
|
// TODO: this should be a describer
|
|
|
|
func (h *DaemonSetHistoryViewer) ViewHistory(namespace, name string, revision int64) (string, error) {
|
2017-07-21 11:52:39 +00:00
|
|
|
versionedAppsClient := versionedAppsClientV1beta1(h.c)
|
2017-08-07 21:49:46 +00:00
|
|
|
versionedExtensionsClient := versionedExtensionsClientV1beta1(h.c)
|
|
|
|
versionedObj, allHistory, err := controlledHistories(versionedAppsClient, versionedExtensionsClient, namespace, name, "DaemonSet")
|
2017-05-18 22:46:20 +00:00
|
|
|
if err != nil {
|
2017-06-06 18:31:04 +00:00
|
|
|
return "", fmt.Errorf("unable to find history controlled by DaemonSet %s: %v", name, err)
|
2017-05-18 22:46:20 +00:00
|
|
|
}
|
|
|
|
historyInfo := make(map[int64]*appsv1beta1.ControllerRevision)
|
|
|
|
for _, history := range allHistory {
|
|
|
|
// TODO: for now we assume revisions don't overlap, we may need to handle it
|
|
|
|
historyInfo[history.Revision] = history
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(historyInfo) == 0 {
|
|
|
|
return "No rollout history found.", nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Print details of a specific revision
|
|
|
|
if revision > 0 {
|
|
|
|
history, ok := historyInfo[revision]
|
|
|
|
if !ok {
|
|
|
|
return "", fmt.Errorf("unable to find the specified revision")
|
|
|
|
}
|
2017-08-07 21:49:46 +00:00
|
|
|
|
|
|
|
versionedDS, ok := versionedObj.(*extensionsv1beta1.DaemonSet)
|
|
|
|
if !ok {
|
|
|
|
return "", fmt.Errorf("unexpected non-DaemonSet object returned: %v", versionedDS)
|
|
|
|
}
|
|
|
|
|
|
|
|
dsOfHistory, err := applyHistory(versionedDS, history)
|
2017-05-18 22:46:20 +00:00
|
|
|
if err != nil {
|
2017-06-06 18:31:04 +00:00
|
|
|
return "", fmt.Errorf("unable to parse history %s", history.Name)
|
2016-11-18 20:58:22 +00:00
|
|
|
}
|
2017-06-06 18:31:04 +00:00
|
|
|
return printTemplate(&dsOfHistory.Spec.Template)
|
2016-06-10 16:31:29 +00:00
|
|
|
}
|
|
|
|
|
2017-05-18 22:46:20 +00:00
|
|
|
// Print an overview of all Revisions
|
2016-01-20 23:48:52 +00:00
|
|
|
// Sort the revisionToChangeCause map by revision
|
2016-06-10 16:31:29 +00:00
|
|
|
revisions := make([]int64, 0, len(historyInfo))
|
|
|
|
for r := range historyInfo {
|
2016-05-18 09:04:29 +00:00
|
|
|
revisions = append(revisions, r)
|
2016-01-20 23:48:52 +00:00
|
|
|
}
|
2016-05-18 09:04:29 +00:00
|
|
|
sliceutil.SortInts64(revisions)
|
2016-01-20 23:48:52 +00:00
|
|
|
|
|
|
|
return tabbedString(func(out io.Writer) error {
|
|
|
|
fmt.Fprintf(out, "REVISION\tCHANGE-CAUSE\n")
|
|
|
|
for _, r := range revisions {
|
|
|
|
// Find the change-cause of revision r
|
2016-06-10 16:31:29 +00:00
|
|
|
changeCause := historyInfo[r].Annotations[ChangeCauseAnnotation]
|
2016-01-20 23:48:52 +00:00
|
|
|
if len(changeCause) == 0 {
|
|
|
|
changeCause = "<none>"
|
|
|
|
}
|
2016-05-18 09:04:29 +00:00
|
|
|
fmt.Fprintf(out, "%d\t%s\n", r, changeCause)
|
2016-01-20 23:48:52 +00:00
|
|
|
}
|
2016-05-18 09:04:29 +00:00
|
|
|
return nil
|
2016-01-20 23:48:52 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-06-04 22:31:23 +00:00
|
|
|
type StatefulSetHistoryViewer struct {
|
|
|
|
c clientset.Interface
|
|
|
|
}
|
|
|
|
|
|
|
|
func getOwner(revision apps.ControllerRevision) *metav1.OwnerReference {
|
|
|
|
ownerRefs := revision.GetOwnerReferences()
|
|
|
|
for i := range ownerRefs {
|
|
|
|
owner := &ownerRefs[i]
|
|
|
|
if owner.Controller != nil && *owner.Controller == true {
|
|
|
|
return owner
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ViewHistory returns a list of the revision history of a statefulset
|
|
|
|
// TODO: this should be a describer
|
|
|
|
// TODO: needs to implement detailed revision view
|
|
|
|
func (h *StatefulSetHistoryViewer) ViewHistory(namespace, name string, revision int64) (string, error) {
|
|
|
|
|
|
|
|
sts, err := h.c.Apps().StatefulSets(namespace).Get(name, metav1.GetOptions{})
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("failed to retrieve statefulset %s", err)
|
|
|
|
}
|
|
|
|
selector, err := metav1.LabelSelectorAsSelector(sts.Spec.Selector)
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("failed to retrieve statefulset history %s", err)
|
|
|
|
}
|
|
|
|
revisions, err := h.c.Apps().ControllerRevisions(namespace).List(metav1.ListOptions{LabelSelector: selector.String()})
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("failed to retrieve statefulset history %s", err)
|
|
|
|
}
|
|
|
|
if len(revisions.Items) <= 0 {
|
|
|
|
return "No rollout history found.", nil
|
|
|
|
}
|
|
|
|
revisionNumbers := make([]int64, len(revisions.Items))
|
|
|
|
for i := range revisions.Items {
|
|
|
|
if owner := getOwner(revisions.Items[i]); owner != nil && owner.UID == sts.UID {
|
|
|
|
revisionNumbers[i] = revisions.Items[i].Revision
|
|
|
|
}
|
|
|
|
}
|
|
|
|
sliceutil.SortInts64(revisionNumbers)
|
|
|
|
|
|
|
|
return tabbedString(func(out io.Writer) error {
|
|
|
|
fmt.Fprintf(out, "REVISION\n")
|
|
|
|
for _, r := range revisionNumbers {
|
|
|
|
fmt.Fprintf(out, "%d\n", r)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-08-07 21:49:46 +00:00
|
|
|
// controlledHistories returns all ControllerRevisions controlled by the given API object
|
|
|
|
func controlledHistories(apps clientappsv1beta1.AppsV1beta1Interface, extensions clientextensionsv1beta1.ExtensionsV1beta1Interface, namespace, name, kind string) (runtime.Object, []*appsv1beta1.ControllerRevision, error) {
|
|
|
|
var obj runtime.Object
|
|
|
|
var labelSelector *metav1.LabelSelector
|
|
|
|
|
|
|
|
switch kind {
|
|
|
|
case "DaemonSet":
|
|
|
|
ds, err := extensions.DaemonSets(namespace).Get(name, metav1.GetOptions{})
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, fmt.Errorf("failed to retrieve DaemonSet %s: %v", name, err)
|
|
|
|
}
|
|
|
|
labelSelector = ds.Spec.Selector
|
|
|
|
obj = ds
|
|
|
|
case "StatefulSet":
|
|
|
|
ss, err := apps.StatefulSets(namespace).Get(name, metav1.GetOptions{})
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, fmt.Errorf("failed to retrieve StatefulSet %s: %v", name, err)
|
|
|
|
}
|
|
|
|
labelSelector = ss.Spec.Selector
|
|
|
|
obj = ss
|
|
|
|
default:
|
|
|
|
return nil, nil, fmt.Errorf("unsupported API object kind: %s", kind)
|
2017-06-06 18:31:04 +00:00
|
|
|
}
|
2017-08-07 21:49:46 +00:00
|
|
|
|
2017-05-18 22:46:20 +00:00
|
|
|
var result []*appsv1beta1.ControllerRevision
|
2017-08-07 21:49:46 +00:00
|
|
|
selector, err := metav1.LabelSelectorAsSelector(labelSelector)
|
2017-05-18 22:46:20 +00:00
|
|
|
if err != nil {
|
2017-06-06 18:31:04 +00:00
|
|
|
return nil, nil, err
|
2017-05-18 22:46:20 +00:00
|
|
|
}
|
2017-08-07 21:49:46 +00:00
|
|
|
historyList, err := apps.ControllerRevisions(namespace).List(metav1.ListOptions{LabelSelector: selector.String()})
|
2017-05-18 22:46:20 +00:00
|
|
|
if err != nil {
|
2017-06-06 18:31:04 +00:00
|
|
|
return nil, nil, err
|
2017-05-18 22:46:20 +00:00
|
|
|
}
|
2017-08-07 21:49:46 +00:00
|
|
|
accessor, err := meta.Accessor(obj)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, fmt.Errorf("failed to obtain accessor for %s named %s: %v", kind, name, err)
|
|
|
|
}
|
2017-05-18 22:46:20 +00:00
|
|
|
for i := range historyList.Items {
|
|
|
|
history := historyList.Items[i]
|
2017-08-07 21:49:46 +00:00
|
|
|
// Only add history that belongs to the API object
|
|
|
|
if metav1.IsControlledBy(&history, accessor) {
|
2017-08-02 10:36:58 +00:00
|
|
|
result = append(result, &history)
|
2017-05-18 22:46:20 +00:00
|
|
|
}
|
|
|
|
}
|
2017-08-07 21:49:46 +00:00
|
|
|
return obj, result, nil
|
2017-06-06 18:31:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// applyHistory returns a specific revision of DaemonSet by applying the given history to a copy of the given DaemonSet
|
|
|
|
func applyHistory(ds *extensionsv1beta1.DaemonSet, history *appsv1beta1.ControllerRevision) (*extensionsv1beta1.DaemonSet, error) {
|
|
|
|
obj, err := api.Scheme.New(ds.GroupVersionKind())
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
clone := obj.(*extensionsv1beta1.DaemonSet)
|
|
|
|
cloneBytes, err := json.Marshal(clone)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
patched, err := strategicpatch.StrategicMergePatch(cloneBytes, history.Data.Raw, clone)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
err = json.Unmarshal(patched, clone)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return clone, nil
|
2017-05-18 22:46:20 +00:00
|
|
|
}
|
|
|
|
|
2017-02-19 22:56:11 +00:00
|
|
|
// TODO: copied here until this becomes a describer
|
|
|
|
func tabbedString(f func(io.Writer) error) (string, error) {
|
|
|
|
out := new(tabwriter.Writer)
|
|
|
|
buf := &bytes.Buffer{}
|
|
|
|
out.Init(buf, 0, 8, 1, '\t', 0)
|
|
|
|
|
|
|
|
err := f(out)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
out.Flush()
|
|
|
|
str := string(buf.String())
|
|
|
|
return str, nil
|
|
|
|
}
|
|
|
|
|
2016-01-20 23:48:52 +00:00
|
|
|
// getChangeCause returns the change-cause annotation of the input object
|
2016-02-02 01:34:48 +00:00
|
|
|
func getChangeCause(obj runtime.Object) string {
|
2016-03-25 08:57:45 +00:00
|
|
|
accessor, err := meta.Accessor(obj)
|
2016-01-20 23:48:52 +00:00
|
|
|
if err != nil {
|
2016-02-02 01:34:48 +00:00
|
|
|
return ""
|
2016-01-20 23:48:52 +00:00
|
|
|
}
|
2016-03-25 08:57:45 +00:00
|
|
|
return accessor.GetAnnotations()[ChangeCauseAnnotation]
|
2016-01-20 23:48:52 +00:00
|
|
|
}
|