Skip to content

Commit

Permalink
Add rollout history
Browse files Browse the repository at this point in the history
  • Loading branch information
hiromu-a5a committed Jan 31, 2023
1 parent 2c89700 commit 291382b
Show file tree
Hide file tree
Showing 11 changed files with 650 additions and 2 deletions.
4 changes: 4 additions & 0 deletions api/v1beta1/common_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,10 @@ const (
// instead of being a source of truth for eventual consistency.
// This annotation can be used to inform MachinePool status during in-progress scaling scenarios.
ReplicasManagedByAnnotation = "cluster.x-k8s.io/replicas-managed-by"

// ChangeCauseAnnotation is the annotation set on MachineSets by users to identify the cause of revision changes.
// This annotation will be shown when users run `clusterctl alpha rollout history`.
ChangeCauseAnnotation = "cluster.x-k8s.io/change-cause"
)

const (
Expand Down
1 change: 1 addition & 0 deletions cmd/clusterctl/client/alpha/rollout.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ type Rollout interface {
ObjectPauser(cluster.Proxy, corev1.ObjectReference) error
ObjectResumer(cluster.Proxy, corev1.ObjectReference) error
ObjectRollbacker(cluster.Proxy, corev1.ObjectReference, int64) error
ObjectViewer(cluster.Proxy, corev1.ObjectReference, int64) error
}

var _ Rollout = &rollout{}
Expand Down
108 changes: 108 additions & 0 deletions cmd/clusterctl/client/alpha/rollout_viewer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
/*
Copyright 2022 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 alpha

import (
"fmt"
"os"
"sort"
"text/tabwriter"

"github.com/pkg/errors"
"gopkg.in/yaml.v2"
corev1 "k8s.io/api/core/v1"

clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
"sigs.k8s.io/cluster-api/cmd/clusterctl/client/cluster"
logf "sigs.k8s.io/cluster-api/cmd/clusterctl/log"
"sigs.k8s.io/cluster-api/internal/controllers/machinedeployment/mdutil"
)

// ObjectViewer will issue a view on the specified cluster-api resource.
func (r *rollout) ObjectViewer(proxy cluster.Proxy, ref corev1.ObjectReference, revision int64) error {
switch ref.Kind {
case MachineDeployment:
deployment, err := getMachineDeployment(proxy, ref.Name, ref.Namespace)
if err != nil || deployment == nil {
return errors.Wrapf(err, "failed to get %v/%v", ref.Kind, ref.Name)
}
if err := viewMachineDeployment(proxy, deployment, revision); err != nil {
return err
}
default:
return errors.Errorf("invalid resource type %q, valid values are %v", ref.Kind, validResourceTypes)
}
return nil
}

func viewMachineDeployment(proxy cluster.Proxy, d *clusterv1.MachineDeployment, revision int64) error {
log := logf.Log
msList, err := getMachineSetsForDeployment(proxy, d)
if err != nil {
return err
}

if revision < 0 {
return errors.Errorf("revision number cannot be negative: %v", revision)
}

// Print details of a specific revision
if revision > 0 {
ms, err := findMachineDeploymentRevision(revision, msList)
if err != nil {
return errors.Errorf("unable to find the spcified revision")
}
output, err := yaml.Marshal(ms.Spec.Template)
if err != nil {
return err
}
fmt.Fprint(os.Stdout, string(output))
return nil
}

// Print an overview of all revisions
// Create a revisionToChangeCause map
historyInfo := make(map[int64]string)
for _, ms := range msList {
v, err := mdutil.Revision(ms)
if err != nil {
log.V(7).Error(err, fmt.Sprintf("unable to get revision from machineset %s for machinedeployment %s in namespace %s", ms.Name, d.Name, d.Namespace))
continue
}
historyInfo[v] = ms.Annotations[clusterv1.ChangeCauseAnnotation]
}

// Sort the revisions
revisions := make([]int64, 0, len(historyInfo))
for r := range historyInfo {
revisions = append(revisions, r)
}
sort.Slice(revisions, func(i, j int) bool { return revisions[i] < revisions[j] })

// Output the revisionToChangeCause map
writer := new(tabwriter.Writer)
writer.Init(os.Stdout, 0, 8, 2, ' ', 0)
fmt.Fprintf(writer, "REVISION\tCHANGE-CAUSE\n")
for _, r := range revisions {
changeCause := historyInfo[r]
if changeCause == "" {
changeCause = "<none>"
}
fmt.Fprintf(writer, "%d\t%s\n", r, changeCause)
}
return writer.Flush()
}
Loading

0 comments on commit 291382b

Please sign in to comment.