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

Add unstructured converter and test fixtures #446

Merged
merged 3 commits into from Feb 25, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
67 changes: 67 additions & 0 deletions pkg/await/fixtures/deployment.go
@@ -0,0 +1,67 @@
// Copyright 2016-2019, Pulumi Corporation.
//
// 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 fixtures

import (
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
)

func DeploymentBasic() *appsv1.Deployment {
return &appsv1.Deployment{
TypeMeta: metav1.TypeMeta{
APIVersion: "apps/v1",
Kind: "Deployment",
},
ObjectMeta: metav1.ObjectMeta{
Name: "foo",
},
Spec: appsv1.DeploymentSpec{
Template: corev1.PodTemplateSpec{
Spec: corev1.PodSpec{
Containers: []corev1.Container{
{
Name: "foo",
Image: "nginx",
},
},
},
},
},
}
}

func DeploymentBasic_Uns() *unstructured.Unstructured {
lblackstone marked this conversation as resolved.
Show resolved Hide resolved
return &unstructured.Unstructured{
Object: map[string]interface{}{
"apiVersion": "apps/v1",
"kind": "Deployment",
"metadata": map[string]interface{}{
"name": "foo"},
"spec": map[string]interface{}{
"template": map[string]interface{}{
"spec": map[string]interface{}{
"containers": []interface{}{
map[string]interface{}{
"name": "foo",
"image": "nginx"}},
},
},
},
},
}
}
58 changes: 58 additions & 0 deletions pkg/await/fixtures/pod.go
@@ -0,0 +1,58 @@
// Copyright 2016-2019, Pulumi Corporation.
//
// 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 fixtures

import (
corev1 "k8s.io/api/core/v1"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
)

func PodBasic() *corev1.Pod {
return &corev1.Pod{
TypeMeta: v1.TypeMeta{
APIVersion: "v1",
Kind: "Pod",
},
ObjectMeta: v1.ObjectMeta{
Name: "foo",
},
Spec: corev1.PodSpec{
Containers: []corev1.Container{
{
Name: "foo",
Image: "nginx",
},
},
},
}
}

func PodBasic_Uns() *unstructured.Unstructured {
return &unstructured.Unstructured{
Object: map[string]interface{}{
"apiVersion": "v1",
"kind": "Pod",
"metadata": map[string]interface{}{
"name": "foo"},
"spec": map[string]interface{}{
"containers": []interface{}{
map[string]interface{}{
"name": "foo",
"image": "nginx"}},
},
},
}
}
61 changes: 61 additions & 0 deletions pkg/clients/unstructured.go
@@ -0,0 +1,61 @@
// Copyright 2016-2019, Pulumi Corporation.
//
// 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 clients

import (
"encoding/json"
"fmt"

"github.com/pulumi/pulumi-kubernetes/pkg/kinds"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
"k8s.io/api/extensions/v1beta1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
)

func FromUnstructured(obj *unstructured.Unstructured) (metav1.Object, error) {
b, err := obj.MarshalJSON()
if err != nil {
return nil, err
}

var output metav1.Object
switch kinds.Kind(obj.GetKind()) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we have docs stating which objects are supported today vs not?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I'll add docs eventually, but for right now, this is the set of objects we're using the in complex await logic.

case kinds.Deployment:
output = new(appsv1.Deployment)
case kinds.Ingress:
output = new(v1beta1.Ingress)
case kinds.PersistentVolume:
output = new(corev1.PersistentVolume)
case kinds.PersistentVolumeClaim:
output = new(corev1.PersistentVolumeClaim)
case kinds.Pod:
output = new(corev1.Pod)
case kinds.ReplicaSet:
output = new(appsv1.ReplicaSet)
case kinds.StatefulSet:
output = new(appsv1.StatefulSet)
default:
return nil, fmt.Errorf("unhandled Kind: %s", obj.GetKind())
}

err = json.Unmarshal(b, &output)
if err != nil {
return nil, err
}

return output, nil
}
56 changes: 56 additions & 0 deletions pkg/clients/unstructured_test.go
@@ -0,0 +1,56 @@
// Copyright 2016-2019, Pulumi Corporation.
//
// 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 clients

import (
"reflect"
"testing"

"github.com/pulumi/pulumi-kubernetes/pkg/await/fixtures"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
)

func TestFromUnstructured(t *testing.T) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are there plans to test the rest of the supported objects other than the Pod and Deployment?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, probably should have been more clear in the description, but this PR is basically a prototype to make sure the approach made sense before I fully committed to it. My intent is to expand these fixtures to replace all the giant YAML strings in the unit tests. I'll keep fleshing things out over multiple PRs.

pod := fixtures.PodBasic()
unsPod := fixtures.PodBasic_Uns()
deployment := fixtures.DeploymentBasic()
unsDeployment := fixtures.DeploymentBasic_Uns()

type args struct {
obj *unstructured.Unstructured
}
tests := []struct {
name string
args args
want v1.Object
wantErr bool
}{
{"valid-pod", args{obj: unsPod}, v1.Object(pod), false},
{"valid-deployment", args{obj: unsDeployment}, v1.Object(deployment), false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := FromUnstructured(tt.args.obj)
if (err != nil) != tt.wantErr {
t.Errorf("FromUnstructured() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("FromUnstructured() = %v, want %v", got, tt.want)
}
})
}
}