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-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"
|
2016-01-20 23:48:52 +00:00
|
|
|
"k8s.io/kubernetes/pkg/api"
|
2016-11-18 20:58:22 +00:00
|
|
|
"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-02-19 22:56:11 +00:00
|
|
|
printersinternal "k8s.io/kubernetes/pkg/printers/internalversion"
|
2016-05-18 09:04:29 +00:00
|
|
|
sliceutil "k8s.io/kubernetes/pkg/util/slice"
|
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
|
|
|
|
}
|
|
|
|
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) {
|
2016-11-18 20:58:22 +00:00
|
|
|
versionedClient := versionedClientsetForDeployment(h.c)
|
2016-12-07 13:26:33 +00:00
|
|
|
deployment, err := versionedClient.Extensions().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-03-16 18:43:09 +00:00
|
|
|
_, allOldRSs, newRS, err := deploymentutil.GetAllReplicaSetsV15(deployment, versionedClient)
|
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")
|
|
|
|
}
|
|
|
|
buf := bytes.NewBuffer([]byte{})
|
2016-11-18 20:58:22 +00:00
|
|
|
internalTemplate := &api.PodTemplateSpec{}
|
|
|
|
if err := v1.Convert_v1_PodTemplateSpec_To_api_PodTemplateSpec(template, internalTemplate, nil); err != nil {
|
|
|
|
return "", fmt.Errorf("failed to convert podtemplate, %v", err)
|
|
|
|
}
|
2017-03-14 14:31:13 +00:00
|
|
|
w := printersinternal.NewPrefixWriter(buf)
|
|
|
|
printersinternal.DescribePodTemplate(internalTemplate, w)
|
2016-06-10 16:31:29 +00:00
|
|
|
return buf.String(), nil
|
|
|
|
}
|
|
|
|
|
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-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
|
|
|
}
|