8e5b17cf13
Signed-off-by: Mrunal Patel <mrunalp@gmail.com>
136 lines
4.1 KiB
Go
136 lines
4.1 KiB
Go
/*
|
|
Copyright 2016 The Kubernetes Authors.
|
|
|
|
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 (
|
|
"bytes"
|
|
"fmt"
|
|
"io"
|
|
|
|
"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"
|
|
"k8s.io/kubernetes/pkg/api"
|
|
"k8s.io/kubernetes/pkg/api/v1"
|
|
"k8s.io/kubernetes/pkg/apis/extensions"
|
|
clientset "k8s.io/kubernetes/pkg/client/clientset_generated/internalclientset"
|
|
deploymentutil "k8s.io/kubernetes/pkg/controller/deployment/util"
|
|
sliceutil "k8s.io/kubernetes/pkg/util/slice"
|
|
)
|
|
|
|
const (
|
|
ChangeCauseAnnotation = "kubernetes.io/change-cause"
|
|
)
|
|
|
|
// HistoryViewer provides an interface for resources have historical information.
|
|
type HistoryViewer interface {
|
|
ViewHistory(namespace, name string, revision int64) (string, error)
|
|
}
|
|
|
|
func HistoryViewerFor(kind schema.GroupKind, c clientset.Interface) (HistoryViewer, error) {
|
|
switch kind {
|
|
case extensions.Kind("Deployment"):
|
|
return &DeploymentHistoryViewer{c}, nil
|
|
}
|
|
return nil, fmt.Errorf("no history viewer has been implemented for %q", kind)
|
|
}
|
|
|
|
type DeploymentHistoryViewer struct {
|
|
c clientset.Interface
|
|
}
|
|
|
|
// ViewHistory returns a revision-to-replicaset map as the revision history of a deployment
|
|
func (h *DeploymentHistoryViewer) ViewHistory(namespace, name string, revision int64) (string, error) {
|
|
versionedClient := versionedClientsetForDeployment(h.c)
|
|
deployment, err := versionedClient.Extensions().Deployments(namespace).Get(name, metav1.GetOptions{})
|
|
if err != nil {
|
|
return "", fmt.Errorf("failed to retrieve deployment %s: %v", name, err)
|
|
}
|
|
_, allOldRSs, newRS, err := deploymentutil.GetAllReplicaSets(deployment, versionedClient)
|
|
if err != nil {
|
|
return "", fmt.Errorf("failed to retrieve replica sets from deployment %s: %v", name, err)
|
|
}
|
|
allRSs := allOldRSs
|
|
if newRS != nil {
|
|
allRSs = append(allRSs, newRS)
|
|
}
|
|
|
|
historyInfo := make(map[int64]*v1.PodTemplateSpec)
|
|
for _, rs := range allRSs {
|
|
v, err := deploymentutil.Revision(rs)
|
|
if err != nil {
|
|
continue
|
|
}
|
|
historyInfo[v] = &rs.Spec.Template
|
|
changeCause := getChangeCause(rs)
|
|
if historyInfo[v].Annotations == nil {
|
|
historyInfo[v].Annotations = make(map[string]string)
|
|
}
|
|
if len(changeCause) > 0 {
|
|
historyInfo[v].Annotations[ChangeCauseAnnotation] = changeCause
|
|
}
|
|
}
|
|
|
|
if len(historyInfo) == 0 {
|
|
return "No rollout history found.", nil
|
|
}
|
|
|
|
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{})
|
|
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)
|
|
}
|
|
DescribePodTemplate(internalTemplate, buf)
|
|
return buf.String(), nil
|
|
}
|
|
|
|
// 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
|
|
})
|
|
}
|
|
|
|
// getChangeCause returns the change-cause annotation of the input object
|
|
func getChangeCause(obj runtime.Object) string {
|
|
accessor, err := meta.Accessor(obj)
|
|
if err != nil {
|
|
return ""
|
|
}
|
|
return accessor.GetAnnotations()[ChangeCauseAnnotation]
|
|
}
|