Update versioned portions of kubectl to use apps/v1 with DaemonSet

pull/6/head
Kenneth Owens 2018-02-14 10:36:50 -08:00
parent cf80186a6c
commit f89afa008f
2 changed files with 15 additions and 57 deletions

View File

@ -23,9 +23,8 @@ import (
"text/tabwriter"
appsv1 "k8s.io/api/apps/v1"
appsv1beta1 "k8s.io/api/apps/v1beta1"
"k8s.io/api/core/v1"
extensionsv1beta1 "k8s.io/api/extensions/v1beta1"
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
@ -35,8 +34,6 @@ import (
"k8s.io/apimachinery/pkg/util/strategicpatch"
"k8s.io/client-go/kubernetes"
clientappsv1 "k8s.io/client-go/kubernetes/typed/apps/v1"
clientappsv1beta1 "k8s.io/client-go/kubernetes/typed/apps/v1beta1"
clientextv1beta1 "k8s.io/client-go/kubernetes/typed/extensions/v1beta1"
api "k8s.io/kubernetes/pkg/apis/core"
apiv1 "k8s.io/kubernetes/pkg/apis/core/v1"
deploymentutil "k8s.io/kubernetes/pkg/controller/deployment/util"
@ -187,11 +184,11 @@ type DaemonSetHistoryViewer struct {
// 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) {
ds, history, err := daemonSetHistory(h.c.ExtensionsV1beta1(), h.c.AppsV1beta1(), namespace, name)
ds, history, err := daemonSetHistory(h.c.AppsV1(), namespace, name)
if err != nil {
return "", err
}
historyInfo := make(map[int64]*appsv1beta1.ControllerRevision)
historyInfo := make(map[int64]*appsv1.ControllerRevision)
for _, history := range history {
// TODO: for now we assume revisions don't overlap, we may need to handle it
historyInfo[history.Revision] = history
@ -290,11 +287,11 @@ func controlledHistoryV1(
// controlledHistories returns all ControllerRevisions in namespace that selected by selector and owned by accessor
func controlledHistory(
apps clientappsv1beta1.AppsV1beta1Interface,
apps clientappsv1.AppsV1Interface,
namespace string,
selector labels.Selector,
accessor metav1.Object) ([]*appsv1beta1.ControllerRevision, error) {
var result []*appsv1beta1.ControllerRevision
accessor metav1.Object) ([]*appsv1.ControllerRevision, error) {
var result []*appsv1.ControllerRevision
historyList, err := apps.ControllerRevisions(namespace).List(metav1.ListOptions{LabelSelector: selector.String()})
if err != nil {
return nil, err
@ -311,10 +308,9 @@ func controlledHistory(
// daemonSetHistory returns the DaemonSet named name in namespace and all ControllerRevisions in its history.
func daemonSetHistory(
ext clientextv1beta1.ExtensionsV1beta1Interface,
apps clientappsv1beta1.AppsV1beta1Interface,
namespace, name string) (*extensionsv1beta1.DaemonSet, []*appsv1beta1.ControllerRevision, error) {
ds, err := ext.DaemonSets(namespace).Get(name, metav1.GetOptions{})
apps clientappsv1.AppsV1Interface,
namespace, name string) (*appsv1.DaemonSet, []*appsv1.ControllerRevision, error) {
ds, err := apps.DaemonSets(namespace).Get(name, metav1.GetOptions{})
if err != nil {
return nil, nil, fmt.Errorf("failed to retrieve DaemonSet %s: %v", name, err)
}
@ -357,7 +353,7 @@ func statefulSetHistory(
}
// applyDaemonSetHistory returns a specific revision of DaemonSet by applying the given history to a copy of the given DaemonSet
func applyDaemonSetHistory(ds *extensionsv1beta1.DaemonSet, history *appsv1beta1.ControllerRevision) (*extensionsv1beta1.DaemonSet, error) {
func applyDaemonSetHistory(ds *appsv1.DaemonSet, history *appsv1.ControllerRevision) (*appsv1.DaemonSet, error) {
clone := ds.DeepCopy()
cloneBytes, err := json.Marshal(clone)
if err != nil {

View File

@ -25,7 +25,6 @@ import (
"syscall"
appsv1 "k8s.io/api/apps/v1"
appsv1beta1 "k8s.io/api/apps/v1beta1"
"k8s.io/api/core/v1"
extv1beta1 "k8s.io/api/extensions/v1beta1"
"k8s.io/apimachinery/pkg/api/meta"
@ -257,7 +256,7 @@ func (r *DaemonSetRollbacker) Rollback(obj runtime.Object, updatedAnnotations ma
if err != nil {
return "", fmt.Errorf("failed to create accessor for kind %v: %s", obj.GetObjectKind(), err.Error())
}
ds, history, err := daemonSetHistory(r.c.ExtensionsV1beta1(), r.c.AppsV1beta1(), accessor.GetNamespace(), accessor.GetName())
ds, history, err := daemonSetHistory(r.c.AppsV1(), accessor.GetNamespace(), accessor.GetName())
if err != nil {
return "", err
}
@ -316,7 +315,7 @@ func (r *StatefulSetRollbacker) Rollback(obj runtime.Object, updatedAnnotations
return "", fmt.Errorf("no last revision to roll back to")
}
toHistory := findHistoryV1(toRevision, history)
toHistory := findHistory(toRevision, history)
if toHistory == nil {
return "", revisionNotFoundErr(toRevision)
}
@ -346,44 +345,16 @@ func (r *StatefulSetRollbacker) Rollback(obj runtime.Object, updatedAnnotations
return rollbackSuccess, nil
}
// TODO: When all the controllers have been updated to use v1, rename this function findHistoryV1()->findHistory() and
// TODO: remove the original findHistory()
// findHistoryV1 returns a controllerrevision of a specific revision from the given controllerrevisions.
// findHistory returns a controllerrevision of a specific revision from the given controllerrevisions.
// It returns nil if no such controllerrevision exists.
// If toRevision is 0, the last previously used history is returned.
func findHistoryV1(toRevision int64, allHistory []*appsv1.ControllerRevision) *appsv1.ControllerRevision {
func findHistory(toRevision int64, allHistory []*appsv1.ControllerRevision) *appsv1.ControllerRevision {
if toRevision == 0 && len(allHistory) <= 1 {
return nil
}
// Find the history to rollback to
var toHistory *appsv1.ControllerRevision
if toRevision == 0 {
// If toRevision == 0, find the latest revision (2nd max)
sort.Sort(historiesByRevisionV1(allHistory))
toHistory = allHistory[len(allHistory)-2]
} else {
for _, h := range allHistory {
if h.Revision == toRevision {
// If toRevision != 0, find the history with matching revision
return h
}
}
}
return toHistory
}
// findHistory returns a controllerrevision of a specific revision from the given controllerrevisions.
// It returns nil if no such controllerrevision exists.
// If toRevision is 0, the last previously used history is returned.
func findHistory(toRevision int64, allHistory []*appsv1beta1.ControllerRevision) *appsv1beta1.ControllerRevision {
if toRevision == 0 && len(allHistory) <= 1 {
return nil
}
// Find the history to rollback to
var toHistory *appsv1beta1.ControllerRevision
if toRevision == 0 {
// If toRevision == 0, find the latest revision (2nd max)
sort.Sort(historiesByRevision(allHistory))
@ -417,19 +388,10 @@ func revisionNotFoundErr(r int64) error {
}
// TODO: copied from daemon controller, should extract to a library
type historiesByRevision []*appsv1beta1.ControllerRevision
type historiesByRevision []*appsv1.ControllerRevision
func (h historiesByRevision) Len() int { return len(h) }
func (h historiesByRevision) Swap(i, j int) { h[i], h[j] = h[j], h[i] }
func (h historiesByRevision) Less(i, j int) bool {
return h[i].Revision < h[j].Revision
}
// TODO: copied from daemon controller, should extract to a library
type historiesByRevisionV1 []*appsv1.ControllerRevision
func (h historiesByRevisionV1) Len() int { return len(h) }
func (h historiesByRevisionV1) Swap(i, j int) { h[i], h[j] = h[j], h[i] }
func (h historiesByRevisionV1) Less(i, j int) bool {
return h[i].Revision < h[j].Revision
}