Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix InstallCRDs method to only install CRDs and ignore other resource types. #607

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion pkg/test/harness.go
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,8 @@ func (h *Harness) Run() {
}

// Install CRDs
crds, err := testutils.InstallManifests(context.TODO(), cl, dClient, h.TestSuite.CRDDir)
crdKind := testutils.NewResource("apiextensions.k8s.io/v1beta1", "CustomResourceDefinition", "", "")
crds, err := testutils.InstallManifests(context.TODO(), cl, dClient, h.TestSuite.CRDDir, crdKind)
if err != nil {
h.T.Fatal(err)
}
Expand Down
19 changes: 12 additions & 7 deletions pkg/test/utils/kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (

"github.com/kudobuilder/kudo/pkg/apis"
kudo "github.com/kudobuilder/kudo/pkg/apis/kudo/v1alpha1"
"github.com/pkg/errors"
"github.com/pmezard/go-difflib/difflib"
corev1 "k8s.io/api/core/v1"
apiextensions "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1beta1"
Expand Down Expand Up @@ -310,7 +311,7 @@ func PrettyDiff(expected runtime.Object, actual runtime.Object) (string, error)
func ConvertUnstructured(in runtime.Object) (runtime.Object, error) {
unstruct, err := runtime.DefaultUnstructuredConverter.ToUnstructured(in)
if err != nil {
return nil, err
return nil, errors.Wrap(err, fmt.Sprintf("erroring converting %s to unstructured", ResourceID(in)))
}

var converted runtime.Object
Expand All @@ -332,7 +333,7 @@ func ConvertUnstructured(in runtime.Object) (runtime.Object, error) {

err = runtime.DefaultUnstructuredConverter.FromUnstructured(unstruct, converted)
if err != nil {
return nil, err
return nil, errors.Wrap(err, fmt.Sprintf("erroring converting %s from unstructured", ResourceID(in)))
}

return converted, nil
Expand Down Expand Up @@ -420,19 +421,19 @@ func LoadYAML(path string) ([]runtime.Object, error) {
if err == io.EOF {
break
}
return nil, err
return nil, errors.Wrap(err, fmt.Sprintf("error reading yaml %s", path))
}

unstructuredObj := &unstructured.Unstructured{}
decoder := yaml.NewYAMLOrJSONDecoder(bytes.NewBuffer(data), len(data))

if err = decoder.Decode(unstructuredObj); err != nil {
return nil, err
return nil, errors.Wrap(err, fmt.Sprintf("error decoding yaml %s", path))
}

obj, err := ConvertUnstructured(unstructuredObj)
if err != nil {
return nil, err
return nil, errors.Wrap(err, fmt.Sprintf("error converting unstructured object %s (%s)", ResourceID(unstructuredObj), path))
}

objects = append(objects, obj)
Expand All @@ -455,7 +456,7 @@ func MatchesKind(obj runtime.Object, kinds ...runtime.Object) bool {
}

// InstallManifests recurses over ManifestsDir to install all resources defined in YAML manifests.
func InstallManifests(ctx context.Context, client client.Client, dClient discovery.DiscoveryInterface, manifestsDir string) ([]runtime.Object, error) {
func InstallManifests(ctx context.Context, client client.Client, dClient discovery.DiscoveryInterface, manifestsDir string, kinds ...runtime.Object) ([]runtime.Object, error) {
objects := []runtime.Object{}

if manifestsDir == "" {
Expand Down Expand Up @@ -486,6 +487,10 @@ func InstallManifests(ctx context.Context, client client.Client, dClient discove
}

for _, obj := range objs {
if len(kinds) > 0 && !MatchesKind(obj, kinds...) {
continue
}

objectKey := ObjectKey(obj)
if objectKey.Namespace == "" {
if _, _, err := Namespaced(dClient, obj, "default"); err != nil {
Expand All @@ -495,7 +500,7 @@ func InstallManifests(ctx context.Context, client client.Client, dClient discove

updated, err := CreateOrUpdate(ctx, client, obj, true)
if err != nil {
return err
return errors.Wrap(err, fmt.Sprintf("error creating resource %s", ResourceID(obj)))
}

action := "created"
Expand Down
2 changes: 1 addition & 1 deletion pkg/test/utils/kubernetes_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ func TestWaitForCRDs(t *testing.T) {
assert.IsType(t, &meta.NoKindMatchError{}, testClient.Create(context.TODO(), instance))

// Install all of the CRDs.
crds, err := InstallManifests(context.TODO(), testClient, testenv.DiscoveryClient, "../../../config/crds/")
crds, err := InstallManifests(context.TODO(), testClient, testenv.DiscoveryClient, "../../../config/crds/", NewResource("apiextensions.k8s.io/v1beta1", "CustomResourceDefinition", "", ""))
assert.Nil(t, err)

// WaitForCRDs to be created.
Expand Down