Uplift cli-utils and cluster-api

Change-Id: I9b46421812dd15c01a250e6102f9c132ce82dd42
This commit is contained in:
Dmitry Ukov 2021-02-08 13:46:27 +04:00
parent 9cd9304a22
commit 1a0ba5647d
6 changed files with 41 additions and 34 deletions

2
go.mod
View File

@ -41,7 +41,7 @@ require (
k8s.io/kubectl v0.17.4
opendev.org/airship/go-redfish v0.0.0-20200318103738-db034d1d753a
opendev.org/airship/go-redfish/client v0.0.0-20200318103738-db034d1d753a
sigs.k8s.io/cli-utils v0.20.6
sigs.k8s.io/cli-utils v0.21.0
sigs.k8s.io/cluster-api v0.3.13
sigs.k8s.io/controller-runtime v0.5.14
sigs.k8s.io/kustomize/api v0.7.2

7
go.sum
View File

@ -1090,8 +1090,8 @@ opendev.org/airship/go-redfish v0.0.0-20200318103738-db034d1d753a/go.mod h1:FEjY
opendev.org/airship/go-redfish/client v0.0.0-20200318103738-db034d1d753a h1:S1dmsP5Cc6OQjAd6OgIKMcNPBiGjh5TDbijVjNE/VGU=
opendev.org/airship/go-redfish/client v0.0.0-20200318103738-db034d1d753a/go.mod h1:s0hwuUpBsRXOrhN0NR+fNVivXGyWgHKpqtyq7qYjpew=
rsc.io/binaryregexp v0.2.0/go.mod h1:qTv7/COck+e2FymRvadv62gMdZztPaShugOCi3I+8D8=
sigs.k8s.io/cli-utils v0.20.6 h1:leIIk2NOzacXqhuNnm0tB328RSzp+1QR9PkAdSP1aRI=
sigs.k8s.io/cli-utils v0.20.6/go.mod h1:Lsj0EXMtqcSYyIPY9IXemMQW/muYDHMEWNfQI/DctrA=
sigs.k8s.io/cli-utils v0.21.0 h1:yvLvbzDyiFSmJFpjb6C6tqU2EqQ+hPK4GBUhS+myUm8=
sigs.k8s.io/cli-utils v0.21.0/go.mod h1:0n6pW2yhMbb0HxIcg8UeI5/Bi+Dh+7NOsXFdTudB/KY=
sigs.k8s.io/cluster-api v0.3.13 h1:dyhvxgt3M00Co06jrM332i27Tfozu9a0EN/qcmQXUFg=
sigs.k8s.io/cluster-api v0.3.13/go.mod h1:qGxyPTEJWNpII9SBkeRwv+Xvy6EZRLLLzaxVfBLsBpA=
sigs.k8s.io/controller-runtime v0.4.0/go.mod h1:ApC79lpY3PHW9xj/w9pj+lYkLgwAAUZwfXkME1Lajns=
@ -1102,8 +1102,7 @@ sigs.k8s.io/kustomize v2.0.3+incompatible h1:JUufWFNlI44MdtnjUqVnvh29rR37PQFzPbL
sigs.k8s.io/kustomize v2.0.3+incompatible/go.mod h1:MkjgH3RdOWrievjo6c9T245dYlB5QeXV4WCbnt/PEpU=
sigs.k8s.io/kustomize/api v0.7.2 h1:ItTD/2XaKO8CosOMFZdaGFdUGTCHdQriW7zQ7AR98rs=
sigs.k8s.io/kustomize/api v0.7.2/go.mod h1:50/vLATrjhRmMr3spZsI1GcpoZJ8IARy9QstPbA9lGE=
sigs.k8s.io/kustomize/kyaml v0.9.2 h1:QNP1Lg4V2wOgBeUim9Kmz1+2GqHtRyfoVEUQH0omrCI=
sigs.k8s.io/kustomize/kyaml v0.9.2/go.mod h1:UTm64bSWVdBUA8EQoYCxVOaBQxUdIOr5LKWxA4GNbkw=
sigs.k8s.io/kustomize/kyaml v0.9.3/go.mod h1:UTm64bSWVdBUA8EQoYCxVOaBQxUdIOr5LKWxA4GNbkw=
sigs.k8s.io/kustomize/kyaml v0.10.6 h1:xUJxc/k8JoWqHUahaB8DTqY0KwEPxTbTGStvW8TOcDc=
sigs.k8s.io/kustomize/kyaml v0.10.6/go.mod h1:K9yg1k/HB/6xNOf5VH3LhTo1DK9/5ykSZO5uIv+Y/1k=
sigs.k8s.io/structured-merge-diff v0.0.0-20190525122527-15d366b2352e/go.mod h1:wWxsB5ozmmv/SG7nM11ayaAW51xMvak/t1r0CSlcokI=

View File

@ -23,13 +23,12 @@ import (
v1 "k8s.io/api/core/v1"
apierror "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/cli-runtime/pkg/resource"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
cmdutil "k8s.io/kubectl/pkg/cmd/util"
cliapply "sigs.k8s.io/cli-utils/pkg/apply"
applyevent "sigs.k8s.io/cli-utils/pkg/apply/event"
"sigs.k8s.io/cli-utils/pkg/apply/poller"
clicommon "sigs.k8s.io/cli-utils/pkg/common"
"sigs.k8s.io/cli-utils/pkg/inventory"
"sigs.k8s.io/cli-utils/pkg/manifestreader"
"sigs.k8s.io/cli-utils/pkg/provider"
"sigs.k8s.io/yaml"
@ -59,7 +58,7 @@ type ReaderFactory func(validate bool, bundle document.Bundle, factory cmdutil.F
// NewApplier returns instance of Applier
func NewApplier(eventCh chan events.Event, f cmdutil.Factory) *Applier {
cf := provider.NewProvider(f, inventory.WrapInventoryObj)
cf := provider.NewProvider(f)
return &Applier{
Factory: f,
ManifestReaderFactory: utils.DefaultManifestReaderFactory,
@ -74,14 +73,14 @@ func NewApplier(eventCh chan events.Event, f cmdutil.Factory) *Applier {
func (a *Applier) ApplyBundle(bundle document.Bundle, ao ApplyOptions) {
defer close(a.eventChannel)
log.Debugf("Getting infos for bundle, inventory id is %s", ao.BundleName)
infos, err := a.getInfos(ao.BundleName, bundle, ao.DryRunStrategy)
objects, err := a.getObjects(ao.BundleName, bundle, ao.DryRunStrategy)
if err != nil {
handleError(a.eventChannel, err)
return
}
ctx := context.Background()
ch := a.Driver.Run(ctx, infos, cliApplyOptions(ao))
ch := a.Driver.Run(ctx, objects, cliApplyOptions(ao))
for e := range ch {
a.eventChannel <- events.Event{
Type: events.ApplierType,
@ -90,10 +89,10 @@ func (a *Applier) ApplyBundle(bundle document.Bundle, ao ApplyOptions) {
}
}
func (a *Applier) getInfos(
func (a *Applier) getObjects(
bundleName string,
bundle document.Bundle,
dryRun clicommon.DryRunStrategy) ([]*resource.Info, error) {
dryRun clicommon.DryRunStrategy) ([]*unstructured.Unstructured, error) {
if bundle == nil {
return nil, ErrNilBundle{}
}
@ -130,7 +129,11 @@ func (a *Applier) getInfos(
if err = a.Driver.Initialize(a.Poller); err != nil {
return nil, err
}
return a.ManifestReaderFactory(false, bundle, a.Factory).Read()
restMapper, err := a.Factory.ToRESTMapper()
if err != nil {
return nil, err
}
return a.ManifestReaderFactory(false, bundle, restMapper).Read()
}
func (a *Applier) ensureNamespaceExists(name string, dryRun clicommon.DryRunStrategy) error {
@ -177,7 +180,7 @@ func cliApplyOptions(ao ApplyOptions) cliapply.Options {
// Driver to cli-utils apply
type Driver interface {
Initialize(p poller.Poller) error
Run(ctx context.Context, infos []*resource.Info, options cliapply.Options) <-chan applyevent.Event
Run(ctx context.Context, infos []*unstructured.Unstructured, options cliapply.Options) <-chan applyevent.Event
}
// Adaptor is implementation of driver interface
@ -200,8 +203,9 @@ func (a *Adaptor) Initialize(p poller.Poller) error {
}
// Run perform apply operation
func (a *Adaptor) Run(ctx context.Context, infos []*resource.Info, options cliapply.Options) <-chan applyevent.Event {
return a.CliUtilsApplier.Run(ctx, infos, options)
func (a *Adaptor) Run(ctx context.Context, objects []*unstructured.Unstructured,
options cliapply.Options) <-chan applyevent.Event {
return a.CliUtilsApplier.Run(ctx, objects, options)
}
// NewInventoryDocument returns default config map with inventory Id to group up the objects

View File

@ -17,8 +17,8 @@ package applier
import (
"context"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/cli-runtime/pkg/resource"
cmdutil "k8s.io/kubectl/pkg/cmd/util"
cliapply "sigs.k8s.io/cli-utils/pkg/apply"
applyevent "sigs.k8s.io/cli-utils/pkg/apply/event"
@ -52,7 +52,7 @@ func (fa FakeAdaptor) Initialize(p poller.Poller) error {
// Run implements driver
func (fa FakeAdaptor) Run(
ctx context.Context,
infos []*resource.Info,
objects []*unstructured.Unstructured,
options cliapply.Options) <-chan applyevent.Event {
ch := make(chan applyevent.Event, len(fa.events))
defer close(ch)
@ -96,7 +96,7 @@ type FakePoller struct {
func (fp *FakePoller) Poll(ctx context.Context, ids []object.ObjMetadata, opts polling.Options) <-chan pollevent.Event {
events := []pollevent.Event{
{
EventType: pollevent.CompletedEvent,
EventType: pollevent.ResourceUpdateEvent,
Resource: &pollevent.ResourceStatus{
Identifier: object.ObjMetadata{
Name: "test-rc",
@ -109,7 +109,7 @@ func (fp *FakePoller) Poll(ctx context.Context, ids []object.ObjMetadata, opts p
},
},
{
EventType: pollevent.CompletedEvent,
EventType: pollevent.ResourceUpdateEvent,
Resource: &pollevent.ResourceStatus{
Identifier: object.ObjMetadata{
Name: "airshipit-test-bundle-4bf1e4a",

View File

@ -19,9 +19,10 @@ import (
"io"
"os"
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/cli-runtime/pkg/genericclioptions"
"k8s.io/cli-runtime/pkg/resource"
cmdutil "k8s.io/kubectl/pkg/cmd/util"
"sigs.k8s.io/cli-utils/pkg/manifestreader"
@ -50,25 +51,25 @@ func Streams() genericclioptions.IOStreams {
type ManifestReaderFactory func(
validate bool,
bundle document.Bundle,
factory cmdutil.Factory) manifestreader.ManifestReader
mapper meta.RESTMapper) manifestreader.ManifestReader
// DefaultManifestReaderFactory default factory function for manifestreader.ManifestReader
var DefaultManifestReaderFactory ManifestReaderFactory = func(
validate bool,
bundle document.Bundle,
factory cmdutil.Factory) manifestreader.ManifestReader {
return NewManifestBundleReader(validate, bundle, factory)
mapper meta.RESTMapper) manifestreader.ManifestReader {
return NewManifestBundleReader(validate, bundle, mapper)
}
// NewManifestBundleReader returns implementation of manifestreader interface
func NewManifestBundleReader(
validate bool,
bundle document.Bundle,
factory cmdutil.Factory) *ManifestBundleReader {
mapper meta.RESTMapper) *ManifestBundleReader {
opts := manifestreader.ReaderOptions{
Mapper: mapper,
Validate: validate,
Namespace: metav1.NamespaceDefault,
Factory: factory,
}
buffer := bytes.NewBuffer([]byte{})
return &ManifestBundleReader{
@ -90,10 +91,10 @@ type ManifestBundleReader struct {
writer io.Writer
}
func (mbr *ManifestBundleReader) Read() ([]*resource.Info, error) {
func (mbr *ManifestBundleReader) Read() ([]*unstructured.Unstructured, error) {
err := mbr.Bundle.Write(mbr.writer)
if err != nil {
return []*resource.Info{}, err
return nil, err
}
return mbr.StreamReader.Read()
}

View File

@ -29,7 +29,9 @@ import (
func TestDefaultManifestFactory(t *testing.T) {
bundle, err := document.NewBundleByPath("testdata/source_bundle")
require.NoError(t, err)
reader := DefaultManifestReaderFactory(false, bundle, FactoryFromKubeConfig("testdata/kubeconfig.yaml", ""))
mapper, err := FactoryFromKubeConfig("testdata/kubeconfig.yaml", "").ToRESTMapper()
require.NoError(t, err)
reader := DefaultManifestReaderFactory(false, bundle, mapper)
require.NotNil(t, reader)
}
@ -64,22 +66,23 @@ func TestManifestBundleReader(t *testing.T) {
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
reader := NewManifestBundleReader(false, bundle, FactoryFromKubeConfig("testdata/kubeconfig.yaml", ""))
mapper, err := FactoryFromKubeConfig("testdata/kubeconfig.yaml", "").ToRESTMapper()
require.NoError(t, err)
reader := NewManifestBundleReader(false, bundle, mapper)
if tt.reader != nil {
reader.StreamReader.Reader = tt.reader
}
if tt.writer != nil {
reader.writer = tt.writer
}
infos, err := reader.Read()
objects, err := reader.Read()
if tt.errString != "" {
require.Error(t, err)
assert.Contains(t, err.Error(), tt.errString)
} else {
require.NoError(t, err)
require.Len(t, infos, 1)
obj := infos[0].Object
gvk := obj.GetObjectKind().GroupVersionKind()
require.Len(t, objects, 1)
gvk := objects[0].GetObjectKind().GroupVersionKind()
assert.Equal(t, gvk, schema.GroupVersionKind{
Kind: "ReplicationController",
Group: "",