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 Sep 16, 2023
1 parent 1735aa5 commit f0cd7c9
Show file tree
Hide file tree
Showing 10 changed files with 654 additions and 1 deletion.
3 changes: 3 additions & 0 deletions api/v1beta1/common_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,9 @@ const (
// VariableDefinitionFromInline indicates a patch or variable was defined in the `.spec` of a ClusterClass
// rather than from an external patch extension.
VariableDefinitionFromInline = "inline"
// 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"
)

// MachineSetPreflightCheck defines a valid MachineSet preflight check.
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 @@ -46,6 +46,7 @@ type Rollout interface {
ObjectPauser(context.Context, cluster.Proxy, corev1.ObjectReference) error
ObjectResumer(context.Context, cluster.Proxy, corev1.ObjectReference) error
ObjectRollbacker(context.Context, cluster.Proxy, corev1.ObjectReference, int64) error
ObjectViewer(context.Context, cluster.Proxy, corev1.ObjectReference, int64) error
}

var _ Rollout = &rollout{}
Expand Down
109 changes: 109 additions & 0 deletions cmd/clusterctl/client/alpha/rollout_viewer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
/*
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 (
"context"
"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(ctx context.Context, proxy cluster.Proxy, ref corev1.ObjectReference, revision int64) error {
switch ref.Kind {
case MachineDeployment:
deployment, err := getMachineDeployment(ctx, 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(ctx, 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(ctx context.Context, proxy cluster.Proxy, d *clusterv1.MachineDeployment, revision int64) error {
log := logf.Log
msList, err := getMachineSetsForDeployment(ctx, 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 f0cd7c9

Please sign in to comment.